From c4f501d1acca7d981320852022b5f52395ceac44 Mon Sep 17 00:00:00 2001
From: Simon Barner <barner@fortiss.org>
Date: Wed, 6 Jun 2018 12:13:25 +0200
Subject: [PATCH] Avoid broken model resources during cross-project
 copy-and-paste

- Use original (non-copied) references only when copying within
  the same model resource.
- Otherwise, the copy in the destination resource will have
  a reference to the original resource.
- NOTE
  - The resulting copy can have null-references which might
    not be handled correctly.
  - Hence, this is only an initial workaround, and a proper
    handling of copying references is needed.

Issue-Ref: 2845

Signed-off-by: Simon Barner <barner@fortiss.org>
---
 .../src/org/fortiss/tooling/kernel/ui/util/.ratings       | 2 +-
 .../fortiss/tooling/kernel/ui/util/CopyPasteUtils.java    | 8 +++++++-
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/util/.ratings b/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/util/.ratings
index bf1fb819d..9636bf706 100644
--- a/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/util/.ratings
+++ b/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/util/.ratings
@@ -1,6 +1,6 @@
 ActionUtils.java 4553e487264e3d1f86f4767da4a7400cce4b9a5d GREEN
 ConstraintsUIUtils.java 69d5e08bbf768baf2790380e36f1020ef826a33e GREEN
-CopyPasteUtils.java 7da2d871bebecc7010e699b4625e1614bf2392ab GREEN
+CopyPasteUtils.java 445f585fe70e79f63c47f278d27cac48253571a8 YELLOW
 DataBindingUtils.java 0818014c193a22d5968435e78590368b95138d5e GREEN
 DragAndDropUtils.java 7aab91518aa12d76533a345bf6ed0be9ac7ff0e5 GREEN
 EObjectSelectionUtils.java 928280b5dd3bb634debc1ac3be9f15b4e45f2683 GREEN
diff --git a/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/util/CopyPasteUtils.java b/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/util/CopyPasteUtils.java
index 7da2d871b..445f585fe 100644
--- a/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/util/CopyPasteUtils.java
+++ b/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/util/CopyPasteUtils.java
@@ -121,7 +121,13 @@ public class CopyPasteUtils {
 			return;
 		}
 
-		EcoreUtil.Copier copier = new EcoreUtil.Copier();
+		// canPasteInto() ensures that clip-board is non-empty
+		boolean sameResource = getClipBoardContent()[0].eResource() == target.eResource();
+
+		// Use original (non-copied) references only when copying within the same model resource.
+		// Otherwise, the copy in the destination resource will have a reference to the original
+		// resource.
+		EcoreUtil.Copier copier = new EcoreUtil.Copier(true, sameResource);
 		BasicEList<EObject> copiedObjects = new BasicEList<EObject>();
 		for(EObject obj : getClipBoardContent()) {
 			EObject copy = copier.copy(obj);
-- 
GitLab