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