Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a234f5c9435446e4fd04c127f4a4dd8b76a0641a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*****************************************************************************
 * Copyright (c) 2014 CEA LIST.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Benoit Maggi (CEA LIST) benoit.maggi@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.common.strategy.paste;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.emf.common.command.CompoundCommand;
import org.eclipse.emf.common.command.UnexecutableCommand;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.commands.Command;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.papyrus.commands.wrappers.EMFtoGEFCommandWrapper;
import org.eclipse.papyrus.infra.core.clipboard.IClipboardAdditionalData;
import org.eclipse.papyrus.infra.core.clipboard.PapyrusClipboard;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.gmfdiag.common.strategy.paste.IPasteStrategy;
import org.eclipse.papyrus.uml.diagram.common.Activator;
import org.eclipse.papyrus.uml.diagram.common.preferences.IStereotypePasteStrategyPreferenceConstant;
import org.eclipse.papyrus.uml.tools.commands.ApplyProfileCommand;
import org.eclipse.papyrus.uml.tools.commands.ApplyStereotypeCommand;
import org.eclipse.papyrus.uml.tools.utils.PackageUtil;
import org.eclipse.swt.graphics.Image;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.Stereotype;
import org.eclipse.uml2.uml.util.UMLUtil;

/**
 * Offer a strategy for copying stereotypes.
 */
public class StereotypePasteStrategy implements IPasteStrategy {

	/** The instance. */
	private static IPasteStrategy instance = new StereotypePasteStrategy();

