]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
9ed9bde5af005655fab528805e4ae2e45a146a0f
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.handlers;\r
2 \r
3 import java.util.Set;\r
4 \r
5 import org.eclipse.core.commands.AbstractHandler;\r
6 import org.eclipse.core.commands.ExecutionEvent;\r
7 import org.eclipse.core.commands.ExecutionException;\r
8 import org.eclipse.jface.window.Window;\r
9 import org.eclipse.swt.widgets.Shell;\r
10 import org.eclipse.ui.IEditorPart;\r
11 import org.eclipse.ui.handlers.HandlerUtil;\r
12 import org.simantics.Simantics;\r
13 import org.simantics.db.layer0.util.ClipboardUtils;\r
14 import org.simantics.db.layer0.util.SimanticsClipboard.Representation;\r
15 import org.simantics.diagram.handler.CopyPasteHandler;\r
16 import org.simantics.diagram.handler.DiagramSelection;\r
17 import org.simantics.diagram.handler.DiagramSelectionRepresentation;\r
18 import org.simantics.diagram.synchronization.SynchronizationHints;\r
19 import org.simantics.g2d.canvas.ICanvasContext;\r
20 import org.simantics.g2d.diagram.IDiagram;\r
21 import org.simantics.modeling.mapping.ElementCopyAdvisor;\r
22 import org.simantics.modeling.mapping.MappedElementCopyAdvisor;\r
23 import org.simantics.modeling.ui.diagramEditor.DiagramEditor;\r
24 import org.simantics.scenegraph.g2d.events.command.Commands;\r
25 import org.simantics.sysdyn.ui.editor.DiagramViewer;\r
26 import org.simantics.sysdyn.ui.editor.participant.SysdynSpecialComponentCopyAdvisor;\r
27 \r
28 /**\r
29  * Handler for copying variables with custom prefix and/or suffix\r
30  * @author Teemu Lempinen\r
31  *\r
32  */\r
33 public class PasteSpecialHandler extends AbstractHandler {\r
34 \r
35     @Override\r
36     public Object execute(ExecutionEvent event) throws ExecutionException {\r
37         \r
38         IEditorPart editor = (IEditorPart)HandlerUtil.getActiveEditor(event);\r
39         if (!(editor instanceof DiagramEditor))\r
40             return null;\r
41         final DiagramViewer viewer = (DiagramViewer) ((DiagramEditor) editor).getViewer();\r
42         if (viewer == null)\r
43             return null;\r
44 \r
45         Object d = viewer.getAdapter(IDiagram.class);\r
46         if(d != null && d instanceof IDiagram) {\r
47             IDiagram diagram = (IDiagram) d;\r
48             MappedElementCopyAdvisor standardCopyAdvisor = diagram.getHint(SynchronizationHints.COPY_ADVISOR);\r
49 \r
50             Shell shell = viewer.getComposite().getShell();\r
51             if(shell == null)\r
52                 return null;\r
53             \r
54             PasteSpecialDialog dialog = new PasteSpecialDialog(shell);\r
55             if(dialog.open() == Window.CANCEL)\r
56                 return null;\r
57             \r
58             String prefix = dialog.getPrefix();\r
59             String suffix = dialog.getSuffix();\r
60             \r
61             diagram.setHint(SynchronizationHints.COPY_ADVISOR, new MappedElementCopyAdvisor(new ElementCopyAdvisor(), new SysdynSpecialComponentCopyAdvisor(prefix, suffix)));\r
62 \r
63             Object v = viewer.getAdapter(ICanvasContext.class);\r
64             if(v != null && v instanceof ICanvasContext) {\r
65                 ICanvasContext ctx = (ICanvasContext) v;\r
66                 CopyPasteHandler handler = ctx.getAtMostOneItemOfClass(CopyPasteHandler.class);\r
67                 if(handler != null) {\r
68                     handler.paste(Commands.PASTE, getClipboardDiagramSelection());\r
69                 }\r
70             }\r
71 \r
72             diagram.setHint(SynchronizationHints.COPY_ADVISOR, standardCopyAdvisor);\r
73 \r
74         }\r
75         return null;\r
76     }\r
77     \r
78     public DiagramSelection getClipboardDiagramSelection() {\r
79         for (Set<Representation> content : Simantics.getClipboard().getContents()) {\r
80             DiagramSelection sel = ClipboardUtils.accept(content, DiagramSelectionRepresentation.KEY_DIAGRAM_SELECTION);\r
81             if (sel != null)\r
82                 return sel;\r
83         }\r
84         return DiagramSelection.EMPTY;\r
85     }\r
86 }\r