Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fe99efb87868cb74664e6b69151822cd164c5893 (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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*****************************************************************************
 * Copyright (c) 2010, 2014 CEA LIST 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:
 * 
 *  Yann Tanguy (CEA LIST) yann.tanguy@cea.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - bug 440108
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.common.helper;

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

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.emf.type.core.ElementTypeRegistry;
import org.eclipse.gmf.runtime.emf.type.core.IContainerDescriptor;
import org.eclipse.gmf.runtime.emf.type.core.IEditHelperContext;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.ISpecializationType;
import org.eclipse.gmf.runtime.emf.type.core.edithelper.IEditHelperAdvice;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateRelationshipRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyDependentsRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest;
import org.eclipse.papyrus.commands.DestroyElementPapyrusCommand;
import org.eclipse.papyrus.infra.elementtypesconfiguration.notification.AbstractNotifierEditHelper;
import org.eclipse.papyrus.infra.emf.commands.UnsetValueCommand;
import org.eclipse.papyrus.infra.emf.requests.UnsetRequest;
import org.eclipse.papyrus.infra.gmfdiag.common.commands.CreateEditBasedElementCommand;
import org.eclipse.papyrus.infra.services.edit.utils.CacheRegistry;
import org.eclipse.papyrus.infra.services.edit.utils.IRequestCacheEntries;

/**
 * <pre>
 * This is a default Helper for UML element.
 * 
 * The only reason to override getDestroyElementWithDependentsCommand and getDestroyElementCommand
 * method here is to propagate the shared IClientContext used by Papyrus during the request creation.
 * Without this changes, the command to destroy dependent element won't be correctly created,
 * in EditHelper(s) the getDestroyDependentsCommand will only be called with default element type
 * (null command) and in AdviceHelper the getBeforeDestroyDependentsCommand will work but will
 * not retrieve command to destroy elements that themselves depend on dependent element to destroy.
 * 
 * The changes are replacing:
 * ElementTypeRegistry.getInstance().getElementType(req.getElementToDestroy());
 * by
 * ElementTypeRegistry.getInstance().getElementType(req.getElementToDestroy(), req.getClientContext()); 
 * 
 * See:
 * - Bug328232 (https://bugs.eclipse.org/bugs/show_bug.cgi?id=328232)
 * - Bug328506 (https://bugs.eclipse.org/bugs/show_bug.cgi?id=328506)
 * 
 * </pre>
 */
public class DefaultEditHelper extends AbstractNotifierEditHelper {

	/** Defined in org.eclipse.gmf.runtime.emf.type.core.internal.requests.RequestCacheEntries (internal) */
	public static final String Cache_Maps = IRequestCacheEntries.Cache_Maps;

	/** Defined in org.eclipse.gmf.runtime.emf.type.core.internal.requests.RequestCacheEntries (internal) */
	public static final String Element_Type = IRequestCacheEntries.Element_Type;

	/** Defined in org.eclipse.gmf.runtime.emf.type.core.internal.requests.RequestCacheEntries (internal) */
	public static final String Checked_Elements = IRequestCacheEntries.Checked_Elements;

	/** Defined in org.eclipse.gmf.runtime.emf.type.core.internal.requests.RequestCacheEntries (internal) */
	public static final String EditHelper_Advice = IRequestCacheEntries.EditHelper_Advice;

	/** Defined in org.eclipse.gmf.runtime.emf.type.core.internal.requests.RequestCacheEntries (internal) */
	public static final String Client_Context = IRequestCacheEntries.Client_Context;

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected boolean approveRequest(IEditCommandRequest request) {
		if (request instanceof CreateRelationshipRequest) {
			return defaultApproveCreateRelationshipRequest((CreateRelationshipRequest) request);
		} else if (request instanceof CreateElementRequest) {
			// check the containment feature.
			Object context = request.getEditHelperContext();
			if (context instanceof EObject) {
				EObject owner = (EObject) context;
				EReference reference = getContainmentFeature((CreateElementRequest) request);
				// The context may not have this reference if some intermediate
				// container is to be created by the edit-helper.
				// But, then, we can only optimistically report that we can create
				// the child
				if ((reference != null) && (owner.eClass().getEAllContainments().contains(reference))) {
					return true;
				} else {
					return false;
				}
			}
		}
		return super.approveRequest(request);
	}

	protected boolean defaultApproveCreateRelationshipRequest(CreateRelationshipRequest request) {
		return true;
	}

	/**
	 * @return
	 */
	protected EReference getContainmentFeature(CreateElementRequest request) {
		EReference containmentFeature = request.getContainmentFeature();
		if (containmentFeature != null) {
			return containmentFeature;
		}

		containmentFeature = computeContainmentFeature(request);
		request.initializeContainmentFeature(containmentFeature);
		return containmentFeature;
	}

	/**
	 * Gets the EClass of the element to be edited.
	 * 
	 * @return the EClass
	 */
	protected EClass getEClassToEdit(CreateElementRequest request) {

		Object context = request.getEditHelperContext();

		if (context instanceof EObject) {
			return ((EObject) context).eClass();

		} else {
			IElementType type = ElementTypeRegistry.getInstance()
					.getElementType(context);

			if (type != null) {
				return type.getEClass();
			}
		}
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected ICommand getCreateCommand(CreateElementRequest req) {
		return new CreateEditBasedElementCommand(req);
	}

	/**
	 * Gets the command to destroy a single child of an element of my kind along
	 * with its dependents (not related by containment). By default, returns a
	 * composite that destroys the elements and zero or more dependents.
	 * 
	 * @param req
	 *            the destroy request
	 * @return a command that destroys the element specified as the request's {@linkplain DestroyElementRequest#getElementToDestroy() element to
	 *         destroy} and its non-containment dependents
	 */
	protected ICommand getDestroyElementWithDependentsCommand(DestroyElementRequest req) {
		ICommand result = getBasicDestroyElementCommand(req);

		EObject initial = (EObject) req.getParameter(DestroyElementRequest.INITIAL_ELEMENT_TO_DESTROY_PARAMETER);

		if (initial == null) {
			// set the parameter to keep track of the initial element to destroy
			req.setParameter(DestroyElementRequest.INITIAL_ELEMENT_TO_DESTROY_PARAMETER, req.getElementToDestroy());
		}

		// get elements dependent on the element we are destroying, that
		// must also be destroyed
		DestroyDependentsRequest ddr = (DestroyDependentsRequest) req.getParameter(DestroyElementRequest.DESTROY_DEPENDENTS_REQUEST_PARAMETER);
		if (ddr == null) {
			// create the destroy-dependents request that will be propagated to
			// destroy requests for all elements destroyed in this operation
			ddr = new DestroyDependentsRequest(req.getEditingDomain(), req.getElementToDestroy(), req.isConfirmationRequired());
			// propagate the parameters, including the initial element to
			// destroy parameter
			ddr.addParameters(req.getParameters());
			ddr.setClientContext(req.getClientContext());
			req.setParameter(DestroyElementRequest.DESTROY_DEPENDENTS_REQUEST_PARAMETER, ddr);
		} else {
			ddr.setElementToDestroy(req.getElementToDestroy());
		}

		IElementType typeToDestroy = null;
		Map cacheMaps = (Map) req.getParameter(Cache_Maps);
		if (cacheMaps != null) {
			Map map = (Map) cacheMaps.get(req.getElementToDestroy());
			if (map != null) {
				typeToDestroy = (IElementType) map.get(Element_Type);
			}
		}

		if (typeToDestroy == null) {
			typeToDestroy = ElementTypeRegistry.getInstance().getElementType(req.getElementToDestroy(), req.getClientContext());
		}

		if (typeToDestroy != null) {
			ICommand command = typeToDestroy.getEditCommand(ddr);

			if (command != null) {
				result = result.compose(command);
			}
		}

		return result;
	}


	/**
	 * Gets the command to destroy a child of an element of my kind. By
	 * default, returns a composite command that destroys the element specified
	 * by the request and all of its contents.
	 * 
	 * @param req
	 *            the destroy request
	 * @return a command that destroys the element specified as the request's {@link DestroyElementRequest#getElementToDestroy() element to destroy} along with its contents and other dependents
	 */
	protected ICommand getDestroyElementCommand(DestroyElementRequest req) {
		ICommand result = null;

		EObject parent = req.getElementToDestroy();

		if (req.getParameter(DestroyElementRequest.INITIAL_ELEMENT_TO_DESTROY_PARAMETER) == null) {
			req.setParameter(DestroyElementRequest.INITIAL_ELEMENT_TO_DESTROY_PARAMETER, parent);
		}

		IElementType parentType = null;

		Map cacheMaps = (Map) req.getParameter(Cache_Maps);
		Set checkedElement = null;
		if (cacheMaps != null) {
			checkedElement = (Set) cacheMaps.get(Checked_Elements);
			checkedElement.add(parent);
			Map parentMap = (Map) cacheMaps.get(parent);
			if (parentMap != null) {
				parentType = (IElementType) parentMap.get(Element_Type);
			} else {
				parentType = ElementTypeRegistry.getInstance().getElementType(parent, req.getClientContext());
			}
		} else {
			parentType = ElementTypeRegistry.getInstance().getElementType(parent, req.getClientContext());
		}

		if (parentType != null) {
			for (Iterator iter = parent.eContents().iterator(); iter.hasNext();) {
				EObject next = (EObject) iter.next();

				DestroyDependentsRequest ddr = (DestroyDependentsRequest) req.getParameter(DestroyElementRequest.DESTROY_DEPENDENTS_REQUEST_PARAMETER);

				// if another object is already destroying this one because it
				// is (transitively) a dependent, then don't destroy it again .
				if ((ddr == null) || ((checkedElement != null) && checkedElement.add(next)) || (!ddr.getDependentElementsToDestroy().contains(next))) {
					// set the element to be destroyed
					req.setElementToDestroy(next);

					ICommand command = parentType.getEditCommand(req);

					if (command != null) {
						if (result == null) {
							result = command;
						} else {
							result = result.compose(command);
						}

						// Under normal circumstances the command is executable.
						// Checking canExecute here slows down large scenarios and it is therefore
						// better to skip this check.
						// if (!command.canExecute()) {
						// // no point in continuing if we're abandoning the works
						// break;
						// }
					}
				}
			}
		}

		// restore the elementToDestroy in the original request
		req.setElementToDestroy(parent);

		ICommand destroyParent = getDestroyElementWithDependentsCommand(req);

		// bottom-up destruction: destroy children before parent
		if (result == null) {
			result = destroyParent;
		} else {
			result = result.compose(destroyParent);
		}

		return result;
	}

	/**
	 * Gets the array of edit helper advice for this request.
	 * 
	 * @param req
	 *            the edit request
	 * @return the edit helper advice, or <code>null</code> if there is none
	 */
	protected IEditHelperAdvice[] getEditHelperAdvice(IEditCommandRequest req) {
		IEditHelperAdvice[] advices = null;
		Object editHelperContext = req.getEditHelperContext();
		if (editHelperContext == null) {
			return null;
		}

		Map cacheMaps = (Map) req.getParameter(Cache_Maps);
		if (cacheMaps != null) {
			if (editHelperContext instanceof IEditHelperContext) {
				IElementType type = ((IEditHelperContext) editHelperContext).getElementType();
				if (type != null) {
					Map contextMap = (Map) cacheMaps.get(type);
					if (contextMap != null) {
						advices = (IEditHelperAdvice[]) contextMap.get(EditHelper_Advice);
					}
				} else {
					// get Type from the eobject
					EObject contextObject = ((IEditHelperContext) editHelperContext).getEObject();
					Map contextMap = (Map) cacheMaps.get(contextObject);
					if (contextMap != null) {
						advices = (IEditHelperAdvice[]) contextMap.get(EditHelper_Advice);
					}

				}

			} else {
				Map contextMap = (Map) cacheMaps.get(editHelperContext);
				if (contextMap != null) {
					advices = (IEditHelperAdvice[]) contextMap.get(EditHelper_Advice);
				}
			}

		}

		if (advices == null) {

			if (editHelperContext instanceof EObject) {
				// IElementType type = ElementTypeRegistry.getInstance().getElementType((EObject) editHelperContext, req.getClientContext());
				// advices = CacheRegistry.getInstance().getEditHelperAdvice(req.getClientContext(), type);
				advices = ElementTypeRegistry.getInstance().getEditHelperAdvice((EObject) editHelperContext, req.getClientContext());

			} else if (editHelperContext instanceof IElementType) {
				advices = CacheRegistry.getInstance().getEditHelperAdvice(req.getClientContext(), ((IElementType) editHelperContext));

			} else if (editHelperContext instanceof IEditHelperContext) {
				IElementType type = ((IEditHelperContext) editHelperContext).getElementType();
				if (type != null) {
					advices = CacheRegistry.getInstance().getEditHelperAdvice(req.getClientContext(), type);
				} else {
					advices = ElementTypeRegistry.getInstance().getEditHelperAdvice(editHelperContext);
				}
			} else {
				advices = ElementTypeRegistry.getInstance().getEditHelperAdvice(editHelperContext);
			}
		}

		return advices;
	}

	@Override
	protected ICommand getBasicDestroyElementCommand(DestroyElementRequest req) {
		ICommand result = req.getBasicDestroyCommand();

		if (result == null) {
			result = new DestroyElementPapyrusCommand(req);
		} else {
			// ensure that re-use of this request will not accidentally
			// propagate this command, which would destroy the wrong object
			req.setBasicDestroyCommand(null);
		}

		return result;
	}

	@Override
	protected ICommand getInsteadCommand(IEditCommandRequest req) {
		ICommand result = null;

		if (req instanceof UnsetRequest) {
			result = new UnsetValueCommand((UnsetRequest) req);
		} else {
			result = super.getInsteadCommand(req);
		}

		return result;
	}

	protected EReference getContainmentFeatureFromSpecializationType(ISpecializationType specializationType, CreateElementRequest req) {
		if (specializationType != null) {

			IContainerDescriptor containerDescriptor = specializationType.getEContainerDescriptor();

			if (containerDescriptor != null) {
				EReference[] features = containerDescriptor.getContainmentFeatures();

				if (features != null) {

					for (int i = 0; i < features.length; i++) {

						Object editHelperContext = req.getEditHelperContext();
						EClass eClass = null;

						if (editHelperContext instanceof EClass) {
							eClass = (EClass) editHelperContext;

						} else if (editHelperContext instanceof EObject) {
							eClass = ((EObject) editHelperContext).eClass();

						} else if (editHelperContext instanceof IElementType) {
							eClass = ((IElementType) editHelperContext).getEClass();
						}

						if (eClass != null && eClass.getEAllReferences().contains(features[i])) {
							// Use the first feature
							return features[i];
						}
					}
				}
			}
		}
		return null;
	}

	protected boolean initializeWithThisSpecializationType(ISpecializationType specializationType, CreateElementRequest req) {
		EReference containmentFeature = getContainmentFeatureFromSpecializationType(specializationType, req);
		if (containmentFeature != null) {
			req.initializeContainmentFeature(containmentFeature);
			return true;
		}
		return false;
	}

	protected boolean isKindOf(EClass a, EClassifier b) {
		if (a.getEAllSuperTypes().contains(b)) {
			return true;
		} else {
			return a.equals(b);
		}
	}

	protected EReference findDefaultContainmentFeature(EClass ownerType, EClass ownedType) {
		for (EReference eReference : ownerType.getEAllContainments()) {
			if (isKindOf(ownedType, eReference.getEReferenceType())) {
				return eReference;
			}
		}
		return null;
	}

	// Override to initialize containment also on the basis of superElementTypes
	@Override
	public void initializeDefaultFeature(CreateElementRequest req) {

		if (req.getContainmentFeature() == null) {
			EReference containmentFeature = computeContainmentFeature(req);
			if (containmentFeature != null) {
				req.initializeContainmentFeature(containmentFeature);
			}
		}
	}


	protected EReference computeContainmentFeature(CreateElementRequest request) {
		// First, try to find the feature from the element type
		ISpecializationType specializationType = (ISpecializationType) request.getElementType().getAdapter(ISpecializationType.class);

		if (specializationType != null) {

			EReference reference = getContainmentFeatureFromSpecializationType(specializationType, request);
			if (reference != null) {
				return reference;
			}

			IElementType[] superTypes = specializationType.getAllSuperTypes();

			// Try to initialize with the superTypes if not already initialized
			for (int i = 0; i < superTypes.length; i++) {
				IElementType superElementType = superTypes[i];

				if (superElementType instanceof ISpecializationType) {
					reference = getContainmentFeatureFromSpecializationType((ISpecializationType) superElementType, request);
					if (reference != null) {
						return reference;
					}
				}
			}
		}

		// reference has not been found from element types ==> return from default containment feature for EClass
		EClass eClass = request.getElementType().getEClass();

		if (eClass != null) {
			// Next, try to get a default feature
			EReference defaultFeature = getDefaultContainmentFeature(eClass);
			if (defaultFeature != null) {
				return defaultFeature;
			}

			// Compute default container (the first feature of container that can contain the new element's type)
			EReference defaultEReference = findDefaultContainmentFeature(request.getContainer().eClass(), eClass);
			if (defaultEReference != null) {
				return defaultEReference;
			}
		}
		// should never happen
		return null;
	}
}

Back to the top