	/**
	 * Gets the single instance of StereotypePasteStrategy.
	 *
	 * @return single instance of StereotypePasteStrategy
	 */
	public static IPasteStrategy getInstance() {
		return instance;
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.papyrus.infra.gmfdiag.common.strategy.paste.IPasteStrategy#getLabel()
	 */
	public String getLabel() {
		return "StereotypeStrategy"; //$NON-NLS-1$
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.papyrus.infra.gmfdiag.common.strategy.paste.IPasteStrategy#getID()
	 */
	public String getID() {
		return Activator.ID + ".StereotypeStrategy"; //".ClassifierToStructureCompDrop"; //$NON-NLS-1$
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.papyrus.infra.gmfdiag.common.strategy.paste.IPasteStrategy#getDescription()
	 */
	public String getDescription() {
		return "Paste stereotype elements"; //$NON-NLS-1$
	}

	/**
	 * Gets the category id.
	 *
	 * @return the category id
	 */
	public String getCategoryID() {
		return "org.eclipse.papyrus.strategy.paste"; //$NON-NLS-1$
	}

	/**
	 * Gets the category label.
	 *
	 * @return the category label
	 */
	public String getCategoryLabel() {
		return "Paste all copied elements"; //$NON-NLS-1$
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.papyrus.infra.gmfdiag.common.strategy.paste.IPasteStrategy#getImage()
	 */
	@Deprecated
	public Image getImage() {
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.papyrus.infra.gmfdiag.common.strategy.paste.IPasteStrategy#getPriority()
	 */
	@Deprecated
	public int getPriority() {
		return 1;
	}

	/**
	 * Sets the options.
	 *
	 * @param options
	 *        the options
	 */
	public void setOptions(Map<String, Object> options) {
		//Nothing
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.papyrus.infra.gmfdiag.common.strategy.paste.IPasteStrategy#getSemanticCommand(org.eclipse.emf.edit.domain.EditingDomain,
	 * org.eclipse.emf.ecore.EObject, org.eclipse.papyrus.infra.core.clipboard.PapyrusClipboard)
	 */
	@Override
	public org.eclipse.emf.common.command.Command getSemanticCommand(EditingDomain domain, EObject targetOwner, PapyrusClipboard<Object> papyrusClipboard) {
		
		if (targetOwner instanceof Element){
			CompoundCommand compoundCommand = new CompoundCommand("Copy all stereotypes"); //$NON-NLS-1$
			
			Map<Profile,List<ApplyStereotypeCommand>> missingProfiles = new HashMap<Profile,List<ApplyStereotypeCommand>>();
			Package targetPackage = ((Element)targetOwner).getNearestPackage();
			
			// 1. init all ApplyStereotypeCommand
			for(Iterator<Object> iterator = papyrusClipboard.iterator(); iterator.hasNext();) {
				Object object = (Object)iterator.next();
				// get target Element
				EObject target = papyrusClipboard.getTragetCopyFromInternalClipboardCopy(object);
				if(target != null && target instanceof Element) {
					// get affiliate StereotypeClipboard
					Map<Object, ?> additionalDataMap = papyrusClipboard.getAdditionalDataForStrategy(getID());
					Object additionnalData = additionalDataMap.get(object);
					if(additionnalData instanceof StereotypeClipboard) {
						StereotypeClipboard stereotypeClipboard = (StereotypeClipboard)additionnalData;
						Collection<Stereotype> stereotypes = stereotypeClipboard.getStereotypes();
//						Collection<Stereotype> stereotypeListInTargetContext = new ArrayList<Stereotype>();
						for(Stereotype stereotype : stereotypes) {
							// reload the stereotype in the new Contex-ResourceSet (Required because in org.eclipse.uml2.uml.internal.operations.PackageOperations
							// L960 in getProfileApplication the test is using == instead of equals)
							Stereotype stereotypeInTargetContext = EMFHelper.reloadIntoContext(stereotype, targetOwner);
							
							Profile profile = stereotypeInTargetContext.getProfile();
							ApplyStereotypeCommand applyStereotypeCommand = new ApplyStereotypeCommand((Element)target, stereotypeInTargetContext, (TransactionalEditingDomain)domain);
							if (isProfileAppliedRecursive(targetPackage, profile)){		
								compoundCommand.append(applyStereotypeCommand);
							} else { // Profile is missing
//								Activator.getDefault().logWarning(profile.getName()+" is missing", null);
								List<ApplyStereotypeCommand> stereotypeListMissingProfiles = missingProfiles.get(profile);
								if (stereotypeListMissingProfiles!= null && !stereotypeListMissingProfiles.isEmpty()){
									stereotypeListMissingProfiles.add(applyStereotypeCommand);
								} else {
									stereotypeListMissingProfiles = new ArrayList<ApplyStereotypeCommand>();
									stereotypeListMissingProfiles.add(applyStereotypeCommand);
									missingProfiles.put(profile, stereotypeListMissingProfiles);
								}
							}
						}
					}
				}
			}
			
			// 2. user preferences (Apply profiles, data...)
			IPreferenceStore preferenceStore = Activator.getDefault().getPreferenceStore();
			String preferenceProfileStrategy = preferenceStore.getString(IStereotypePasteStrategyPreferenceConstant.PROFILE_STRATEGY);	
		
			if (IStereotypePasteStrategyPreferenceConstant.ASK_POPUP.equals(preferenceProfileStrategy)){// Ask user for instruction
				// TODO:  2. user choices (Apply profiles, data...)
			} else if (IStereotypePasteStrategyPreferenceConstant.IMPORT_MISSING_PROFILE.equals(preferenceProfileStrategy)){ // apply profile
				Set<Profile> keySet = missingProfiles.keySet();
				Package rootPackage = PackageUtil.getRootPackage((Element)targetOwner);
				ApplyProfileCommand applyProfileCommand = new ApplyProfileCommand(rootPackage, keySet, (TransactionalEditingDomain)domain);
				compoundCommand.append(applyProfileCommand);
				for(Profile profile : keySet) {
					List<ApplyStereotypeCommand> list = missingProfiles.get(profile);
					for(ApplyStereotypeCommand applyStereotypeCommand : list) {
						compoundCommand.append(applyStereotypeCommand);
					}
				}
			}
			
			
			// An empty can't be executed 
			if(compoundCommand.getCommandList().isEmpty()) {
				return null;
			}
			return compoundCommand;			
		}
		return UnexecutableCommand.INSTANCE;

	}




	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.papyrus.infra.gmfdiag.common.strategy.paste.IPasteStrategy#getGraphicalCommand(org.eclipse.emf.edit.domain.EditingDomain,
	 * org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart, org.eclipse.papyrus.infra.core.clipboard.PapyrusClipboard)
	 */
	@Override
	public Command getGraphicalCommand(EditingDomain domain, org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart targetEditPart, PapyrusClipboard<Object> papyrusClipboard) {
		org.eclipse.gef.commands.CompoundCommand compoundCommand = new org.eclipse.gef.commands.CompoundCommand("Stereotype Semantic And Graphical paste"); //$NON-NLS-1$
		Map<Profile,List<ApplyStereotypeCommand>> missingProfiles = new HashMap<Profile,List<ApplyStereotypeCommand>>();
		View view = (View)targetEditPart.getModel();
		EObject modelTargetOwner = (EObject)view.getElement();
		Package targetPackage = ((Element)modelTargetOwner).getNearestPackage();
		// apply stereotypes 
		for(Iterator<Object> iterator = papyrusClipboard.iterator(); iterator.hasNext();) {
			Object object = (Object)iterator.next();
			// get target Element
			EObject target = papyrusClipboard.getTragetCopyFromInternalClipboardCopy(object);
			if(target != null && target instanceof Element) {
				// get affiliate StereotypeClipboard
				Map<Object, ?> additionalDataMap = papyrusClipboard.getAdditionalDataForStrategy(getID());
				Object additionnalData = additionalDataMap.get(object);
				if(additionnalData instanceof StereotypeClipboard) {
					StereotypeClipboard stereotypeClipboard = (StereotypeClipboard)additionnalData;
					Collection<Stereotype> stereotypes = stereotypeClipboard.getStereotypes();
					for(Stereotype stereotype : stereotypes) {
						// reload the stereotype in the new Contex-ResourceSet (Required because in org.eclipse.uml2.uml.internal.operations.PackageOperations
						// L960 in getProfileApplication the test is using == instead of equals)
						Stereotype stereotypeInTargetContext = EMFHelper.reloadIntoContext(stereotype, modelTargetOwner);
						ApplyStereotypeCommand applyStereotypeCommand = new ApplyStereotypeCommand((Element)target, stereotypeInTargetContext, (TransactionalEditingDomain)domain);
						
						Profile profile = stereotypeInTargetContext.getProfile();
						if (isProfileAppliedRecursive(targetPackage, profile)){
							EMFtoGEFCommandWrapper emFtoGEFCommandWrapper = new EMFtoGEFCommandWrapper(applyStereotypeCommand);
							compoundCommand.add(emFtoGEFCommandWrapper);
						} else { // Profile is missing
							List<ApplyStereotypeCommand> stereotypeListMissingProfiles = missingProfiles.get(profile);
							if (stereotypeListMissingProfiles!= null && !stereotypeListMissingProfiles.isEmpty()){
								stereotypeListMissingProfiles.add(applyStereotypeCommand);
							} else {
								stereotypeListMissingProfiles = new ArrayList<ApplyStereotypeCommand>();
								stereotypeListMissingProfiles.add(applyStereotypeCommand);
								missingProfiles.put(profile, stereotypeListMissingProfiles);
							}	
						}	
					}

				}
			}
		}

		// 2. user preferences (Apply profiles, data...)
		IPreferenceStore preferenceStore = Activator.getDefault().getPreferenceStore();
		String preferenceProfileStrategy = preferenceStore.getString(IStereotypePasteStrategyPreferenceConstant.PROFILE_STRATEGY);	
	
		if (IStereotypePasteStrategyPreferenceConstant.ASK_POPUP.equals(preferenceProfileStrategy)){// Ask user for instruction
			// TODO:  2. user choices (Apply profiles, data...)
		} else if (IStereotypePasteStrategyPreferenceConstant.IMPORT_MISSING_PROFILE.equals(preferenceProfileStrategy)){ // apply profile
			Set<Profile> keySet = missingProfiles.keySet();
			Package rootPackage = PackageUtil.getRootPackage((Element)modelTargetOwner);
			ApplyProfileCommand applyProfileCommand = new ApplyProfileCommand(rootPackage, keySet, (TransactionalEditingDomain)domain);
			EMFtoGEFCommandWrapper emFtoGEFCommandWrapper = new EMFtoGEFCommandWrapper(applyProfileCommand);
			compoundCommand.add(emFtoGEFCommandWrapper);
			for(Profile profile : keySet) {
				List<ApplyStereotypeCommand> list = missingProfiles.get(profile);
				for(ApplyStereotypeCommand applyStereotypeCommand : list) {
					compoundCommand.add(new EMFtoGEFCommandWrapper(applyStereotypeCommand));
				}
			}
		}		
		
		
		if(compoundCommand.size() == 0) {// TODO : use unwrap if no use of UnexecutableCommand.INSTANCE
			return null;
		}
		return compoundCommand;
	}


	/**
	 * Checks recursively if a profile is applied .
	 *
	 * @param element
	 *        the element
	 * @param profile
	 *        the profile
	 * @return true, if is profile applied recursive
	 */
	private boolean isProfileAppliedRecursive(Element element, Profile profile) {
		if(element instanceof Package) {
			Package pkg = (Package)element;
			boolean profileApplied = pkg.isProfileApplied(profile);

			if(pkg instanceof Model) {
				return profileApplied;
			}

			if(!profileApplied) {
				return isProfileAppliedRecursive(element.getOwner(), profile);
			} else {
				return true;
			}
		}
		return isProfileAppliedRecursive(element.getOwner(), profile);
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.papyrus.infra.gmfdiag.common.strategy.paste.IPasteStrategy#dependsOn()
	 */
	@Override
	public IPasteStrategy dependsOn() {
		return DefaultPasteStrategy.getInstance();
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.papyrus.infra.gmfdiag.common.strategy.paste.IPasteStrategy#prepare(org.eclipse.papyrus.infra.core.clipboard.PapyrusClipboard)
	 */
	@Override
	public void prepare(PapyrusClipboard<Object> papyrusClipboard) {
		Map<Object, IClipboardAdditionalData> mapCopyToStereotypeData = new HashMap<Object, IClipboardAdditionalData>();
		for(Iterator<EObject> iterator = papyrusClipboard.iterateOnSource(); iterator.hasNext();) {
			EObject eObjectSource = (EObject)iterator.next();
			if(eObjectSource instanceof Element) {
				Element element = (Element)eObjectSource;
				EList<EObject> stereotypeApplications = element.getStereotypeApplications();
				if(stereotypeApplications != null && !stereotypeApplications.isEmpty()) {
					List<Stereotype> stereotypeList = new ArrayList<Stereotype>();
					for(EObject stereotypeApplication : stereotypeApplications) {
						Stereotype stereotype = UMLUtil.getStereotype(stereotypeApplication);
						stereotypeList.add(stereotype);
					}
					StereotypeClipboard stereotypeClipboard = new StereotypeClipboard(stereotypeList);
					Object copy = papyrusClipboard.getCopyFromSource(eObjectSource);
					mapCopyToStereotypeData.put(copy, stereotypeClipboard);
				}
			}
		}
		papyrusClipboard.pushAdditionalData(getID(), mapCopyToStereotypeData);
	}


	//	protected ICommand getOpenDialogCommand(final TransactionalEditingDomain domain, final Collection<EditPart> selectedEditPart, final Map<EditPart, Set<EObject>> availableLinks, final Collection<EObject> initialSelection, final Map<EObject, LinkEndsMapper> linkMapping) {
	//		final ICommand cmd = new AbstractTransactionalCommand(domain, "Open Show/HideDialogCommand", null) {//$NON-NLS-1$
	//
	//			@Override
	//			protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
	//				final ShowHideRelatedLinkSelectionDialog dialog = new ShowHideRelatedLinkSelectionDialog(Display.getDefault().getActiveShell(), new UMLLabelProvider(), new AbstractShowHideRelatedLinkEditPolicy.LinkContentProvider(availableLinks), availableLinks, linkMapping);
	//				dialog.setTitle("Show/Hide Links");//$NON-NLS-1$
	//				dialog.setMessage("Choose the links to show.");//$NON-NLS-1$
	//				dialog.setInput(selectedEditPart);
	//				dialog.setInitialSelection(initialSelection);
	//				dialog.setExpandedElements(selectedEditPart.toArray());
	//				dialog.setContainerMode(true);
	//				int status = dialog.open();
	//				if(status == Window.CANCEL) {
	//					return CommandResult.newCancelledCommandResult();
	//				}
	//				return CommandResult.newOKCommandResult(Arrays.asList(dialog.getResult()));
	//			}
	//		};
	//
	//		return cmd;
	//	}



	// TODO : remove no internal class
	/**
	 * The Class StereotypeClipboard.
	 */
	private class StereotypeClipboard implements IClipboardAdditionalData {

		/** The stereotype. */
		private Collection<Stereotype> stereotypes;

		/**
		 * @param stereotypes
		 */
		public StereotypeClipboard(Collection<Stereotype> stereotypes) {
			this.stereotypes = stereotypes;
		}

		/**
		 * @return
		 */
		public Collection<Stereotype> getStereotypes() {
			return stereotypes;
		}

	}





}

Back to the top