1 package org.simantics.modeling.ui;
3 import java.util.ArrayList;
4 import java.util.Collection;
7 import org.eclipse.jface.viewers.ISelection;
8 import org.simantics.db.Resource;
9 import org.simantics.modeling.ui.modelBrowser.handlers.StandardCopyHandler;
10 import org.simantics.modeling.ui.modelBrowser.handlers.StandardPasteHandler;
11 import org.simantics.scenegraph.g2d.events.command.Commands;
12 import org.simantics.utils.threads.ThreadUtils;
13 import org.simantics.utils.ui.ISelectionUtils;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.layer0.adapter.PasteHandler;
16 import org.simantics.g2d.canvas.ICanvasContext;
17 import org.simantics.g2d.diagram.DiagramHints;
18 import org.simantics.g2d.diagram.IDiagram;
19 import org.simantics.g2d.diagram.handler.DataElementMap;
20 import org.simantics.g2d.diagram.participant.Selection;
21 import org.simantics.g2d.element.IElement;
22 import org.simantics.g2d.utils.CanvasUtils;
24 public class SCLClipboard {
26 public static void copyNode (Resource node) {
28 ISelection selection = ISelectionUtils.createSelection(node);
29 Resource[] rs = new Resource[] {node};
30 StandardCopyHandler.copyResourcesToClipboard(rs, selection);
33 public static void pasteNode (Resource target) {
35 PasteHandler handler = StandardPasteHandler.getPasteHandlerFromResource(target, PasteHandler.class);
36 StandardPasteHandler.pasteResourceFromClipboardWithoutMonitor(handler);
39 public static void copyPasteNode(Resource node, Resource target) {
45 public static boolean copyPasteDiagramContents (final ICanvasContext source_ctx, final ICanvasContext target_ctx, List<Resource> modules) throws DatabaseException {
47 IDiagram idiagram = source_ctx.getDefaultHintContext().getHint(DiagramHints.KEY_DIAGRAM);
49 DataElementMap dem = idiagram.getDiagramClass().getAtMostOneItemOfClass(DataElementMap.class);
51 final Collection<IElement> newSelection = new ArrayList<IElement>();
52 for (Resource module : modules) {
53 IElement element = dem.getElement(idiagram, module);
54 if (element != null) {
55 newSelection.add(element);
57 throw new DatabaseException("Could not find IElement for " + element);
61 ThreadUtils.syncExec(source_ctx.getThreadAccess(), new Runnable() {
64 if (source_ctx.isDisposed())
66 Selection selection = source_ctx.getAtMostOneItemOfClass(Selection.class);
67 if (selection != null) {
68 // This prevents workbench selection from being left over.
69 // Also prevents scene graph crap from being left on the screen.
70 selection.setSelection(0, newSelection);
72 CanvasUtils.sendCommand(source_ctx, Commands.COPY);
73 CanvasUtils.sendCommand(target_ctx, Commands.PASTE);
79 while(source_ctx.getEventQueue().size() > 0) {
82 } catch (InterruptedException e) {
83 throw new DatabaseException(e);
87 ThreadUtils.syncExec(source_ctx.getThreadAccess(), new Runnable() {
97 public static boolean delete (final ICanvasContext source_ctx, List<Resource> modules) throws DatabaseException {
99 IDiagram idiagram = source_ctx.getDefaultHintContext().getHint(DiagramHints.KEY_DIAGRAM);
101 DataElementMap dem = idiagram.getDiagramClass().getAtMostOneItemOfClass(DataElementMap.class);
103 final Collection<IElement> newSelection = new ArrayList<IElement>();
104 for (Resource module : modules) {
105 IElement element = dem.getElement(idiagram, module);
106 if (element != null) {
107 newSelection.add(element);
109 throw new DatabaseException("Could not find IElement for " + element);
113 ThreadUtils.syncExec(source_ctx.getThreadAccess(), new Runnable() {
116 if (source_ctx.isDisposed())
118 Selection selection = source_ctx.getAtMostOneItemOfClass(Selection.class);
119 if (selection != null) {
120 // This prevents workbench selection from being left over.
121 // Also prevents scene graph crap from being left on the screen.
122 selection.setSelection(0, newSelection);
124 CanvasUtils.sendCommand(source_ctx, Commands.DELETE);
130 while(source_ctx.getEventQueue().size() > 0) {
133 } catch (InterruptedException e) {
134 throw new DatabaseException(e);
138 ThreadUtils.syncExec(source_ctx.getThreadAccess(), new Runnable() {