Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ab47b8acf16a06f42eadbd48b8102e0510934130 (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
package org.eclipse.papyrus.uml.compare.merger.utils;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.compare.EMFCompareMessages;
import org.eclipse.emf.compare.FactoryException;
import org.eclipse.emf.compare.util.EFactory;
import org.eclipse.emf.ecore.EEnum;
import org.eclipse.emf.ecore.EEnumLiteral;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.BasicInternalEList;
import org.eclipse.emf.ecore.util.InternalEList;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.osgi.util.NLS;
import org.eclipse.papyrus.uml.compare.merger.Activator;
import org.eclipse.papyrus.uml.merger.provider.PapyrusMergeCommandProvider;

//TODO : merge with thepapyrus table command factory?
/**
 * 
 * This class is adapted from {@link EFactory}
 * 
 */
public class PapyrusEFactory {


	private PapyrusEFactory() {
		//nothing to do
	}

	public static final <T> Command getEAddCommand(EObject object, String name, T arg) throws FactoryException {
		return getEAddCommand(object, name, arg, -1);
	}
	
	public static final <T>  Command getEAddCommand(EObject object, String name, T arg, int elementIndex) throws FactoryException {
		return getEAddCommand(object, name, arg, elementIndex, false);
	}
	
	public static final <T> Command getEAddCommand(EObject object, String name, T arg, int elementIndex, boolean reorder) throws FactoryException {
		final TransactionalEditingDomain domain = MergerUtils.getEditingDomain();
		Command returnedCommand = null;
		final EStructuralFeature feature = eStructuralFeature(object, name);
		if(feature.isMany() && arg != null) {
			final Object manyValue = object.eGet(feature);
			//			if (manyValue instanceof InternalEList<?>) {
			//				final InternalEList<? super T> internalEList = (InternalEList<? super T>)manyValue;
			//				final int listSize = internalEList.size();
			//				if (elementIndex > -1 && elementIndex < listSize) {
			//					internalEList.addUnique(elementIndex, arg);
			//				} else {
			//					internalEList.addUnique(arg);
			//				}
			//				if (reorder) {
			//					attachRealPositionEAdapter(arg, elementIndex);
			//					reorderList(internalEList);
			//				}
			//			} else if (manyValue instanceof List<?>) {
			//				final List<? super T> list = (List<? super T>)manyValue;
			//				final int listSize = list.size();
			//				if (elementIndex > -1 && elementIndex < listSize) {
			//					list.add(elementIndex, arg);
			//				} else {
			//					list.add(arg);
			//				}
			//				if (reorder) {
			//					attachRealPositionEAdapter(arg, elementIndex);
			//					reorderList(list);
			//				}
			//			} else if (manyValue instanceof Collection<?>) {
			//				((Collection<? super T>)manyValue).add(arg);
			//			}
			
			if(manyValue instanceof Collection<?>) {
				List<Object> newValue = new ArrayList<Object>((Collection<?>)manyValue);
				final int listSize = newValue.size();
				if(manyValue instanceof List<?>) {
					if(elementIndex > -1 && elementIndex < listSize) {
						newValue.add(elementIndex, arg);
					} else {
						newValue.add(arg);
					}
					if(reorder) {
						attachRealPositionEAdapter(arg, elementIndex);
						reorderList((List<?>)newValue);
					}
				} if(manyValue instanceof Collection<?>) {
					newValue.add(arg);
				}
				returnedCommand = PapyrusMergeCommandProvider.INSTANCE.getSetCommand(domain, object, feature, newValue);
			}
		} else if(!feature.isMany()) {
			//			eSet(object, name, arg);
			returnedCommand = PapyrusMergeCommandProvider.INSTANCE.getSetCommand(domain, object, feature, arg);
		}
		return returnedCommand;
	}

	//TODO a tester
	public static final Command getERemove(EObject object, String name, Object arg) throws FactoryException {
//		final Object list = object.eGet(eStructuralFeature(object, name));
//		if (list instanceof List) {
//			if (arg != null) {
//				((List<?>)list).remove(arg);
//			}
//		} else {
//			eSet(object, name, null);
//		}
		final Object list = object.eGet(eStructuralFeature(object, name));
		if (list instanceof List) {
			if (arg != null) {
				List<?> newValue = new ArrayList((List<?>)list);
				((List<?>)newValue).remove(arg);
				return getEAddCommand(object, name, newValue);
			}
		} else {
			return getESetCommand(object, name, null);
		}
		return null;
	}
	/**
	 * 
	 * @param object
	 * @param name
	 * @param arg
	 * @return
	 * @throws FactoryException
	 */
	public static final Command getESetCommand(final EObject object, final String name, final Object arg) throws FactoryException {
		final TransactionalEditingDomain domain = MergerUtils.getEditingDomain();
		Command returnedCommand;
		final EStructuralFeature feature = eStructuralFeature(object, name);
		if(!feature.isChangeable())
			//TODO : change for papyrus factory?
			throw new FactoryException(EMFCompareMessages.getString("EFactory.UnSettableFeature", name)); //$NON-NLS-1$

		if(feature.getEType() instanceof EEnum && arg instanceof String) {
			final EEnumLiteral literal = ((EEnum)feature.getEType()).getEEnumLiteral((String)arg);
			//object.eSet(feature, literal);
			returnedCommand = PapyrusMergeCommandProvider.INSTANCE.getSetCommand(domain, object, feature, literal);
		} else {
			if(arg == null && feature.isMany()) {
				//object.eSet(feature, Collections.EMPTY_LIST);
				returnedCommand = PapyrusMergeCommandProvider.INSTANCE.getSetCommand(domain, object, feature, Collections.EMPTY_LIST);
			} else if(arg == null) {
				//object.eSet(feature, feature.getDefaultValue());
				returnedCommand = PapyrusMergeCommandProvider.INSTANCE.getSetCommand(domain, object, feature, feature.getDefaultValue());
			} else {
				//object.eSet(feature, arg);
				returnedCommand = PapyrusMergeCommandProvider.INSTANCE.getSetCommand(domain, object, feature, arg);
			}
		}
		return returnedCommand;
	}


	public static EStructuralFeature eStructuralFeature(EObject object, String name) throws FactoryException {
		return EFactory.eStructuralFeature(object, name);
	}



	/**
	 * If we could not merge a given object at its expected position in a list, we'll attach an Adapter to it
	 * in order to "remember" that "expected" position. That will allow us to reorder the list later on if
	 * need be.
	 * 
	 * @param object
	 *        The object on which to attach an Adapter.
	 * @param expectedPosition
	 *        The expected position of <code>object</code> in its list.
	 */
	public static void attachRealPositionEAdapter(Object object, int expectedPosition) {
		Class<?> myClass = null;
		try {
			myClass = Class.forName("org.eclipse.emf.compare.util.EFactory");
		} catch (ClassNotFoundException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		Class[] parameterTypes = new Class[2];
		parameterTypes[0] = java.lang.Object.class;
		parameterTypes[1] = Integer.TYPE;
		Method m = null;

		try {
			m = myClass.getDeclaredMethod("attachRealPositionEAdapter", parameterTypes);
		} catch (SecurityException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (NoSuchMethodException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		m.setAccessible(true);
		Object[] parameters = new Object[2];
		parameters[0] = object;
		parameters[1] = expectedPosition;

		Object result = null;
		try {
			result = (Object)m.invoke(myClass, parameters);
		} catch (IllegalArgumentException e) {
			Activator.log.error(e);
		} catch (IllegalAccessException e) {
			Activator.log.error(e);
		} catch (InvocationTargetException e) {
			Activator.log.error(e);
		}
	}


	/**
	 * Reorders the given list if it contains EObjects associated with a PositionAdapter which are not located
	 * at their expected positions.
	 * 
	 * @param list
	 *        The list that is to be reordered.
	 * @param <T>
	 *        type of the list's elements.
	 */
	public static <T> void reorderList(final List<T> list) {
		Class<?> myClass = null;
		try {
			myClass = Class.forName("org.eclipse.emf.compare.util.EFactory");
		} catch (ClassNotFoundException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		Class[] parameterTypes = new Class[1];
		parameterTypes[0] = java.util.List.class;
		Method m = null;

		try {
			m = myClass.getDeclaredMethod("reorderList", parameterTypes);
		} catch (SecurityException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (NoSuchMethodException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		m.setAccessible(true);
		Object[] parameters = new Object[1];
		parameters[0] = list;


		Object result = null;
		try {
			result = (Object)m.invoke(myClass, parameters);
		} catch (IllegalArgumentException e) {
			Activator.log.error(e);
		} catch (IllegalAccessException e) {
			Activator.log.error(e);
		} catch (InvocationTargetException e) {
			Activator.log.error(e);
		}


	}

	/**
	 * This method should never been called, except by copyCollection;
	 * This method allows to invoke the clone method on Cloneable object.
	 * 
	 * @param cloneable
	 *        a cloneable object
	 * @return
	 *         a copy of the cloned object
	 * @throws UnsupportedOperationException
	 *         used when the object is {@link Cloneable}, but not implemented (like LinkedList...)
	 */
	private static Object clone(final Cloneable cloneable) throws UnsupportedOperationException {
		Class<?> cloneableClass = cloneable.getClass();
		Method cloneMethod = null;
		Object newValue = null;
		try {
			cloneMethod = cloneableClass.getMethod("clone", new Class[0]);
		} catch (SecurityException e) {
			Activator.log.error(e);
		} catch (NoSuchMethodException e) {
			Activator.log.error(e);
		}
		cloneMethod.setAccessible(true);//useful?
		try {
			newValue = cloneMethod.invoke(cloneable, new Object[0]);
		} catch (IllegalArgumentException e) {
			Activator.log.error(e);
		} catch (IllegalAccessException e) {
			Activator.log.error(e);
		} catch (InvocationTargetException e) {
			Activator.log.error(e);
		} catch (UnsupportedOperationException e) {
			Activator.log.error(NLS.bind("I can't clone this object : {0}", cloneableClass), e);
			throw e;
		}
		return newValue;
	}

	/**
	 * 
	 * @param coll
	 *        a collection
	 * @return
	 *         a copy of the collection
	 */
	private static <T> Collection<?> copyCollection(final Collection<T> coll) {
		if(coll instanceof Cloneable) {
			try {
				return (Collection<?>)clone((Cloneable)coll);
			} catch (UnsupportedOperationException e) {
				Activator.log.warn(NLS.bind("I can't clone this object : {0}", coll.getClass()));
			}
		}
		if(coll instanceof InternalEList) {
			return new BasicInternalEList(Object.class, coll);//TODO : not sure for Object.class
		} else if(coll instanceof List) {
			return new ArrayList<T>(coll);
		}
		return new ArrayList<T>(coll);//
	}
}

Back to the top