]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/SCLClipboard.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / SCLClipboard.java
diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/SCLClipboard.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/SCLClipboard.java
new file mode 100644 (file)
index 0000000..572df2f
--- /dev/null
@@ -0,0 +1,148 @@
+package org.simantics.modeling.ui;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.List;\r
+\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.simantics.db.Resource;\r
+import org.simantics.modeling.ui.modelBrowser.handlers.StandardCopyHandler;\r
+import org.simantics.modeling.ui.modelBrowser.handlers.StandardPasteHandler;\r
+import org.simantics.scenegraph.g2d.events.command.Commands;\r
+import org.simantics.utils.threads.ThreadUtils;\r
+import org.simantics.utils.ui.ISelectionUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.PasteHandler;\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.diagram.DiagramHints;\r
+import org.simantics.g2d.diagram.IDiagram;\r
+import org.simantics.g2d.diagram.handler.DataElementMap;\r
+import org.simantics.g2d.diagram.participant.Selection;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.utils.CanvasUtils;\r
+\r
+public class SCLClipboard {\r
+       \r
+       public static void copyNode (Resource node) {\r
+               \r
+               ISelection selection = ISelectionUtils.createSelection(node);\r
+               Resource[] rs = new Resource[] {node};\r
+               StandardCopyHandler.copyResourcesToClipboard(rs, selection);\r
+       }\r
+       \r
+       public static void pasteNode (Resource target) {\r
+               \r
+               PasteHandler handler = StandardPasteHandler.getPasteHandlerFromResource(target, PasteHandler.class);\r
+               StandardPasteHandler.pasteResourceFromClipboardWithoutMonitor(handler);\r
+       }\r
+       \r
+       public static void copyPasteNode(Resource node, Resource target) {\r
+               \r
+               copyNode(node);\r
+               pasteNode(target);\r
+       }\r
+       \r
+    public static boolean copyPasteDiagramContents (final ICanvasContext source_ctx, final ICanvasContext target_ctx, List<Resource> modules) throws DatabaseException {\r
+       \r
+       IDiagram idiagram = source_ctx.getDefaultHintContext().getHint(DiagramHints.KEY_DIAGRAM);\r
+\r
+               DataElementMap dem = idiagram.getDiagramClass().getAtMostOneItemOfClass(DataElementMap.class);\r
+               if (dem != null) {\r
+                       final Collection<IElement> newSelection = new ArrayList<IElement>();\r
+                       for (Resource module : modules) {\r
+                               IElement element = dem.getElement(idiagram, module);\r
+                               if (element != null) {\r
+                                       newSelection.add(element);\r
+                               } else {\r
+                                       throw new DatabaseException("Could not find IElement for " + element);\r
+                               }\r
+                       }\r
+                       \r
+                       ThreadUtils.syncExec(source_ctx.getThreadAccess(), new Runnable() {\r
+                   @Override\r
+                   public void run() {\r
+                       if (source_ctx.isDisposed())\r
+                           return;\r
+                       Selection selection = source_ctx.getAtMostOneItemOfClass(Selection.class);\r
+                       if (selection != null) {\r
+                           // This prevents workbench selection from being left over.\r
+                           // Also prevents scene graph crap from being left on the screen.\r
+                           selection.setSelection(0, newSelection);\r
+                       }\r
+                               CanvasUtils.sendCommand(source_ctx, Commands.COPY);\r
+                               CanvasUtils.sendCommand(target_ctx, Commands.PASTE);\r
+                   }\r
+               });\r
+                       \r
+               //}\r
+               \r
+               while(source_ctx.getEventQueue().size() > 0) {\r
+                       try {\r
+                               Thread.sleep(10);\r
+                       } catch (InterruptedException e) {\r
+                               throw new DatabaseException(e);\r
+                       }\r
+               }\r
+\r
+               ThreadUtils.syncExec(source_ctx.getThreadAccess(), new Runnable() {\r
+            @Override\r
+            public void run() {\r
+            }\r
+        });            \r
+                               \r
+               }\r
+               return true;\r
+    }\r
+    \r
+    public static boolean delete (final ICanvasContext source_ctx, List<Resource> modules) throws DatabaseException {\r
+       \r
+       IDiagram idiagram = source_ctx.getDefaultHintContext().getHint(DiagramHints.KEY_DIAGRAM);\r
+\r
+               DataElementMap dem = idiagram.getDiagramClass().getAtMostOneItemOfClass(DataElementMap.class);\r
+               if (dem != null) {\r
+                       final Collection<IElement> newSelection = new ArrayList<IElement>();\r
+                       for (Resource module : modules) {\r
+                               IElement element = dem.getElement(idiagram, module);\r
+                               if (element != null) {\r
+                                       newSelection.add(element);\r
+                               } else {\r
+                                       throw new DatabaseException("Could not find IElement for " + element);\r
+                               }\r
+                       }\r
+                       \r
+                       ThreadUtils.syncExec(source_ctx.getThreadAccess(), new Runnable() {\r
+                   @Override\r
+                   public void run() {\r
+                       if (source_ctx.isDisposed())\r
+                           return;\r
+                       Selection selection = source_ctx.getAtMostOneItemOfClass(Selection.class);\r
+                       if (selection != null) {\r
+                           // This prevents workbench selection from being left over.\r
+                           // Also prevents scene graph crap from being left on the screen.\r
+                           selection.setSelection(0, newSelection);\r
+                       }\r
+                               CanvasUtils.sendCommand(source_ctx, Commands.DELETE);\r
+                   }\r
+               });\r
+                       \r
+               //}\r
+               \r
+               while(source_ctx.getEventQueue().size() > 0) {\r
+                       try {\r
+                               Thread.sleep(10);\r
+                       } catch (InterruptedException e) {\r
+                               throw new DatabaseException(e);\r
+                       }\r
+               }\r
+\r
+               ThreadUtils.syncExec(source_ctx.getThreadAccess(), new Runnable() {\r
+            @Override\r
+            public void run() {\r
+            }\r
+        });            \r
+                               \r
+               }\r
+               return true;\r
+    }\r
+       \r
+}
\ No newline at end of file