Skip to content
Snippets Groups Projects
Commit c0ad607e authored by Simon Barner's avatar Simon Barner
Browse files

Merge branch '4198' into 'master'

Fix incomplete copy and paste function of components: Changed clipboard content to store also resource

Closes af3#4198

See merge request !168
parents 5b95ca95 69cf9a9c
No related branches found
No related tags found
1 merge request!168Fix incomplete copy and paste function of components: Changed clipboard content to store also resource
ActionUtils.java 4553e487264e3d1f86f4767da4a7400cce4b9a5d GREEN
CopyPasteUtils.java 62ac1094c1d56c83251b01ce7b52c2217625a79c GREEN
CopyPasteUtils.java c60f168d973a0167c0577e7c10ba00629ab2b236 GREEN
DataBindingUtils.java 631c47881caa13fc567679a7e4416eb777af0713 GREEN
DragAndDropUtils.java 7aab91518aa12d76533a345bf6ed0be9ac7ff0e5 GREEN
EObjectSelectionUtils.java 128cf8f96c6b9478171dff3deda662d5934f5f44 GREEN
......
......@@ -15,15 +15,21 @@
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.kernel.ui.util;
import static java.lang.String.join;
import static org.conqat.ide.commons.ui.dialog.MessageUtils.showWarning;
import static org.eclipse.jface.dialogs.MessageDialog.openError;
import static org.fortiss.tooling.kernel.ui.util.SelectionUtils.setSelection;
import static org.fortiss.tooling.kernel.utils.EcoreUtils.pickInstanceOf;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.DND;
......@@ -32,6 +38,7 @@ import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.fortiss.tooling.kernel.clipboard.ClipboardObjectWithResource;
import org.fortiss.tooling.kernel.extension.data.IElementCompositionContext;
import org.fortiss.tooling.kernel.model.INamedElement;
import org.fortiss.tooling.kernel.model.ISpeciallyCopyiable;
......@@ -58,11 +65,14 @@ public class CopyPasteUtils {
*/
public static void copyToClipboard(Collection<EObject> sel) {
try {
EObject[] content = new EObject[sel.size()];
ClipboardObjectWithResource[] content = new ClipboardObjectWithResource[sel.size()];
int i = 0;
EcoreUtil.Copier copier = new EcoreUtil.Copier(true, true);
for(EObject obj : sel) {
content[i] = copier.copy(obj);
EObject copiedElement = copier.copy(obj);
ClipboardObjectWithResource copyForClipboard =
new ClipboardObjectWithResource(copiedElement, obj.eResource());
content[i] = copyForClipboard;
i++;
}
copier.copyReferences();
......@@ -84,16 +94,42 @@ public class CopyPasteUtils {
}
}
/** Returns elements read from clipboard. */
private static EObject[] getClipBoardContent() {
/**
* Returns elements read from clipboard as container elements
* {@link ClipboardObjectWithResource}.
*/
private static ClipboardObjectWithResource[] getClipBoardContent() {
Clipboard clipboard = new Clipboard(Display.getDefault());
EObject[] content = (EObject[])clipboard
ClipboardObjectWithResource[] content = (ClipboardObjectWithResource[])clipboard
.getContents(CompositionServiceLocalCopyPasteTransfer.getInstance());
clipboard.dispose();
return content;
}
/** Returns elements read from clipboard as single {@link EObject}s. */
private static EObject[] getClipBoardContentElements() {
ClipboardObjectWithResource[] rawContent = getClipBoardContent();
if(rawContent != null) {
EObject[] elementContent =
Arrays.stream(rawContent).map(obj -> obj.getElement()).toArray(EObject[]::new);
return elementContent;
}
return null;
}
/** Returns the stored original {@link Resource} of clipboard's elements. */
private static Resource getResourceOfClipBoardContent() {
ClipboardObjectWithResource[] rawContent = getClipBoardContent();
if(rawContent != null && rawContent.length > 0) {
// [0]: rawContent has at least one element
return rawContent[0].getResource();
}
return null;
}
/**
* Attempts to paste the contents of the clipboard into the given target
* object. The given element composition context is forwarded to the element
......@@ -110,14 +146,29 @@ public class CopyPasteUtils {
}
// canPasteInto() ensures that clip-board is non-empty
boolean sameResource = getClipBoardContent()[0].eResource() == target.eResource();
boolean sameResource = getResourceOfClipBoardContent() == target.eResource();
if(!sameResource) {
List<String> textLines = new ArrayList<String>();
textLines.add("You are pasting content into a new resource/project.");
textLines.add(
"If you have external references (e.g. references to complex data types, functions or allocations) in this copied content, please know the following:\n");
textLines.add(
"It is currently not possible to automatically take all referenced elements by the copied content to the new resource and update the outdated reference links.");
textLines.add(
"Therefore, it is possible that your copied content is now missing some references.\n");
textLines.add(
"If your content has such external references, you need to manually add them (ports might still have the correct name of the data type but the saved link to the definition does not exist in the background anymore).");
showWarning("Possible missing references", join("\n", textLines));
// TODO #4208: This warning is too generic and not user-friendly. The copy&paste
// functionality should automatically handle these external references, too.
}
// 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()) {
for(EObject obj : getClipBoardContentElements()) {
EObject copy = copier.copy(obj);
adaptCopyNames(copy, target);
IElementCompositorService.getInstance().compose(target, copy,
......@@ -126,7 +177,7 @@ public class CopyPasteUtils {
}
copier.copyReferences();
for(EObject obj : getClipBoardContent()) {
for(EObject obj : getClipBoardContentElements()) {
if(obj instanceof ISpeciallyCopyiable) {
((ISpeciallyCopyiable)obj).specialCopyHook(copier);
}
......@@ -151,7 +202,7 @@ public class CopyPasteUtils {
*/
public static boolean canPasteInto(EObject target, IElementCompositionContext context) {
EObject[] contents = getClipBoardContent();
EObject[] contents = getClipBoardContentElements();
if(contents == null) {
return false;
}
......
......@@ -15,6 +15,7 @@ Require-Bundle: org.fortiss.tooling.common;bundle-version="2.20.0";visibility:=r
Bundle-RequiredExecutionEnvironment: JavaSE-11
Bundle-ActivationPolicy: lazy
Export-Package: org.fortiss.tooling.kernel;uses:="org.eclipse.core.runtime,org.osgi.framework",
org.fortiss.tooling.kernel.clipboard,
org.fortiss.tooling.kernel.constraint,
org.fortiss.tooling.kernel.extension,
org.fortiss.tooling.kernel.extension.base,
......
ClipboardObjectWithResource.java e01926b68e75c1c0e0f17efaa8913fa9b578efa8 GREEN
/*-------------------------------------------------------------------------+
| Copyright 2022 fortiss GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.kernel.clipboard;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
/**
* The {@link ClipboardObjectWithResource} is a container that stores an {@link EObject} together
* with its (original) resource. This is for example needed to not lose the reference to this
* resource when moving the {@link EObject} via the clipboard.
*
* @author bergemann
*/
public class ClipboardObjectWithResource {
/** The element as main artifact of this container. */
private EObject element;
/** The original resource of the element. */
private Resource resourceOfCurrentElement;
/**
* Constructor (The contained artifacts can also be changed later via getter/setter).
*
* @param element
* The element that should be stored as main artifact inside this container object
* @param originalResource
* The resource of the element that should is stored in the container object
*/
public ClipboardObjectWithResource(EObject element, Resource originalResource) {
setElement(element);
setResource(originalResource);
}
/** Returns the (main) element of this container. */
public EObject getElement() {
return element;
}
/** Sets the (main) element of this container. */
public void setElement(EObject element) {
this.element = element;
}
/** Returns the resource of the currently stored element of this container. */
public Resource getResource() {
return resourceOfCurrentElement;
}
/** Sets the resource of the currently stored element of this container. */
public void setResource(Resource resourceOfElement) {
this.resourceOfCurrentElement = resourceOfElement;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment