]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/SCLClipboard.java
Sync git svn branch with SVN repository r33319.
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / SCLClipboard.java
1 package org.simantics.modeling.ui;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collection;\r
5 import java.util.List;\r
6 \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
23 \r
24 public class SCLClipboard {\r
25         \r
26         public static void copyNode (Resource node) {\r
27                 \r
28                 ISelection selection = ISelectionUtils.createSelection(node);\r
29                 Resource[] rs = new Resource[] {node};\r
30                 StandardCopyHandler.copyResourcesToClipboard(rs, selection);\r
31         }\r
32         \r
33         public static void pasteNode (Resource target) {\r
34                 \r
35                 PasteHandler handler = StandardPasteHandler.getPasteHandlerFromResource(target, PasteHandler.class);\r
36                 StandardPasteHandler.pasteResourceFromClipboardWithoutMonitor(handler);\r
37         }\r
38         \r
39         public static void copyPasteNode(Resource node, Resource target) {\r
40                 \r
41                 copyNode(node);\r
42                 pasteNode(target);\r
43         }\r
44         \r
45     public static boolean copyPasteDiagramContents (final ICanvasContext source_ctx, final ICanvasContext target_ctx, List<Resource> modules) throws DatabaseException {\r
46         \r
47         IDiagram idiagram = source_ctx.getDefaultHintContext().getHint(DiagramHints.KEY_DIAGRAM);\r
48 \r
49                 DataElementMap dem = idiagram.getDiagramClass().getAtMostOneItemOfClass(DataElementMap.class);\r
50                 if (dem != null) {\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
56                                 } else {\r
57                                         throw new DatabaseException("Could not find IElement for " + element);\r
58                                 }\r
59                         }\r
60                         \r
61                         ThreadUtils.syncExec(source_ctx.getThreadAccess(), new Runnable() {\r
62                     @Override\r
63                     public void run() {\r
64                         if (source_ctx.isDisposed())\r
65                             return;\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
71                         }\r
72                                 CanvasUtils.sendCommand(source_ctx, Commands.COPY);\r
73                                 CanvasUtils.sendCommand(target_ctx, Commands.PASTE);\r
74                     }\r
75                 });\r
76                         \r
77                 //}\r
78                 \r
79                 while(source_ctx.getEventQueue().size() > 0) {\r
80                         try {\r
81                                 Thread.sleep(10);\r
82                         } catch (InterruptedException e) {\r
83                                 throw new DatabaseException(e);\r
84                         }\r
85                 }\r
86 \r
87                 ThreadUtils.syncExec(source_ctx.getThreadAccess(), new Runnable() {\r
88             @Override\r
89             public void run() {\r
90             }\r
91         });             \r
92                                 \r
93                 }\r
94                 return true;\r
95     }\r
96     \r
97     public static boolean delete (final ICanvasContext source_ctx, List<Resource> modules) throws DatabaseException {\r
98         \r
99         IDiagram idiagram = source_ctx.getDefaultHintContext().getHint(DiagramHints.KEY_DIAGRAM);\r
100 \r
101                 DataElementMap dem = idiagram.getDiagramClass().getAtMostOneItemOfClass(DataElementMap.class);\r
102                 if (dem != null) {\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
108                                 } else {\r
109                                         throw new DatabaseException("Could not find IElement for " + element);\r
110                                 }\r
111                         }\r
112                         \r
113                         ThreadUtils.syncExec(source_ctx.getThreadAccess(), new Runnable() {\r
114                     @Override\r
115                     public void run() {\r
116                         if (source_ctx.isDisposed())\r
117                             return;\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
123                         }\r
124                                 CanvasUtils.sendCommand(source_ctx, Commands.DELETE);\r
125                     }\r
126                 });\r
127                         \r
128                 //}\r
129                 \r
130                 while(source_ctx.getEventQueue().size() > 0) {\r
131                         try {\r
132                                 Thread.sleep(10);\r
133                         } catch (InterruptedException e) {\r
134                                 throw new DatabaseException(e);\r
135                         }\r
136                 }\r
137 \r
138                 ThreadUtils.syncExec(source_ctx.getThreadAccess(), new Runnable() {\r
139             @Override\r
140             public void run() {\r
141             }\r
142         });             \r
143                                 \r
144                 }\r
145                 return true;\r
146     }\r
147         \r
148 }