Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 01d0e109d7c071e88608e66625cf104d7ac64246 (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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*****************************************************************************
 * Copyright (c) 2009, 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:
 *  Remi Schnekenburger (CEA LIST) remi.schnekenburger@cea.fr - Initial API and implementation
 *  Nizar GUEDIDI (CEA LIST) - update getUMLElement()
 *  Christian W. Damus (CEA) - bug 440197
 *  Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.net - Bug 393532
 *  Celine JANSSENS (ALL4TEC) celine.janssens@all4tec.net - Bug 455311 Stereotype Display
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.common.editpolicies;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.diagram.core.commands.DeleteCommand;
import org.eclipse.gmf.runtime.diagram.core.listener.DiagramEventBroker;
import org.eclipse.gmf.runtime.diagram.core.listener.NotificationListener;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.gef.ui.internal.editpolicies.GraphicalEditPolicyEx;
import org.eclipse.gmf.runtime.notation.BasicCompartment;
import org.eclipse.gmf.runtime.notation.DecorationNode;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.core.listenerservice.IPapyrusListener;
import org.eclipse.papyrus.infra.gmfdiag.common.model.NotationUtils;
import org.eclipse.papyrus.uml.diagram.common.Activator;
import org.eclipse.papyrus.uml.diagram.common.stereotype.display.command.CreateAppliedStereotypeCompartmentCommand;
import org.eclipse.papyrus.uml.diagram.common.stereotype.display.command.CreateAppliedStereotypePropertyViewCommand;
import org.eclipse.papyrus.uml.diagram.common.stereotype.display.command.CreateStereotypeLabelCommand;
import org.eclipse.papyrus.uml.diagram.common.stereotype.display.helper.StereotypeDisplayConstant;
import org.eclipse.papyrus.uml.diagram.common.stereotype.display.helper.StereotypeDisplayUtil;
import org.eclipse.papyrus.uml.diagram.common.stereotype.migration.StereotypeMigrationHelper;
import org.eclipse.papyrus.uml.diagram.common.util.CommandUtil;
import org.eclipse.papyrus.uml.tools.listeners.StereotypeElementListener.StereotypeExtensionNotification;
import org.eclipse.swt.graphics.Image;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Extension;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Stereotype;

/**
 * Specific edit policy for label displaying stereotypes and their properties
 * for representing UML elements.
 */
public abstract class AbstractAppliedStereotypeDisplayEditPolicy extends GraphicalEditPolicyEx implements NotificationListener, IPapyrusListener {

	protected String EMPTY_STRING = "";//$NON-NLS-1$

	/** constant for this edit policy role */
	public final static String STEREOTYPE_LABEL_POLICY = "AppliedStereotypeDisplayEditPolicy";//$NON-NLS-1$

	/** host semantic element */
	protected Element hostSemanticElement;

	/** Helper to manipulate applied Stereotype Display model */
	protected StereotypeDisplayUtil helper = StereotypeDisplayUtil.getInstance();
	protected StereotypeMigrationHelper migrationHelper = StereotypeMigrationHelper.getInstance();



	protected IGraphicalEditPart hostEditPart;

	protected View hostView = null;

	protected EList<Stereotype> stereotypeList;


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

	/**
	 *
	 * {@inheritDoc}
	 */
	@Override
	public void activate() {

		if (getHost() instanceof IGraphicalEditPart) {
			initialisation();
			// getDiagramEventBroker().addNotificationListener(view, this);
			getDiagramEventBroker().addNotificationListener(hostSemanticElement, this);

			// Create and Delete nodes if necessary
			refreshNotationStructure();


		}

	}



	/**
	 * Initialize Variables.
	 */
	private void initialisation() {
		hostEditPart = (IGraphicalEditPart) getHost();
		hostSemanticElement = getUMLElement();
		hostView = hostEditPart.getNotationView();
		if (hostSemanticElement != null) {
			stereotypeList = hostSemanticElement.getAppliedStereotypes();
		}
	}



	/**
	 * @see org.eclipse.gmf.runtime.gef.ui.internal.editpolicies.GraphicalEditPolicyEx#refresh()
	 *      This method must extend GraphicalEditPolicyEx, in order to call the edit policy refresh method when the EditPart is Refreshed
	 */
	@Override
	public void refresh() {

		initialisation();
		refreshDisplay();
	}

	/**
	 * Refreshes the display for the element controlled by the edit part with
	 * this edit policies
	 */
	public abstract void refreshDisplay();



	/**
	 * 
	 * {@inheritedDoc}
	 */
	@Override
	public void notifyChanged(Notification notification) {

		int eventType = notification.getEventType();

		if (eventType == StereotypeExtensionNotification.STEREOTYPE_APPLIED_TO_ELEMENT) {
			initialisation();
			refreshNotationStructure();

		} else if (eventType == StereotypeExtensionNotification.STEREOTYPE_UNAPPLIED_FROM_ELEMENT) {
			initialisation();
			refreshNotationStructure();
		}
	}

	/**
	 * Create the Notation Structure according to the UML Model for the Stereotypes
	 */
	public void refreshNotationStructure() {

		if (hostView != null) {

			removeUnappliedStereotypes(hostView);

			if (!stereotypeList.isEmpty()) {
				refreshStereotypeStructure();
			}
		}
	}

	/**
	 * Refresh the NotationStructure of the Node for the StereotypeList
	 * 
	 */
	public void refreshStereotypeStructure() {

		// rebuild the structure from the Stereotype List
		if (!stereotypeList.isEmpty()) {
			for (Stereotype stereotype : stereotypeList) {
				refreshStereotypeLabelStructure(stereotype);
				refreshStereotypeBraceStructure(stereotype);
			}
		}
	}

	/**
	 * Refresh Brace Compartment and Properties Structure into notation model.
	 * 
	 * @param stereotype
	 *            The stereotype for which the Structure is Refreshed
	 */
	public void refreshStereotypeBraceStructure(Stereotype stereotype) {
		BasicCompartment compartment = helper.getStereotypeBraceCompartment(hostView, stereotype);
		if (compartment == null) { // No Label Exist for this Stereotype
			createAppliedStereotypeBraceCompartment(stereotype);
			createAppliedStereotypeBraceProperties(stereotype);

		}

	}

	/**
	 * Refresh The StereotypeLabel notation structure.
	 * 
	 * @param stereotype
	 *            The stereotype of which the Label has to be refreshed.
	 */
	public void refreshStereotypeLabelStructure(Stereotype stereotype) {

		DecorationNode label = helper.getStereotypeLabel(hostView, stereotype);
		if (label == null) { // No Label Exist for this Stereotype
			createAppliedStereotypeLabel(stereotype);
		}
	}

	/**
	 * Returns the image to be displayed for the applied stereotypes.
	 *
	 * @return the image that represents the first applied stereotype or <code>null</code> if no image has to be displayed
	 */
	public Image stereotypeIconToDisplay() {
		Image icon = null;
		boolean displayIcon = NotationUtils.getBooleanValue(hostView, StereotypeDisplayConstant.DISPLAY_ICON, false);
		if (displayIcon) {
			// retrieve the first stereotype in the list of displayed stereotype
			Stereotype appliedStereotype;
			Iterator<Stereotype> stereotypeIterator = getUMLElement().getAppliedStereotypes().iterator();
			while (stereotypeIterator.hasNext() && null == icon) {
				appliedStereotype = stereotypeIterator.next();
				icon = Activator.getIconElement(getUMLElement(), appliedStereotype, false);
			}

		}
		return icon;
	}

	/**
	 * This method creates a node for the compartment of stereotype if it does not exist.
	 *
	 * @param stereotype
	 *            the stereotype of which the Label is created
	 */
	protected void createAppliedStereotypeLabel(final Stereotype stereotype) {

		final View node = hostEditPart.getNotationView();
		// create only if the Label doesn't exist yet
		if (!helper.isLabelExist(node, stereotype)) {
			// Create the Decoration Node
			executeStereotypeLabelCreation(hostEditPart, stereotype);
		}
	}


	/**
	 * This method creates a node for the compartment of stereotype if it does not exist.
	 *
	 * @param stereotypeApplication
	 *            the stereotype application
	 */
	protected void createAppliedStereotypeBraceCompartment(final Stereotype stereotype) {
		final View node = hostEditPart.getNotationView();
		// doesn't exist already
		if (!helper.isBraceCompartmentExist(node, stereotype)) {
			// Create Compartment
			executeAppliedStereotypeBraceCompartmentCreation(hostEditPart, stereotype);

		}
	}

	/**
	 * In charge of properties view creation
	 * 
	 * @param stereotype
	 *            The stereotype of which the Properties should be created
	 */
	protected void createAppliedStereotypeBraceProperties(final Stereotype stereotype) {

		Node compartment = helper.getStereotypeBraceCompartment(hostEditPart.getNotationView(), stereotype);
		if (compartment != null && stereotype != null) {

			EList<Property> properties = stereotype.allAttributes();
			for (Property property : properties) {
				createAppliedStereotypeBraceProperty(compartment, property);
			}
		}
	}



	/**
	 * In Charge of PropertyView Creation
	 * 
	 * @param compartment
	 *            The Compartment owner of the Property that will be created
	 * @param property
	 *            The UML Property of the View to create
	 */
	protected void createAppliedStereotypeBraceProperty(Node compartment, Property property) {
		// if stereotype is null all property of stereotype has to be removed!
		if (property != null && !property.getName().startsWith(Extension.METACLASS_ROLE_PREFIX)) {
			if (!helper.isBracePropertyExist(compartment, property)) {
				// go through each stereotype property
				executeAppliedStereotypeBracePropertyViewCreation(hostEditPart, compartment, property);

			}
		}
	}

	/**
	 * List on all the existing node contained into the main view.
	 * For each node related to the stereotype structure, check if the related Stereotype is still applied
	 * If no, remove the non relevant node.
	 * 
	 * @param view
	 *            The main parent view of the host EditPart
	 */
	protected void removeUnappliedStereotypes(View view) {

		if (view != null) {
			List<Object> children = new ArrayList<Object>(view.getChildren());
			Iterator<Object> iter = children.iterator();
			while (iter.hasNext()) {
				Object child = iter.next();
				if (helper.isStereotypeView(child)) {
					if (((View) child).getElement() instanceof Stereotype) {
						Stereotype childStereotype = (Stereotype) ((View) child).getElement();
						if (!hostSemanticElement.isStereotypeApplied(childStereotype)) {
							executeStereotypeViewRemove(hostEditPart, (View) child);
						}
					}
				}
			}
		}
	}

	/**
	 * 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 executeAppliedStereotypeBraceCompartmentCreation(final IGraphicalEditPart editPart, final Stereotype stereotype) {
		CreateAppliedStereotypeCompartmentCommand command = new CreateAppliedStereotypeCompartmentCommand(editPart.getEditingDomain(), editPart.getNotationView(), stereotype, StereotypeDisplayConstant.STEREOTYPE_BRACE_TYPE);
		CommandUtil.executeUnsafeCommand(command, editPart);

	}


	/**
	 * This method is used to create a notation node for the property of the stereotype.
	 *
	 * @param editPart
	 *            the editPart container
	 * @param compartment
	 * @param stereotypeApplication
	 * @param stereotype
	 *            the stereotype associated to compartment node
	 */
	protected void executeAppliedStereotypeBracePropertyViewCreation(final IGraphicalEditPart editPart, final Node compartment, final Property stereotypeProperty) {
		CreateAppliedStereotypePropertyViewCommand command = new CreateAppliedStereotypePropertyViewCommand(editPart.getEditingDomain(), compartment, stereotypeProperty, StereotypeDisplayConstant.STEREOTYPE_PROPERTY_BRACE_TYPE);
		CommandUtil.executeUnsafeCommand(command, editPart);

	}

	/**
	 * The goal of this method is to execute the a command to create a notation node for a stereotype Label.
	 *
	 * @param editPart
	 *            The editPart owner of the new compartment
	 * @param stereotype
	 *            The stereotype related to the Label
	 */
	protected void executeStereotypeLabelCreation(final IGraphicalEditPart editPart, final Stereotype stereotype) {
		CreateStereotypeLabelCommand command = new CreateStereotypeLabelCommand(editPart.getEditingDomain(), editPart.getNotationView(), stereotype);
		CommandUtil.executeUnsafeCommand(command, editPart);


	}

	/**
	 * Gets the diagram event broker from the editing domain.
	 *
	 * @return the diagram event broker
	 */
	protected DiagramEventBroker getDiagramEventBroker() {
		TransactionalEditingDomain theEditingDomain = ((IGraphicalEditPart) getHost()).getEditingDomain();
		if (theEditingDomain != null) {
			return DiagramEventBroker.getInstance(theEditingDomain);
		}
		return null;
	}

	/**
	 * Returns the uml element controlled by the host edit part
	 *
	 * @return the uml element controlled by the host edit part
	 */
	protected Element getUMLElement() {
		EObject element = getView().getElement();
		if (element instanceof Element) {
			return (Element) element;
		}
		return null;
	}

	/**
	 * Returns the view controlled by the host edit part
	 *
	 * @return the view controlled by the host edit part
	 */
	protected View getView() {
		return (View) getHost().getModel();
	}

	/**
	 * Execute the command to remove the Stereotype.
	 * 
	 * @param editPart
	 *            Edit Part on which the command is executed
	 * @param label
	 *            DecorationNode of the Stereotype Label that has to be removed
	 */
	protected void executeStereotypeViewRemove(final IGraphicalEditPart editPart, final View view) {

		DeleteCommand command = new DeleteCommand(view);
		CommandUtil.executeUnsafeCommand(command, editPart);

	}


	/**
	 *
	 * {@inheritDoc}
	 */
	@Override
	public void deactivate() {
		// retrieve the view and the element managed by the edit part
		View view = getView();
		if (view != null) {
			getDiagramEventBroker().removeNotificationListener(view, this);
			if (hostSemanticElement != null) {

				// remove listeners to applied stereotyped
				for (EObject stereotypeApplication : hostSemanticElement.getStereotypeApplications()) {
					getDiagramEventBroker().removeNotificationListener(stereotypeApplication, this);
				}
				// remove notification on element
				getDiagramEventBroker().removeNotificationListener(hostSemanticElement, this);
				// removes the reference to the semantic element
				hostSemanticElement = null;
			}
		}
	}

}

Back to the top