Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f5322faed53674fefd7648442314311731841598 (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
/*****************************************************************************
 * 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.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.emf.common.command.CompoundCommand;
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.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
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.IPasteStrategy;
import org.eclipse.papyrus.uml.diagram.common.Activator;
import org.eclipse.papyrus.uml.tools.commands.ApplyStereotypeCommand;
import org.eclipse.swt.graphics.Image;
import org.eclipse.uml2.uml.Element;
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#getCommand(org.eclipse.gef.Request, org.eclipse.gef.EditPart)
	 */
	@Override
	public Command getCommand(Request request, EditPart targetEditPart) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (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> p) {
		CompoundCommand compoundCommand = new CompoundCommand("Copy all stereotypes"); //$NON-NLS-1$
		for (Iterator<Object> iterator = p.iterator(); iterator.hasNext();) {
			Object object = (Object) iterator.next();
			// get target Element
			EObject target= p.getTragetCopyFromInternalClipboardCopy(object);
			if (target != null && target instanceof Element){
				// get affiliate StereotypeClipboard
				Map<Object, ?> additionalDataMap = p.getAdditionalDataForStrategy(getID());
				Object additionnalData = additionalDataMap.get(object);
				if (additionnalData instanceof StereotypeClipboard){
					StereotypeClipboard stereotypeClipboard = (StereotypeClipboard) additionnalData;
					Stereotype stereotype = stereotypeClipboard.getStereotype();
					if (stereotype != null) {
						// append command to apply stereotype
						ApplyStereotypeCommand applyStereotypeCommand = new ApplyStereotypeCommand((Element)target, stereotype, (TransactionalEditingDomain) domain);
						compoundCommand.append(applyStereotypeCommand);					
					}
				}
			}
		}
		// An empty can't be executed 
		 if (compoundCommand.getCommandList().isEmpty()) {
			 return null;
		 }
		 return compoundCommand;
	}

	/* (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 targetOwner,
			PapyrusClipboard<Object> papyrusClipboard) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	/* (non-Javadoc)
	 * @see org.eclipse.papyrus.infra.gmfdiag.common.strategy.paste.IPasteStrategy#dependsOn()
	 */
	@Override
	public IPasteStrategy dependsOn() {
		// TODO Auto-generated method stub
		return ImportResourcePasteStrategy.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) {
		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();
				Map<Object,IClipboardAdditionalData> hashMap = new HashMap<Object,IClipboardAdditionalData>();
				for (EObject stereotypeApplication : stereotypeApplications) { // TODO : manage many stereotypes
					Stereotype stereotype = UMLUtil.getStereotype(stereotypeApplication);
					StereotypeClipboard stereotypeClipboard = new StereotypeClipboard(stereotype);
					Object copy = papyrusClipboard.getCopyFromSource(eObjectSource);
					hashMap.put(copy, stereotypeClipboard);
				} 	
				papyrusClipboard.pushAdditionalData(getID(), hashMap);					
			}
		}
	}


	
	// TODO : remove no internal class
	/**
	 * The Class StereotypeClipboard.
	 */
	private class StereotypeClipboard implements IClipboardAdditionalData{
		
		/** The stereotype. */
		private Stereotype stereotype;
		
		/**
		 * Gets the stereotype.
		 *
		 * @return the stereotype
		 */
		public Stereotype getStereotype() {
			return stereotype;
		}

		/**
		 * Instantiates a new stereotype clipboard.
		 *
		 * @param pStereotype the stereotype
		 */
		public StereotypeClipboard(Stereotype pStereotype) {
			stereotype = pStereotype;
		}
		
	}




	
}

Back to the top