Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 761930945f5038604112e3336fa57100fa638b61 (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
/*****************************************************************************
 * 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.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
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.emf.ecore.util.EcoreUtil;
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.draw2d.ui.render.factory.RenderedImageFactory;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.gmfdiag.common.service.shape.AbstractShapeProvider;
import org.eclipse.papyrus.infra.gmfdiag.common.service.shape.ProviderNotificationManager;
import org.eclipse.papyrus.infra.gmfdiag.common.service.shape.ShapeService;
import org.eclipse.papyrus.uml.diagram.symbols.Activator;
import org.eclipse.papyrus.uml.diagram.symbols.IPapyrusInternalProfileConstants;
import org.eclipse.papyrus.uml.tools.listeners.PapyrusStereotypeListener.StereotypeCustomNotification;
import org.eclipse.uml2.uml.Stereotype;
import org.eclipse.uml2.uml.Type;
import org.eclipse.uml2.uml.TypedElement;
import org.eclipse.uml2.uml.UMLPackage;
import org.w3c.dom.svg.SVGDocument;

/**
 * This provider is linked to the {@link ShapeService}. It returns the shapes
 * for a given typed element corresponding to the stereotypes applied on the
 * business element.
 */
public class TypedElementShapeProvider extends AbstractShapeProvider {

	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<RenderedImage> getShapes(EObject view) {
		Type type = getType(view);
		String path = getSymbolPath(type);
		if(path != null && path.length() > 0) {

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

			URL url;
			try {
				url = new URL(path);
				return Arrays.asList(RenderedImageFactory.getInstance(url));
			} catch (MalformedURLException e) {
				URI typeResourceURI = type.eResource().getURI();
				if(typeResourceURI != null) {
					String workspaceRelativeFolderPath = typeResourceURI.trimSegments(1).toPlatformString(true);
					try {

						url = new URL("platform:/resource/" + workspaceRelativeFolderPath + File.separatorChar + path);
						return Arrays.asList(RenderedImageFactory.getInstance(url));
					} catch (MalformedURLException e1) {
						Activator.log.error(e1);
					}
				}
			}
		}
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<SVGDocument> getSVGDocument(EObject view) {
		Type type = getType(view);
		String path = getSymbolPath(type);
		if(path != null && path.length() > 0) {
			try {
				new URL(path);
				SVGDocument document = getSVGDocument(path);
				if(document != null) {
					return Arrays.asList(document);
				}

				return null;
			} catch (MalformedURLException e) {
				URI typeResourceURI = EcoreUtil.getURI(type).trimFragment();
				if(typeResourceURI != null) {
					String workspaceRelativeFolderPath = typeResourceURI.trimSegments(1).toPlatformString(true);
					String newPath = URI.createPlatformResourceURI(workspaceRelativeFolderPath + File.separatorChar + path, false).toString();
					SVGDocument document = getSVGDocument(newPath);
					if(document != null) {
						return Arrays.asList(document);
					}
				}
			}
		}
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean providesShapes(EObject view) {
		Type type = getType(view);
		String path = getSymbolPath(type);
		if(path != null && path.length() > 0) {
			return true;
		}
		return false;
	}

	/**
	 * Returns the type associated to the semantic element linkerd to the view
	 * 
	 * @param view
	 *        the view from which type should be looked
	 * @return the type of the semantic element or <code>null</code> if none was
	 *         found
	 */
	protected Type getType(EObject view) {
		if(!(view instanceof View)) {
			return null;
		}

		Object element = ((View)view).getElement();
		if(element instanceof Type) {
			return (Type)element;
		} else if(element instanceof TypedElement) {
			// retrieve type and tries to find a shape for it
			return ((TypedElement)element).getType();
		}
		return null;
	}

	/**
	 * Returns the path for the symbol associated to the element
	 * 
	 * @param view
	 * @return
	 */
	protected String getSymbolPath(Type type) {
		if(type == null) {
			return null;
		}

		// type is not null. Look for the stereotype symbol definition
		Stereotype stereotype = type.getAppliedStereotype(IPapyrusInternalProfileConstants.TYPE_SYMBOL_DEFINITION_QUALIFIED_NAME);
		if(stereotype == null) {
			return null;
		}

		Object path = type.getValue(stereotype, IPapyrusInternalProfileConstants.TYPE_SYMBOL_DEFINITION_SYMBOL_PATH);
		if(path instanceof String && ((String)path).length() > 0) {
			return path.toString();
		}
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public ProviderNotificationManager createProviderNotificationManager(DiagramEventBroker diagramEventBroker, EObject view, NotificationListener listener) {
		if(view == null || !(view instanceof View)) {
			return null;
		}
		TypedElementShapeProviderNotificationManager notificationManager = new TypedElementShapeProviderNotificationManager(diagramEventBroker, view, listener);
		return notificationManager;
	}

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

		/**
		 * Creates a new StereotypedElementShapeProviderNotificationManager.
		 * 
		 * @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 TypedElementShapeProviderNotificationManager(DiagramEventBroker diagramEventBroker, EObject view, NotificationListener listener) {
			super(diagramEventBroker, view, listener);
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		protected void registerListeners() {
			if(view == null || !(view instanceof View)) {
				return;
			}
			// should register this listener on the type, to listen for
			// stereotype modification
			// should listen to new value of the typedElement_type feature, in
			// order to remove/add the stereotype listener...
			Object semanticElement = ((View)view).getElement();
			if(semanticElement instanceof Type) {
				diagramEventBroker.addNotificationListener((Type)semanticElement, this);
			} else if(semanticElement instanceof TypedElement) {
				diagramEventBroker.addNotificationListener((TypedElement)semanticElement, this);
				Type type = ((TypedElement)semanticElement).getType();
				if(type != null) {
					diagramEventBroker.addNotificationListener(type, this);
				}
			}
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		public void dispose() {
			if(view == null || !(view instanceof View)) {
				return;
			}
			Object semanticElement = ((View)view).getElement();
			if(semanticElement instanceof Type) {
				diagramEventBroker.removeNotificationListener((Type)semanticElement, this);
			} else if(semanticElement instanceof TypedElement) {
				diagramEventBroker.removeNotificationListener((TypedElement)semanticElement, this);

				Type type = ((TypedElement)semanticElement).getType();
				if(type != null) {
					diagramEventBroker.removeNotificationListener(type, 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);
				// add/remove stereotype listener if needed...
				Object oldValue = notification.getOldValue();
				if(oldValue instanceof Type) {
					// a type was removed => remove the listener from this old
					// type
					diagramEventBroker.removeNotificationListener((Type)oldValue, this);
				}
				Object newValue = notification.getNewValue();
				if(newValue instanceof Type) {
					// a type was added => adds the listener from this new type
					diagramEventBroker.removeNotificationListener((Type)newValue, this);
				}
			} else if(notification instanceof StereotypeCustomNotification) {
				// call refresh for all stereotype notifications. could target
				// specific modification on the stereotype Symbol definition
				listener.notifyChanged(notification);
			}

		}
	}

}

Back to the top