Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorvlorenzo2012-04-13 05:25:51 -0400
committervlorenzo2012-04-13 05:25:51 -0400
commit991e44d8216de92e3a104285cd51a5e890a1edad (patch)
tree9d97ea319425260f7dc62f48b7bf3fec6c144f50 /sandbox/UMLCompareMergerExample
parent4f0d9b2284b54540ea3273f678044efc39ee1af8 (diff)
downloadorg.eclipse.papyrus-991e44d8216de92e3a104285cd51a5e890a1edad.tar.gz
org.eclipse.papyrus-991e44d8216de92e3a104285cd51a5e890a1edad.tar.xz
org.eclipse.papyrus-991e44d8216de92e3a104285cd51a5e890a1edad.zip
342163: [Usability] Papyrus merge should use the service edit of Papyrus
https://bugs.eclipse.org/bugs/show_bug.cgi?id=342163
Diffstat (limited to 'sandbox/UMLCompareMergerExample')
-rw-r--r--sandbox/UMLCompareMergerExample/org.eclipse.papyrus.uml.compare.merger/src/org/eclipse/papyrus/uml/compare/merger/utils/PapyrusEFactory.java331
-rw-r--r--sandbox/UMLCompareMergerExample/org.eclipse.papyrus.uml.compare.merger/src/org/eclipse/papyrus/uml/merger/provider/CModelElementChangeLeftTargetMerger.java86
-rw-r--r--sandbox/UMLCompareMergerExample/org.eclipse.papyrus.uml.compare.merger/src/org/eclipse/papyrus/uml/merger/provider/CModelElementChangeRightTargetMerger.java103
3 files changed, 462 insertions, 58 deletions
diff --git a/sandbox/UMLCompareMergerExample/org.eclipse.papyrus.uml.compare.merger/src/org/eclipse/papyrus/uml/compare/merger/utils/PapyrusEFactory.java b/sandbox/UMLCompareMergerExample/org.eclipse.papyrus.uml.compare.merger/src/org/eclipse/papyrus/uml/compare/merger/utils/PapyrusEFactory.java
new file mode 100644
index 00000000000..ab47b8acf16
--- /dev/null
+++ b/sandbox/UMLCompareMergerExample/org.eclipse.papyrus.uml.compare.merger/src/org/eclipse/papyrus/uml/compare/merger/utils/PapyrusEFactory.java
@@ -0,0 +1,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);//
+ }
+}
diff --git a/sandbox/UMLCompareMergerExample/org.eclipse.papyrus.uml.compare.merger/src/org/eclipse/papyrus/uml/merger/provider/CModelElementChangeLeftTargetMerger.java b/sandbox/UMLCompareMergerExample/org.eclipse.papyrus.uml.compare.merger/src/org/eclipse/papyrus/uml/merger/provider/CModelElementChangeLeftTargetMerger.java
index 9a84a254bc4..54163a98ae8 100644
--- a/sandbox/UMLCompareMergerExample/org.eclipse.papyrus.uml.compare.merger/src/org/eclipse/papyrus/uml/merger/provider/CModelElementChangeLeftTargetMerger.java
+++ b/sandbox/UMLCompareMergerExample/org.eclipse.papyrus.uml.compare.merger/src/org/eclipse/papyrus/uml/merger/provider/CModelElementChangeLeftTargetMerger.java
@@ -21,9 +21,11 @@ import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.xmi.XMIResource;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
+import org.eclipse.osgi.util.NLS;
import org.eclipse.papyrus.uml.compare.merger.utils.MergerUtils;
+import org.eclipse.papyrus.uml.compare.merger.utils.PapyrusEFactory;
-
+//TODO : should be redone, to re-implements Efactory.eAdd, ...aso
public class CModelElementChangeLeftTargetMerger extends ModelElementChangeLeftTargetMerger {
/**
@@ -49,85 +51,65 @@ public class CModelElementChangeLeftTargetMerger extends ModelElementChangeLeftT
protected void doUndoInTarget() {
if(MergerUtils.usePapyrusMerger()) {
TransactionalEditingDomain domain = MergerUtils.getEditingDomain();
- CompoundCommand cmd = new CompoundCommand("ModelElementChangeLeftTargetCommand");
+ CompoundCommand cmd = new CompoundCommand("ModelElementChangeLeftTargetCommand#doUndoInTarget");
final ModelElementChangeLeftTarget theDiff = (ModelElementChangeLeftTarget)this.diff;
// we should copy the element to the Origin one.
final EObject origin = theDiff.getRightParent();
final EObject element = theDiff.getLeftElement();
final EObject newOne = copy(element);
final EReference ref = element.eContainmentFeature();
- if(ref != null) {
- // try {
- int elementIndex = -1;
- if(ref.isMany()) {
- final Object containmentRefVal = element.eContainer().eGet(ref);
- if(containmentRefVal instanceof List<?>) {
- @SuppressWarnings("unchecked")
- final List<EObject> listVal = (List<EObject>)containmentRefVal;
- elementIndex = listVal.indexOf(element);
+ if (ref != null) {
+ try {
+ int elementIndex = -1;
+ if (ref.isMany()) {
+ final Object containmentRefVal = element.eContainer().eGet(ref);
+ if (containmentRefVal instanceof List<?>) {
+ @SuppressWarnings("unchecked")
+ final List<EObject> listVal = (List<EObject>)containmentRefVal;
+ elementIndex = listVal.indexOf(element);
+ }
}
+// EFactory.eAdd(origin, ref.getName(), newOne, elementIndex, true);
+// setXMIID(newOne, getXMIID(element));
+ cmd.append(PapyrusEFactory.getEAddCommand(origin, ref.getName(),newOne,elementIndex,true));
+ cmd.append(PapyrusMergeCommandProvider.INSTANCE.getSetXMIIDCommand(domain, newOne, getXMIID(element)));
+ } catch (final FactoryException e) {
+ EMFComparePlugin.log(e, true);
}
-
- // EFactory.eAdd(origin, ref.getName(), newOne, elementIndex, true);
-
-
- if(elementIndex != -1) {
- List<Object> newList = new ArrayList<Object>();
- Collections.copy(newList, ((List<Object>)origin.eGet(ref)));
- newList.add(elementIndex, newOne);
- //testable with the example ModelElementChangeLeftTargetExample1
- cmd.append(PapyrusMergeCommandProvider.INSTANCE.getSetCommand(domain, origin, ref, newList));
- } else {
- //testable with the example ModelElementChangeLeftTargetExample2
- cmd.append(PapyrusMergeCommandProvider.INSTANCE.getSetCommand(domain, origin, ref, newOne));
- }
- // setXMIID(newOne, getXMIID(element));
- cmd.append(PapyrusMergeCommandProvider.INSTANCE.getSetXMIIDCommand(domain, newOne, getXMIID(element)));
- // } catch (final FactoryException e) {
- // EMFComparePlugin.log(e, true);
- // }
- } else if(origin == null && getDiffModel().getRightRoots().size() > 0) {
- //TODO : no example for this case
- // getDiffModel().getRightRoots().get(0).eResource().getContents().add(newOne);
+ } else if (origin == null && getDiffModel().getRightRoots().size() > 0) {
+// getDiffModel().getRightRoots().get(0).eResource().getContents().add(newOne);
cmd.append(PapyrusMergeCommandProvider.INSTANCE.getAddToResourceCommand(domain, getDiffModel().getRightRoots().get(0).eResource(), newOne));
-
- } else if(origin != null) {
- //TODO : no example for this case
- // origin.eResource().getContents().add(newOne);
+ } else if (origin != null) {
+// origin.eResource().getContents().add(newOne);
cmd.append(PapyrusMergeCommandProvider.INSTANCE.getAddToResourceCommand(domain, origin.eResource(), newOne));
-
-
} else {
- // FIXME throw exception : couldn't merge this
+ throw new UnsupportedOperationException(NLS.bind("We can't merge this diff : {0}", theDiff));
}
// we should now have a look for RemovedReferencesLinks needing elements to apply
final Iterator<EObject> siblings = getDiffModel().eAllContents();
- while(siblings.hasNext()) {
+ while (siblings.hasNext()) {
final Object op = siblings.next();
- if(op instanceof ReferenceChangeLeftTarget) {
- //TODO
+ if (op instanceof ReferenceChangeLeftTarget) {
final ReferenceChangeLeftTarget link = (ReferenceChangeLeftTarget)op;
// now if I'm in the target References I should put my copy in the origin
- if(link.getRightTarget() != null && link.getRightTarget() == element) {
+ if (link.getRightTarget() != null && link.getRightTarget() == element) {
link.setLeftTarget(newOne);
}
- //TODO
- throw new UnsupportedOperationException();
- } else if(op instanceof ReferenceOrderChange) {
-
+ } else if (op instanceof ReferenceOrderChange) {
final ReferenceOrderChange link = (ReferenceOrderChange)op;
- if(link.getRightElement() == origin && link.getReference() == ref) {
+ if (link.getRightElement() == origin && link.getReference() == ref) {
final ListIterator<EObject> targetIterator = link.getRightTarget().listIterator();
boolean replaced = false;
- while(!replaced && targetIterator.hasNext()) {
+ while (!replaced && targetIterator.hasNext()) {
final EObject target = targetIterator.next();
- if(target.eIsProxy() && equalProxyURIs(((InternalEObject)target).eProxyURI(), EcoreUtil.getURI(element))) {
+ if (target.eIsProxy()
+ && equalProxyURIs(((InternalEObject)target).eProxyURI(),
+ EcoreUtil.getURI(element))) {
targetIterator.set(newOne);
replaced = true;
}
}
}
- throw new UnsupportedOperationException();
}
}
domain.getCommandStack().execute(cmd);
diff --git a/sandbox/UMLCompareMergerExample/org.eclipse.papyrus.uml.compare.merger/src/org/eclipse/papyrus/uml/merger/provider/CModelElementChangeRightTargetMerger.java b/sandbox/UMLCompareMergerExample/org.eclipse.papyrus.uml.compare.merger/src/org/eclipse/papyrus/uml/merger/provider/CModelElementChangeRightTargetMerger.java
index ae7220b0279..661f4d5fd7f 100644
--- a/sandbox/UMLCompareMergerExample/org.eclipse.papyrus.uml.compare.merger/src/org/eclipse/papyrus/uml/merger/provider/CModelElementChangeRightTargetMerger.java
+++ b/sandbox/UMLCompareMergerExample/org.eclipse.papyrus.uml.compare.merger/src/org/eclipse/papyrus/uml/merger/provider/CModelElementChangeRightTargetMerger.java
@@ -1,25 +1,116 @@
package org.eclipse.papyrus.uml.merger.provider;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+
+import org.eclipse.emf.common.command.Command;
+import org.eclipse.emf.common.command.CompoundCommand;
+import org.eclipse.emf.compare.EMFComparePlugin;
+import org.eclipse.emf.compare.FactoryException;
import org.eclipse.emf.compare.diff.internal.merge.impl.ModelElementChangeRightTargetMerger;
+import org.eclipse.emf.compare.diff.metamodel.DiffElement;
+import org.eclipse.emf.compare.diff.metamodel.ModelElementChangeRightTarget;
+import org.eclipse.emf.compare.diff.metamodel.ReferenceChangeRightTarget;
+import org.eclipse.emf.compare.diff.metamodel.ReferenceOrderChange;
+import org.eclipse.emf.compare.util.EFactory;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EReference;
+import org.eclipse.emf.ecore.EStructuralFeature;
+import org.eclipse.emf.ecore.InternalEObject;
+import org.eclipse.emf.ecore.util.EcoreUtil;
+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.utils.MergerUtils;
+import org.eclipse.papyrus.uml.compare.merger.utils.PapyrusEFactory;
public class CModelElementChangeRightTargetMerger extends ModelElementChangeRightTargetMerger {
+
@Override
- public void doUndoInTarget() {
+ public void doApplyInOrigin() {
if(MergerUtils.usePapyrusMerger()) {
- throw new UnsupportedOperationException();
+ TransactionalEditingDomain domain = MergerUtils.getEditingDomain();
+ CompoundCommand cmd = new CompoundCommand("ModelElementChangeRightTargetCommand#doApplyInOrigin");
+ final ModelElementChangeRightTarget theDiff = (ModelElementChangeRightTarget)this.diff;
+ final EObject origin = theDiff.getLeftParent();
+ final EObject element = theDiff.getRightElement();
+ final EObject newOne = copy(element);
+ final EReference ref = element.eContainmentFeature();
+ if(ref != null) {
+ try {
+ int expectedIndex = -1;
+ if(ref.isMany()) {
+ final Object containmentRefVal = element.eContainer().eGet(ref);
+ if(containmentRefVal instanceof List<?>) {
+ @SuppressWarnings("unchecked")
+ final List<EObject> listVal = (List<EObject>)containmentRefVal;
+ expectedIndex = listVal.indexOf(element);
+ }
+ }
+ // EFactory.eAdd(origin, ref.getName(), newOne, expectedIndex, true);
+ // setXMIID(newOne, getXMIID(element));
+ cmd.append(PapyrusEFactory.getEAddCommand(origin, ref.getName(), newOne, expectedIndex, true));
+ cmd.append(PapyrusMergeCommandProvider.INSTANCE.getSetXMIIDCommand(domain, newOne, getXMIID(element)));
+ } catch (final FactoryException e) {
+ EMFComparePlugin.log(e, true);
+ }
+ } else if(origin == null && getDiffModel().getLeftRoots().size() > 0) {
+ // getDiffModel().getLeftRoots().get(0).eResource().getContents().add(newOne);
+ cmd.append(PapyrusMergeCommandProvider.INSTANCE.getAddToResourceCommand(domain, getDiffModel().getLeftRoots().get(0).eResource(), newOne));
+ } else if(origin != null) {
+ // origin.eResource().getContents().add(newOne);
+ cmd.append(PapyrusMergeCommandProvider.INSTANCE.getAddToResourceCommand(domain, origin.eResource(), newOne));
+ } else {
+ throw new UnsupportedOperationException(NLS.bind("We can't merge this diff : {0}", theDiff));
+ }
+ // we should now have a look for AddReferencesLinks needing this object
+ final Iterator<EObject> siblings = getDiffModel().eAllContents();
+ while(siblings.hasNext()) {
+ final DiffElement op = (DiffElement)siblings.next();
+ if(op instanceof ReferenceChangeRightTarget) {
+ final ReferenceChangeRightTarget link = (ReferenceChangeRightTarget)op;
+ // now if I'm in the target References I should put my copy in the origin
+ if(link.getLeftTarget() != null && link.getLeftTarget() == element) {
+ link.setRightTarget(newOne);
+ }
+ } else if(op instanceof ReferenceOrderChange) {
+ final ReferenceOrderChange link = (ReferenceOrderChange)op;
+ if(link.getLeftElement() == origin && link.getReference() == ref) {
+ final ListIterator<EObject> targetIterator = link.getLeftTarget().listIterator();
+ boolean replaced = false;
+ while(!replaced && targetIterator.hasNext()) {
+ final EObject target = targetIterator.next();
+ if(target.eIsProxy() && equalProxyURIs(((InternalEObject)target).eProxyURI(), EcoreUtil.getURI(element))) {
+ targetIterator.set(newOne);
+ replaced = true;
+ }
+ }
+ }
+ }
+ }
+ domain.getCommandStack().execute(cmd);
} else {
- super.doUndoInTarget();
+ super.doApplyInOrigin();
}
+
}
+
@Override
- public void doApplyInOrigin() {
+ public void doUndoInTarget() {
if(MergerUtils.usePapyrusMerger()) {
- throw new UnsupportedOperationException();
+ final ModelElementChangeRightTarget theDiff = (ModelElementChangeRightTarget)this.diff;
+ final EObject element = theDiff.getRightElement();
+ // final EObject parent = theDiff.getRightElement().eContainer();
+ // EcoreUtil.remove(element);
+ final TransactionalEditingDomain domain = MergerUtils.getEditingDomain();
+ final Command cmd = PapyrusMergeCommandProvider.INSTANCE.getDestroyCommand(domain, element);
+ domain.getCommandStack().execute(cmd);
} else {
- super.doApplyInOrigin();
+ super.doUndoInTarget();
}
}
}

Back to the top