Skip to main content
summaryrefslogtreecommitdiffstats
blob: c53aa2a17fc20a17d8c6829b15c33f628085a73d (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
/*****************************************************************************
 * Copyright (c) 2012, 2014 CEA LIST and others.
 *
 *    
 * 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:
 *  Patrick Tessier (CEA LIST) Patrick.tessier@cea.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - bug 323802
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.stereotype.edition.editpolicies;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.StringTokenizer;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.impl.DynamicEObjectImpl;
import org.eclipse.gmf.runtime.diagram.core.commands.DeleteCommand;
import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.gmfdiag.common.commands.SetNodeVisibilityCommand;
import org.eclipse.papyrus.infra.gmfdiag.common.utils.GMFUnsafe;
import org.eclipse.papyrus.uml.appearance.helper.AppliedStereotypeHelper;
import org.eclipse.papyrus.uml.appearance.helper.UMLVisualInformationPapyrusConstant;
import org.eclipse.papyrus.uml.diagram.common.Activator;
import org.eclipse.papyrus.uml.diagram.common.editpolicies.AppliedStereotypeNodeLabelDisplayEditPolicy;
import org.eclipse.papyrus.uml.diagram.common.figure.node.IPapyrusNodeUMLElementFigure;
import org.eclipse.papyrus.uml.diagram.stereotype.edition.command.CreateAppliedStereotypeViewCommand;
import org.eclipse.papyrus.uml.diagram.stereotype.edition.editpart.AppliedStereotypeConpartmentEditPart;
import org.eclipse.papyrus.uml.tools.listeners.PapyrusStereotypeListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Stereotype;
import org.eclipse.uml2.uml.util.UMLUtil;

/**
 * this edit policy can be apply only on {@link IPapyrusEditPart} in order to
 * access to primary figure. the primary figure has to be a {@link IPapyrusNodeUMLElementFigure}
 */
public class AppliedStereotypeCompartmentEditPolicy extends AppliedStereotypeNodeLabelDisplayEditPolicy {

	/**
	 * Creates a new AppliedStereotype display edit policy
	 */
	public AppliedStereotypeCompartmentEditPolicy() {
		super();
	}



	@Override
	public void activate() {
		super.activate();

		// if stereotype has been applied, compartment has to be created
		Element umlElement = getUMLElement();
		//umlElement may be null if the semantic element has been deleted and the view hasn't been cleaned
		if(umlElement != null) {
			Iterator<EObject> iterator = umlElement.getStereotypeApplications().iterator();
			while(iterator.hasNext()) {
				final EObject appliedstereotype = iterator.next();
				createAppliedStereotypeCompartment(appliedstereotype);
			}
		}
	}

/**
 * this method has in charge to calculate if a compartment must be displayed.
 * To know that it computes if a property of stereotype is displayed  into it.
 * @param applicationOfStereotype the eobject that represent the stereotype application
 * @return true if the compartment  display a property of sterotype
 */
	protected boolean hasToDisplayCompartment(EObject applicationOfStereotype) {
		String stereotypesPropertiesToDisplay = AppliedStereotypeHelper.getAppliedStereotypesPropertiesToDisplay((View)getHost().getModel());
		String stereotypesLocalizationToDisplay = AppliedStereotypeHelper.getAppliedStereotypesPropertiesLocalization((View)getHost().getModel());
		if(!(stereotypesLocalizationToDisplay.equals(UMLVisualInformationPapyrusConstant.STEREOTYPE_COMPARTMENT_LOCATION))) {
			return false;
		}
		HashSet<org.eclipse.uml2.uml.Stereotype> stereoSet = new HashSet<org.eclipse.uml2.uml.Stereotype>();
		ArrayList<String> stPropList = new ArrayList<String>();

		// fill our data structure in order to generate the string
		StringTokenizer propStringTokenizer = new StringTokenizer(stereotypesPropertiesToDisplay, ",");
		while(propStringTokenizer.hasMoreElements()) {
			// extract property to display
			String propertyQN = propStringTokenizer.nextToken();
			// stereotype
			String stereotypeQN = propertyQN.substring(0, propertyQN.indexOf("."));

			Stereotype stereotype = hostSemanticElement.getAppliedStereotype(stereotypeQN);

			if(stereotype != null) {
				stereoSet.add(stereotype);
			}

			stPropList.add(propertyQN);
		}

		// Display each stereotype
		Iterator<org.eclipse.uml2.uml.Stereotype> stereoIter = stereoSet.iterator();
		while(stereoIter.hasNext()) {
			Stereotype stereotype = stereoIter.next();
			if(stereotype != null) {
				if(applicationOfStereotype.equals(hostSemanticElement.getStereotypeApplication(stereotype))) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * the goal of this method is to execute the a command to create a notation node for a compartment of stereotype
	 * 
	 * @param editPart
	 *        the editpart owner of the new compartment
	 * @param appliedstereotype
	 *        the stereotype application
	 */
	protected void executeAppliedStereotypeCompatmentCreation(final GraphicalEditPart editPart, final EObject appliedstereotype) {
		try {
			editPart.getEditingDomain().runExclusive(new Runnable() {

				public void run() {
					Display.getCurrent().syncExec(new Runnable() {
						
						public void run() {
					CreateAppliedStereotypeViewCommand command = new CreateAppliedStereotypeViewCommand(editPart.getEditingDomain(), editPart.getNotationView(), appliedstereotype, hasToDisplayCompartment(appliedstereotype));
					//use to avoid to put it in the command stack
					try {
						GMFUnsafe.write(editPart.getEditingDomain(), command);
					} catch (Exception e) {
						Activator.log.error(e);
					}
						}
					});
	
				}
			});
		} catch (Exception e) {
			Activator.log.error(e);
		}
	}

	/**
	 * 
	 * {@inheritedDoc}
	 */
	@Override
	public void notifyChanged(Notification notification) {
		// change the label of the figure managed by the host edit part (managed
		// by the parent edit
		// part in general...)
		// it must be changed only if:
		// - the annotation corresponding to the display of the stereotype
		// changes
		// - the stereotype application list has changed
		final int eventType = notification.getEventType();
		if(eventType == PapyrusStereotypeListener.APPLIED_STEREOTYPE) {
			// a stereotype was applied to the notifier
			// then a new listener should be added to the stereotype application
			getDiagramEventBroker().addNotificationListener((EObject)notification.getNewValue(), this);
			createAppliedStereotypeCompartment((EObject)notification.getNewValue());
		} else if(eventType == PapyrusStereotypeListener.UNAPPLIED_STEREOTYPE) {
			getDiagramEventBroker().removeNotificationListener((EObject)notification.getOldValue(), this);
			cleanStereotypeDisplayInEAnnotation();
			removeAppliedStereotypeCompartment((EObject)notification.getNewValue());

		}
		// if element that has changed is a stereotype => refresh the label.
		if(notification.getNotifier() instanceof Node && (notification.getEventType() == Notification.ADD) && (notification.getNewValue() instanceof EAnnotation)) {
			if(UMLVisualInformationPapyrusConstant.STEREOTYPE_ANNOTATION == ((EAnnotation)notification.getNewValue()).getSource()) {
				// stereotype annotation has changed => refresh label display
				refreshDisplay();
			}
		}
		// if element that has changed is a stereotype => refresh the label.
		if(notification.getNotifier() instanceof EAnnotation && (notification.getEventType() == Notification.ADD)) {
			if(UMLVisualInformationPapyrusConstant.STEREOTYPE_ANNOTATION == ((EAnnotation)notification.getNotifier()).getSource()) {
				// stereotype annotation has changed => refresh label display
				refreshDisplay();
			}
		}
	}



	/**
	 * this method creates a node for the compartment of stereotype if it does not exist.
	 * 
	 * @param stereotypeApplication
	 *        the stereotype application
	 */
	public void createAppliedStereotypeCompartment(final EObject stereotypeApplication) {
		final GraphicalEditPart editPart = (GraphicalEditPart)getHost();
		final View node = editPart.getNotationView();
		// llok for the coressponded node for the stereotype application
		View correspondedAppliedStereotype = getCoresspondedStereotypeApplication(stereotypeApplication, node);
		//it does not exist
		if(correspondedAppliedStereotype == null) {
			executeAppliedStereotypeCompatmentCreation(editPart, stereotypeApplication);
		}
	}

	/**
	 * the goal of this method is to execute the a command to create a notation node for a compartment of stereotype
	 * 
	 * @param editPart
	 *        the editpart owner of the new compartment
	 * @param appliedstereotype
	 *        the stereotype application
	 */
	protected void setVisivility(final View view, final boolean isVisible) {
		try {
			final GraphicalEditPart editPart = (GraphicalEditPart)getHost();
			editPart.getEditingDomain().runExclusive(new Runnable() {

				public void run() {
					SetNodeVisibilityCommand setCommand = new SetNodeVisibilityCommand(editPart.getEditingDomain(), view, isVisible);
					//use to avoid to put it in the command stack
					try {
						GMFUnsafe.write(editPart.getEditingDomain(), setCommand);
					} catch (Exception e) {
						Activator.log.error(e);
					}
				}
			});
		} catch (Exception e) {
			Activator.log.error(e);
		}
	}

	/**
	 * the goal of this method is to return the node that references the stereotype application
	 * 
	 * @param stereotypeApplication
	 *        the application of the sterotype
	 * @param node
	 *        the notation node where we look for all subnodes
	 * @return the corresponded node or null
	 */
	protected View getCoresspondedStereotypeApplication(final EObject stereotypeApplication, final View node) {
		View correspondedAppliedStereotype = null;
		int i = 0;
		// wee look for through all sub nodes
		while(correspondedAppliedStereotype == null && i < node.getChildren().size()) {
			if((node.getChildren().get(i)) instanceof Node) {
				if(stereotypeApplication.equals(((Node)(node.getChildren().get(i))).getElement())) {
					correspondedAppliedStereotype = ((Node)(node.getChildren().get(i)));
				}
			}
			i++;
		}
		return correspondedAppliedStereotype;
	}

	/**
	 * this method suppress the sub-nodes that references the stereotype application
	 * it cleans also all sub-nodes with the type ApplicationStereotype that not references an application of stereotypes
	 * (this is the case when a stereotype has been unapplied without suppress the compartment.
	 * 
	 * @param stereotypeApplication
	 */
	public void removeAppliedStereotypeCompartment(final EObject stereotypeApplication) {
		if(stereotypeApplication == null) {
			return;
		}
		final GraphicalEditPart editPart = (GraphicalEditPart)getHost();
		final View node = editPart.getNotationView();
		try {
			int i = 0;
			//we go through all sub nodes 
			while(i < node.getChildren().size()) {
				if((node.getChildren().get(i)) instanceof Node) {
					final Node currentNode = (Node)(node.getChildren().get(i));
					//it references the stereotype application?
					if(stereotypeApplication.equals(currentNode.getElement())) {
						//yes, Execution of the Deletion command
						editPart.getEditingDomain().runExclusive(new Runnable() {

							public void run() {
								DeleteCommand command = new DeleteCommand(currentNode);
								//use to avoid to put it in the command stack
								try {
									GMFUnsafe.write(editPart.getEditingDomain(), command);
								} catch (Exception e) {
									Activator.log.error(e);
								}
							}
						});
					}
					// the sub nodes has the type appliedStereotypeCompartment but does not references a application of stereotype
					if((currentNode.getType().equals(AppliedStereotypeConpartmentEditPart.ID)) && (!(currentNode.getElement() instanceof DynamicEObjectImpl))) {
						//yes, Execution of the Deletion command
						editPart.getEditingDomain().runExclusive(new Runnable() {

							public void run() {
								if(currentNode != null && editPart.getEditingDomain() != null) {
									DeleteCommand command = new DeleteCommand(editPart.getEditingDomain(), currentNode);
									//use to avoid to put it in the command stack
									try {
										GMFUnsafe.write(editPart.getEditingDomain(), command);
									} catch (Exception e) {
										Activator.log.error(e);
									}
								}
							}
						});
					}
				}
				i++;
			}
		} catch (Exception e) {
			Activator.log.error(e);
		}
	}

	/**
	 * Refreshes the stereotype display
	 */
	@Override
	protected void refreshAppliedStereotypesPropertiesInCompartment(String stereotypesPropertiesToDisplay, IPapyrusNodeUMLElementFigure figure) {
		final boolean displayInCompartment = AppliedStereotypeHelper.hasAppliedStereotypesPropertiesToDisplay((View)getHost().getModel(), UMLVisualInformationPapyrusConstant.STEREOTYPE_COMPARTMENT_LOCATION);
		// if the string is not empty, then, the figure has to display it. Else,
		// it displays nothing
		final GraphicalEditPart editPart = (GraphicalEditPart)getHost();
		final View node = editPart.getNotationView();
		int i = 0;
		//we go through all sub nodes 
		while(i < node.getChildren().size()) {
			if((node.getChildren().get(i)) instanceof Node) {
				final Node currentNode = (Node)(node.getChildren().get(i));
				if(currentNode.getType().equals(AppliedStereotypeConpartmentEditPart.ID)) {
					EObject stereotypeApplication = currentNode.getElement();
					Stereotype stereotype = UMLUtil.getStereotype(stereotypeApplication);
					if(stereotype != null && stereotypesPropertiesToDisplay.contains(stereotype.getQualifiedName())) {
						setVisivility(currentNode, displayInCompartment);
					} else {
						setVisivility(currentNode, false);
					}
				}
			}
			i++;
		}
	}
}

Back to the top