Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: abf19823ad5dd559e1af466e389aa1b982bb39b2 (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
464
465
466
467
468
469
470
471
/*****************************************************************************
 * Copyright (c) 2009-2010 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:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.common.service;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.ImageFigure;
import org.eclipse.draw2d.Locator;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
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.diagram.ui.editpolicies.XYLayoutEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.services.decorator.IDecoration;
import org.eclipse.gmf.runtime.diagram.ui.services.decorator.IDecorator;
import org.eclipse.gmf.runtime.diagram.ui.services.decorator.IDecoratorTarget;
import org.eclipse.gmf.runtime.diagram.ui.services.decorator.IDecoratorTarget.Direction;
import org.eclipse.gmf.runtime.draw2d.ui.mapmode.IMapMode;
import org.eclipse.gmf.runtime.draw2d.ui.mapmode.MapModeUtil;
import org.eclipse.gmf.runtime.notation.DecorationNode;
import org.eclipse.gmf.runtime.notation.DescriptionStyle;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.uml.diagram.common.Activator;
import org.eclipse.papyrus.uml.diagram.common.layout.OverlayLocator;
import org.eclipse.papyrus.uml.diagram.common.util.DiagramEditPartsUtil;
import org.eclipse.papyrus.uml.diagram.common.util.Util;
import org.eclipse.swt.graphics.Image;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Operation;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Type;
import org.eclipse.uml2.uml.UMLPackage;

/**
 * 
 * The decorator to represent inherited element This decorator adds a small
 * image ( the generalization icon) next to the UML Element which are inherited.
 * 3 positions are defined for the decoration :
 * <ul>
 * <li>if the UML Element is represented like an affixed child node : in {@link PositionConstants#NORTH_WEST} or {@link PositionConstants#SOUTH_EAST}
 * following its position/parent and margin =1</li>
 * <li>else if the element is in a compartment list : {@link PositionConstants#EAST} and margin =-1</li>
 * <li>else {@link PositionConstants#SOUTH_EAST} and margin = -1</li>
 * </ul>
 */
public class InheritedDecorator implements IDecorator {

	/** the object to be decorated */
	private IDecoratorTarget decoratorTarget;

	/** the decoration being displayed */
	private IDecoration decoration;

	/** the plugin where owning the icons for the UML Element */
	public static final String pluginID = "org.eclipse.papyrus.uml.diagram.common"; //$NON-NLS-1$

	/** the image path */
	public static final String imagePath = "/icons/hyperlink_13x13.gif"; //$NON-NLS-1$

	/** the image used added to represent an inherited element */
	private static final Image ICON_HYPERLINK = Activator.getPluginIconImage(pluginID, imagePath);

	/**
	 * Creates a new <code>AbstractDecorator</code> for the decorator target
	 * passed in.
	 * 
	 * @param decoratorTarget
	 *        the object to be decorated
	 */
	public InheritedDecorator(IDecoratorTarget decoratorTarget) {
		this.decoratorTarget = decoratorTarget;
	}

	/**
	 * Gets the object to be decorated.
	 * 
	 * @return Returns the object to be decorated
	 */
	protected IDecoratorTarget getDecoratorTarget() {
		return decoratorTarget;
	}

	/**
	 * @return Returns the decoration.
	 */
	public IDecoration getDecoration() {
		return decoration;
	}

	/**
	 * @param decoration
	 *        The decoration to set.
	 */
	public void setDecoration(IDecoration decoration) {
		this.decoration = decoration;
	}

	/**
	 * Removes the decoration if it exists and sets it to null.
	 */
	protected void removeDecoration() {
		if(decoration != null) {
			decoratorTarget.removeDecoration(decoration);
			decoration = null;
		}
	}

	/**
	 * getDecoratorTargetClassifier Utility method to determine if the
	 * decoratorTarget is a supported type for this decorator and return the
	 * associated Classifier element.
	 * 
	 * @param decoratorTarget
	 *        IDecoratorTarget to check and return valid Classifier target.
	 * @return node Node if IDecoratorTarget can be supported, null otherwise.
	 */
	static public Node getDecoratorTargetNode(IDecoratorTarget decoratorTarget) {
		DescriptionStyle descStyle = null;
		View node = (View)decoratorTarget.getAdapter(View.class);
		if(node != null && !(node instanceof Diagram)) {
			descStyle = (DescriptionStyle)node.getStyle(NotationPackage.eINSTANCE.getDescriptionStyle());

			if(descStyle != null) {
				return (Node)node;
			}
		}
		return null;

	}

	/**
	 * Creates the appropriate review decoration if all the criteria is
	 * satisfied by the view passed in.
	 */

	public void refresh() {
		removeDecoration();

		Node node = getDecoratorTargetNode(getDecoratorTarget());
		IGraphicalEditPart gep = (IGraphicalEditPart)getDecoratorTarget().getAdapter(IGraphicalEditPart.class);

		if(node != null) {
			DescriptionStyle descStyle = getDescriptionStyle(node);

			if(descStyle != null) {
				if(isInherited(node)) {
					// if(Util.isAffixedChildNode(gep)) {

					// setDecoration(getDecoratorTarget().addDecoration(figure,
					// locator, false));
					// } else {
					// setDecoration(getDecoratorTarget().addShapeDecoration(figure,
					// getDirection(node), -1, false));
					// }

					if(gep != null && gep.getRoot() != null) {// if the gep has
																// no parent, we
																// can't test if
																// the container
																// is a
																// compartment
																// list
																// (because, we
																// call the
																// method
																// DiagramEditPartsUtil.getEditPartFromView((View)container,
																// gep);
						IFigure figure = getFigure(ICON_HYPERLINK);
						if(isInCompartmentList(node) && !Util.isAffixedChildNode(gep)) {
							setDecoration(getDecoratorTarget().addShapeDecoration(figure, getDirection(node), -1, false));
						} else {
							Locator locator = new OverlayLocator(gep.getFigure(), getDirection(node));
							setDecoration(getDecoratorTarget().addDecoration(figure, locator, false));
						}
					}

				}
			}
		}
	}

	/**
	 * Returns a figure corresponding to this image
	 * 
	 * @param image
	 *        a image
	 * @return a figure corresponding to this image
	 */
	public IFigure getFigure(Image image) {
		IMapMode mm = MapModeUtil.getMapMode(((IGraphicalEditPart)getDecoratorTarget().getAdapter(IGraphicalEditPart.class)).getFigure());
		ImageFigure fig = new ImageFigure();
		fig.setImage(image);
		fig.setSize(mm.DPtoLP(image.getBounds().width), mm.DPtoLP(image.getBounds().height));
		return fig;
	}

	/**
	 * Returns the direction to set the decorator for the node
	 * 
	 * @param node
	 *        the node
	 * @return the direction to set the decorator for the node direction can be
	 *         :
	 *         <ul>
	 *         <li> {@link PositionConstants#NORTH_WEST} or {@link PositionConstants#SOUTH_EAST}</li> if the node is an Affixed Child Node
	 *         <li>{@link PositionConstants#EAST}</li> if the node is in a compartment list
	 *         <li>{@link PositionConstants#SOUTH_EAST}</li> in other cases
	 *         </ul>
	 */
	protected Direction getDirection(Node node) {
		IGraphicalEditPart gep = (IGraphicalEditPart)getDecoratorTarget().getAdapter(IGraphicalEditPart.class);
		assert gep != null;
		// test if its an affixed ChildNode
		// if(Util.isAffixedChildNode(gep)) {
		//
		// IBorderItemLocator loc =
		// ((BorderNamedElementEditPart)gep).getBorderItemLocator();
		// int location = loc.getCurrentSideOfParent();
		// if(PositionConstants.NONE == location) { //sometimes
		// getBorderItemLocator doesn't work correctly!
		// location = PositionConstants.NORTH_WEST;
		// }
		// switch(location) {
		// case PositionConstants.NORTH:
		// case PositionConstants.NORTH_WEST:
		// case PositionConstants.WEST:
		// case PositionConstants.SOUTH_WEST:
		// // return IDecoratorTarget.Direction.NORTH_WEST;
		// default:
		// return IDecoratorTarget.Direction.SOUTH_EAST;
		// }
		// }
		if(gep.getParent() != null) {
			if(isInCompartmentList(node) && !Util.isAffixedChildNode(gep)) {
				return IDecoratorTarget.Direction.EAST;
			}
		}
		return IDecoratorTarget.Direction.SOUTH_WEST;
	}

	/**
	 * Tests if the compartment is a compartment list
	 * 
	 * @param node
	 *        the node on which we want add an Overlay
	 * @return <code>true</code> if the compartment is managed by an {@link XYLayoutEditPolicy}
	 */
	protected boolean isInCompartmentList(Node node) {
		IGraphicalEditPart gep = (IGraphicalEditPart)getDecoratorTarget().getAdapter(IGraphicalEditPart.class);
		if(gep != null && gep.getRoot() != null) {
			EObject container = node.eContainer();
			if(container instanceof View) {
				EditPart EP = DiagramEditPartsUtil.getEditPartFromView((View)container, gep);
				EditPolicy editPolicy = EP.getEditPolicy(EditPolicy.LAYOUT_ROLE);
				if(!(editPolicy instanceof XYLayoutEditPolicy)) {// we are in a
																	// compartment
																	// list
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * Tests if the node is an inherited element
	 * 
	 * @param node
	 *        a node
	 * @return <code>true</code> if the node is an inherited element <code>false</code> if not
	 */
	protected boolean isInherited(Node node) {
		EObject element = node.getElement();
		if(element instanceof Element) {
			EObject container = node.eContainer();
			EObject graphicalParent = null;
			if(container instanceof DecorationNode) {
				graphicalParent = ((DecorationNode)container).getElement();
			} else if(container instanceof View) {
				graphicalParent = ((View)container).getElement();
			}
			if(graphicalParent instanceof Property || graphicalParent instanceof Classifier) {
				Classifier classifier = null;
				if(graphicalParent instanceof Property) {
					Type type = ((Property)graphicalParent).getType();
					if(type instanceof Classifier) {
						classifier = (Classifier)type;
					}
				} else {
					classifier = (Classifier)graphicalParent;
				}
				if(classifier != null) {
					EList<NamedElement> inheritedMembers = classifier.getInheritedMembers();
					return inheritedMembers.contains(element);
				}
			}
		}
		return false;
	}

	/**
	 * getDescriptionStyle Accessor to retrieve the description style from a
	 * Node.
	 * 
	 * @param node
	 *        Node to retrieve the description style from.
	 * @return DescriptionStyle style object
	 */
	protected DescriptionStyle getDescriptionStyle(Node node) {
		return (DescriptionStyle)node.getStyle(NotationPackage.eINSTANCE.getDescriptionStyle());
	}

	/**
	 * A listener used to listen the change of location and type (for Property)
	 */
	private NotificationListener notificationListener = new NotificationListener() {

		/**
		 * 
		 * @see org.eclipse.gmf.runtime.diagram.core.listener.NotificationListener#notifyChanged(org.eclipse.emf.common.notify.Notification)
		 * 
		 * @param notification
		 */
		public void notifyChanged(Notification notification) {

			if(notification.getEventType() == Notification.REMOVE) {
				if(notification.getNotifier() instanceof Classifier) {
					IGraphicalEditPart gep = (IGraphicalEditPart)getDecoratorTarget().getAdapter(IGraphicalEditPart.class);
					assert gep != null;
					// we remove the listener on the container (because it's
					// changing
					DiagramEventBroker.getInstance(gep.getEditingDomain()).removeNotificationListener((EObject)notification.getNotifier(), notificationListener);
				}
			}
			// we update the listeners It's useful when an Element with overlay
			// changes of parent
			deactivate();
			activate();
			refresh();
		}

	};

	/**
	 * Adds listeners on
	 * <ul>
	 * <li>Affixed Child Node</li>
	 * <li>graphical parent, when its a {@link Property} (we add the listener on its Type)</li>
	 * </ul>
	 */
	public void activate() {

		IGraphicalEditPart gep = (IGraphicalEditPart)getDecoratorTarget().getAdapter(IGraphicalEditPart.class);
		assert gep != null;
		View view = ((View)gep.getModel());

		if(view instanceof Node) {
			// the location of the decorator can change if it's an Affixed Child
			// Node
			if(isInherited((Node)view) && Util.isAffixedChildNode(gep)) {
				DiagramEventBroker.getInstance(gep.getEditingDomain()).addNotificationListener(gep.getNotationView(), notificationListener);
			}
		}

		// if the graphical parent is a Property, we add a listener on the type
		// of the property, to refresh the decoration
		EObject parent = view.eContainer();
		if(parent instanceof DecorationNode) {
			parent = parent.eContainer();
		}
		if(parent instanceof View) {
			EObject el = ((View)parent).getElement();
			if(el instanceof Property) {
				DiagramEventBroker.getInstance(gep.getEditingDomain()).addNotificationListener(el, UMLPackage.eINSTANCE.getTypedElement_Type(), notificationListener);
			}
		}

		/*
		 * We listen the changes on the UML parent, in order to know if the
		 * element is changing of parent Adding a listener using the following
		 * EReference doesn't work UMLPackage.eINSTANCE.getElement_Owner();
		 * UMLPackage.eINSTANCE.getProperty_Class();
		 * UMLPackage.eINSTANCE.getNamedElement_Namespace(); that's why we
		 * listen the parent
		 */
		if(view.getElement() instanceof Element) {
			Element semanticElement = (Element)view.getElement();

			/*
			 * We need add a listener only if the element is an element which
			 * can be inherited, like Property, Operation, Signal, Classifier...
			 */
			if(semanticElement != null && canBeInherited(semanticElement)) {
				// we listen if the container of the element changes!
				if(semanticElement.eContainer() != null) {
					DiagramEventBroker.getInstance(gep.getEditingDomain()).addNotificationListener(semanticElement.eContainer(), notificationListener);
				}
			}
		}
	}

	/**
	 * Tests if the element can be inherited
	 * 
	 * @param semanticElement
	 *        the element to test
	 * @return <code>true</code> if the element can be inherited
	 */
	protected boolean canBeInherited(Element semanticElement) {
		/*
		 * maybe we could replace these tests by RedefinableElement? or not?
		 */

		if(semanticElement instanceof Classifier) {
			return true;
		} else if(semanticElement instanceof Property) {
			return true;
		} else if(semanticElement instanceof Operation) {
			return true;
		}
		return false;
	}

	/**
	 * Removes the listeners and the decorations
	 */
	public void deactivate() {
		removeDecoration();

		IGraphicalEditPart gep = (IGraphicalEditPart)getDecoratorTarget().getAdapter(IGraphicalEditPart.class);
		assert gep != null;
		DiagramEventBroker.getInstance(gep.getEditingDomain()).removeNotificationListener(gep.getNotationView(), notificationListener);
		View view = ((View)gep.getModel());

		EObject parent = view.eContainer();
		if(parent instanceof View) {
			EObject el = ((View)parent).getElement();
			if(el instanceof Property) {
				DiagramEventBroker.getInstance(gep.getEditingDomain()).removeNotificationListener(el, UMLPackage.eINSTANCE.getTypedElement_Type(), notificationListener);
			}
		}

		if(view.getElement() instanceof Element) {
			Element semanticElement = (Element)view.getElement();
			if(semanticElement != null) {
				if(semanticElement.eContainer() != null) {
					DiagramEventBroker.getInstance(gep.getEditingDomain()).removeNotificationListener(semanticElement.eContainer(), notificationListener);
				}
			}
		}
	}
}

Back to the top