1 package org.simantics.modeling.ui;
3 import java.util.ArrayList;
4 import java.util.Collection;
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;
25 public class SCLClipboard {
27 public static void copyNode (Resource node) {
29 ISelection selection = ISelectionUtils.createSelection(node);
30 Resource[] rs = new Resource[] {node};
31 StandardCopyHandler.copyResourcesToClipboard(rs, selection, new NullProgressMonitor());
34 public static void pasteNode (Resource target) {
36 PasteHandler handler = StandardPasteHandler.getPasteHandlerFromResource(target, PasteHandler.class);
37 StandardPasteHandler.pasteResourceFromClipboardWithoutMonitor(handler);
40 public static void copyPasteNode(Resource node, Resource target) {
46 public static boolean copyPasteDiagramContents (final ICanvasContext source_ctx, final ICanvasContext target_ctx, List<Resource> modules) throws DatabaseException {
48 IDiagram idiagram = source_ctx.getDefaultHintContext().getHint(DiagramHints.KEY_DIAGRAM);
50 DataElementMap dem = idiagram.getDiagramClass().getAtMostOneItemOfClass(DataElementMap.class);
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);
58 throw new DatabaseException("Could not find IElement for " + element);
62 ThreadUtils.syncExec(source_ctx.getThreadAccess(), new Runnable() {
65 if (source_ctx.isDisposed())
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);
73 CanvasUtils.sendCommand(source_ctx, Commands.COPY);
74 CanvasUtils.sendCommand(target_ctx, Commands.PASTE);
80 while(source_ctx.getEventQueue().size() > 0) {
83 } catch (InterruptedException e) {
84 throw new DatabaseException(e);
88 ThreadUtils.syncExec(source_ctx.getThreadAccess(), new Runnable() {
98 public static boolean delete (final ICanvasContext source_ctx, List<Resource> modules) throws DatabaseException {
100 IDiagram idiagram = source_ctx.getDefaultHintContext().getHint(DiagramHints.KEY_DIAGRAM);
102 DataElementMap dem = idiagram.getDiagramClass().getAtMostOneItemOfClass(DataElementMap.class);
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);
110 throw new DatabaseException("Could not find IElement for " + element);
114 ThreadUtils.syncExec(source_ctx.getThreadAccess(), new Runnable() {
117 if (source_ctx.isDisposed())
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);
125 CanvasUtils.sendCommand(source_ctx, Commands.DELETE);
131 while(source_ctx.getEventQueue().size() > 0) {
134 } catch (InterruptedException e) {
135 throw new DatabaseException(e);
139 ThreadUtils.syncExec(source_ctx.getThreadAccess(), new Runnable() {