Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7505329b8641cd6889da39c3d448c72750b93de8 (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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*****************************************************************************
 * Copyright (c) 2014, 2016 CEA LIST, Christian W. Damus, 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:
 *	Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.net - Initial API and implementation
 *  Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.fr - Bug 393532
 *  Christian W. Damus - bugs 450523, 399859, 492482
 *  
 *****************************************************************************/
package org.eclipse.papyrus.uml.tools.listeners;

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

import org.eclipse.emf.common.command.AbstractCommand;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.notify.impl.NotificationChainImpl;
import org.eclipse.emf.common.notify.impl.NotificationImpl;
import org.eclipse.emf.common.util.ECollections;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.transaction.NotificationFilter;
import org.eclipse.emf.transaction.ResourceSetChangeEvent;
import org.eclipse.emf.transaction.ResourceSetListenerImpl;
import org.eclipse.emf.transaction.RollbackException;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Extension;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Stereotype;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.util.UMLUtil;


/**
 * Listener of all stereotype application actions in Resource set.
 *
 * @author Gabriel Pascual
 *
 */
public class StereotypeElementListener extends ResourceSetListenerImpl {

	/** The stereotype filter notification. */
	private StereotypeNotificationFilter stereotypeFilter = new StereotypeNotificationFilter();

	private final StereotypeExtensionFinder finder;

	/**
	 * Instantiates a new stereotype element listener on an editing domain.
	 *
	 * @param editingDomain
	 *            the editing-domain context in which the listener responds to events
	 */
	public StereotypeElementListener(TransactionalEditingDomain editingDomain) {
		this(editingDomain.getResourceSet());
	}

	/**
	 * Instantiates a new stereotype element listener on a resource set.
	 *
	 * @param resourceSet
	 *            the resource-set context in which the listener responds to events
	 */
	public StereotypeElementListener(ResourceSet resourceSet) {
		super();

		this.finder = new StereotypeExtensionFinder(resourceSet);
	}

	/**
	 * I am a pre-commit listener. Trigger listeners need to be able to react to
	 * these events. And they will be captured and automatically distributed again
	 * when the transaction commits (if it doesn't roll back).
	 *
	 * @return {@code true}
	 */
	@Override
	public boolean isPrecommitOnly() {
		return true;
	}

	@Override
	public Command transactionAboutToCommit(ResourceSetChangeEvent event) throws RollbackException {
		Command result = null;

		if (!event.getNotifications().isEmpty()) {
			// Get filtered notifications
			final NotificationChain chain = new NotificationChainImpl();
			List<Notification> filteredNotificationsList = new ArrayList<Notification>(event.getNotifications());

			// Handle each filtered notification
			for (Notification notification : filteredNotificationsList) {
				handleFilteredNotification(notification, chain);
			}

			result = new AbstractCommand("Inject Stereotype Notifications") {
				private NotificationChain notifications;

				@Override
				protected boolean prepare() {
					this.notifications = chain;
					return true;
				}

				@Override
				public void execute() {
					dispatchAndInvert();
				}

				@Override
				public void undo() {
					dispatchAndInvert();
				}

				@Override
				public void redo() {
					dispatchAndInvert();
				}

				private void dispatchAndInvert() {
					notifications.dispatch();
					this.notifications = invertNotifications(notifications);
				}

				private NotificationChain invertNotifications(NotificationChain notifications) {
					// The chain is implemented as an EList
					@SuppressWarnings("unchecked")
					EList<Notification> list = (EList<Notification>) notifications;

					// Reverse the order
					ECollections.reverse(list);

					// And flip the type from applied <--> unapplied
					for (Notification next : list) {
						((StereotypeExtensionNotification) next).invert();
					}

					return notifications;
				}
			};
		}

		return result;
	}

	/**
	 * Handle filtered notification.
	 *
	 * @param notification
	 *            the notification
	 * @param notifications
	 *            a notification chain on which to collect notifications for later dispatch. If {@code null}, then dispatch immediately
	 */
	private void handleFilteredNotification(Notification notification, NotificationChain notifications) {
		final Object notifier = notification.getNotifier();

		Element extendedElement = null;
		Stereotype stereotype = null;
		boolean isBaseFeature = true;

		// Use UML tools to get Stereotype and base Element
		if (notifier instanceof EObject) {
			stereotype = finder.getDefiningStereotype((EObject) notifier);
			if (stereotype != null) {
				extendedElement = finder.getExtendedElement((EObject) notifier);
				isBaseFeature = ((EStructuralFeature) notification.getFeature()).getName().startsWith(Extension.METACLASS_ROLE_PREFIX);
			}
		}


		int eventType = 0;
		// Check if notifier is an stereotype
		if (stereotype != null) {
			if (!isBaseFeature) {
				eventType = StereotypeExtensionNotification.MODIFIED_STEREOTYPE_OF_ELEMENT;
			} else if (extendedElement == null) {
				// Check state of base Element on stereotype application
				extendedElement = (Element) notification.getOldValue();
				eventType = StereotypeExtensionNotification.STEREOTYPE_UNAPPLIED_FROM_ELEMENT;
			} else {
				extendedElement = (Element) notification.getNewValue();
				eventType = StereotypeExtensionNotification.STEREOTYPE_APPLIED_TO_ELEMENT;
			}
		}


		if (stereotype != null) {
			Object oldValue = null;
			Object newValue = null;
			Element element = extendedElement;
			switch (eventType) {
			case StereotypeExtensionNotification.STEREOTYPE_UNAPPLIED_FROM_ELEMENT:
				oldValue = notifier;
				break;
			case StereotypeExtensionNotification.STEREOTYPE_APPLIED_TO_ELEMENT:
				newValue = notifier;
				break;
			case StereotypeExtensionNotification.MODIFIED_STEREOTYPE_OF_ELEMENT:
				// New value is the modified element
				newValue = notification.getNewValue();
				oldValue = notification.getOldValue();
				break;
			default:
				// Nothing to do

			}

			// Bug 450523: If element is null, the notification has already been sent. Simply ignore this case
			if (element != null) {
				NotificationImpl newNotification = new StereotypeExtensionNotification(element, eventType, oldValue, newValue, stereotype);
				if (notifications == null) {
					// Dispatch it now
					newNotification.dispatch();
				} else {
					notifications.add(newNotification);
				}
			}
		}

	}

	/**
	 * Gets the filter.
	 *
	 * @return the filter
	 * @see org.eclipse.emf.transaction.ResourceSetListenerImpl#getFilter()
	 */
	@Override
	public NotificationFilter getFilter() {
		return stereotypeFilter;
	}

	/**
	 * The Class StereotypeExtensionFinder.
	 */
	class StereotypeExtensionFinder extends UMLUtil {
		private final ResourceSet resourceSet;

		StereotypeExtensionFinder(ResourceSet resourceSet) {
			this.resourceSet = resourceSet;
		}

		Stereotype getDefiningStereotype(EObject stereotypeApplication) {
			Stereotype result = null;

			NamedElement umlDefinition = getUMLDefinition(stereotypeApplication.eClass(), stereotypeApplication);
			if (umlDefinition instanceof Stereotype) {
				result = (Stereotype) umlDefinition;
			}

			return result;
		}

		Property getExtensionEnd(EObject stereotypeApplication, EStructuralFeature ecoreDefinition) {
			Property result = null;

			NamedElement umlDefinition = getUMLDefinition(ecoreDefinition, stereotypeApplication);
			if (umlDefinition instanceof Property) {
				Property property = (Property) umlDefinition;

				// Is the property a member end of a metaclass extension?
				if (property.getAssociation() instanceof Extension) {
					result = property;
				}
			}

			return result;
		}

		NamedElement getUMLDefinition(ENamedElement ecoreElement, EObject context) {

			// Bug 450523: Quick pre-check: verify the existence of a base_X property typed with Element
			boolean mayBeStereotype = false;
			for (EReference reference : context.eClass().getEAllReferences()) {
				String name = reference.getName();
				if (name != null && name.startsWith(Extension.METACLASS_ROLE_PREFIX)) {
					EClass type = reference.getEType() instanceof EClass ? (EClass) reference.getEType() : null;
					if (type != null && UMLPackage.eINSTANCE.getElement().isSuperTypeOf(type)) {
						mayBeStereotype = true;
						break;
					}
				}
			}

			// If the quick check failed, don't go further
			if (!mayBeStereotype) {
				return null;
			}

			// If the quick pre-check is successful, do a complete check
			NamedElement result = getNamedElement(ecoreElement, context);

			if ((result == null) && (resourceSet != null)) {
				// Try again in the resource set context, with some loaded object for context
				EObject anyLoadedObject = getAnyEObject(resourceSet);
				if (anyLoadedObject != null) {
					// If there is no loaded object, then there's no point in any of this because
					// obviously there can't be a profile definition without any profiles loaded
					result = getNamedElement(ecoreElement, anyLoadedObject);
				}
			}

			return result;
		}

		/**
		 * @precondition the {@code stereotypeApplication} is known to be an instance of a {@link Stereotype} according to {@link #getDefiningStereotype(EObject)}
		 */
		Element getExtendedElement(EObject stereotypeApplication) {
			EClass eClass = stereotypeApplication.eClass();
			return getBaseElement(eClass, stereotypeApplication);
		}

		private EObject getAnyEObject(ResourceSet resourceSet) {
			EObject result = null;

			for (Resource next : resourceSet.getResources()) {
				if (next.isLoaded() && !next.getContents().isEmpty()) {
					result = next.getContents().get(0);
				}
			}

			return result;
		}
	}

	/**
	 * Notification filter on Stereotype application.
	 * <ul>
	 * <li>Set a {@link Stereotype} to {@link Element}</li>
	 * <li>Unset a {@link Stereotype} on {@link Element}</li>
	 * </ul>
	 */
	class StereotypeNotificationFilter extends NotificationFilter.Custom {

		/**
		 * Instantiates a new stereotype notification filter.
		 */
		StereotypeNotificationFilter() {
			super();
		}

		/**
		 * Matches.
		 *
		 * @param notification
		 *            the notification
		 * @return true, if successful
		 * @see org.eclipse.emf.transaction.NotificationFilter#matches(org.eclipse.emf.common.notify.Notification)
		 */
		@Override
		public boolean matches(Notification notification) {

			// Get notification field to filter
			Object notifier = notification.getNotifier();
			Object feature = notification.getFeature();
			Object baseElement = null;
			boolean isBaseFeature = false;

			// Check if feature is a based feature of a stereotype extension
			if (feature instanceof EStructuralFeature) {
				isBaseFeature = ((EStructuralFeature) feature).getName().startsWith(Extension.METACLASS_ROLE_PREFIX);
			}

			switch (notification.getEventType()) {
			case Notification.RESOLVE:
				// Only interested in proxy resolution when it is the base feature, because this is how we detect
				// application of a stereotype by loading a resource containing stereotype instances
				if (!isBaseFeature) {
					return false;
				}
				break;
			default:
				if (notification.isTouch()) {
					return false;
				}
				break;
			}

			// Fields of a stereotype extension
			Property extensionProperty = null;

			// Use UML tools to get extension property
			if (notifier instanceof EObject) {
				Stereotype stereotype = finder.getDefiningStereotype((EObject) notifier);
				if (stereotype != null) {
					extensionProperty = finder.getExtensionEnd((EObject) notifier, (EStructuralFeature) feature);
					if (extensionProperty == null) {
						baseElement = StereotypeExtensionFinder.getBaseElement((EObject) notifier);
					}
				}
			}

			return (extensionProperty != null && isBaseFeature) || (!isBaseFeature && baseElement instanceof Element);
		}
	}

	/**
	 * Specific notification handled by this listener.
	 * <p>
	 * It implements the {@link ENotificationImpl} notification.
	 * </p>
	 */
	public class StereotypeExtensionNotification extends ENotificationImpl {

		/** Event type notification for stereotype application action. */
		public static final int STEREOTYPE_APPLIED_TO_ELEMENT = Notification.EVENT_TYPE_COUNT + 21;

		/** Event type notification for stereotype unapplication action. */
		public static final int STEREOTYPE_UNAPPLIED_FROM_ELEMENT = Notification.EVENT_TYPE_COUNT + 22;

		/** The Constant MODIFIED_STEREOTYPE_OF_ELEMENT. */
		public static final int MODIFIED_STEREOTYPE_OF_ELEMENT = Notification.EVENT_TYPE_COUNT + 23;

		private Stereotype stereotype;

		/**
		 * Creates a new StereotypeExtensionNotification.
		 *
		 * @param notifier
		 *            the notifier that sends this notification
		 * @param eventType
		 *            the type of event
		 * @param oldValue
		 *            the instance (an {@link EObject} of some kind) of the {@link Stereotype} that was unapplied, or {@code null} if this is notification of a stereotype application;
		 *            or the old value of a stereotype property if this is a notification of a stereotype modification
		 * @param newValue
		 *            the instance (an {@link EObject} of some kind) of the {@link Stereotype} that was applied, or {@code null} if this is notification of a stereotype unapplication;
		 *            or the new value of a stereotype property if this is a notification of a stereotype modification
		 * @param stereotype
		 *            the {@link Stereotype} defining the extension object that was attached to or unattached from its base element or was modified
		 */
		public StereotypeExtensionNotification(Element notifier, int eventType, Object oldValue, Object newValue, Stereotype stereotype) {
			super((InternalEObject) notifier, eventType, null, oldValue, newValue);

			this.stereotype = stereotype;
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		public boolean isTouch() {
			return false;
		}

		/**
		 * Queries the stereotype that was applied to or unapplied from a model element, or an instance of which changed in some other way.
		 * 
		 * @return the stereotype defining the element extension instance that changed
		 */
		public Stereotype getStereotype() {
			return stereotype;
		}

		/**
		 * Flips an "applied" notification to "unapplied" and vice versa.
		 */
		void invert() {
			switch (getEventType()) {
			case STEREOTYPE_APPLIED_TO_ELEMENT:
				eventType = STEREOTYPE_UNAPPLIED_FROM_ELEMENT;
				break;
			case STEREOTYPE_UNAPPLIED_FROM_ELEMENT:
				eventType = STEREOTYPE_APPLIED_TO_ELEMENT;
				break;
			default:
				// Stereotype-modified events are their own inverses
				break;
			}

			// Swap the old and new values
			Object swap = oldValue;
			oldValue = newValue;
			newValue = swap;
		}
	}

}

Back to the top