Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 68e6003b0f510e8fef7f4e148d5865efb4bbb59e (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
/*****************************************************************************
 * Copyright (c) 2014, 2015, 2021 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 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:
 *   CEA LIST - Initial API and implementation
 *   Christian W. Damus - bug 465416
 *   Christian W. Damus - bug 477384
 *   Quentin Le Menez - bug 570177
 *
 *****************************************************************************/

package org.eclipse.papyrus.infra.gmfdiag.common.sync;

import java.util.Iterator;
import java.util.Map;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.emf.common.command.AbstractCommand;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CommandWrapper;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.edit.command.DeleteCommand;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.Request;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewRequest;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.gmfdiag.common.commands.SemanticElementAdapter;
import org.eclipse.papyrus.infra.gmfdiag.common.service.visualtype.VisualTypeService;
import org.eclipse.papyrus.infra.sync.EStructuralFeatureSyncFeature;
import org.eclipse.papyrus.infra.sync.SyncBucket;
import org.eclipse.papyrus.infra.sync.SyncItem;
import org.eclipse.papyrus.infra.sync.SyncRegistry;
import org.eclipse.papyrus.infra.sync.service.ISyncService;
import org.eclipse.papyrus.infra.sync.service.SyncServiceRunnable;
import org.eclipse.papyrus.infra.tools.util.TypeUtils;
import org.eclipse.swt.widgets.Control;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.MapMaker;

/**
 * Common implementation of a synchronization feature for nested views in a GMF diagram.
 *
 * @author Laurent Wouters
 *
 * @param <M>
 *            The type of the underlying model element common to all synchronized items in a single bucket
 * @param <N>
 *            The type of the model element visualized by the nested diagram views that I synchronize
 * @param <T>
 *            The type of the backend element to synchronize
 */
public abstract class AbstractNestedDiagramViewsSyncFeature<M extends EObject, N extends EObject, T extends EditPart> extends EStructuralFeatureSyncFeature<M, T> {
	private final SyncRegistry<N, T, Notification> nestedRegistry;

	private Map<EObject, EObject> lastKnownElements = new MapMaker().weakKeys().weakValues().makeMap();

	/**
	 * Initializes me with my controlling bucket and one or more references
	 * in the GMF notation model that provide nested views.
	 *
	 * @param bucket
	 *            The bucket doing the synchronization
	 * @param reference
	 *            a notation view reference
	 * @param more
	 *            additional (optional) notation view references
	 */
	public AbstractNestedDiagramViewsSyncFeature(SyncBucket<M, T, Notification> bucket, EReference reference, EReference... more) {
		super(bucket, reference, more);

		nestedRegistry = getSyncRegistry(getNestedSyncRegistryType());
	}

	protected abstract Class<? extends SyncRegistry<N, T, Notification>> getNestedSyncRegistryType();

	protected SyncRegistry<N, T, Notification> getNestedSyncRegistry() {
		return nestedRegistry;
	}

	/**
	 * Gets the edit part that shall be observed and modified from the specified one
	 *
	 * @param parent
	 *            The edit part we work on
	 * @return The effective edit part that is observed and modified
	 */
	protected abstract EditPart getEffectiveEditPart(EditPart parent);

	@Override
	protected final Iterable<? extends T> getContents(T backend) {
		return filterContents(basicGetContents(backend));
	}

	/**
	 * Gets an unfiltered view of the nested edit-parts with the specified {@code backend} edit-part, which depends
	 * on the kind of edit-part that it is.
	 *
	 * @param backend
	 *            a back-end edit-part
	 * @return the raw view of its children, either nested nodes or contained/attached connections, as appropriate
	 */
	abstract Iterable<? extends T> basicGetContents(T backend);

	protected Iterable<? extends T> filterContents(Iterable<? extends T> rawContents) {
		return Iterables.filter(rawContents, new Predicate<T>() {
			private final Class<? extends N> nestedType = nestedRegistry.getModelType();

			@Override
			public boolean apply(T input) {
				View view = TypeUtils.as(input.getModel(), View.class);
				return (view != null) && nestedType.isInstance(view.getElement());
			}
		});
	}

	/**
	 * Filters the addition of new elements to add only those that are of the nested model element type.
	 */
	@Override
	protected boolean shouldAdd(SyncItem<M, T> from, SyncItem<M, T> to, EObject newSource) {
		return getNestedSyncRegistry().getModelType().isInstance(newSource);
	}

	/**
	 * Override this one because we need to execute certain post-actions asynchronously.
	 */
	@Override
	protected Command getAddCommand(final SyncItem<M, T> from, final SyncItem<M, T> to, final EObject newModel) {
		return new AbstractCommand() {
			private T child;

			private EObject objectToDrop;
			private org.eclipse.gef.commands.Command dropCommand;

			@Override
			protected boolean prepare() {
				return true;
			}

			/**
			 * We need to defer the calculation of the drop command until it is time to execute it,
			 * because otherwise the object that we need to drop may not yet exist in the target
			 * model if there is some kind of master/slave mapping in the semantic model, also.
			 *
			 * @return the drop command
			 */
			private org.eclipse.gef.commands.Command getDropCommand() {
				if (dropCommand == null) {
					EditPartViewer viewer = getEffectiveEditPart(to.getBackend()).getViewer();
					Control control = viewer.getControl();
					FigureCanvas figureCanvas = (FigureCanvas) control;
					Point location = figureCanvas.getViewport().getViewLocation();

					objectToDrop = getTargetModel(from, to, newModel);
					IGraphicalEditPart dropEditPart = (IGraphicalEditPart) getEffectiveEditPart(to.getBackend());

					CreateRequest createRequest = getCreateRequest(dropEditPart, objectToDrop, location);
					if (createRequest != null) {
						dropCommand = getCreateCommand(dropEditPart, createRequest);
					}

					if (dropCommand == null) {
						dropCommand = org.eclipse.gef.commands.UnexecutableCommand.INSTANCE;
					}
				}

				return dropCommand;
			}

			@Override
			public void execute() {
				getDropCommand().execute();
				onDo();
			}

			@Override
			public void undo() {
				if (child != null) {
					Command additional = onTargetRemoved(to, child);
					if (additional != null) {
						additional.execute();
					}
				}
				getDropCommand().undo();
			}

			@Override
			public void redo() {
				getDropCommand().redo();
				onDo();
			}

			private void onDo() {
				runAsync(new SyncServiceRunnable.Safe<T>() {
					@Override
					public T run(ISyncService syncService) {
						child = findChild(to.getBackend(), getTargetModel(from, to, objectToDrop));
						if (child != null) {
							Command additional = onTargetAdded(from, newModel, to, child);
							if (additional != null) {
								syncService.execute(additional);
							}
						}
						return child;
					}
				});
			}
		};
	}

	protected CreateRequest getCreateRequest(IGraphicalEditPart parentPart, EObject element, Point atLocation) {
		CreateRequest result = null;

		View parentView = parentPart.getNotationView();

		// Consult the visual type service to get the appropriate view type
		String viewType = VisualTypeService.getInstance().getNodeType(parentView, element);
		if (viewType != null) {
			IElementType elementType = VisualTypeService.getInstance().getElementType(parentView.getDiagram(), viewType);
			IAdaptable elementAdapter = new SemanticElementAdapter(element, elementType);

			CreateViewRequest.ViewDescriptor descriptor = new CreateViewRequest.ViewDescriptor(
					elementAdapter,
					Node.class,
					viewType,
					parentPart.getDiagramPreferencesHint());
			result = new CreateViewRequest(descriptor);
			result.setLocation(atLocation);
		}

		return result;
	}

	protected org.eclipse.gef.commands.Command getCreateCommand(IGraphicalEditPart parentPart, CreateRequest request) {
		EditPart targetEditPart = getTargetEditPart(parentPart, request);
		if (targetEditPart instanceof IGraphicalEditPart) {
			parentPart = (IGraphicalEditPart) targetEditPart;
		}

		return parentPart.getCommand(request);
	}

	protected EditPart getTargetEditPart(EditPart parentEditPart, Request request) {
		return parentEditPart.getTargetEditPart(request);
	}

	/**
	 * Retrieves the child edit part that represents the specified model element
	 *
	 * @param parent
	 *            The parent edit part
	 * @param model
	 *            The model element to look for
	 * @return The child edit part, or <code>null</code> if none is found
	 */
	protected T findChild(T parent, EObject model) {
		if (parent == null || model == null) {
			return null;
		}
		Iterable<? extends T> children = getContents(parent);
		for (Iterator<? extends T> iter = children.iterator(); iter.hasNext();) {
			T child = iter.next();
			if (model == getModelOf(child)) {
				return child;
			}
		}
		return null;
	}

	/**
	 * Override this one because we need to execute certain post-actions asynchronously.
	 */
	@Override
	protected Command getRemoveCommand(final SyncItem<M, T> from, final EObject oldSource, final SyncItem<M, T> to, T _oldTarget) {
		final View oldView = (View) _oldTarget.getModel();

		return new CommandWrapper(doGetRemoveCommand(from, oldSource, to, _oldTarget)) {
			private T oldTarget;

			@Override
			public void execute() {
				updateOldTarget();
				super.execute();
				onDo();
			}

			private void updateOldTarget() {
				Object editPart = to.getBackend().getViewer().getEditPartRegistry().get(oldView);
				oldTarget = AbstractNestedDiagramViewsSyncFeature.this.getNestedSyncRegistry().getBackendType().cast(editPart);
			}

			@Override
			public void undo() {
				super.undo();

				// Only notify of add if we had done that in the first place (this is not an initial sync that is being undone)
				if (oldSource != null) {
					runAsync(new SyncServiceRunnable.Safe<Command>() {
						@Override
						public Command run(ISyncService syncService) {
							// A new edit-part is always created when a deleted view is restored, so find it.
							updateOldTarget();

							Command additional = onTargetAdded(from, oldSource, to, oldTarget);
							if (additional != null) {
								syncService.execute(additional);
							}
							return additional;
						}
					});
				}
			}

			@Override
			public void redo() {
				updateOldTarget();
				super.redo();
				onDo();
			}

			private void onDo() {
				Command additional = onTargetRemoved(to, oldTarget);
				if (additional != null) {
					additional.execute();
				}
			}
		};
	}

	@Override
	protected Command doGetRemoveCommand(final SyncItem<M, T> from, final EObject oldSource, final SyncItem<M, T> to, final T oldTarget) {
		return DeleteCommand.create(getEditingDomain(), oldTarget.getModel());
	}

	@Override
	protected EObject getModelOfNotifier(EObject backendNotifier) {
		EObject result = ((View) backendNotifier).getElement();

		if (result != null) {
			// Update the cache of the last known element, if required
			if (lastKnownElements.get(backendNotifier) != result) {
				lastKnownElements.put(backendNotifier, result);
			}
		} else {
			result = lastKnownElements.get(backendNotifier);
		}

		return result;
	}

	@Override
	protected EObject getNotifier(T backend) {
		return (View) getEffectiveEditPart(backend).getModel();
	}
}

Back to the top