]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/SCLClipboard.java
Configurable connection crossing styles
[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.core.runtime.NullProgressMonitor;
8 import org.eclipse.jface.viewers.ISelection;
9 import org.simantics.db.Resource;
10 import org.simantics.modeling.ui.modelBrowser.handlers.StandardCopyHandler;
11 import org.simantics.modeling.ui.modelBrowser.handlers.StandardPasteHandler;
12 import org.simantics.scenegraph.g2d.events.command.Commands;
13 import org.simantics.utils.threads.ThreadUtils;
14 import org.simantics.utils.ui.ISelectionUtils;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.layer0.adapter.PasteHandler;
17 import org.simantics.g2d.canvas.ICanvasContext;
18 import org.simantics.g2d.diagram.DiagramHints;
19 import org.simantics.g2d.diagram.IDiagram;
20 import org.simantics.g2d.diagram.handler.DataElementMap;
21 import org.simantics.g2d.diagram.participant.Selection;
22 import org.simantics.g2d.element.IElement;
23 import org.simantics.g2d.utils.CanvasUtils;
24
25 public class SCLClipboard {
26         
27         public static void copyNode (Resource node) {
28                 
29                 ISelection selection = ISelectionUtils.createSelection(node);
30                 Resource[] rs = new Resource[] {node};
31                 StandardCopyHandler.copyResourcesToClipboard(rs, selection, new NullProgressMonitor());
32         }
33         
34         public static void pasteNode (Resource target) {
35                 
36                 PasteHandler handler = StandardPasteHandler.getPasteHandlerFromResource(target, PasteHandler.class);
37                 StandardPasteHandler.pasteResourceFromClipboardWithoutMonitor(handler);
38         }
39         
40         public static void copyPasteNode(Resource node, Resource target) {
41                 
42                 copyNode(node);
43                 pasteNode(target);
44         }
45         
46     public static boolean copyPasteDiagramContents (final ICanvasContext source_ctx, final ICanvasContext target_ctx, List<Resource> modules) throws DatabaseException {
47         
48         IDiagram idiagram = source_ctx.getDefaultHintContext().getHint(DiagramHints.KEY_DIAGRAM);
49
50                 DataElementMap dem = idiagram.getDiagramClass().getAtMostOneItemOfClass(DataElementMap.class);
51                 if (dem != null) {
52                         final Collection<IElement> newSelection = new ArrayList<IElement>();
53                         for (Resource module : modules) {
54                                 IElement element = dem.getElement(idiagram, module);
55                                 if (element != null) {
56                                         newSelection.add(element);
57                                 } else {
58                                         throw new DatabaseException("Could not find IElement for " + element); //$NON-NLS-1$
59                                 }
60                         }
61                         
62                         ThreadUtils.syncExec(source_ctx.getThreadAccess(), new Runnable() {
63                     @Override
64                     public void run() {
65                         if (source_ctx.isDisposed())
66                             return;
67                         Selection selection = source_ctx.getAtMostOneItemOfClass(Selection.class);
68                         if (selection != null) {
69                             // This prevents workbench selection from being left over.
70                             // Also prevents scene graph crap from being left on the screen.
71                             selection.setSelection(0, newSelection);
72                         }
73                                 CanvasUtils.sendCommand(source_ctx, Commands.COPY);
74                                 CanvasUtils.sendCommand(target_ctx, Commands.PASTE);
75                     }
76                 });
77                         
78                 //}
79                 
80                 while(source_ctx.getEventQueue().size() > 0) {
81                         try {
82                                 Thread.sleep(10);
83                         } catch (InterruptedException e) {
84                                 throw new DatabaseException(e);
85                         }
86                 }
87
88                 ThreadUtils.syncExec(source_ctx.getThreadAccess(), new Runnable() {
89             @Override
90             public void run() {
91             }
92         });             
93                                 
94                 }
95                 return true;
96     }
97     
98     public static boolean delete (final ICanvasContext source_ctx, List<Resource> modules) throws DatabaseException {
99         
100         IDiagram idiagram = source_ctx.getDefaultHintContext().getHint(DiagramHints.KEY_DIAGRAM);
101
102                 DataElementMap dem = idiagram.getDiagramClass().getAtMostOneItemOfClass(DataElementMap.class);
103                 if (dem != null) {
104                         final Collection<IElement> newSelection = new ArrayList<IElement>();
105                         for (Resource module : modules) {
106                                 IElement element = dem.getElement(idiagram, module);
107                                 if (element != null) {
108                                         newSelection.add(element);
109                                 } else {
110                                         throw new DatabaseException("Could not find IElement for " + element); //$NON-NLS-1$
111                                 }
112                         }
113                         
114                         ThreadUtils.syncExec(source_ctx.getThreadAccess(), new Runnable() {
115                     @Override
116                     public void run() {
117                         if (source_ctx.isDisposed())
118                             return;
119                         Selection selection = source_ctx.getAtMostOneItemOfClass(Selection.class);
120                         if (selection != null) {
121                             // This prevents workbench selection from being left over.
122                             // Also prevents scene graph crap from being left on the screen.
123                             selection.setSelection(0, newSelection);
124                         }
125                                 CanvasUtils.sendCommand(source_ctx, Commands.DELETE);
126                     }
127                 });
128                         
129                 //}
130                 
131                 while(source_ctx.getEventQueue().size() > 0) {
132                         try {
133                                 Thread.sleep(10);
134                         } catch (InterruptedException e) {
135                                 throw new DatabaseException(e);
136                         }
137                 }
138
139                 ThreadUtils.syncExec(source_ctx.getThreadAccess(), new Runnable() {
140             @Override
141             public void run() {
142             }
143         });             
144                                 
145                 }
146                 return true;
147     }
148         
149 }