Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0f5c52eb5e70135ef75a1b1e3c04c5088311acf6 (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
/*****************************************************************************
 * Copyright (c) 2009 CEA LIST.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *  Patrick Tessier (CEA LIST) patrick.tessier@cea.fr - Initial API and implementation
 *  Remi Schnekenburger (CEA LIST) remi.schnekenburger@cea.fr - additional features
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.sequence.edit.policies;

import static org.eclipse.papyrus.uml.diagram.common.Activator.log;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.Transaction;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.workspace.AbstractEMFOperation;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.UnexecutableCommand;
import org.eclipse.gef.editpolicies.AbstractEditPolicy;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.common.core.util.StringStatics;
import org.eclipse.gmf.runtime.diagram.core.commands.DeleteCommand;
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.commands.ICommandProxy;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramGraphicalViewer;
import org.eclipse.gmf.runtime.diagram.ui.util.EditPartUtil;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.core.listenerservice.IPapyrusListener;
import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils;
import org.eclipse.papyrus.infra.services.edit.service.IElementEditService;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.LifelineEditPart;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.TimeConstraintEditPart;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.TimeObservationEditPart;
import org.eclipse.papyrus.uml.diagram.sequence.util.SequenceUtil;
import org.eclipse.uml2.uml.DurationObservation;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.OccurrenceSpecification;
import org.eclipse.uml2.uml.TimeConstraint;
import org.eclipse.uml2.uml.TimeObservation;

/**
 * Edit Policy in charge of the removal of time/duration constraint/observation which no longer have associated events.
 * <P>
 * This view checks that the host edit part, a time/duration constraint/observation edit part, has necessary associated events. It listens for model notifications. As soon as it receives a remove event, it checks whether the time element should be also
 * deleted.<BR/>
 * </P>
 */
public class DeleteTimeElementWithoutEventPolicy extends AbstractEditPolicy implements NotificationListener, IPapyrusListener {

	/** The key to install this edit policy */
	public static final String KEY = "DeleteTimeElementWithoutEvent";

	/** list of element to listen */
	protected HashMap<EObject, List<View>> additionalParentToListen = new HashMap<>();

	/** stores the host associated semantic element */
	protected EObject hostSemanticElement;

	/**
	 * Adds additional listeners to the diagram event broker.
	 */
	@Override
	public void activate() {
		// retrieve the view and the element associated to the host edit part
		final View hostView = (View) getHost().getModel();
		hostSemanticElement = hostView.getElement();
		// adds listener to the event broker, listening for the view and the semantic element associated to the host edit part
		getDiagramEventBroker().addNotificationListener(hostView, this);
		getDiagramEventBroker().addNotificationListener(hostSemanticElement, this);
		// retrieve the list of linked view to listen parents
		for (View linkedView : getLinkedViews()) {
			getDiagramEventBroker().addNotificationListener(linkedView.eContainer(), this);
		}
		super.activate();
	}

	/**
	 * Removes this edit policy as listener for changes to the specified semanticParent
	 *
	 * @param semanticParent
	 *            the semantic parent to stop listen
	 * @param childView
	 *            the view that does not requires this additional listener
	 */
	protected void removeAdditionalParentToListen(EObject semanticParent, View childView) {
		// removes the view from the list of views that requires a listener for the semantic parent
		if (additionalParentToListen.containsKey(semanticParent)) {
			List<View> views = additionalParentToListen.get(semanticParent);
			assert (views != null) : "list should not be null";
			views.remove(childView);
			if (views.isEmpty()) {
				additionalParentToListen.remove(semanticParent);
				// check this is not the parent semantic element of the host's semantic element
				if (!semanticParent.equals(((View) getHost().getModel()).getElement())) {
					getDiagramEventBroker().removeNotificationListener(semanticParent, this);
				}
			}
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void deactivate() {
		// retrieve the view and the element associated to the host edit part
		final View hostView = (View) getHost().getModel();
		// removes all notification listeners for the additional parent to listen
		for (EObject parent : additionalParentToListen.keySet()) {
			getDiagramEventBroker().removeNotificationListener(parent, this);
		}
		additionalParentToListen.clear();
		additionalParentToListen = null;
		getDiagramEventBroker().removeNotificationListener(hostView, this);
		getDiagramEventBroker().removeNotificationListener(hostSemanticElement, this);
		// removes the reference to the semantic element
		hostSemanticElement = null;
		super.deactivate();
	}

	/**
	 * Deletes the time element.
	 */
	protected final void deleteTimeElement() {
		Command cmd = getDeleteElementCommand(false);
		if (cmd.canExecute()) {
			executeCommand(cmd);
		}
	}

	/**
	 * Deletes a time element view.
	 */
	protected final void deleteTimeView() {
		Command cmd = getDeleteElementCommand(true);
		if (cmd.canExecute()) {
			executeCommand(cmd);
		}
	}

	/**
	 * Executes the supplied command inside an <code>unchecked action</code>
	 *
	 * @param cmd
	 *            command that can be executed (i.e., cmd.canExecute() == true)
	 */
	protected void executeCommand(final Command cmd) {
		Map<String, Boolean> options = null;
		EditPart ep = getHost();
		boolean isActivating = true;
		// use the viewer to determine if we are still initializing the diagram
		// do not use the DiagramEditPart.isActivating since ConnectionEditPart's
		// parent will not be a diagram edit part
		EditPartViewer viewer = ep.getViewer();
		if (viewer instanceof DiagramGraphicalViewer) {
			isActivating = ((DiagramGraphicalViewer) viewer).isInitializing();
		}
		if (isActivating || !EditPartUtil.isWriteTransactionInProgress((IGraphicalEditPart) getHost(), false, false)) {
			options = Collections.singletonMap(Transaction.OPTION_UNPROTECTED, Boolean.TRUE);
		}
		AbstractEMFOperation operation = new AbstractEMFOperation(((IGraphicalEditPart) getHost()).getEditingDomain(), StringStatics.BLANK, options) {

			@Override
			protected IStatus doExecute(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
				((IGraphicalEditPart) getHost()).getDiagramEditDomain().getDiagramCommandStack().execute(cmd);
				return Status.OK_STATUS;
			}

			@Override
			protected IStatus doUndo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
				cmd.undo();
				return Status.OK_STATUS;
			}
		};
		try {
			operation.execute(new NullProgressMonitor(), null);
		} catch (ExecutionException e) {
			log.error(e);
		}
	}

	/**
	 * Check if time element is correctly defined
	 *
	 * @return true if it has necessary information, false if it is to be deleted
	 */
	protected boolean isTimeElementDefined() {
		if (hostSemanticElement instanceof TimeObservation) {
			return ((TimeObservation) hostSemanticElement).getEvent() != null;
		} else if (hostSemanticElement instanceof DurationObservation) {
			return ((DurationObservation) hostSemanticElement).getEvents().size() >= 2;
		} else if (hostSemanticElement instanceof TimeConstraint) {
			return ((TimeConstraint) hostSemanticElement).getConstrainedElements().size() > 0;
		}
		return true;
	}

	/**
	 * Get the list of other required figures
	 *
	 * @return list of views
	 */
	protected List<View> getLinkedViews() {
		if (getHost() instanceof TimeObservationEditPart && hostSemanticElement instanceof TimeObservation) {
			LifelineEditPart lifeline = SequenceUtil.getParentLifelinePart(getHost());
			NamedElement occ = ((TimeObservation) hostSemanticElement).getEvent();
			if (occ instanceof OccurrenceSpecification) {
				EditPart part = SequenceUtil.getLinkedEditPart(lifeline, (OccurrenceSpecification) occ);
				if (part != null) {
					return Collections.singletonList((View) part.getModel());
				}
			}
			return Collections.emptyList();
		} else if (getHost() instanceof TimeConstraintEditPart && hostSemanticElement instanceof TimeConstraint) {
			LifelineEditPart lifeline = SequenceUtil.getParentLifelinePart(getHost());
			List<Element> occs = ((TimeConstraint) hostSemanticElement).getConstrainedElements();
			if (occs.size() > 0 && occs.get(0) instanceof OccurrenceSpecification) {
				EditPart part = SequenceUtil.getLinkedEditPart(lifeline, (OccurrenceSpecification) occs.get(0));
				if (part != null) {
					return Collections.singletonList((View) part.getModel());
				}
			}
			return Collections.emptyList();
		}
		// a label on a message always has its parent message
		return Collections.emptyList();
	}

	/**
	 * Check if time element has required other figures
	 *
	 * @return true if the time element figure miss one of the figure representing its ends.
	 */
	protected boolean timeElementMissAnEventFigure() {
		if (getHost() instanceof TimeObservationEditPart && hostSemanticElement instanceof TimeObservation) {
			LifelineEditPart lifeline = SequenceUtil.getParentLifelinePart(getHost());
			NamedElement occ = ((TimeObservation) hostSemanticElement).getEvent();
			if (occ instanceof OccurrenceSpecification) {
				return SequenceUtil.getLinkedEditPart(lifeline, (OccurrenceSpecification) occ) == null;
			}
			return true;
		} else if (getHost() instanceof TimeConstraintEditPart && hostSemanticElement instanceof TimeConstraint) {
			LifelineEditPart lifeline = SequenceUtil.getParentLifelinePart(getHost());
			List<Element> occs = ((TimeConstraint) hostSemanticElement).getConstrainedElements();
			if (occs.size() > 0 && occs.get(0) instanceof OccurrenceSpecification) {
				return SequenceUtil.getLinkedEditPart(lifeline, (OccurrenceSpecification) occs.get(0)) == null;
			}
			return true;
		}
		return false;
	}

	/**
	 * Returns a {@link Command} to delete the host element
	 *
	 * @param graphOnly
	 *            true if the view only must be removed
	 * @return the command that destroys the host element
	 */
	protected Command getDeleteElementCommand(boolean graphOnly) {
		if (graphOnly) {
			TransactionalEditingDomain editingDomain = ((IGraphicalEditPart) getHost()).getEditingDomain();
			return new ICommandProxy(new DeleteCommand(editingDomain, (View) getHost().getModel()));
		} else {
			DestroyElementRequest req = new DestroyElementRequest(hostSemanticElement, false);
			return getDestroyElementCommand(req);
		}
	}

	/**
	 * Copied from a generated method from a semantic edit policy which supports the edit element service
	 *
	 * @param req
	 *            the DestroyElementRequest
	 * @return the destroy command
	 */
	protected Command getDestroyElementCommand(DestroyElementRequest req) {
		EObject selectedEObject = req.getElementToDestroy();
		IElementEditService provider = ElementEditServiceUtils.getCommandProvider(selectedEObject);
		if (provider != null) {
			// Retrieve delete command from the Element Edit service
			ICommand deleteCommand = provider.getEditCommand(req);
			if (deleteCommand != null) {
				return new ICommandProxy(deleteCommand);
			}
		}
		return UnexecutableCommand.INSTANCE;
	}

	/**
	 * Gets the diagram event broker from the editing domain.
	 *
	 * @return the diagram event broker
	 */
	protected DiagramEventBroker getDiagramEventBroker() {
		TransactionalEditingDomain theEditingDomain = ((IGraphicalEditPart) getHost()).getEditingDomain();
		if (theEditingDomain != null) {
			return DiagramEventBroker.getInstance(theEditingDomain);
		}
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void notifyChanged(Notification notification) {
		Object notifier = notification.getNotifier();
		if (notifier.equals(hostSemanticElement)) {
			if (Notification.REMOVE == notification.getEventType() || Notification.SET == notification.getEventType()) {
				deleteTimeElementIfNeeded();
			}
		} else if (notifier instanceof View) {
			// a view has been modified
			if (Notification.REMOVE == notification.getEventType()) {
				deleteTimeElementIfNeeded();
			}
		}
	}

	/**
	 * Removes a listener for the specified view, if it exists.
	 *
	 * @param oldView
	 *            the old view to check
	 */
	protected void removeListenerForView(View oldView) {
		// create a temp list of elements to delete (iterator concurrent modification..)
		Map<EObject, List<View>> parentsToDelete = new HashMap<>();
		for (EObject parent : additionalParentToListen.keySet()) {
			List<View> parentViews = additionalParentToListen.get(parent);
			if (parentViews.contains(oldView)) {
				List<View> views = parentsToDelete.get(parent);
				if (views == null) {
					views = new ArrayList<>();
				}
				views.add(oldView);
				parentsToDelete.put(parent, views);
			}
		}
	}

	/**
	 * Updates the listeners for the specified semantic parent
	 */
	protected void removeListeners(List<View> impactedViews) {
		// create a temp list of elements to delete (iterator concurrent modification..)
		Map<EObject, List<View>> parentsToDelete = new HashMap<>();
		// collect the elements to delete
		for (View view : impactedViews) {
			for (EObject parent : additionalParentToListen.keySet()) {
				List<View> parentViews = additionalParentToListen.get(parent);
				if (parentViews.contains(view)) {
					List<View> views = parentsToDelete.get(parent);
					if (views == null) {
						views = new ArrayList<>();
					}
					views.add(view);
					parentsToDelete.put(parent, views);
				}
			}
		}
		// do the job
		for (EObject object : parentsToDelete.keySet()) {
			List<View> views = parentsToDelete.get(object);
			for (View view : views) {
				removeAdditionalParentToListen(object, view);
			}
		}
	}

	/**
	 * Check if the time element should be deleted and delete it if necessary.
	 */
	protected void deleteTimeElementIfNeeded() {
		if (!isTimeElementDefined()) {
			// delete the time element
			deleteTimeElement();
		}
		if (timeElementMissAnEventFigure()) {
			// delete the view
			deleteTimeView();
		}
	}

	/**
	 * launch a weak synchronization. It could be useful in order to clean a diagram by an external tool.
	 */
	public void forceRefresh() {
		deleteTimeElementIfNeeded();
	}
}

Back to the top