Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 516a4025208c6f1a6ce1dc4b9bcc2a6f9bf61bd6 (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
/***************************************************
 * Copyright (c) 2010 Atos Origin.

 * 
 * 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 Origin - Initial API and implementation
 *
 ****************************************************/
package org.eclipse.papyrus.navigator.commands;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

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.util.EList;
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.emf.ecore.EStructuralFeature;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
import org.eclipse.gmf.runtime.emf.type.core.commands.DestroyElementCommand;
import org.eclipse.gmf.runtime.emf.type.core.commands.SetValueCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.core.utils.PapyrusEcoreUtils;

/**
 * The Class EObjectInheritanceCopyCommand. it takes an eobject in parameter and copy all the elements contained in the
 * source to the target and adds the target to the container of the source
 */
public class EObjectInheritanceCopyCommand extends CompositeCommand {

	private final EObject sourceEObject;

	private final EObject targetEObject;

	private final TransactionalEditingDomain editingDomain;

	private Collection<Object> alreadyManaged = new LinkedList<Object>();

	public EObjectInheritanceCopyCommand(EObject source, EClass target, TransactionalEditingDomain adapterFactoryEditingDomain) {
		super("Inheritance copy");
		this.sourceEObject = source;
		this.targetEObject = target.getEPackage().getEFactoryInstance().create(target);
		this.editingDomain = adapterFactoryEditingDomain;
		if(sourceEObject == null || targetEObject == null || editingDomain == null) {
			throw new IllegalArgumentException("Please provide non null arguments");
		}
		init();
		if(sourceEObject.eContainingFeature().isMany()) {
			replace(sourceEObject.eContainer(), sourceEObject, targetEObject, sourceEObject.eContainingFeature());
		} else {
			add(new CustomSetCommand(editingDomain, sourceEObject.eContainer(), sourceEObject.eContainingFeature(), targetEObject, sourceEObject, sourceEObject.eContainingFeature()));
			add(new DestroyElementCommand(new DestroyElementRequest(editingDomain, sourceEObject, false)));
		}
	}

	private void init() {
		modelCopy(sourceEObject, targetEObject);
		crossReference(sourceEObject, targetEObject);
	}

	/**
	 * Model copy, copy the eobject source attributes to target's
	 * 
	 * @param mixedDomain
	 *        the mixed domain
	 * @param source
	 *        the source
	 * @param target
	 *        the target
	 */
	private void modelCopy(EObject source, EObject target) {
		EClass eclass = source.eClass();
		if(eclass != null) {
			EList<EStructuralFeature> eAllStructuralFeatures = eclass.getEAllStructuralFeatures();
			for(EStructuralFeature e : eAllStructuralFeatures) {
				if(contains(target.eClass(), e) && isCompatible(e.getEType(), target.eClass().getEStructuralFeature(e.getName()).getEType())) {
					manageFeature(source, target, e);
				}
			}
		}
	}

	/**
	 * Contains. check if the target eclass contains a estructuralfeature with the same name less rigorous can work for
	 * many cases
	 * 
	 * @param target
	 *        the target
	 * @param e
	 *        the e
	 * 
	 * @return true, if successful
	 */
	private boolean contains(EClass target, EStructuralFeature e) {
		EList<EStructuralFeature> features = target.getEAllStructuralFeatures();
		for(EStructuralFeature f : features) {
			if(f.getName().equals(e.getName())) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Manage feature for cross.
	 * 
	 * @param mixedDomain
	 *        the mixed domain
	 * @param theObjectWithCross
	 *        the the object with cross
	 * @param source
	 *        the source
	 * @param target
	 *        the target
	 * @param structuralFeature
	 *        the structural feature
	 */
	private void manageFeatureForCross(EObject theObjectWithCross, EObject source, EObject target, EStructuralFeature structuralFeature) {
		boolean compatible = isCompatible(structuralFeature.getEType(), target.eClass());

		if(compatible && structuralFeature.isChangeable() && !structuralFeature.isDerived()) {
			if(structuralFeature.isMany()) {
				replace(theObjectWithCross, source, target, structuralFeature);
			} else {
				add(new SetValueCommand(new SetRequest(editingDomain, theObjectWithCross, structuralFeature, target)));
			}
		} else if(!compatible) {
			if(structuralFeature.isMany()) {
				remove(theObjectWithCross, source, structuralFeature);
			} else {
				add(new SetValueCommand(new SetRequest(editingDomain, theObjectWithCross, structuralFeature, null)));
			}
		}

	}

	private void remove(EObject owner, Object source, EStructuralFeature structuralFeature) {
		if(!alreadyManaged.contains(source)) {

			if(owner == null && structuralFeature == null) {
				if(source instanceof EObject) {
					add(new DestroyElementCommand(new DestroyElementRequest(editingDomain, (EObject)source, false)));
				}
			} else {
				Object value = owner.eGet(structuralFeature);
				if(value instanceof Collection<?>) {
					List<Object> newList = new ArrayList<Object>((Collection<?>)value);
					newList.remove(source);
					add(new SetValueCommand(new SetRequest(editingDomain, owner, structuralFeature, newList)));
				} else if(source.equals(value)) {
					add(new SetValueCommand(new SetRequest(editingDomain, owner, structuralFeature, null)));
				} else {
					add(new SetValueCommand(new SetRequest(editingDomain, owner, structuralFeature, null)));
				}
			}
			alreadyManaged.add(source);
		}
	}

	private void replace(EObject owner, Object source, Object target, EStructuralFeature structuralFeature) {
		if(!alreadyManaged.contains(source)) {

			if(owner == null && structuralFeature == null) {
				if(source instanceof EObject) {
					add(new DestroyElementCommand(new DestroyElementRequest(editingDomain, (EObject)source, false)));
				}
			} else {
				Object value = owner.eGet(structuralFeature);
				if(value instanceof Collection<?>) {
					List<Object> newList = new ArrayList<Object>((Collection<?>)value);
					int index = newList.indexOf(source);
					if(index >= 0) {
						newList.remove(index);
						newList.add(index, target);
						add(new SetValueCommand(new SetRequest(editingDomain, owner, structuralFeature, newList)));
					}
				} else if(source.equals(value)) {
					add(new SetValueCommand(new SetRequest(editingDomain, owner, structuralFeature, target)));
				} else {
					add(new SetValueCommand(new SetRequest(editingDomain, owner, structuralFeature, target)));
				}
			}
			alreadyManaged.add(source);
		}
	}

	@Override
	public IStatus undo(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
		return super.undo(progressMonitor, info);
	}

	/**
	 * Cross reference. Manage eobjects referencing the source eobject
	 * 
	 * @param mixedDomain
	 *        the mixed domain
	 * @param source
	 *        the source eobject
	 * @param target
	 *        the target eobject
	 */
	private void crossReference(EObject source, EObject target) {
		Collection<EStructuralFeature.Setting> collection = PapyrusEcoreUtils.getUsages(source);
		if(collection != null) {
			for(EStructuralFeature.Setting nonNavigableInverseReference : collection) {
				EStructuralFeature structuralFeature = nonNavigableInverseReference.getEStructuralFeature();
				if(!(nonNavigableInverseReference.getEObject() instanceof View)) {
					manageFeatureForCross(nonNavigableInverseReference.getEObject(), source, target, structuralFeature);
				} else if(nonNavigableInverseReference.getEObject() instanceof Diagram) {
					Diagram di = (Diagram)nonNavigableInverseReference.getEObject();
					remove(null, di, null);
				}
			}
		}
	}

	/**
	 * Checks if a type is compatible to another.
	 * 
	 * @param type
	 *        the type
	 * @param target
	 *        the target
	 * 
	 * @return true, if is compatible
	 */
	public static boolean isCompatible(EClassifier type, EClassifier target) {
		Collection<EClassifier> types = new LinkedList<EClassifier>();
		if(target instanceof EClass) {
			EClass eclass = (EClass)target;
			types.addAll(eclass.getEAllSuperTypes());
		}
		if(!types.contains(target)) {
			types.add(target);
		}
		return types.contains(type);
	}

	/**
	 * Manage a feature for the attribute's copy.
	 * 
	 * @param mixedDomain
	 *        the mixed domain
	 * @param source
	 *        the source
	 * @param target
	 *        the target
	 * @param feature
	 *        the e
	 */
	@SuppressWarnings("unchecked")
	private void manageFeature(EObject source, EObject target, EStructuralFeature feature) {
		EStructuralFeature targetFeature = getFeature(target, feature.getName());

		if(feature.getUpperBound() <= targetFeature.getUpperBound() && feature.getLowerBound() >= targetFeature.getLowerBound()) {
			if(feature.isChangeable() && !feature.isDerived()) {
				Object value = source.eGet(feature);
				if(feature.isMany() && targetFeature.isMany()) {
					Collection<EObject> list = (Collection<EObject>)value;
					if(list != null && !list.isEmpty()) {
						Collection<EObject> newList = new LinkedList<EObject>();
						newList.addAll(list);
						if(feature instanceof EReference && !((EReference)feature).isContainment()) {
							add(new SetValueCommand(new SetRequest(editingDomain, target, targetFeature, newList)));
						} else if(feature instanceof EReference && ((EReference)feature).isContainment()) {
							Collection<Object> toTreat = new LinkedList<Object>();
							for(Object o : newList) {
								if(!alreadyManaged.contains(o)) {
									toTreat.add(o);
									alreadyManaged.add(o);
								}
							}
							add(new CustomAddCommand(editingDomain, target, targetFeature, newList, source, feature));
						}
					}
				} else if(!feature.isMany() && !targetFeature.isMany()) {
					if(value != null) {
						if(!alreadyManaged.contains(value)) {
							alreadyManaged.add(value);
							add(new CustomSetCommand(editingDomain, target, targetFeature, value, source, feature));
						}
					}
				}
			}
		}

	}

	/**
	 * Gets a feature from a name
	 * 
	 * @param eobject
	 *        the eobject
	 * @param name
	 *        the name
	 * 
	 * @return the feature
	 */
	private EStructuralFeature getFeature(EObject eobject, String name) {
		return eobject.eClass().getEStructuralFeature(name);
	}

	/**
	 * Gets the result eobject.
	 * 
	 * @return the result eobject
	 */
	public EObject getResultEobject() {
		return targetEObject;
	}

	/**
	 * The Class CustomSetCommand. permits to change a value from an eobject to eanother
	 */
	private class CustomSetCommand extends SetValueCommand {

		private EObject oldObject = null;

		private EStructuralFeature oldFeature = null;

		private Object oldValue = null;

		public CustomSetCommand(TransactionalEditingDomain domain, EObject owner, EStructuralFeature feature, Object value, EObject old, EStructuralFeature structuralFeature) {
			super(new SetRequest(domain, owner, feature, value));
			oldObject = old;
			oldFeature = structuralFeature;
			oldValue = value;
		}

		@Override
		protected IStatus doUndo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
			IStatus result = super.doUndo(monitor, info);
			oldObject.eSet(oldFeature, oldValue);
			return result;
		}

	}

	/**
	 * The Class CustomSetCommand. permits to change a value from an eobject to eanother
	 */
	private class CustomAddCommand extends SetValueCommand {

		private EObject oldObject = null;

		private EStructuralFeature oldFeature;

		private EStructuralFeature newFeature;

		public CustomAddCommand(TransactionalEditingDomain editingDomain, EObject target, EStructuralFeature targetFeature, Collection<EObject> newList, EObject source, EStructuralFeature e) {
			super(new SetRequest(editingDomain, target, targetFeature, newList));
			oldObject = source;
			oldFeature = e;
			newFeature = targetFeature;
		}

		@Override
		protected IStatus doUndo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
			Object values = getElementToEdit().eGet(newFeature);
			IStatus result = super.doUndo(monitor, info);
			// this test permit to avoid modification from other command
			// if getOwner list is empty it will perform error we avoid it
			if(values instanceof Collection<?> && !((Collection<?>)values).isEmpty()) {
				Collection<?> collection = (Collection<?>)values;
				Collection<Object> collecOldObject = (Collection)oldObject.eGet(oldFeature);
				for(Object o : collection) {
					if(!collecOldObject.contains(o)) {
						collecOldObject.add(o);
					}
				}
			}
			return result;
		}
	}
}

Back to the top