From 74fe12aa5c892bb594aebcf34797fa0ffb952c9b Mon Sep 17 00:00:00 2001 From: lempinen Date: Fri, 23 Nov 2012 08:51:42 +0000 Subject: [PATCH] Special paste -option to diagram context menu (fixes #783) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@26398 ac1ea38d-2e2b-0410-8846-a27921b304fc --- org.simantics.sysdyn.ui/plugin.xml | 14 +++ .../SysdynComponentCopyAdvisor.java | 4 +- .../SysdynSpecialComponentCopyAdvisor.java | 44 ++++++++++ .../ui/handlers/PasteSpecialDialog.java | 85 ++++++++++++++++++ .../ui/handlers/PasteSpecialHandler.java | 86 +++++++++++++++++++ 5 files changed, 231 insertions(+), 2 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/SysdynSpecialComponentCopyAdvisor.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/PasteSpecialDialog.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/PasteSpecialHandler.java diff --git a/org.simantics.sysdyn.ui/plugin.xml b/org.simantics.sysdyn.ui/plugin.xml index ba2cf754..dc0753c9 100644 --- a/org.simantics.sysdyn.ui/plugin.xml +++ b/org.simantics.sysdyn.ui/plugin.xml @@ -934,6 +934,12 @@ label="Paste" style="push"> + + + + @@ -1304,6 +1314,10 @@ + + diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/SysdynComponentCopyAdvisor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/SysdynComponentCopyAdvisor.java index 94e3a9f0..d42556ef 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/SysdynComponentCopyAdvisor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/SysdynComponentCopyAdvisor.java @@ -64,12 +64,12 @@ public class SysdynComponentCopyAdvisor extends ComponentCopyAdvisor { } - renameComponent(context, graph, source, copy, sourceContainer, targetContainer); + rename(context, graph, source, copy, sourceContainer, targetContainer); return copy; } - public static String renameComponent(ISynchronizationContext context, WriteGraph graph, Resource source, + public String rename(ISynchronizationContext context, WriteGraph graph, Resource source, Resource copy, Resource sourceContainer, Resource targetContainer) throws DatabaseException { Layer0 l0 = Layer0.getInstance(graph); String copyName = NameUtils.getSafeName(graph, copy); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/SysdynSpecialComponentCopyAdvisor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/SysdynSpecialComponentCopyAdvisor.java new file mode 100644 index 00000000..40c38350 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/SysdynSpecialComponentCopyAdvisor.java @@ -0,0 +1,44 @@ +package org.simantics.sysdyn.ui.editor.participant; + +import org.simantics.databoard.Bindings; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.diagram.synchronization.ISynchronizationContext; +import org.simantics.layer0.Layer0; +import org.simantics.modeling.ComponentUtils; + +/** + * Copy advisor for copying sysdyn elements with custom prefix and/or suffix + * @author Teemu Lempinen + * + */ +public class SysdynSpecialComponentCopyAdvisor extends SysdynComponentCopyAdvisor { + + private String prefix = ""; + private String suffix = ""; + + public SysdynSpecialComponentCopyAdvisor(String prefix, String suffix) { + this.prefix = prefix; + this.suffix = suffix; + } + + @Override + public String rename(ISynchronizationContext context, WriteGraph graph, Resource source, + Resource copy, Resource sourceContainer, Resource targetContainer) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + String copyName = NameUtils.getSafeName(graph, copy); + Resource configurationRoot = ComponentUtils.getCompositeConfigurationRoot(graph, targetContainer); + + if(prefix == null) + prefix = ""; + if(suffix == null) + suffix = ""; + + String name = NameUtils.findFreshName(graph, prefix + copyName + suffix, configurationRoot, l0.ConsistsOf, "%s%d"); + graph.claimLiteral(copy, l0.HasName, name, Bindings.STRING); + return name; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/PasteSpecialDialog.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/PasteSpecialDialog.java new file mode 100644 index 00000000..a593b186 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/PasteSpecialDialog.java @@ -0,0 +1,85 @@ +package org.simantics.sysdyn.ui.handlers; + +import org.eclipse.jface.dialogs.Dialog; +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Text; + +/** + * Paste special dialog for copying variables in diagram. Default method is to add a number to the end of the variable name. + * Special paste enables users to define their own prefix and/or suffix to the copied variable name(s). + * @author Teemu Lempinen + * + */ +public class PasteSpecialDialog extends Dialog { + + private Text prefixText, suffixText; + private String prefix, suffix; + + protected PasteSpecialDialog(Shell parentShell) { + super(parentShell); + } + + @Override + protected Control createDialogArea(Composite parent) { + Composite composite = ( Composite )super.createDialogArea(parent); + composite.getShell().setText("Paste Special"); + GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(composite); + + Label label = new Label(composite, SWT.NONE); + label.setText("Prefix will be added to the beginning of\nthe copied variables, suffix to the end."); + GridDataFactory.fillDefaults().span(2, 1).grab(true, false).applyTo(label); + + + label = new Label(composite, SWT.NONE); + label.setText("Prefix: "); + label.setToolTipText("Text added to the beginning of a copied variable name"); + GridDataFactory.fillDefaults().applyTo(label); + + prefixText = new Text(composite, SWT.BORDER); + prefixText.setToolTipText("Text added to the beginning of a copied variable name"); + GridDataFactory.fillDefaults().applyTo(prefixText); + + label = new Label(composite, SWT.NONE); + label.setText("Suffix: "); + label.setToolTipText("Text added to the end of a copied variable name"); + GridDataFactory.fillDefaults().applyTo(label); + + suffixText = new Text(composite, SWT.BORDER); + suffixText.setToolTipText("Text added to the end of a copied variable name"); + GridDataFactory.fillDefaults().applyTo(suffixText); + + return composite; + } + + public String getPrefix() { + return prefix; + } + + public String getSuffix() { + return suffix; + } + + @Override + protected void cancelPressed() { + prefix = ""; + suffix = ""; + setReturnCode(CANCEL); + close(); + } + + @Override + protected void okPressed() { + prefix = prefixText.getText(); + suffix = suffixText.getText(); + setReturnCode(OK); + close(); + } + + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/PasteSpecialHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/PasteSpecialHandler.java new file mode 100644 index 00000000..9ed9bde5 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/PasteSpecialHandler.java @@ -0,0 +1,86 @@ +package org.simantics.sysdyn.ui.handlers; + +import java.util.Set; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.jface.window.Window; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.handlers.HandlerUtil; +import org.simantics.Simantics; +import org.simantics.db.layer0.util.ClipboardUtils; +import org.simantics.db.layer0.util.SimanticsClipboard.Representation; +import org.simantics.diagram.handler.CopyPasteHandler; +import org.simantics.diagram.handler.DiagramSelection; +import org.simantics.diagram.handler.DiagramSelectionRepresentation; +import org.simantics.diagram.synchronization.SynchronizationHints; +import org.simantics.g2d.canvas.ICanvasContext; +import org.simantics.g2d.diagram.IDiagram; +import org.simantics.modeling.mapping.ElementCopyAdvisor; +import org.simantics.modeling.mapping.MappedElementCopyAdvisor; +import org.simantics.modeling.ui.diagramEditor.DiagramEditor; +import org.simantics.scenegraph.g2d.events.command.Commands; +import org.simantics.sysdyn.ui.editor.DiagramViewer; +import org.simantics.sysdyn.ui.editor.participant.SysdynSpecialComponentCopyAdvisor; + +/** + * Handler for copying variables with custom prefix and/or suffix + * @author Teemu Lempinen + * + */ +public class PasteSpecialHandler extends AbstractHandler { + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + + IEditorPart editor = (IEditorPart)HandlerUtil.getActiveEditor(event); + if (!(editor instanceof DiagramEditor)) + return null; + final DiagramViewer viewer = (DiagramViewer) ((DiagramEditor) editor).getViewer(); + if (viewer == null) + return null; + + Object d = viewer.getAdapter(IDiagram.class); + if(d != null && d instanceof IDiagram) { + IDiagram diagram = (IDiagram) d; + MappedElementCopyAdvisor standardCopyAdvisor = diagram.getHint(SynchronizationHints.COPY_ADVISOR); + + Shell shell = viewer.getComposite().getShell(); + if(shell == null) + return null; + + PasteSpecialDialog dialog = new PasteSpecialDialog(shell); + if(dialog.open() == Window.CANCEL) + return null; + + String prefix = dialog.getPrefix(); + String suffix = dialog.getSuffix(); + + diagram.setHint(SynchronizationHints.COPY_ADVISOR, new MappedElementCopyAdvisor(new ElementCopyAdvisor(), new SysdynSpecialComponentCopyAdvisor(prefix, suffix))); + + Object v = viewer.getAdapter(ICanvasContext.class); + if(v != null && v instanceof ICanvasContext) { + ICanvasContext ctx = (ICanvasContext) v; + CopyPasteHandler handler = ctx.getAtMostOneItemOfClass(CopyPasteHandler.class); + if(handler != null) { + handler.paste(Commands.PASTE, getClipboardDiagramSelection()); + } + } + + diagram.setHint(SynchronizationHints.COPY_ADVISOR, standardCopyAdvisor); + + } + return null; + } + + public DiagramSelection getClipboardDiagramSelection() { + for (Set content : Simantics.getClipboard().getContents()) { + DiagramSelection sel = ClipboardUtils.accept(content, DiagramSelectionRepresentation.KEY_DIAGRAM_SELECTION); + if (sel != null) + return sel; + } + return DiagramSelection.EMPTY; + } +} -- 2.47.1