1 package org.simantics.sysdyn.ui.handlers;
\r
3 import java.util.Set;
\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
29 * Handler for copying variables with custom prefix and/or suffix
\r
30 * @author Teemu Lempinen
\r
33 public class PasteSpecialHandler extends AbstractHandler {
\r
36 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
38 IEditorPart editor = (IEditorPart)HandlerUtil.getActiveEditor(event);
\r
39 if (!(editor instanceof DiagramEditor))
\r
41 final DiagramViewer viewer = (DiagramViewer) ((DiagramEditor) editor).getViewer();
\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
50 Shell shell = viewer.getComposite().getShell();
\r
54 PasteSpecialDialog dialog = new PasteSpecialDialog(shell);
\r
55 if(dialog.open() == Window.CANCEL)
\r
58 String prefix = dialog.getPrefix();
\r
59 String suffix = dialog.getSuffix();
\r
61 diagram.setHint(SynchronizationHints.COPY_ADVISOR, new MappedElementCopyAdvisor(new ElementCopyAdvisor(), new SysdynSpecialComponentCopyAdvisor(prefix, suffix)));
\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
72 diagram.setHint(SynchronizationHints.COPY_ADVISOR, standardCopyAdvisor);
\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
84 return DiagramSelection.EMPTY;
\r