From: miettinen Date: Tue, 12 Nov 2013 09:36:25 +0000 (+0000) Subject: Automatically activate rename when creating a new Sysdyn variable either by drag... X-Git-Tag: 1.8.1~199 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=1bca96e3de242bdcd4e4f7cbbcf22337c7de50fc;p=simantics%2Fsysdyn.git Automatically activate rename when creating a new Sysdyn variable either by drag and drop or keyboard shortcut (refs #870). git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@28243 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ontology/graph.tg b/org.simantics.sysdyn.ontology/graph.tg index 69f8f669..d606923c 100644 Binary files a/org.simantics.sysdyn.ontology/graph.tg and b/org.simantics.sysdyn.ontology/graph.tg differ diff --git a/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph b/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph index f2380596..8c5efed8 100644 --- a/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph +++ b/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph @@ -163,6 +163,7 @@ SYSDYN.IndependentVariable -- SYSDYN.IndependentVariable.rangeStep --> L0.Double -- SYSDYN.IndependentVariable.activeExpression --> SYSDYN.Expression -- SYSDYN.IndependentVariable.isUninitialized --> SYSDYN.IndependentVariable referrableVariables = g.syncRequest(new ObjectsWithType(configuration, L0.ConsistsOf, SR.IndependentVariable)); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/CreateVariablesShortcutParticipant.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/CreateVariablesShortcutParticipant.java index 7866f7d8..a5a2e5a3 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/CreateVariablesShortcutParticipant.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/CreateVariablesShortcutParticipant.java @@ -73,6 +73,9 @@ public class CreateVariablesShortcutParticipant extends AbstractDiagramParticipa ShapeNode node; G2DParentNode parent; + private boolean createVar; + private IDiagram createVarDiagram; + @SGInit(designation = SGDesignation.CANVAS) public void init(G2DParentNode parent) { this.parent = parent; @@ -175,6 +178,11 @@ public class CreateVariablesShortcutParticipant extends AbstractDiagramParticipa if (variableInformation != null && (kre.keyCode == variableInformation.shortcutKey || kre.keyCode == java.awt.event.KeyEvent.VK_SHIFT)) { if (node != null) { + // If there is a variable to be created, do it when a key is released. + if (createVar) { + createVar = false; + createVariableOnDiagram(createVarDiagram); + } variableInformation = null; removeSG(); return true; @@ -220,12 +228,20 @@ public class CreateVariablesShortcutParticipant extends AbstractDiagramParticipa { return false; } - - + final IDiagram d = getHint(DiagramHints.KEY_DIAGRAM); if (d == null) return false; + + // Need to create a new variable, save the diagram to do this later. + createVar = true; + createVarDiagram = d; + + return true; + } + + private void createVariableOnDiagram(IDiagram d) { DiagramUtils.mutateDiagram(d, new Callback() { @Override public void run(DiagramMutator m) { @@ -264,7 +280,6 @@ public class CreateVariablesShortcutParticipant extends AbstractDiagramParticipa synchronizer.getCanvasContext().getContentContext().setDirty(); - return true; } private class VariableInformation { diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/HoverTextElementNoBounds.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/HoverTextElementNoBounds.java index b623b9d7..9ac131dc 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/HoverTextElementNoBounds.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/HoverTextElementNoBounds.java @@ -15,15 +15,21 @@ import java.awt.Color; import java.awt.Font; import java.awt.geom.AffineTransform; +import org.eclipse.swt.widgets.Control; +import org.eclipse.ui.IWorkbenchPage; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.common.request.ReadRequest; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.DatabaseException; +import org.simantics.diagram.elements.DiagramNodeUtil; import org.simantics.diagram.elements.ITextListener; import org.simantics.diagram.elements.TextElementNoBounds; import org.simantics.diagram.elements.TextNode; +import org.simantics.diagram.participant.SGFocusParticipant; +import org.simantics.g2d.canvas.ICanvasContext; +import org.simantics.g2d.chassis.SWTChassis; import org.simantics.g2d.diagram.DiagramUtils; import org.simantics.g2d.diagram.IDiagram; import org.simantics.g2d.element.ElementHints; @@ -33,7 +39,11 @@ import org.simantics.g2d.element.handler.LifeCycle; import org.simantics.g2d.utils.Alignment; import org.simantics.layer0.Layer0; import org.simantics.modeling.ModelingResources; +import org.simantics.modeling.ui.diagramEditor.DiagramEditor; +import org.simantics.modeling.ui.diagramEditor.DiagramViewer; import org.simantics.scenegraph.g2d.G2DParentNode; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.utils.SysdynWorkbenchUtils; import org.simantics.sysdyn.ui.utils.VariableNameValidator; import org.simantics.ui.SimanticsUI; import org.simantics.utils.datastructures.Callback; @@ -177,10 +187,7 @@ public class HoverTextElementNoBounds extends TextElementNoBounds implements Lif public static double SCALE = 0.235; - @Override - public void init(final IElement e, G2DParentNode parent) { - HoverTextNode node = getTextNode(e, parent); - + protected void initNode(IElement e, HoverTextNode node) { //Font font = new Font("Tahoma", 0, 12); Font font = ElementUtils.getTextFont(e); Color color = ElementUtils.getTextColor(e); @@ -199,6 +206,99 @@ public class HoverTextElementNoBounds extends TextElementNoBounds implements Lif if(at != null) node.setTransform(at); } + + protected void activateEdit(final HoverTextNode node, final IElement e) { + final ICanvasContext ctx = DiagramNodeUtil.getCanvasContext(node); + // FIXME: needed only because eventdelegator registrations are done before adding node to scene graph. + if (ctx == null) + return; + if (!node.isEditMode()) { + + // Get the active editor + IWorkbenchPage page = SysdynWorkbenchUtils.getActivePageOfEditor(); + final DiagramEditor editor = (DiagramEditor)page.getActiveEditor(); + final ICanvasContext editorCtx = (ICanvasContext) editor.getViewer().getAdapter(ICanvasContext.class); + + editor.getViewer().getComposite().getDisplay().asyncExec(new Runnable() { + + @Override + public void run() { + Control c = editor.getViewer().getComposite().getDisplay().getFocusControl(); + if (c == null || "BasicSymbols".equals(c.getParent().getToolTipText())) { + // If the variable has been drag and dropped, set focus to diagram and then activate edit. + + editorCtx.add(new SGFocusParticipant((SWTChassis)editor.getViewer().getComposite(), DiagramViewer.DIAGRAMMING_CONTEXT) { + + @Override + public void focusGained(java.awt.event.FocusEvent event) { + + // When focus has been gained, acticate edit and destroy the listener. + editor.getViewer().getComposite().getDisplay().asyncExec(new Runnable() { + + @Override + public void run() { + if (Boolean.TRUE.equals(node.setEditMode(true))) { + node.activateEdit(0, e, ctx, true); + node.repaint(); + } + } + }); + ctx.remove(this); + } + + @Override + public void focusLost(java.awt.event.FocusEvent e) { + } + }); + + editor.setFocus(); + } else { + // If the variable has been created with shortcut key, just activate the edit. + if (Boolean.TRUE.equals(node.setEditMode(true))) { + node.activateEdit(0, e, ctx, true); + node.repaint(); + } + } + } + }); + } + } + + @Override + public void init(final IElement e, G2DParentNode parent) { + final HoverTextNode node = getTextNode(e, parent); + initNode(e, node); + + Object o = e.getHint(ElementHints.KEY_OBJECT); + if (o instanceof Resource) { + final Resource element = (Resource)o; + try { + SimanticsUI.getSession().syncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + SysdynResource SR = SysdynResource.getInstance(graph); + ModelingResources MR = ModelingResources.getInstance(graph); + Resource component = graph.getPossibleObject(element, MR.ElementToComponent); + if (component == null) + return; + + // See if the resource of the element has just been created. + Resource r = graph.getPossibleObject(component, SR.IndependentVariable_isUninitialized); + if (r == null){ + return; + } + + // If the resource is just been created, activate editing its name. + activateEdit(node, e); + graph.deny(component, SR.IndependentVariable_isUninitialized, r); + } + }); + } catch (DatabaseException e1) { + e1.printStackTrace(); + } + } + } @Override public void cleanup(IElement e) { diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/HoverTextNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/HoverTextNode.java index be323011..c469a1b5 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/HoverTextNode.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/HoverTextNode.java @@ -19,6 +19,7 @@ import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import org.simantics.diagram.elements.DiagramNodeUtil; +import org.simantics.diagram.elements.TextEditActivation; import org.simantics.diagram.elements.TextNode; import org.simantics.g2d.canvas.ICanvasContext; import org.simantics.g2d.element.IElement; @@ -158,4 +159,10 @@ public class HoverTextNode extends TextNode implements ISelectionPainterNode { } return false; } + + public TextEditActivation activateEdit(int mouseId, IElement e, ICanvasContext ctx, boolean save) { + if (save) + return editActivation = super.activateEdit(mouseId, e, ctx); + return super.activateEdit(mouseId, e, ctx); + } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/function/ModulesSearchFunction.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/function/ModulesSearchFunction.java index 4e565d5c..3caae72c 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/function/ModulesSearchFunction.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/function/ModulesSearchFunction.java @@ -18,10 +18,7 @@ import java.util.Map; import java.util.Set; import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.ui.IWorkbench; import org.eclipse.ui.IWorkbenchPage; -import org.eclipse.ui.IWorkbenchWindow; -import org.eclipse.ui.PlatformUI; import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; @@ -40,6 +37,7 @@ import org.simantics.scl.runtime.function.FunctionImpl5; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.ui.handlers.FindSearchTrim; import org.simantics.sysdyn.ui.handlers.FindSearchTrim.Scope; +import org.simantics.sysdyn.ui.utils.SysdynWorkbenchUtils; import org.simantics.workbench.search.NameAndTypeRow; import org.simantics.workbench.search.NamedResource; import org.simantics.workbench.search.SearchQuery; @@ -76,25 +74,9 @@ public class ModulesSearchFunction extends FunctionImpl5