diff options
author | lgoubet | 2011-08-02 12:54:41 +0000 |
---|---|---|
committer | lgoubet | 2011-08-04 11:09:03 +0000 |
commit | 429c4b695933c1df96b06f1b4c849ed0d636dbf4 (patch) | |
tree | ff798bb82135b093450f3e4eb3df58643f8ead64 | |
parent | edc52a3d2c11a4d5bd3abd037e13f8c9bea0fa33 (diff) | |
download | org.eclipse.emf.compare-429c4b695933c1df96b06f1b4c849ed0d636dbf4.tar.gz org.eclipse.emf.compare-429c4b695933c1df96b06f1b4c849ed0d636dbf4.tar.xz org.eclipse.emf.compare-429c4b695933c1df96b06f1b4c849ed0d636dbf4.zip |
Allow for the use of the EMFCompareEObjectCopier for single valued
references, and use it from the UpdateReferenceMerger. (Basically, "make
the merging of UpdateReferences work".)
3 files changed, 74 insertions, 45 deletions
diff --git a/plugins/org.eclipse.emf.compare.diff/src/org/eclipse/emf/compare/diff/engine/check/ReferencesCheck.java b/plugins/org.eclipse.emf.compare.diff/src/org/eclipse/emf/compare/diff/engine/check/ReferencesCheck.java index c1d458838..c499d0559 100644 --- a/plugins/org.eclipse.emf.compare.diff/src/org/eclipse/emf/compare/diff/engine/check/ReferencesCheck.java +++ b/plugins/org.eclipse.emf.compare.diff/src/org/eclipse/emf/compare/diff/engine/check/ReferencesCheck.java @@ -141,11 +141,14 @@ public class ReferencesCheck extends AbstractCheck { final List<Integer> removedIndices = new ArrayList<Integer>();
// Purge "left" list of all reference values that have been added to it
for (EObject leftValue : new ArrayList<EObject>(leftElementReferences)) {
- if (isUnmatched(leftValue) || leftValue.eContainer() != getMatchedEObject(leftValue).eContainer())
+ if (isUnmatched(leftValue)
+ || getMatchedEObject(leftValue.eContainer()) != getMatchedEObject(leftValue).eContainer())
leftElementReferences.remove(leftValue);
}
for (EObject rightValue : new ArrayList<EObject>(rightElementReferences)) {
- if (isUnmatched(rightValue)) {
+ if (isUnmatched(rightValue)
+ || getMatchedEObject(rightValue.eContainer()) != getMatchedEObject(rightValue)
+ .eContainer()) {
removedIndices.add(Integer.valueOf(rightElementReferences.indexOf(rightValue)));
}
}
@@ -688,8 +691,8 @@ public class ReferencesCheck extends AbstractCheck { operation.setRightElement(mapping.getRightElement());
operation.setReference(reference);
- EObject leftTarget = getMatchedEObject(remotelyAdded.get(0));
- EObject rightTarget = getMatchedEObject(remotelyDeleted.get(0));
+ EObject leftTarget = getMatchedEObject(remotelyDeleted.get(0));
+ EObject rightTarget = getMatchedEObject(remotelyAdded.get(0));
// checks if target are defined remotely
if (leftTarget == null) {
leftTarget = remotelyDeleted.get(0);
@@ -798,8 +801,8 @@ public class ReferencesCheck extends AbstractCheck { operation.setRightElement(right);
operation.setReference(reference);
- EObject leftTarget = getMatchedEObject(addedValue);
- EObject rightTarget = getMatchedEObject(deletedValue);
+ EObject leftTarget = getMatchedEObject(deletedValue);
+ EObject rightTarget = getMatchedEObject(addedValue);
// checks if target are defined remotely
if (leftTarget == null && addedValue != null) {
leftTarget = deletedValue;
diff --git a/plugins/org.eclipse.emf.compare.diff/src/org/eclipse/emf/compare/diff/internal/merge/impl/UpdateReferenceMerger.java b/plugins/org.eclipse.emf.compare.diff/src/org/eclipse/emf/compare/diff/internal/merge/impl/UpdateReferenceMerger.java index 2f582813f..d7cf739e9 100644 --- a/plugins/org.eclipse.emf.compare.diff/src/org/eclipse/emf/compare/diff/internal/merge/impl/UpdateReferenceMerger.java +++ b/plugins/org.eclipse.emf.compare.diff/src/org/eclipse/emf/compare/diff/internal/merge/impl/UpdateReferenceMerger.java @@ -10,12 +10,11 @@ *******************************************************************************/ package org.eclipse.emf.compare.diff.internal.merge.impl; -import org.eclipse.emf.compare.EMFComparePlugin; -import org.eclipse.emf.compare.FactoryException; import org.eclipse.emf.compare.diff.merge.DefaultMerger; +import org.eclipse.emf.compare.diff.merge.service.MergeService; import org.eclipse.emf.compare.diff.metamodel.UpdateReference; -import org.eclipse.emf.compare.util.EFactory; import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EReference; /** * Merger for an {@link UpdateUniqueReferenceValue} operation. @@ -31,13 +30,14 @@ public class UpdateReferenceMerger extends DefaultMerger { @Override public void applyInOrigin() { final UpdateReference theDiff = (UpdateReference)this.diff; + final EReference reference = theDiff.getReference(); final EObject element = theDiff.getLeftElement(); - final EObject leftTarget = theDiff.getLeftTarget(); - try { - EFactory.eSet(element, theDiff.getReference().getName(), leftTarget); - } catch (final FactoryException e) { - EMFComparePlugin.log(e, true); - } + final EObject leftTarget = (EObject)theDiff.getRightElement().eGet(reference); + final EObject matchedLeftTarget = theDiff.getLeftTarget(); + + MergeService.getCopier(diff) + .copyReferenceValue(reference, element, leftTarget, matchedLeftTarget, -1); + super.applyInOrigin(); } @@ -49,13 +49,14 @@ public class UpdateReferenceMerger extends DefaultMerger { @Override public void undoInTarget() { final UpdateReference theDiff = (UpdateReference)this.diff; + final EReference reference = theDiff.getReference(); final EObject element = theDiff.getRightElement(); - final EObject rightTarget = theDiff.getRightTarget(); - try { - EFactory.eSet(element, theDiff.getReference().getName(), rightTarget); - } catch (final FactoryException e) { - EMFComparePlugin.log(e, true); - } + final EObject rightTarget = (EObject)theDiff.getLeftElement().eGet(reference); + final EObject matchedRightTarget = theDiff.getRightTarget(); + + MergeService.getCopier(diff).copyReferenceValue(reference, element, rightTarget, matchedRightTarget, + -1); + super.undoInTarget(); } } diff --git a/plugins/org.eclipse.emf.compare.diff/src/org/eclipse/emf/compare/diff/merge/EMFCompareEObjectCopier.java b/plugins/org.eclipse.emf.compare.diff/src/org/eclipse/emf/compare/diff/merge/EMFCompareEObjectCopier.java index 0f80c8e8f..70276deae 100644 --- a/plugins/org.eclipse.emf.compare.diff/src/org/eclipse/emf/compare/diff/merge/EMFCompareEObjectCopier.java +++ b/plugins/org.eclipse.emf.compare.diff/src/org/eclipse/emf/compare/diff/merge/EMFCompareEObjectCopier.java @@ -164,8 +164,9 @@ public class EMFCompareEObjectCopier extends org.eclipse.emf.ecore.util.EcoreUti * @param value * The value that is to be copied. * @param index - * an optional index in case the target is a List (-1 is a good default, the value will be - * appended to the list) + * An optional index in case the target is a List. -1 can be used to either append to the end + * of the list, or copy the value of a single-valued reference ( + * <code>targetReference.isMany() == false</code>). * @return The copied value. * @since 1.3 */ @@ -186,32 +187,48 @@ public class EMFCompareEObjectCopier extends org.eclipse.emf.ecore.util.EcoreUti copy = copy(value); } } - if (copy.eIsProxy() && copy instanceof InternalEObject) { - // only add if the element is not already there. - final URI proxURI = ((InternalEObject)copy).eProxyURI(); - boolean found = false; - final Iterator<Object> it = ((List<Object>)target.eGet(targetReference)).iterator(); - while (!found && it.hasNext()) { - final Object obj = it.next(); - if (obj instanceof InternalEObject) { - found = proxURI.equals(((InternalEObject)obj).eProxyURI()); + + final Object referenceValue = target.eGet(targetReference); + if (referenceValue instanceof List && targetReference.isMany()) { + if (copy.eIsProxy() && copy instanceof InternalEObject) { + // only add if the element is not already there. + final URI proxURI = ((InternalEObject)copy).eProxyURI(); + boolean found = false; + final Iterator<Object> it = ((List<Object>)referenceValue).iterator(); + while (!found && it.hasNext()) { + final Object obj = it.next(); + if (obj instanceof InternalEObject) { + found = proxURI.equals(((InternalEObject)obj).eProxyURI()); + } } - } - if (!found) { - final List<Object> targetList = (List<Object>)target.eGet(targetReference); + if (!found) { + final List<Object> targetList = (List<Object>)referenceValue; + if (index > -1 && index < targetList.size()) { + targetList.add(index, copy); + } else { + targetList.add(copy); + } + } + + } else { + final List<Object> targetList = (List<Object>)referenceValue; if (index > -1 && index < targetList.size()) { targetList.add(index, copy); } else { targetList.add(copy); } } - } else { - final List<Object> targetList = (List<Object>)target.eGet(targetReference); - if (index > -1 && index < targetList.size()) { - targetList.add(index, copy); + if (copy.eIsProxy() && copy instanceof InternalEObject) { + // only change value if the URI changes + final URI proxURI = ((InternalEObject)copy).eProxyURI(); + if (referenceValue instanceof InternalEObject) { + if (!proxURI.equals(((InternalEObject)referenceValue).eProxyURI())) { + target.eSet(targetReference, copy); + } + } } else { - targetList.add(copy); + target.eSet(targetReference, copy); } } return copy; @@ -244,12 +261,18 @@ public class EMFCompareEObjectCopier extends org.eclipse.emf.ecore.util.EcoreUti } if (matchedValue != null) { put(actualValue, matchedValue); - final List<Object> targetList = (List<Object>)target.eGet(targetReference); - final int targetListSize = targetList.size(); - if (index > -1 && index < targetListSize) { - targetList.add(index, matchedValue); + + final Object referenceValue = target.eGet(targetReference); + if (referenceValue instanceof List) { + final List<Object> targetList = (List<Object>)referenceValue; + final int targetListSize = targetList.size(); + if (index > -1 && index < targetListSize) { + targetList.add(index, matchedValue); + } else { + targetList.add(matchedValue); + } } else { - targetList.add(matchedValue); + target.eSet(targetReference, matchedValue); } return matchedValue; } @@ -269,7 +292,9 @@ public class EMFCompareEObjectCopier extends org.eclipse.emf.ecore.util.EcoreUti * Matched value of <tt>value</tt> if it is known. Will behave like * {@link #copyReferenceValue(EReference, EObject, EObject)} if <code>null</code>. * @return The copied value. + * @deprecated use {@link #copyReferenceValue(EReference, EObject, EObject, int)} instead */ + @Deprecated public EObject copyReferenceValue(EReference targetReference, EObject target, EObject value, EObject matchedValue) { return copyReferenceValue(targetReference, target, value, matchedValue, -1); |