Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8426d8f383534ad1a7c488de374055899ba63bf5 (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
/*****************************************************************************
 * Copyright (c) 2012 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
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.timing.custom.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.Assert;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gmf.runtime.diagram.core.commands.DeleteCommand;
import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil;
import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
import org.eclipse.gmf.runtime.emf.type.core.commands.DestroyElementCommand;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.uml.diagram.common.Activator;
import org.eclipse.papyrus.uml.diagram.common.util.CrossReferencerUtil;
import org.eclipse.papyrus.uml.diagram.timing.custom.Messages;
import org.eclipse.papyrus.uml.diagram.timing.edit.parts.DestructionOccurrenceSpecificationEditPartCN;
import org.eclipse.papyrus.uml.diagram.timing.edit.parts.FullStateInvariantVerticalLineEditPart;
import org.eclipse.papyrus.uml.diagram.timing.edit.parts.InteractionEditPartTN;
import org.eclipse.papyrus.uml.diagram.timing.edit.parts.MessageOccurrenceSpecificationEditPartCN;
import org.eclipse.papyrus.uml.diagram.timing.edit.parts.OccurrenceSpecificationEditPartCN;
import org.eclipse.papyrus.uml.diagram.timing.edit.parts.TimingDiagramEditPart;
import org.eclipse.papyrus.uml.diagram.timing.part.UMLVisualIDRegistry;
import org.eclipse.uml2.uml.InteractionFragment;
import org.eclipse.uml2.uml.Lifeline;
import org.eclipse.uml2.uml.OccurrenceSpecification;
import org.eclipse.uml2.uml.StateInvariant;

/** Utility class for manipulating {@link OccurrenceSpecification}s */
public final class OccurrenceSpecificationUtils {

	/** The key for the EAnnotation on an OccurrenceSpecification that indicates that its name is autogenerated */
	private static final String AUTOGENERATED_OCCURRENCE_SPECIFICATION_NAME = "org.eclipse.papyrus.uml.diagram.timing.autogeneratedOccurrenceSpecificationName"; //$NON-NLS-1$

	private OccurrenceSpecificationUtils() {
		// utility class
	}

	public static boolean isOccurrenceSpecificationEditPart(final int visualID) {
		return visualID == OccurrenceSpecificationEditPartCN.VISUAL_ID || visualID == MessageOccurrenceSpecificationEditPartCN.VISUAL_ID
				|| visualID == DestructionOccurrenceSpecificationEditPartCN.VISUAL_ID;
	}

	public static boolean isOccurrenceSpecificationEditPart(final EditPart editPart) {
		return editPart instanceof OccurrenceSpecificationEditPartCN || editPart instanceof MessageOccurrenceSpecificationEditPartCN
				|| editPart instanceof DestructionOccurrenceSpecificationEditPartCN;
	}

	public static boolean isOccurrenceSpecificationView(final View view) {
		final int visualID = UMLVisualIDRegistry.getVisualID(view);
		return isOccurrenceSpecificationEditPart(visualID);
	}

	private static EAnnotation getAutogeneratedAnnotation(final OccurrenceSpecification occurrenceSpecification) {
		return occurrenceSpecification.getEAnnotation(AUTOGENERATED_OCCURRENCE_SPECIFICATION_NAME);
	}

	/**
	 * Marks with an EAnnotation whether the given OccurrenceSpecification has an auto-generated name.
	 * 
	 * @param occurrenceSpecification
	 *            the OccurrenceSpecification for which to specify whether it has an auto-generated name
	 * @param autogenerated
	 *            whether the name is auto-generated
	 */
	public static void setAutogeneratedName(final OccurrenceSpecification occurrenceSpecification, final boolean autogenerated) {
		EAnnotation eAnnotation = getAutogeneratedAnnotation(occurrenceSpecification);
		if (autogenerated && eAnnotation == null) {
			eAnnotation = EcoreFactory.eINSTANCE.createEAnnotation();
			eAnnotation.setSource(AUTOGENERATED_OCCURRENCE_SPECIFICATION_NAME);
			occurrenceSpecification.getEAnnotations().add(eAnnotation);
		} else if (!autogenerated && eAnnotation != null) {
			occurrenceSpecification.getEAnnotations().remove(eAnnotation);
		}
	}

	/**
	 * Returns whether the given OccurrenceSpecification has an auto-generated name.
	 * 
	 * @param occurrenceSpecification
	 *            the OccurrenceSpecification for which to determine whether it has an auto-generated name
	 * @return whether the name of the given OccurrenceSpecification is auto-generated
	 */
	public static boolean isAutogeneratedName(final OccurrenceSpecification occurrenceSpecification) {
		final EAnnotation eAnnotation = getAutogeneratedAnnotation(occurrenceSpecification);
		return eAnnotation != null;
	}

	/**
	 * Get all elements to delete when deleting an OccurrenceSpecification.
	 * 
	 * @param occurrenceSpecification
	 *            the OccurrenceSpecification being deleted
	 * @return the elements (EObjects and Views) that should be deleted together with the OccurrenceSpecification
	 */
	public static Collection<EObject> getElementsToDelete(final OccurrenceSpecification occurrenceSpecification) {
		final Set<EObject> elementsToDestroy = new HashSet<EObject>();
		final Set<View> crossReferencingViews = CrossReferencerUtil.getCrossReferencingViews(occurrenceSpecification, TimingDiagramEditPart.MODEL_ID);
		for (final View view : crossReferencingViews) {
			elementsToDestroy.addAll(getElementsToRemove(view, false));
		}
		return elementsToDestroy;
	}

	/**
	 * Get all Views to remove when hiding an OccurrenceSpecification (i.e., deleting an OccurrenceSpecification's
	 * View).
	 * 
	 * @param occurrenceSpecification
	 *            the OccurrenceSpecification being hidden
	 * @return the Views that should be hidden together with the given OccurrenceSpecification View
	 */
	public static Collection<View> getViewsToHide(final View occurrenceSpecificationView) {
		final Set<View> viewsToHide = new HashSet<View>();
		final Collection<EObject> elementsToRemove = getElementsToRemove(occurrenceSpecificationView, true);
		for (final EObject eObject : elementsToRemove) {
			if (eObject instanceof View) {
				viewsToHide.add((View) eObject);
			} else {
				throw new IllegalStateException("Only views should be returned"); //$NON-NLS-1$
			}
		}
		return viewsToHide;
	}

	/**
	 * Get all elements to remove when removing an OccurrenceSpecification.
	 * 
	 * @param occurrenceSpecificationView
	 *            the OccurrenceSpecification View being removed
	 * @param hideOnly
	 *            if <code>true</code>, only return the Views, not the semantic elements
	 * @return the elements (Views and EObjects if <code>hideOnly</code> is <code>false</code>) that should be removed
	 *         together with the OccurrenceSpecification
	 */
	public static Collection<EObject> getElementsToRemove(final View occurrenceSpecificationView, final boolean hideOnly) {
		final Set<EObject> elementsToRemove = new HashSet<EObject>();
		if (OccurrenceSpecificationUtils.isOccurrenceSpecificationView(occurrenceSpecificationView)) {
			// remove associated ticks
			final List<Node> associatedTickViews = TickUtils.getAssociatedTickViews(occurrenceSpecificationView);
			elementsToRemove.addAll(associatedTickViews);

			// only remove Views under the same Interaction
			final View interactionView = ViewUtils.findSuperViewWithId(occurrenceSpecificationView, InteractionEditPartTN.VISUAL_ID);
			final EObject eContainer = occurrenceSpecificationView.eContainer();
			if (eContainer instanceof Node) {
				final Node node = (Node) eContainer;
				@SuppressWarnings("unchecked")
				final EList<View> children = node.getChildren();

				final int childrenSize = children.size();
				final int index = children.indexOf(occurrenceSpecificationView);

				boolean done = false;
				// the Lifeline starts by an OccurrenceSpecification (created with a Create Message)
				if (index == 0) {
					if (index + 1 < childrenSize) {
						final View following = children.get(index + 1);
						if (following.getType().equals(Constants.verticalLineId)) {
							elementsToRemove.add(following);
						}
					}
					// let the Lifeline start from a StateInvariant again
					done = true;
				}

				// state change in a full lifeline
				if (!done && index + 2 < childrenSize) {
					final View following1 = children.get(index + 1);
					final View following2 = children.get(index + 2);
					if (following1.getType().equals(Constants.verticalLineId) && following2.getType().equals(Constants.fullStateInvariantId)) {
						elementsToRemove.add(following1);
						elementsToRemove.add(following2);
						if (!hideOnly) {
							elementsToRemove.add(following2.getElement());
						}
						elementsToRemove.addAll(StateInvariantUtils.getRelatedElementsToRemove(following2.getElement(), hideOnly, interactionView));
						done = true;
					}
				}

				// state change in a compact lifeline, or non-state-change OccurrenceSpecification in any Lifeline
				if (!done && index + 1 < childrenSize) {
					final View following = children.get(index + 1);
					if (following.getType().equals(Constants.fullStateInvariantId) || following.getType().equals(Constants.compactStateInvariantId)) {
						elementsToRemove.add(following);
						if (!hideOnly) {
							elementsToRemove.add(following.getElement());
						}
						elementsToRemove.addAll(StateInvariantUtils.getRelatedElementsToRemove(following.getElement(), hideOnly, interactionView));
					}
				}
			}

			final OccurrenceSpecification occurrenceSpecification = (OccurrenceSpecification) occurrenceSpecificationView.getElement();
			elementsToRemove.addAll(getRelatedElementsToRemove(occurrenceSpecification, hideOnly, interactionView));

		}
		return elementsToRemove;
	}

	public static Collection<? extends EObject> getRelatedElementsToRemove(final EObject occurrenceSpecification, final boolean hideOnly,
			final View interactionView) {
		Assert.isLegal(occurrenceSpecification instanceof OccurrenceSpecification);
		final Set<EObject> elementsToRemove = new HashSet<EObject>();
		elementsToRemove.addAll(TimeElementUtils.getTimeElementsToRemove(occurrenceSpecification, hideOnly, interactionView));
		elementsToRemove.addAll(MessageUtils.getReferencingMessagesToRemove(occurrenceSpecification, hideOnly, interactionView));
		elementsToRemove.addAll(GeneralOrderingUtils.getReferencingGeneralOrderingsToRemove(occurrenceSpecification, hideOnly, interactionView));
		return elementsToRemove;
	}

	/**
	 * Delete all {@link StateInvariant}s and {@link OccurrenceSpecification}s after the given
	 * DestructionOccurrenceSpecification (which signifies the end of a Lifeline). Doesn't delete time elements.
	 * 
	 * @param occurrenceSpecification
	 *            the OccurrenceSpecification after which everything must be deleted
	 * @param occurrenceSpecificationView
	 *            the OccurrenceSpecification View after which other views must be deleted (if <code>null</code>, then
	 *            the views will be searched)
	 */
	private static void deleteEverythingAfterOrBefore(final OccurrenceSpecification occurrenceSpecification, final View occurrenceSpecificationView,
			final boolean before) {
		// remove fragments from UML model
		final EList<Lifeline> coveredLifelines = occurrenceSpecification.getCovereds();
		for (final Lifeline lifeline : coveredLifelines) {
			final EList<InteractionFragment> coveredBys = lifeline.getCoveredBys();
			int index = coveredBys.indexOf(occurrenceSpecification);
			if (index != -1) {
				// if we are removing everything before an OccurrenceSpecification in the middle of a StateInvariant,
				// then we need to move the StateInvariant after the OccurrenceSpecification
				if (before && index + 1 < coveredBys.size() && !(coveredBys.get(index + 1) instanceof StateInvariant)) {
					if (movePreviousStateInvariantAfter(coveredBys, occurrenceSpecification)) {
						// the OccurrenceSpecification moved since we removed an element before it
						index--;
					}
				}
				final List<InteractionFragment> fragmentsToRemove = new ArrayList<InteractionFragment>();
				for (int i = before ? index - 1 : index + 1; before ? i >= 0 : i < coveredBys.size(); i = (before ? i - 1 : i + 1)) {
					fragmentsToRemove.add(coveredBys.get(i));
				}
				for (final InteractionFragment fragmentToRemove : fragmentsToRemove) {
					// remove the related elements
					Collection<? extends EObject> relatedElementsToRemove = Collections.emptyList();
					if (fragmentToRemove instanceof OccurrenceSpecification) {
						final OccurrenceSpecification occurrence = (OccurrenceSpecification) fragmentToRemove;
						relatedElementsToRemove = OccurrenceSpecificationUtils.getRelatedElementsToRemove(occurrence, false, null);
					} else if (fragmentToRemove instanceof StateInvariant) {
						final StateInvariant stateInvariant = (StateInvariant) fragmentToRemove;
						relatedElementsToRemove = StateInvariantUtils.getRelatedElementsToRemove(stateInvariant, false, null);
					}
					for (final EObject eObject : relatedElementsToRemove) {
						DestroyElementCommand.destroy(eObject);
					}
					// remove the fragment
					DestroyElementCommand.destroy(fragmentToRemove);
				}
			}
		}
		// remove Views from notation model
		final Collection<View> views;
		if (occurrenceSpecificationView == null) {
			views = CrossReferencerUtil.getCrossReferencingViews(occurrenceSpecification, TimingDiagramEditPart.MODEL_ID);
		} else {
			views = Collections.singletonList(occurrenceSpecificationView);
		}
		for (final View view : views) {
			final View parentView = (View) view.eContainer();
			@SuppressWarnings("unchecked")
			final EList<View> children = parentView.getChildren();
			final int index = children.indexOf(view);
			if (index != -1) {
				final List<View> viewsToRemove = new ArrayList<View>();
				for (int i = before ? index - 1 : index + 1; before ? i >= 0 : i < children.size(); i = (before ? i - 1 : i + 1)) {
					final View childView = children.get(i);
					final int visualID = UMLVisualIDRegistry.getVisualID(childView);
					if (isOccurrenceSpecificationEditPart(visualID) || StateInvariantUtils.isStateInvariantEditPart(visualID)
							|| visualID == FullStateInvariantVerticalLineEditPart.VISUAL_ID) {
						viewsToRemove.add(childView);
					}
				}
				// remove the vertical line (a state change is not possible anymore
				// since we deleted the preceding state invariant)
				if (before && index + 1 < children.size()) {
					final View childView = children.get(index + 1);
					final int visualID = UMLVisualIDRegistry.getVisualID(childView);
					if (visualID == FullStateInvariantVerticalLineEditPart.VISUAL_ID) {
						viewsToRemove.add(childView);
					}
				}
				for (final View viewToRemove : viewsToRemove) {
					ViewUtil.destroy(viewToRemove);
				}
			}
		}
	}

	private static boolean movePreviousStateInvariantAfter(final EList<InteractionFragment> fragments, final OccurrenceSpecification occurrenceSpecification) {
		final int umlIndex = fragments.indexOf(occurrenceSpecification);
		// move the StateInvariant in the UML model
		final int previousStateInvariantIndex = findPreviousStateInvariantIndex(umlIndex, fragments);
		if (previousStateInvariantIndex == -1) {
			Activator.log.error(new IllegalStateException("No StateInvariant after or before the OccurrenceSpecification")); //$NON-NLS-1$
			return false;
		}
		final StateInvariant stateInvariant = (StateInvariant) fragments.get(previousStateInvariantIndex);
		fragments.move(umlIndex, previousStateInvariantIndex);

		// move the StateInvariant View in the notation model
		final Collection<View> stateInvariantViews = CrossReferencerUtil.getCrossReferencingViews(stateInvariant, TimingDiagramEditPart.MODEL_ID);
		for (final View stateInvariantView : stateInvariantViews) {
			final View parentView = (View) stateInvariantView.eContainer();
			@SuppressWarnings("unchecked")
			final EList<View> children = parentView.getPersistedChildren();
			// find the OccurrenceSpecification View index
			int viewIndex = -1;
			for (int i = 0; i < children.size(); i++) {
				final View view = children.get(i);
				if (view.getElement() == occurrenceSpecification) {
					viewIndex = i;
					break;
				}
			}
			children.move(viewIndex, stateInvariantView);
		}
		return true;
	}

	private static int findPreviousStateInvariantIndex(final int index, final EList<InteractionFragment> fragments) {
		for (int i = index - 1; i >= 0; i--) {
			final InteractionFragment fragment = fragments.get(i);
			if (fragment instanceof StateInvariant) {
				return i;
			}
		}
		return -1;
	}

	/**
	 * Delete all {@link StateInvariant}s and {@link OccurrenceSpecification}s after the given
	 * DestructionOccurrenceSpecification (which signifies the end of a Lifeline). Doesn't delete time elements.
	 * 
	 * @param occurrenceSpecification
	 *            the OccurrenceSpecification after which everything must be deleted
	 * @param occurrenceSpecificationView
	 *            the OccurrenceSpecification View after which other views must be deleted (if <code>null</code>, then
	 *            the views will be searched)
	 */
	public static void deleteEverythingAfter(final OccurrenceSpecification occurrenceSpecification, final View occurrenceSpecificationView) {
		deleteEverythingAfterOrBefore(occurrenceSpecification, occurrenceSpecificationView, false);
	}

	/**
	 * Delete all {@link StateInvariant}s and {@link OccurrenceSpecification}s before the given
	 * {@link OccurrenceSpecification} (which signifies the creation of a Lifeline). Doesn't delete time elements.
	 * 
	 * @param occurrenceSpecification
	 *            the OccurrenceSpecification before which everything must be deleted
	 * @param occurrenceSpecificationView
	 *            the OccurrenceSpecification View before which other views must be deleted (if <code>null</code>, then
	 *            the views will be searched)
	 */
	public static void deleteEverythingBefore(final OccurrenceSpecification occurrenceSpecification, final View occurrenceSpecificationView) {
		deleteEverythingAfterOrBefore(occurrenceSpecification, occurrenceSpecificationView, true);
	}

	/**
	 * Find at which index to insert an OccurrenceSpecification in a compartment, based on a location.
	 * 
	 * @param pt
	 *            the location
	 * @param compartmentView
	 *            the compartment view in which the OccurrenceSpecification will be inserted
	 * @param viewer
	 *            the viewer in which the EditParts corresponding to the Views are registered
	 * @return the index in the compartment where the OccurrenceSpecification should be inserted
	 */
	public static int findInsertionIndexFor(final Point pt, final View compartmentView, final EditPartViewer viewer) {
		@SuppressWarnings("unchecked")
		final EList<View> children = compartmentView.getChildren();
		int index = 0;
		for (final View view : children) {
			if (view instanceof Node) {
				final Node node = (Node) view;
				final Object editPart = viewer.getEditPartRegistry().get(node);
				if (editPart instanceof GraphicalEditPart) {
					final GraphicalEditPart graphicalEditPart = (GraphicalEditPart) editPart;
					final Rectangle bounds = graphicalEditPart.getFigure().getBounds();
					if (pt.x < bounds.x()) {
						break;
					}
				} else {
					Activator.log.error(new Exception("No EditPart found for the Node")); //$NON-NLS-1$
				}
			}
			index++;
		}
		return index;
	}

	/**
	 * Wrap the given <code>baseCommand</code> (that hides an OccurrenceSpecification View) into a
	 * {@link CompoundCommand}, and add a deletion of other Views associated to the OccurrenceSpecification.
	 */
	public static CompoundCommand getHideOccurrenceSpecificationCommand(final EditPart occurrenceSpecificationEditPart, final Command baseCommand) {
		final View view = (View) occurrenceSpecificationEditPart.getModel();
		if (!OccurrenceSpecificationUtils.isOccurrenceSpecificationEditPart(UMLVisualIDRegistry.getVisualID(view))) {
			return null;
		}
		final Collection<View> viewsToHide = OccurrenceSpecificationUtils.getViewsToHide(view);
		final CompoundCommand compoundCommand = new CompoundCommand(Messages.OccurrenceSpecificationUtils_HideOccurrenceSpecification);
		if (baseCommand != null) {
			compoundCommand.add(baseCommand);
		}
		for (final View viewToHide : viewsToHide) {
			compoundCommand.add(new ICommandProxy(new DeleteCommand(viewToHide)));
		}
		if (compoundCommand.isEmpty()) {
			return null;
		}
		return compoundCommand;
	}

}

Back to the top