Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 877e8a9ba2f119ae780495e150a510ac5581a838 (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
/*****************************************************************************
 * Copyright (c) 2011 Atos.
 *
 *
 * 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:
 *   Atos - Initial API and implementation
 *   Bug 366159 - [ActivityDiagram] Activity Diagram should be able to handle correctly Interruptible Edge
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.activity.listeners;

import java.util.Collection;
import java.util.Collections;

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.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EStructuralFeature.Setting;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.ECrossReferenceAdapter;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.transaction.NotificationFilter;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.UnexecutableCommand;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.diagram.core.services.ViewService;
import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil;
import org.eclipse.gmf.runtime.diagram.ui.commands.CommandProxy;
import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.core.editor.IMultiDiagramEditor;
import org.eclipse.papyrus.uml.diagram.activity.edit.part.interfaces.InterruptibleEdge;
import org.eclipse.papyrus.uml.diagram.activity.edit.parts.ControlFlowInterruptibleIconEditPart;
import org.eclipse.papyrus.uml.diagram.activity.edit.parts.ObjectFlowInterruptibleIconEditPart;
import org.eclipse.papyrus.uml.diagram.activity.part.UMLDiagramEditorPlugin;
import org.eclipse.papyrus.uml.diagram.activity.request.InterruptibleEdgeRequest;
import org.eclipse.papyrus.uml.diagram.common.listeners.AbstractPapyrusModifcationTriggerListener;
import org.eclipse.papyrus.uml.diagram.common.util.DiagramEditPartsUtil;
import org.eclipse.papyrus.uml.diagram.common.util.functions.EObjectToViewFunction;
import org.eclipse.papyrus.uml.diagram.common.util.functions.SettingToEObjectFunction;
import org.eclipse.papyrus.uml.diagram.common.util.predicates.ReferencingViewPredicate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.uml.ActivityEdge;
import org.eclipse.uml2.uml.UMLPackage;

import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.Iterables;

/**
 * This listener handle Interruptible Edge
 *
 * @author arthur daussy
 *
 */
public class InterruptibleEdgeListener extends AbstractPapyrusModifcationTriggerListener {

	/**
	 * Id of all Visual ID of the interruptible Icon
	 */
	private static ImmutableBiMap<EClass, String> INTERRUPTIBLE_EDGE_ICON_VISUAL_ID_COLLECTION = ImmutableBiMap.of(UMLPackage.Literals.OBJECT_FLOW, String.valueOf(ObjectFlowInterruptibleIconEditPart.VISUAL_ID), UMLPackage.Literals.CONTROL_FLOW,
			String.valueOf(ControlFlowInterruptibleIconEditPart.VISUAL_ID));

	private static NotificationFilter FEATURE_FILTER = null;

	public static NotificationFilter getFEATURE_FILTER() {
		if (FEATURE_FILTER == null) {
			FEATURE_FILTER = NotificationFilter.createFeatureFilter(UMLPackage.Literals.ACTIVITY_EDGE__INTERRUPTS);
		}
		return FEATURE_FILTER;
	}

	/**
	 * get the edit part registry
	 *
	 * @return
	 */
	protected DiagramEditPart getDiagramEditPart() {
		IWorkbench wb = PlatformUI.getWorkbench();
		IWorkbenchPage page = wb.getActiveWorkbenchWindow().getActivePage();
		IEditorPart editor = page.getActiveEditor();
		if (editor instanceof IMultiDiagramEditor) {
			IMultiDiagramEditor papyrusEditor = (IMultiDiagramEditor) editor;
			return (DiagramEditPart) papyrusEditor.getAdapter(DiagramEditPart.class);
		}
		return null;
	}

	/**
	 * This command will react on a SET event of the structural feature describe in {@link InterruptibleEdgeListener#isCorrectStructuralfeature(EStructuralFeature)} This will create a new view if the newValue != null or delete
	 * it if null
	 */
	@Override
	protected ICommand getModificationCommand(Notification notif) {
		if (Notification.SET == notif.getEventType()) {
			CompositeCommand cc = new CompositeCommand("Interruptible Edge Command");//
			// Handling views
			final Iterable<IGraphicalEditPart> edgesEditPart = DiagramEditPartsUtil.getChildrenByEObject((EObject) notif.getNotifier(), getDiagramEditPart(), true);
			InterruptibleEdgeRequest request = new InterruptibleEdgeRequest();
			Iterable<View> views = getReferencingView(notif);
			if (notif.getNewValue() != null) {
				// handle create view
				request.setType(InterruptibleEdgeRequest.SET_INTERRUPTIBLE_EDGE);
				for (View view : views) {
					try {
						String visualID = INTERRUPTIBLE_EDGE_ICON_VISUAL_ID_COLLECTION.get(view.getElement().eClass());
						ICommand createViewCommand = createInterruptibleEdgeIcon(view, visualID);
						if (createViewCommand != null && createViewCommand.canExecute()) {
							cc.compose(createViewCommand);
						}
					} catch (NullPointerException e) {
						throw new RuntimeException("Unable to find the Visual ID of the Icon of the interruptible Edge for element" + view.getElement());
					}
				}
			} else {
				// handle delete view
				request.setType(InterruptibleEdgeRequest.UNSET_INTERRUPTIBLE_EDGE);
				for (View view : views) {
					try {
						String visualID = INTERRUPTIBLE_EDGE_ICON_VISUAL_ID_COLLECTION.get(view.getElement().eClass());
						ICommand destroyCommand = destroyInterruptibleIcon(view, visualID);
						if (destroyCommand != null && destroyCommand.canExecute()) {
							cc.compose(destroyCommand);
						}
					} catch (NullPointerException e) {
						throw new RuntimeException("Unable to find the Visual ID of the Icon of the interruptible Edge for element" + view.getElement());
					}
				}
			}
			for (IGraphicalEditPart edgeEditPart : edgesEditPart) {
				if (edgeEditPart != null && edgeEditPart instanceof InterruptibleEdge && edgeEditPart.getModel() instanceof View) {
					// Ask for the edit if something more else has to be done
					Command command = edgeEditPart.getCommand(request);
					if (command != null && command.canExecute()) {
						cc.compose(new CommandProxy(command));
					}
				}
			}
			return cc;
		}
		return null;
	}

	/**
	 * Get all the view from Notation Model which represent the notifier.
	 * Will only return the view of the same type than the notifier view
	 *
	 * @param notif
	 * @param edgeEditPart
	 * @return
	 */
	public Iterable<View> getReferencingView(Notification notif) {
		final ActivityEdge element = getElement(notif);
		Resource eResource = element.eResource();
		if (eResource != null) {
			ECrossReferenceAdapter adapter = ECrossReferenceAdapter.getCrossReferenceAdapter(eResource.getResourceSet());
			if (adapter == null) {
				adapter = new ECrossReferenceAdapter();
			}
			Collection<Setting> inverseReferences = adapter.getInverseReferences(element);
			Iterable<EObject> settings = Iterables.transform(inverseReferences, new SettingToEObjectFunction());
			Iterable<EObject> eObjects = Iterables.filter(settings, new ReferencingViewPredicate(element));
			Iterable<View> views = Iterables.transform(eObjects, new EObjectToViewFunction());
			return views;
		}
		return Collections.emptyList();
	}

	private TransactionalEditingDomain getEditingDomain(View model) {
		DiagramEditPart diagramEditPart = getDiagramEditPart();
		if (diagramEditPart != null) {
			return diagramEditPart.getEditingDomain();
		}
		EditingDomain editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(model);
		if (editingDomain instanceof TransactionalEditingDomain) {
			return (TransactionalEditingDomain) editingDomain;
		}
		return null;
	}

	/**
	 * Create the command to withdraw interruptible edge icon from the activity edge
	 *
	 * @return
	 */
	private ICommand destroyInterruptibleIcon(final View model, final String visualID) {
		TransactionalEditingDomain editingDomain = getEditingDomain(model);
		if (editingDomain != null) {
			AbstractTransactionalCommand cmd = new AbstractTransactionalCommand(editingDomain, "Destroy Interruptible Edge Icon", null) {

				@Override
				protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
					if (model != null) {
						View interruptibleEdgeIconView = ViewUtil.getChildBySemanticHint(model, visualID);
						ViewUtil.destroy(interruptibleEdgeIconView);
						return CommandResult.newOKCommandResult();
					}
					return null;
				}
			};
			return cmd;
		}
		return null;
	}

	/**
	 * Create the Interruptible Edge Icon View
	 *
	 * @return Command to be executed or {@link UnexecutableCommand#INSTANCE} if unable to create
	 */
	private ICommand createInterruptibleEdgeIcon(final View model, final String visualID) {
		TransactionalEditingDomain editingDomain = getEditingDomain(model);
		if (editingDomain != null) {
			AbstractTransactionalCommand cmd = new AbstractTransactionalCommand(editingDomain, "Create Interruptible Edge Icon", null) {

				@Override
				protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
					if (model != null) {
						Node node = ViewService.createNode(model, visualID, UMLDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
						if (node != null) {
							return CommandResult.newOKCommandResult(node);
						} else {
							return CommandResult.newErrorCommandResult("Unable to create the view for Interruptible Edge label");
						}
					}
					return null;
				}

				/*
				 * TODO test if needed
				 * (non-Javadoc)
				 *
				 * @see
				 * org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand#doUndo(org.eclipse.core.runtime.IProgressMonitor,
				 * org.eclipse.core.runtime.IAdaptable)
				 */
				@Override
				protected IStatus doUndo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
					IStatus status = super.doUndo(monitor, info);
					getDiagramEditPart().refresh();
					return status;
				}

				/*
				 * TODO test if needed
				 * (non-Javadoc)
				 *
				 * @see
				 * org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand#doUndo(org.eclipse.core.runtime.IProgressMonitor,
				 * org.eclipse.core.runtime.IAdaptable)
				 */
				@Override
				protected IStatus doRedo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
					IStatus status = super.doRedo(monitor, info);
					getDiagramEditPart().refresh();
					return status;
				}
			};
			return cmd;
		}
		return null;
	}

	protected ActivityEdge getElement(Notification notif) {
		Object element = notif.getNotifier();
		if (element instanceof ActivityEdge) {
			return (ActivityEdge) element;
		}
		return null;
	}

	@Override
	public NotificationFilter getFilter() {
		return getFEATURE_FILTER();
	}
}

Back to the top