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