Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a32daffe631145d6ca7593ae7b70d24a703623c4 (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
/*****************************************************************************
 * Copyright (c) 2011 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:
 *		
 *		CEA LIST - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.symbols.provider;

import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.diagram.core.listener.DiagramEventBroker;
import org.eclipse.gmf.runtime.diagram.core.listener.NotificationListener;
import org.eclipse.gmf.runtime.draw2d.ui.render.RenderedImage;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.emf.utils.BusinessModelResolver;
import org.eclipse.papyrus.infra.gmfdiag.common.service.shape.AbstractShapeProvider;
import org.eclipse.papyrus.infra.gmfdiag.common.service.shape.ProviderNotificationManager;
import org.eclipse.papyrus.uml.diagram.symbols.Activator;
import org.eclipse.uml2.uml.AcceptEventAction;
import org.eclipse.uml2.uml.ActivityFinalNode;
import org.eclipse.uml2.uml.DecisionNode;
import org.eclipse.uml2.uml.DurationObservation;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Event;
import org.eclipse.uml2.uml.FlowFinalNode;
import org.eclipse.uml2.uml.InitialNode;
import org.eclipse.uml2.uml.MergeNode;
import org.eclipse.uml2.uml.SendSignalAction;
import org.eclipse.uml2.uml.TimeEvent;
import org.eclipse.uml2.uml.TimeObservation;
import org.eclipse.uml2.uml.Trigger;
import org.eclipse.uml2.uml.TypedElement;
import org.eclipse.uml2.uml.UMLPackage;
import org.w3c.dom.svg.SVGDocument;


/**
 * Shape Provider for {@link DurationObservation} or for {@link TypedElement} that are typed by an {@link DurationObservation}.
 */
public class UMLElementShapeProvider extends AbstractShapeProvider {

	private static final String FLAG_SVG_PATH = "/icons/symbols/flag.svg";

	private static final String HOURGLASS_SVG_PATH = "/icons/symbols/hourglass.svg";

	private static final String DURATION_OBSERVATION_SVG_PATH = "/icons/symbols/DurationObservation.svg";

	private static final String TIME_OBSERVATION_SVG_PATH = "/icons/symbols/TimeObservation.svg";

	/** The Constant SYMBOL_SVG_PATH. */
	private static final String ROUND_WITH_DOT_SVG_PATH = "/icons/symbols/round_with_dot.svg";

	/** The Constant SYMBOL_SVG_PATH. */
	private static final String ROUND_FULL_SVG_PATH = "/icons/symbols/round_full.svg";

	/** The Constant SYMBOL_SVG_PATH. */
	private static final String ROUND_WITH_CROSS_SVG_PATH = "/icons/symbols/round_with_cross.svg";

	/** The Constant SYMBOL_SVG_PATH. */
	private static final String ARROW_SVG_PATH = "/icons/symbols/arrow.svg";

	/** The Constant SYMBOL_SVG_PATH. */
	private static final String DIAMOND_SVG_PATH = "/icons/symbols/diamond.svg";

	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<RenderedImage> getShapes(EObject view) {
		if (providesShapes(view)) {
			List<SVGDocument> documents = getSVGDocument(view);

			if (documents != null) {
				List<RenderedImage> result = new LinkedList<RenderedImage>();
				for (SVGDocument document : documents) {
					try {
						result.add(renderSVGDocument(view, document));
					} catch (IOException ex) {
						Activator.log.error(ex);
						continue;
					}
				}
				return result;
			}
		}
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean providesShapes(EObject view) {
		if (!(view instanceof View)) {
			return false;
		}

		EObject element = ((View) view).getElement();

		if (element instanceof DurationObservation || (element instanceof TypedElement && ((TypedElement) element).getType() instanceof DurationObservation)) {
			return true;
		}// duration
		if (element instanceof TimeObservation || (element instanceof TypedElement && ((TypedElement) element).getType() instanceof TimeObservation)) {
			return true;
		}// time
		if (element instanceof SendSignalAction || (element instanceof TypedElement && ((TypedElement) element).getType() instanceof SendSignalAction)) {
			return true;
		}// Arrow
		if (element instanceof DecisionNode || (element instanceof TypedElement && ((TypedElement) element).getType() instanceof DecisionNode)) {
			return true;
		}// Diamond
		if (element instanceof MergeNode || (element instanceof TypedElement && ((TypedElement) element).getType() instanceof MergeNode)) {
			return true;
		}// Diamond
		if (element instanceof AcceptEventAction || (element instanceof TypedElement && ((TypedElement) element).getType() instanceof AcceptEventAction)) {
			return true;
		}// flag and hourglass
		if (element instanceof InitialNode || (element instanceof TypedElement && ((TypedElement) element).getType() instanceof InitialNode)) {
			return true;
		}// roundfull
		if (element instanceof FlowFinalNode || (element instanceof TypedElement && ((TypedElement) element).getType() instanceof FlowFinalNode)) {
			return true;
		}// roundWCross
		if (element instanceof ActivityFinalNode || (element instanceof TypedElement && ((TypedElement) element).getType() instanceof ActivityFinalNode)) {
			return true;
		}// RoundWDot

		return false;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public ProviderNotificationManager createProviderNotificationManager(DiagramEventBroker diagramEventBroker, EObject view, NotificationListener listener) {
		// retrieve semantic element from the view and add a notification listener on the Type feature if the semantic element is a TypedElement
		if (view == null || !(view instanceof View)) {
			return null;
		}

		DurationObservationShapeProviderNotificationManager notificationManager = new DurationObservationShapeProviderNotificationManager(diagramEventBroker, view, listener);
		return notificationManager;
	}

	/**
	 * Notification Manager for the {@link UMLElementShapeProvider}.
	 */
	public class DurationObservationShapeProviderNotificationManager extends ProviderNotificationManager implements NotificationListener {

		/**
		 * Creates a new DurationObservationShapeProviderNotificationManager.
		 * 
		 * @param diagramEventBroker
		 *            event broker specific to the diargam displaying the shapes.
		 * @param view
		 *            the view from which all elements to listen will be computed.
		 * @param listener
		 *            the listener to which notifications will be forwarded.
		 */
		public DurationObservationShapeProviderNotificationManager(DiagramEventBroker diagramEventBroker, EObject view, NotificationListener listener) {
			super(diagramEventBroker, view, listener);
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		protected void registerListeners() {
			if (view == null || !(view instanceof View)) {
				return;
			}
			Object semanticElement = BusinessModelResolver.getInstance().getBusinessModel(view);
			if (semanticElement instanceof Element) {
				diagramEventBroker.addNotificationListener((Element) semanticElement, this);
			}
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		public void dispose() {
			if (view == null || !(view instanceof View)) {
				return;
			}
			Object semanticElement = BusinessModelResolver.getInstance().getBusinessModel(view);
			if (semanticElement instanceof Element) {
				diagramEventBroker.removeNotificationListener((Element) semanticElement, this);
			}
			super.dispose();
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		public void notifyChanged(Notification notification) {
			if (listener == null) {
				return;
			}
			if (UMLPackage.eINSTANCE.getTypedElement_Type().equals(notification.getFeature())) {
				listener.notifyChanged(notification);
			}
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<SVGDocument> getSVGDocument(EObject view) {
		if (providesShapes(view)) {
			EObject element = ((View) view).getElement();
			URI uri = URI.createPlatformPluginURI("", true);
			if (element instanceof DurationObservation || (element instanceof TypedElement && ((TypedElement) element).getType() instanceof DurationObservation)) {
				uri = URI.createPlatformPluginURI(org.eclipse.papyrus.uml.diagram.common.Activator.ID + DURATION_OBSERVATION_SVG_PATH, true);
			}// duration
			if (element instanceof TimeObservation || (element instanceof TypedElement && ((TypedElement) element).getType() instanceof TimeObservation)) {
				uri = URI.createPlatformPluginURI(org.eclipse.papyrus.uml.diagram.common.Activator.ID + TIME_OBSERVATION_SVG_PATH, true);
			}// time
			if (element instanceof SendSignalAction || (element instanceof TypedElement && ((TypedElement) element).getType() instanceof SendSignalAction)) {
				uri = URI.createPlatformPluginURI(org.eclipse.papyrus.infra.gmfdiag.common.Activator.ID + ARROW_SVG_PATH, true);
			}// Arrow
			if (element instanceof DecisionNode || (element instanceof TypedElement && ((TypedElement) element).getType() instanceof DecisionNode)) {
				uri = URI.createPlatformPluginURI(org.eclipse.papyrus.infra.gmfdiag.common.Activator.ID + DIAMOND_SVG_PATH, true);
			}// Diamond
			if (element instanceof MergeNode || (element instanceof TypedElement && ((TypedElement) element).getType() instanceof MergeNode)) {
				uri = URI.createPlatformPluginURI(org.eclipse.papyrus.infra.gmfdiag.common.Activator.ID + DIAMOND_SVG_PATH, true);
			}// Diamond
			if (element instanceof AcceptEventAction || (element instanceof TypedElement && ((TypedElement) element).getType() instanceof AcceptEventAction)) {
				if (isAcceptTimeEventAction((AcceptEventAction) element)) {
					uri = URI.createPlatformPluginURI(org.eclipse.papyrus.infra.gmfdiag.common.Activator.ID + HOURGLASS_SVG_PATH, true);
				} else {
					uri = URI.createPlatformPluginURI(org.eclipse.papyrus.infra.gmfdiag.common.Activator.ID + FLAG_SVG_PATH, true);
				}
			}// flag and hourglass
			if (element instanceof InitialNode || (element instanceof TypedElement && ((TypedElement) element).getType() instanceof InitialNode)) {
				uri = URI.createPlatformPluginURI(org.eclipse.papyrus.infra.gmfdiag.common.Activator.ID + ROUND_FULL_SVG_PATH, true);
			}// roundfull
			if (element instanceof FlowFinalNode || (element instanceof TypedElement && ((TypedElement) element).getType() instanceof FlowFinalNode)) {
				uri = URI.createPlatformPluginURI(org.eclipse.papyrus.infra.gmfdiag.common.Activator.ID + ROUND_WITH_CROSS_SVG_PATH, true);
			}// roundWCross
			if (element instanceof ActivityFinalNode || (element instanceof TypedElement && ((TypedElement) element).getType() instanceof ActivityFinalNode)) {
				uri = URI.createPlatformPluginURI(org.eclipse.papyrus.infra.gmfdiag.common.Activator.ID + ROUND_WITH_DOT_SVG_PATH, true);
			}// RoundWDot

			String path = uri.toString();
			SVGDocument document = getSVGDocument(path);
			if (document == null) {
				return null;
			}

			return Arrays.asList(document);
		}
		return null;
	}

	/**
	 * For AcceptEventAction, test whether the action may be considered as a time event action
	 * Come from org.eclipse.papyrus.uml.diagram.activity.helper.CustomAcceptEventActionEditHelper
	 * 
	 * @param action
	 *            the action to test
	 * @return true if action is a time event action
	 */
	public static boolean isAcceptTimeEventAction(AcceptEventAction action) {
		if (action.getTriggers() != null) {
			boolean hasTimeEvent = false;
			for (Trigger trigger : action.getTriggers()) {
				if (trigger != null) {
					Event event = trigger.getEvent();
					if (event instanceof TimeEvent) {
						hasTimeEvent = true;
					} else {
						return false;
					}
				}
			}
			// only time events have been encountered, but maybe no event at all.
			return hasTimeEvent;
		}
		return false;
	}
}

Back to the top