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