Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4b140657d283ea7cadb8d7eb2035d64e9dd8dc78 (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
/*****************************************************************************
 * 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.sysml.diagram.common.strategy.paste;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.emf.common.command.CompoundCommand;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.gef.commands.Command;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.MoveRequest;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.commands.wrappers.EMFtoGEFCommandWrapper;
import org.eclipse.papyrus.commands.wrappers.GMFtoEMFCommandWrapper;
import org.eclipse.papyrus.infra.core.clipboard.IClipboardAdditionalData;
import org.eclipse.papyrus.infra.core.clipboard.PapyrusClipboard;
import org.eclipse.papyrus.infra.gmfdiag.common.strategy.paste.DefaultPasteStrategy;
import org.eclipse.papyrus.infra.gmfdiag.common.strategy.paste.IPasteStrategy;
import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils;
import org.eclipse.papyrus.infra.services.edit.service.IElementEditService;
import org.eclipse.papyrus.sysml.blocks.Block;
import org.eclipse.papyrus.uml.diagram.common.Activator;
import org.eclipse.swt.graphics.Image;
import org.eclipse.uml2.uml.Association;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.util.UMLUtil;

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

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

	/**
	 * 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 "partPasteStrategy"; //$NON-NLS-1$
	}

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

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.papyrus.infra.gmfdiag.common.strategy.paste.IPasteStrategy#getDescription()
	 */
	public String getDescription() {
		return "Paste part elements with association"; //$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 Classifier || targetOwner instanceof Package) { // test if block or package
			// get affiliate AdditionnalData
			Map<Object, ?> additionalDataMap = papyrusClipboard.getAdditionalDataForStrategy(getID());
			if(additionalDataMap != null) {
				CompoundCommand compoundCommand = new CompoundCommand("Copy associations for parts"); //$NON-NLS-1$
				for(Iterator<Object> iterator = papyrusClipboard.iterator(); iterator.hasNext();) {
					Object object = iterator.next();
					EObject target = papyrusClipboard.getTragetCopyFromInternalClipboardCopy(object);
					Object additionnalData = additionalDataMap.get(object);
					EList<Association> listDuplicatedAssociation = new BasicEList<Association>();
					EObject associationContainer = null;
					if(target != null && target instanceof Property && targetOwner instanceof Classifier && additionnalData instanceof PartAdditionalData) { // test if it is a part
						Classifier block = (Classifier)targetOwner;
						Property property = (Property)target;
						PartAdditionalData partAdditionalData = (PartAdditionalData)additionnalData;
						Association newAssociation = partAdditionalData.getDuplicatedAssociation();
						restoreAssociationPartContext(block, property, newAssociation);
						listDuplicatedAssociation.add(newAssociation);
						associationContainer = block.eContainer();
					} else if(target != null && target instanceof Classifier && targetOwner instanceof Package && additionnalData instanceof PartBlockAdditionalData) { // test if it is a block
						Classifier classifier = (Classifier)target;
						EList<Property> allAttributes = classifier.getAllAttributes();
						PartBlockAdditionalData partBlockAdditionalData = (PartBlockAdditionalData)additionnalData;
						for(Property property : allAttributes) {
							Association duplicatedAssociation = partBlockAdditionalData.getDuplicatedAssociationByPropertyName(property.getName());
							if (duplicatedAssociation != null){
								restoreAssociationPartContext(classifier, property, duplicatedAssociation);
								listDuplicatedAssociation.add(duplicatedAssociation);								
							}
						}
						associationContainer = targetOwner;
					}
					if(associationContainer != null && !listDuplicatedAssociation.isEmpty()) {
						// add associations to the nearest container	
						MoveRequest moveRequest = new MoveRequest(associationContainer, listDuplicatedAssociation);
						IElementEditService provider = ElementEditServiceUtils.getCommandProvider(targetOwner);
						if(provider != null) {
							ICommand editCommand = provider.getEditCommand(moveRequest);
							GMFtoEMFCommandWrapper gmFtoEMFCommandWrapper = new GMFtoEMFCommandWrapper(editCommand);
							compoundCommand.append(gmFtoEMFCommandWrapper);
						}
					}
				}
				// An empty command can't be executed 
				if(compoundCommand.getCommandList().isEmpty()) {
					return null;
				}
				return compoundCommand;
			}
		}
		return null;
	}





	/*
	 * (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) {
		View targetView = (View)targetEditPart.getModel();
		EObject targetOwner = (EObject)targetView.getElement();
		org.eclipse.emf.common.command.Command semanticCommand = getSemanticCommand(domain, targetOwner, papyrusClipboard);
		if(semanticCommand != null) {
			org.eclipse.gef.commands.CompoundCommand compoundCommand = new org.eclipse.gef.commands.CompoundCommand("Association Part Semantic And Graphical paste"); //$NON-NLS-1$
			EMFtoGEFCommandWrapper emFtoGEFCommandWrapper = new EMFtoGEFCommandWrapper(semanticCommand);
			compoundCommand.add(emFtoGEFCommandWrapper);
			return compoundCommand;
		}
		return null;
	}

	/*
	 * (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, Collection<EObject> selection) {
		Map<Object, IClipboardAdditionalData> mapCopyPartData = new HashMap<Object, IClipboardAdditionalData>();
		for(Iterator<EObject> iterator = papyrusClipboard.iterateOnSource(); iterator.hasNext();) {
			EObject eObjectSource = iterator.next();
			if(eObjectSource instanceof Element) {
				Element elementSource = (Element)eObjectSource;
				IClipboardAdditionalData clipboardAdditionalData = null;
				if(elementSource instanceof Property && UMLUtil.getStereotypeApplication(elementSource.getOwner(), Block.class) != null) { // is part 
					Property propertySource = (Property)elementSource;
					Association association = propertySource.getAssociation();
					if (association != null){
						clipboardAdditionalData = new PartAdditionalData(association);
					}
				} else if(elementSource instanceof Classifier && UMLUtil.getStereotypeApplication(elementSource, Block.class) != null) {// is Block
					Classifier block = (Classifier)elementSource;
					clipboardAdditionalData = new PartBlockAdditionalData(block);
				}
				if(clipboardAdditionalData != null) {
					Object copy = papyrusClipboard.getCopyFromSource(eObjectSource);
					mapCopyPartData.put(copy, clipboardAdditionalData);
				}
			}

		}
		papyrusClipboard.pushAdditionalData(getID(), mapCopyPartData);
	}


	/**
	 * Duplicate the association
	 * @param association
	 * @return
	 */
	protected Association duplicateAssociation(Association association) {
		EcoreUtil.Copier copier = new EcoreUtil.Copier();
		copier.copy(association);
		EObject eObject = copier.get(association);
		return (Association)eObject;
	}

	/**
	 * Init the association in the new context
	 * @param classifier
	 * @param property
	 * @param association
	 */
	protected void restoreAssociationPartContext(Classifier classifier, Property property, Association association) {
		EList<Property> memberEnds = association.getMemberEnds(); // should have only one element
		if (memberEnds != null && !memberEnds.isEmpty()){
			Property blockProperty = memberEnds.get(0);
			blockProperty.setType(classifier);
		}
		memberEnds.add(property);
	}


	/**
	 * The Class PartAdditionalData.
	 */
	protected class PartAdditionalData implements IClipboardAdditionalData {

		/** The association. */
		protected Association asssociation;

		/**
		 * @param association
		 */
		public PartAdditionalData(Association asssociation) {
			this.asssociation = duplicateAssociation(asssociation);
		}

		/**
		 * @return a copy of the association
		 */
		public Association getDuplicatedAssociation() {
			return duplicateAssociation(asssociation);
		}

	}

	/**
	 * The Class PartBlockAdditionalData.
	 */
	protected class PartBlockAdditionalData implements IClipboardAdditionalData {

		/** The associations by property name. */
		protected Map<String, Association> mapPropertyNameToAssociation;

		/**
		 * @param block
		 */
		public PartBlockAdditionalData(Classifier block) {
			this.mapPropertyNameToAssociation = new HashMap<String, Association>();
			EList<Property> allAttributes = block.getAllAttributes();
			for(Property property : allAttributes) {
				Association association = property.getAssociation();
				Association newAssociation = duplicateAssociation(association);
				mapPropertyNameToAssociation.put(property.getName(), newAssociation);
			}

		}

		/**
		 * @param propertyName
		 * @return  a copy of the association
		 */
		public Association getDuplicatedAssociationByPropertyName(String propertyName) {
			Association association = mapPropertyNameToAssociation.get(propertyName);
			return duplicateAssociation(association);
		}
	}
}

Back to the top