]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Add radio buttons in main toolbar for different diagram editor modes.
authorjkauttio <jkauttio@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 29 Oct 2013 11:52:18 +0000 (11:52 +0000)
committerjkauttio <jkauttio@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 29 Oct 2013 11:52:18 +0000 (11:52 +0000)
#fixes #4486

git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@28145 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/icons/cursor.png [new file with mode: 0644]
org.simantics.sysdyn.ui/icons/dependency.png [new file with mode: 0644]
org.simantics.sysdyn.ui/icons/flow.png [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/DiagramViewer.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/PointerInteractor.java [deleted file]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/SysdynConnectTool.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/SysdynPointerInteractor.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/SysdynElementHints.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/DiagramToolHandler.java [new file with mode: 0644]

diff --git a/org.simantics.sysdyn.ui/icons/cursor.png b/org.simantics.sysdyn.ui/icons/cursor.png
new file mode 100644 (file)
index 0000000..532f532
Binary files /dev/null and b/org.simantics.sysdyn.ui/icons/cursor.png differ
diff --git a/org.simantics.sysdyn.ui/icons/dependency.png b/org.simantics.sysdyn.ui/icons/dependency.png
new file mode 100644 (file)
index 0000000..6a604a3
Binary files /dev/null and b/org.simantics.sysdyn.ui/icons/dependency.png differ
diff --git a/org.simantics.sysdyn.ui/icons/flow.png b/org.simantics.sysdyn.ui/icons/flow.png
new file mode 100644 (file)
index 0000000..b1a1819
Binary files /dev/null and b/org.simantics.sysdyn.ui/icons/flow.png differ
index b7b961d7e5cfd4cbb17881344eaae806ba8c92bf..4d1f39a42762b70096ec10ee6a79e0de595b024a 100644 (file)
@@ -177,7 +177,7 @@ public class DiagramViewer extends org.simantics.modeling.ui.diagramEditor.Diagr
 \r
        @Override \r
        protected PointerInteractor getPointerInteractor() {\r
-               return new org.simantics.sysdyn.ui.editor.participant.PointerInteractor(true, true, true, false, true, false, synchronizer.getElementClassProvider());\r
+               return new org.simantics.sysdyn.ui.editor.participant.SysdynPointerInteractor(true, true, true, false, true, false, synchronizer.getElementClassProvider());\r
        }\r
 \r
        @Override\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/PointerInteractor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/PointerInteractor.java
deleted file mode 100644 (file)
index 16b08a6..0000000
+++ /dev/null
@@ -1,148 +0,0 @@
-package org.simantics.sysdyn.ui.editor.participant;\r
-/*******************************************************************************\r
- * Copyright (c) 2010 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-\r
-import java.awt.Shape;\r
-import java.awt.geom.Point2D;\r
-import java.awt.geom.Rectangle2D;\r
-import java.util.List;\r
-\r
-import org.simantics.diagram.participant.ConnectTool2;\r
-import org.simantics.g2d.canvas.Hints;\r
-import org.simantics.g2d.canvas.IToolMode;\r
-import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
-import org.simantics.g2d.canvas.impl.DependencyReflection.Reference;\r
-import org.simantics.g2d.connection.IConnectionAdvisor;\r
-import org.simantics.g2d.diagram.DiagramHints;\r
-import org.simantics.g2d.diagram.handler.PickContext;\r
-import org.simantics.g2d.diagram.participant.Selection;\r
-import org.simantics.g2d.diagram.participant.TerminalPainter;\r
-import org.simantics.g2d.diagram.participant.pointertool.TerminalUtil;\r
-import org.simantics.g2d.diagram.participant.pointertool.TerminalUtil.TerminalInfo;\r
-import org.simantics.g2d.element.ElementClasses;\r
-import org.simantics.g2d.element.IElement;\r
-import org.simantics.g2d.element.IElementClassProvider;\r
-import org.simantics.g2d.participant.KeyUtil;\r
-import org.simantics.g2d.participant.MouseUtil;\r
-import org.simantics.g2d.participant.TransformUtil;\r
-import org.simantics.g2d.utils.GeometryUtils;\r
-import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
-import org.simantics.scenegraph.g2d.events.MouseEvent;\r
-import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonPressedEvent;\r
-import org.simantics.sysdyn.ui.editor.participant.SysdynElementClassProviders.ISysdynElementClassProvider;\r
-import org.simantics.sysdyn.ui.editor.routing.DependencyRouter;\r
-import org.simantics.sysdyn.ui.editor.routing.FlowRouter;\r
-import org.simantics.sysdyn.ui.elements.AuxiliaryFactory;\r
-import org.simantics.sysdyn.ui.elements.CloudFactory;\r
-import org.simantics.sysdyn.ui.elements.InputFactory;\r
-import org.simantics.sysdyn.ui.elements.ModuleFactory;\r
-import org.simantics.sysdyn.ui.elements.connections.ConnectionClasses;\r
-\r
-/**\r
- * Pointer tool does the following operations with mouse:\r
- *   - Selections\r
- *   - Scale\r
- *   - Rotate\r
- *   - Translate\r
- *   - Draws connections\r
- *\r
- *\r
- * Pointer tool is active only when KEY_TOOLMODE is PointerToolMode\r
- *\r
- * TODO Pick rectangle not a point\r
- *\r
- * @author Toni Kalajainen\r
- */\r
-public class PointerInteractor extends org.simantics.diagram.participant.PointerInteractor2 {\r
-\r
-       @Dependency Selection selection;\r
-       @Dependency KeyUtil keys;\r
-       @Dependency TransformUtil util;\r
-       @Dependency PickContext pickContext;\r
-       @Dependency MouseUtil mice;\r
-       @Reference TerminalPainter terminalPainter;\r
-\r
-       public PointerInteractor(boolean clickSelect, boolean boxSelect, boolean dragElement, boolean dndDragElement, boolean connect, boolean doubleClickEdit, IElementClassProvider newConnectionClassProvider) {\r
-               super(clickSelect, boxSelect, dragElement, dndDragElement, connect, doubleClickEdit, newConnectionClassProvider);\r
-       }\r
-\r
-       @EventHandler(priority = TOOL_PRIORITY)\r
-       public boolean handlePress(MouseButtonPressedEvent me) {\r
-               if (!connects()) return false;\r
-               \r
-               IToolMode mode = getHint(Hints.KEY_TOOL);\r
-\r
-               if (mode != Hints.POINTERTOOL && mode != Hints.CONNECTTOOL) return false;\r
-               assertDependencies();\r
-               Point2D         curCanvasPos    = util.controlToCanvas(me.controlPosition, null);\r
-\r
-               // Pick Terminal\r
-               TerminalInfo ti = pickTerminal(me.controlPosition);\r
-\r
-               if((me.stateMask & MouseEvent.ALT_MASK) == 0) return false;\r
-\r
-               if(elementClassProvider != null) {\r
-                       ConnectTool2 bsi = null;\r
-                       if (ti != null) {\r
-\r
-                               IElement terminalElement = ti.e;\r
-\r
-                               if( me.button == MouseEvent.LEFT_BUTTON) {\r
-                                       if(terminalElement.getElementClass().getId().equals(CloudFactory.class.getSimpleName())) return false;\r
-                                       diagram.setHint(DiagramHints.ROUTE_ALGORITHM, new DependencyRouter());\r
-                                       diagram.setHint(DiagramHints.KEY_USE_CONNECTION_FLAGS, false);\r
-                                       ISysdynElementClassProvider secp = (ISysdynElementClassProvider)elementClassProvider;\r
-                                       secp.put(ElementClasses.CONNECTION, elementClassProvider.get(ConnectionClasses.DEPENDENCY));\r
-\r
-                               } else if (me.button == MouseEvent.RIGHT_BUTTON) {\r
-                                       String id  = terminalElement.getElementClass().getId();\r
-                                       if(id.equals(AuxiliaryFactory.class.getSimpleName())\r
-                                                       || id.equals(InputFactory.class.getSimpleName())\r
-                                                       || id.equals(ModuleFactory.class.getSimpleName())) return false;\r
-                                       diagram.setHint(DiagramHints.ROUTE_ALGORITHM, new FlowRouter(false));\r
-                                       diagram.setHint(DiagramHints.KEY_USE_CONNECTION_FLAGS, true);\r
-                                       ISysdynElementClassProvider secp = (ISysdynElementClassProvider)elementClassProvider;\r
-                                       secp.put(ElementClasses.CONNECTION, elementClassProvider.get(ConnectionClasses.FLOW));\r
-\r
-                               }\r
-\r
-                               IConnectionAdvisor advisor = diagram.getHint(DiagramHints.CONNECTION_ADVISOR);\r
-                               if (advisor == null || (advisor != null && advisor.canBeginConnection(null, ti.e, ti.t))) {\r
-                                       bsi = new SysdynConnectTool(ti, me.mouseId, curCanvasPos);\r
-                               }\r
-                       }  \r
-                       else if (me.button == MouseEvent.RIGHT_BUTTON) {\r
-                                // Start connection out of thin air, without a terminal.\r
-                                diagram.setHint(DiagramHints.ROUTE_ALGORITHM, new FlowRouter(false));\r
-                                diagram.setHint(DiagramHints.KEY_USE_CONNECTION_FLAGS, true);\r
-                                ISysdynElementClassProvider secp = (ISysdynElementClassProvider)elementClassProvider;\r
-                                secp.put(ElementClasses.CONNECTION, elementClassProvider.get(ConnectionClasses.FLOW));\r
-                                bsi = new SysdynConnectTool(null, me.mouseId, curCanvasPos);\r
-                       }\r
-\r
-                       if (bsi != null) {\r
-                               getContext().add(bsi);\r
-                               return true;\r
-                       }\r
-\r
-               }\r
-               return false;\r
-       }\r
-\r
-       public List<TerminalInfo> pickTerminals(Point2D controlPos)\r
-       {\r
-               Rectangle2D     controlPickRect     = new Rectangle2D.Double(controlPos.getX()-PointerInteractor.PICK_DIST, controlPos.getY()-PointerInteractor.PICK_DIST, PointerInteractor.PICK_DIST*2+1, PointerInteractor.PICK_DIST*2+1);\r
-               Shape           canvasPickRect      = GeometryUtils.transformShape(controlPickRect, util.getInverseTransform());\r
-               List<TerminalInfo> ti = TerminalUtil.pickTerminals(diagram, canvasPickRect, false, true);\r
-               return ti;\r
-       }\r
-}\r
index 9182e3b1515d8e7e38a25137ae7033f89d200de7..797b18258f77c0b6c123c07d1c0a1d05da99916e 100644 (file)
@@ -309,7 +309,7 @@ public class SysdynConnectTool extends ConnectTool2 {
             endFlagNode.setTransform(AffineTransform.getTranslateInstance(mouseCanvasPos.getX(), mouseCanvasPos.getY()));\r
         }\r
 \r
-               List<TerminalInfo> tiList = ((org.simantics.sysdyn.ui.editor.participant.PointerInteractor)pi).pickTerminals(me.controlPosition);\r
+               List<TerminalInfo> tiList = ((org.simantics.sysdyn.ui.editor.participant.SysdynPointerInteractor)pi).pickTerminals(me.controlPosition);\r
                TerminalInfo ti = null;\r
 \r
                IConnectionAdvisor advisor = diagram.getHint(DiagramHints.CONNECTION_ADVISOR);\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/SysdynPointerInteractor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/SysdynPointerInteractor.java
new file mode 100644 (file)
index 0000000..55df646
--- /dev/null
@@ -0,0 +1,170 @@
+package org.simantics.sysdyn.ui.editor.participant;\r
+/*******************************************************************************\r
+ * Copyright (c) 2010 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+\r
+import java.awt.Shape;\r
+import java.awt.geom.Point2D;\r
+import java.awt.geom.Rectangle2D;\r
+import java.util.List;\r
+\r
+import org.simantics.g2d.canvas.Hints;\r
+import org.simantics.g2d.canvas.ICanvasParticipant;\r
+import org.simantics.g2d.canvas.IToolMode;\r
+import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
+import org.simantics.g2d.canvas.impl.DependencyReflection.Reference;\r
+import org.simantics.g2d.connection.IConnectionAdvisor;\r
+import org.simantics.g2d.diagram.DiagramHints;\r
+import org.simantics.g2d.diagram.handler.PickContext;\r
+import org.simantics.g2d.diagram.participant.Selection;\r
+import org.simantics.g2d.diagram.participant.TerminalPainter;\r
+import org.simantics.g2d.diagram.participant.pointertool.PointerInteractor;\r
+import org.simantics.g2d.diagram.participant.pointertool.TerminalUtil;\r
+import org.simantics.g2d.diagram.participant.pointertool.TerminalUtil.TerminalInfo;\r
+import org.simantics.g2d.element.ElementClasses;\r
+import org.simantics.g2d.element.IElementClassProvider;\r
+import org.simantics.g2d.participant.KeyUtil;\r
+import org.simantics.g2d.participant.MouseUtil;\r
+import org.simantics.g2d.participant.TransformUtil;\r
+import org.simantics.g2d.utils.GeometryUtils;\r
+import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
+import org.simantics.scenegraph.g2d.events.MouseEvent;\r
+import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonPressedEvent;\r
+import org.simantics.sysdyn.ui.editor.participant.SysdynElementClassProviders.ISysdynElementClassProvider;\r
+import org.simantics.sysdyn.ui.editor.routing.DependencyRouter;\r
+import org.simantics.sysdyn.ui.editor.routing.FlowRouter;\r
+import org.simantics.sysdyn.ui.elements.AuxiliaryFactory;\r
+import org.simantics.sysdyn.ui.elements.CloudFactory;\r
+import org.simantics.sysdyn.ui.elements.InputFactory;\r
+import org.simantics.sysdyn.ui.elements.ModuleFactory;\r
+import org.simantics.sysdyn.ui.elements.SysdynElementHints;\r
+import org.simantics.sysdyn.ui.elements.connections.ConnectionClasses;\r
+\r
+/**\r
+ * Pointer tool does the following operations with mouse:\r
+ *   - Selections\r
+ *   - Scale\r
+ *   - Rotate\r
+ *   - Translate\r
+ *   - Draws connections\r
+ *\r
+ * Pointer tool is active only when KEY_TOOLMODE is PointerToolMode\r
+ *\r
+ * TODO Pick rectangle not a point\r
+ *\r
+ * @author Toni Kalajainen\r
+ */\r
+public class SysdynPointerInteractor extends PointerInteractor {\r
+\r
+       @Dependency Selection selection;\r
+       @Dependency KeyUtil keys;\r
+       @Dependency TransformUtil util;\r
+       @Dependency PickContext pickContext;\r
+       @Dependency MouseUtil mice;\r
+       @Reference TerminalPainter terminalPainter;\r
+\r
+       public SysdynPointerInteractor(boolean clickSelect, boolean boxSelect, boolean dragElement, boolean dndDragElement, boolean connect, boolean doubleClickEdit, IElementClassProvider newConnectionClassProvider) {\r
+               super(clickSelect, boxSelect, dragElement, dndDragElement, connect, doubleClickEdit, newConnectionClassProvider);\r
+       }\r
+\r
+       @EventHandler(priority = TOOL_PRIORITY)\r
+       public boolean handlePress(MouseButtonPressedEvent me) {\r
+               if (!connects()) \r
+                       return false;\r
+               if (elementClassProvider == null)\r
+                       return false;\r
+               \r
+               IToolMode mode = getHint(Hints.KEY_TOOL);\r
+               if (!Hints.CONNECTTOOL.equals(mode))\r
+                       return false;\r
+               \r
+               IToolMode sysdynMode = getHint(SysdynElementHints.SYSDYN_KEY_TOOL);\r
+               \r
+               assertDependencies();\r
+               \r
+               TerminalInfo ti = pickTerminal(me.controlPosition);\r
+               Point2D curCanvasPos = util.controlToCanvas(me.controlPosition, null);\r
+               \r
+               ICanvasParticipant bsi = null;\r
+               \r
+               if (me.button == MouseEvent.LEFT_BUTTON) {\r
+                       if (SysdynElementHints.DEPENDENCY_TOOL.equals(sysdynMode))\r
+                               bsi = getDependencyConnectTool(ti, me.mouseId, curCanvasPos);\r
+                       else if (SysdynElementHints.FLOW_TOOL.equals(sysdynMode))\r
+                               bsi = getFlowConnectTool(ti, me.mouseId, curCanvasPos);\r
+                       else\r
+                               bsi = getDependencyConnectTool(ti, me.mouseId, curCanvasPos);\r
+               }\r
+               else if (me.button == MouseEvent.RIGHT_BUTTON) {\r
+                       if (SysdynElementHints.DEPENDENCY_TOOL.equals(sysdynMode) ||\r
+                                       SysdynElementHints.FLOW_TOOL.equals(sysdynMode))\r
+                               return false;\r
+                       else\r
+                               bsi = getFlowConnectTool(ti, me.mouseId, curCanvasPos);\r
+               }\r
+\r
+               if (bsi != null) {\r
+                       getContext().add(bsi);\r
+                       return true;\r
+               }\r
+               \r
+               return false;\r
+       }\r
+       \r
+       private ICanvasParticipant getDependencyConnectTool(TerminalInfo ti, int mouseId, Point2D curCanvasPos) {\r
+               // can not have dependencies that start from thin air\r
+               if (ti == null)\r
+                       return null;\r
+               // can not have dependencies that start from clouds\r
+               if (ti.e.getElementClass().getId().equals(CloudFactory.class.getSimpleName())) \r
+                       return null;\r
+               \r
+               diagram.setHint(DiagramHints.ROUTE_ALGORITHM, new DependencyRouter());\r
+               diagram.setHint(DiagramHints.KEY_USE_CONNECTION_FLAGS, false);\r
+               ISysdynElementClassProvider secp = (ISysdynElementClassProvider)elementClassProvider;\r
+               secp.put(ElementClasses.CONNECTION, elementClassProvider.get(ConnectionClasses.DEPENDENCY));\r
+\r
+               // not sure if this is necessary\r
+               IConnectionAdvisor advisor = diagram.getHint(DiagramHints.CONNECTION_ADVISOR);\r
+               if (advisor == null || advisor.canBeginConnection(null, ti.e, ti.t)) {\r
+                       return new SysdynConnectTool(ti, mouseId, curCanvasPos);\r
+               }\r
+               \r
+               return null;\r
+       }\r
+       \r
+       private ICanvasParticipant getFlowConnectTool(TerminalInfo ti, int mouseId, Point2D curCanvasPos) {\r
+               // flows must start from thin air or clouds\r
+               if (ti != null && (ti.e.getElementClass().getId().equals(AuxiliaryFactory.class.getSimpleName()) || \r
+                               ti.e.getElementClass().getId().equals(InputFactory.class.getSimpleName()) || \r
+                               ti.e.getElementClass().getId().equals(ModuleFactory.class.getSimpleName()))) \r
+                       return null;\r
+               \r
+               diagram.setHint(DiagramHints.ROUTE_ALGORITHM, new FlowRouter());\r
+               diagram.setHint(DiagramHints.KEY_USE_CONNECTION_FLAGS, true);\r
+               ISysdynElementClassProvider secp = (ISysdynElementClassProvider)elementClassProvider;\r
+               secp.put(ElementClasses.CONNECTION, elementClassProvider.get(ConnectionClasses.FLOW));\r
+               \r
+               // not sure if this is necessary\r
+               IConnectionAdvisor advisor = diagram.getHint(DiagramHints.CONNECTION_ADVISOR);\r
+               if (ti == null || advisor == null || advisor.canBeginConnection(null, ti.e, ti.t)) {\r
+                       return new SysdynConnectTool(ti, mouseId, curCanvasPos);\r
+               }\r
+               \r
+               return null;\r
+       }\r
+\r
+       public List<TerminalInfo> pickTerminals(Point2D controlPos) {\r
+               Rectangle2D controlPickRect = new Rectangle2D.Double(controlPos.getX()-SysdynPointerInteractor.PICK_DIST, controlPos.getY()-SysdynPointerInteractor.PICK_DIST, SysdynPointerInteractor.PICK_DIST*2+1, SysdynPointerInteractor.PICK_DIST*2+1);\r
+               Shape       canvasPickRect  = GeometryUtils.transformShape(controlPickRect, util.getInverseTransform());\r
+               return TerminalUtil.pickTerminals(diagram, canvasPickRect, false, true);\r
+       }\r
+}\r
index 1b9210205cae74e3f9ed261b7bc32d394f2d9ce3..4a46af469131a2cdd952d2821f89101e914a44fb 100644 (file)
@@ -11,6 +11,8 @@
  *******************************************************************************/\r
 package org.simantics.sysdyn.ui.elements;\r
 \r
+import org.simantics.g2d.canvas.IToolMode;\r
+import org.simantics.g2d.canvas.impl.ToolMode;\r
 import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
 \r
@@ -18,6 +20,10 @@ public class SysdynElementHints {
 \r
     public static final Key KEY_INPUT_REFERENCE = new KeyOf(String.class, "INPUT_REFERENCE");\r
     public static final Key KEY_ORIENTATION = new KeyOf(String.class, "ORIENTATION");   \r
-    public static final Key KEY_LOCATION = new KeyOf(String.class, "LOCATION");       \r
+    public static final Key KEY_LOCATION = new KeyOf(String.class, "LOCATION");     \r
+    \r
+    public static final Key       SYSDYN_KEY_TOOL = new KeyOf(ToolMode.class, "SysdynKeyTool");\r
+    public static final IToolMode DEPENDENCY_TOOL = new ToolMode("DependencyTool");\r
+    public static final IToolMode FLOW_TOOL       = new ToolMode("FlowTool");\r
     \r
 }\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/DiagramToolHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/DiagramToolHandler.java
new file mode 100644 (file)
index 0000000..e162c85
--- /dev/null
@@ -0,0 +1,61 @@
+package org.simantics.sysdyn.ui.handlers;\r
+\r
+import org.eclipse.core.commands.AbstractHandler;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.ui.handlers.HandlerUtil;\r
+import org.eclipse.ui.handlers.RadioState;\r
+import org.simantics.g2d.canvas.Hints;\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.canvas.IToolMode;\r
+import org.simantics.sysdyn.ui.elements.SysdynElementHints;\r
+import org.simantics.utils.threads.ThreadUtils;\r
+\r
+public class DiagramToolHandler extends AbstractHandler {\r
+\r
+       @Override\r
+       public Object execute(ExecutionEvent event) throws ExecutionException {\r
+               final ICanvasContext context = (ICanvasContext)HandlerUtil.getActiveEditor(event).getAdapter(ICanvasContext.class);\r
+               if (context == null) {\r
+                       throw new ExecutionException("Could not get context from editor");\r
+               }\r
+\r
+               String value = (String)event.getCommand().getState(RadioState.STATE_ID).getValue();\r
+               \r
+               final IToolMode mode;\r
+               if ("pointer".equals(value)) {\r
+                       mode = Hints.POINTERTOOL;\r
+               }\r
+               else if ("dependency".equals(value)) {\r
+                       mode = SysdynElementHints.DEPENDENCY_TOOL;\r
+               }\r
+               else if ("flow".equals(value)) {\r
+                       mode = SysdynElementHints.FLOW_TOOL;\r
+               }\r
+               else {\r
+                       return null;\r
+               }\r
+               \r
+               ThreadUtils.syncExec(context.getThreadAccess(), new Runnable() {\r
+                       @Override\r
+                       public void run() {\r
+                               if (mode.equals(SysdynElementHints.DEPENDENCY_TOOL) || mode.equals(SysdynElementHints.FLOW_TOOL)) {\r
+                                       // if one of the connection modes is selected, use the\r
+                                       // default connection tool as the base and indicate the\r
+                                       // desired connection type in another hint (this is done\r
+                                       // to make sure all features of the connection tool work\r
+                                       // as expected)\r
+                                       context.getDefaultHintContext().setHint(Hints.KEY_TOOL, Hints.CONNECTTOOL);\r
+                                       context.getDefaultHintContext().setHint(SysdynElementHints.SYSDYN_KEY_TOOL, mode);\r
+                               }\r
+                               else {\r
+                                       context.getDefaultHintContext().setHint(Hints.KEY_TOOL, mode);\r
+                                       context.getDefaultHintContext().removeHint(SysdynElementHints.SYSDYN_KEY_TOOL);\r
+                               }\r
+                       }\r
+               });\r
+               \r
+               return null;\r
+       }\r
+\r
+}\r