]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Shortcut keys for different types of variables. SHIFT + variable (Auxiliary = A,...
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 20 Aug 2010 09:58:11 +0000 (09:58 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 20 Aug 2010 09:58:11 +0000 (09:58 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@17411 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/DiagramViewer.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/CreateVariablesShortcutParticipant.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/AuxiliaryFactory.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/CloudFactory.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/InputFactory.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/StockFactory.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ValveFactory.java

index fa54697977a80f05e76abfd20f4ff278f863aad9..9245e4ec9f3bb493b9559a01a66c5411f433a880 100644 (file)
@@ -34,6 +34,7 @@ import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;
 import org.simantics.modeling.ui.diagramEditor.handlers.LinkBrowsingHandler;\r
 import org.simantics.structural2.modelingRules.IModelingRules;\r
 import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.ui.editor.participant.CreateVariablesShortcutParticipant;\r
 import org.simantics.sysdyn.ui.elements2.connections.ConnectionClasses;\r
 import org.simantics.sysdyn.ui.elements2.connections.SysdynConnectionClass;\r
 import org.simantics.sysdyn.ui.properties.SysdynPropertyPage;\r
@@ -83,6 +84,7 @@ public class DiagramViewer extends org.simantics.modeling.ui.diagramEditor.Diagr
     protected void addKeyBindingParticipants(CanvasContext ctx) {\r
         ctx.add(new DeleteHandler(getEditorSite().getActionBars().getStatusLineManager()));\r
         ctx.add(new CopyPasteHandler(getEditorSite().getActionBars().getStatusLineManager()));\r
+        ctx.add(new CreateVariablesShortcutParticipant(synchronizer));\r
     }\r
 \r
     @Override\r
@@ -131,7 +133,7 @@ public class DiagramViewer extends org.simantics.modeling.ui.diagramEditor.Diagr
 //        Remove double click handler, because it is not working properly\r
 //        ctx.add(new StructuralBrowsingHandler(getSite(), sessionContext, getResourceInput2()));\r
         ctx.add(new LinkBrowsingHandler(getSite(), this, sessionContext));\r
-\r
+        \r
     }\r
 \r
     @Override\r
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
new file mode 100644 (file)
index 0000000..1e77596
--- /dev/null
@@ -0,0 +1,263 @@
+package org.simantics.sysdyn.ui.editor.participant;\r
+\r
+import java.awt.geom.AffineTransform;\r
+import java.awt.geom.Point2D;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.Queries;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.diagram.adapter.GraphToDiagramSynchronizer;\r
+import org.simantics.diagram.handler.DiagramSelection;\r
+import org.simantics.diagram.query.DiagramRequests;\r
+import org.simantics.g2d.canvas.SGDesignation;\r
+import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
+import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit;\r
+import org.simantics.g2d.diagram.DiagramHints;\r
+import org.simantics.g2d.diagram.DiagramMutator;\r
+import org.simantics.g2d.diagram.DiagramUtils;\r
+import org.simantics.g2d.diagram.IDiagram;\r
+import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;\r
+import org.simantics.g2d.diagram.participant.ElementPainter;\r
+import org.simantics.g2d.diagram.participant.Selection;\r
+import org.simantics.g2d.element.ElementClass;\r
+import org.simantics.g2d.element.ElementUtils;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.event.EventHandlerReflection.EventHandler;\r
+import org.simantics.g2d.event.KeyEvent;\r
+import org.simantics.g2d.event.KeyEvent.KeyPressedEvent;\r
+import org.simantics.g2d.event.KeyEvent.KeyReleasedEvent;\r
+import org.simantics.g2d.event.MouseEvent;\r
+import org.simantics.g2d.event.MouseEvent.MouseMovedEvent;\r
+import org.simantics.g2d.participant.MouseUtil;\r
+import org.simantics.g2d.participant.MouseUtil.MouseClickEvent;\r
+import org.simantics.g2d.participant.MouseUtil.MouseInfo;\r
+import org.simantics.scenegraph.g2d.G2DParentNode;\r
+import org.simantics.scenegraph.g2d.nodes.ShapeNode;\r
+import org.simantics.sysdyn.ui.elements2.AuxiliaryFactory;\r
+import org.simantics.sysdyn.ui.elements2.CloudFactory;\r
+import org.simantics.sysdyn.ui.elements2.InputFactory;\r
+import org.simantics.sysdyn.ui.elements2.StockFactory;\r
+import org.simantics.sysdyn.ui.elements2.ValveFactory;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.utils.datastructures.Callback;\r
+import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
+import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
+import org.simantics.utils.ui.ExceptionUtils;\r
+\r
+public class CreateVariablesShortcutParticipant extends AbstractDiagramParticipant {\r
+\r
+       public static final Key KEY_DIAGRAM_SELECTION = new KeyOf(\r
+                       DiagramSelection.class);\r
+\r
+       private GraphToDiagramSynchronizer synchronizer;\r
+\r
+       private VariableInformation variableInformation;\r
+\r
+       @Dependency\r
+       MouseUtil mouseUtil;\r
+\r
+       @Dependency\r
+       Selection selection;\r
+\r
+       @Dependency\r
+       ElementPainter diagramPainter;\r
+\r
+       ShapeNode node;\r
+       G2DParentNode parent;\r
+\r
+       @SGInit(designation = SGDesignation.CANVAS)\r
+       public void init(G2DParentNode parent) {\r
+               this.parent = parent;\r
+       }\r
+\r
+       public void removeSG() {\r
+               node.remove();\r
+               node = null;\r
+               setDirty();\r
+       }\r
+\r
+       void updateSG() {\r
+\r
+               if (node == null) {\r
+                       node = variableInformation.node;\r
+               }\r
+\r
+               MouseInfo mi = mouseUtil.getMouseInfo(0);\r
+               if (mi == null)\r
+                       return;\r
+\r
+               Point2D newPos = mi.canvasPosition;\r
+               double x = newPos.getX();\r
+               double y = newPos.getY();\r
+\r
+               AffineTransform origAt = node.getTransform();\r
+               double oldX = origAt.getTranslateX();\r
+               double oldY = origAt.getTranslateY();\r
+               AffineTransform move = new AffineTransform();\r
+               move.setToTranslation(x - oldX, y - oldY);\r
+               AffineTransform at2 = new AffineTransform(origAt);\r
+               at2.preConcatenate(move);\r
+               node.setTransform(at2);\r
+               setDirty();\r
+       }\r
+\r
+       public CreateVariablesShortcutParticipant(GraphToDiagramSynchronizer synchronizer) {\r
+               this.synchronizer = synchronizer;\r
+       }\r
+\r
+       @EventHandler(priority = -10)\r
+       public boolean handleKeyboardEvent(KeyEvent ke) {\r
+\r
+               KeyPressedEvent kpe;\r
+               if (ke instanceof KeyPressedEvent) {\r
+                       kpe = (KeyPressedEvent) ke;\r
+                       \r
+                       if (kpe.keyCode == java.awt.event.KeyEvent.VK_A) {\r
+                               variableInformation = new VariableInformation(\r
+                               java.awt.event.KeyEvent.VK_A,\r
+                               "http://www.simantics.org/Sysdyn-1.0/AuxiliarySymbol",\r
+                               (ShapeNode)AuxiliaryFactory.AUX_STATIC_IMAGE.init(parent)\r
+                               );\r
+                       } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_S) {\r
+                               variableInformation = new VariableInformation(\r
+                                               java.awt.event.KeyEvent.VK_S,\r
+                                               "http://www.simantics.org/Sysdyn-1.0/StockSymbol",\r
+                                               (ShapeNode)StockFactory.STOCK_IMAGE.init(parent)\r
+                                               );\r
+                       } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_C) {\r
+                               variableInformation = new VariableInformation(\r
+                                               java.awt.event.KeyEvent.VK_C,\r
+                                               "http://www.simantics.org/Sysdyn-1.0/CloudSymbol",\r
+                                               (ShapeNode)CloudFactory.CLOUD_IMAGE.init(parent)\r
+                                               );\r
+                       } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_V) {\r
+                               variableInformation = new VariableInformation(\r
+                                               java.awt.event.KeyEvent.VK_V,\r
+                                               "http://www.simantics.org/Sysdyn-1.0/ValveSymbol",\r
+                                               (ShapeNode)ValveFactory.VALVE_STATIC_IMAGE.init(parent)\r
+                                               );\r
+                       } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_I) {\r
+                               variableInformation = new VariableInformation(\r
+                                               java.awt.event.KeyEvent.VK_I,\r
+                                               "http://www.simantics.org/Sysdyn-1.0/InputSymbol",\r
+                                               (ShapeNode)InputFactory.INPUT_IMAGE.init(parent)\r
+                                               );\r
+                       }\r
+\r
+                       if (variableInformation != null) {\r
+                               updateSG();\r
+                               return true;\r
+                       }\r
+               }\r
+\r
+               KeyReleasedEvent kre;\r
+               if (ke instanceof KeyReleasedEvent) {\r
+                       kre = (KeyReleasedEvent) ke;\r
+\r
+                       if (variableInformation != null && kre.keyCode == variableInformation.shortcutKey) {\r
+                               if (node != null) {\r
+                                       variableInformation = null;\r
+                                       removeSG();\r
+                                       return true;\r
+                               }\r
+                       }\r
+               }\r
+\r
+               return false;\r
+\r
+       }\r
+\r
+       @EventHandler(priority = -10)\r
+       public boolean handleMouse(MouseMovedEvent e) {\r
+\r
+               if (variableInformation != null ) {\r
+                       updateSG();\r
+               } else {\r
+                       if (node != null) {\r
+                               removeSG();\r
+                       }\r
+               }\r
+               return false;\r
+       }\r
+\r
+\r
+       @EventHandler(priority = 100)\r
+       public boolean handleMouseEvent(MouseEvent me) {\r
+\r
+\r
+               MouseClickEvent mce;\r
+               if (me instanceof MouseClickEvent) {\r
+                       mce = (MouseClickEvent) me;\r
+               } else {\r
+                       return false;\r
+               }\r
+\r
+               if (!\r
+                               (\r
+                                               mce.button == MouseEvent.LEFT_BUTTON && \r
+                                               variableInformation != null && \r
+                                               (mce.stateMask == 0 || mce.stateMask ==  MouseEvent.SHIFT_MASK)\r
+                               )) \r
+               {\r
+                       return false;\r
+               }\r
+\r
+\r
+               final IDiagram d = getHint(DiagramHints.KEY_DIAGRAM);\r
+               if (d == null)\r
+                       return false;\r
+\r
+               DiagramUtils.mutateDiagram(d, new Callback<DiagramMutator>() {\r
+                       @Override\r
+                       public void run(DiagramMutator m) {\r
+\r
+                               Resource r;\r
+                               try {\r
+                                       r = SimanticsUI\r
+                                       .getSession()\r
+                                       .syncRequest(\r
+                                                       Queries\r
+                                                       .resource(variableInformation.symbolURI));\r
+                                       ElementClass ec = SimanticsUI.getSession().syncRequest(\r
+                                                       DiagramRequests.getElementClass(r, diagram));\r
+\r
+                                       IElement element = m.newElement(ec);\r
+\r
+                                       // MouseUtil mutil = new MouseUtil();\r
+                                       MouseInfo minfo = mouseUtil.getMouseInfo(0);\r
+\r
+                                       //at least when using breakpoints this is possible\r
+                                       if(minfo == null) \r
+                                               return;\r
+\r
+                                       Point2D p = minfo.canvasPosition;\r
+                                       //FIXME - Arto element doesn't know its size at first. Hopefully temp fix.\r
+                                       p.setLocation(p.getX()-5.46, p.getY()+1);\r
+\r
+                                       ElementUtils.setPos(element, p);\r
+\r
+                               } catch (DatabaseException e) {\r
+                                       ExceptionUtils.logAndShowError(e);\r
+                               }\r
+\r
+                       }\r
+               });\r
+\r
+               synchronizer.getCanvasContext().getContentContext().setDirty();\r
+\r
+               return true;\r
+       }\r
+\r
+       private class VariableInformation {\r
+               public String symbolURI;\r
+               public ShapeNode node;\r
+               public int shortcutKey;\r
+\r
+               public VariableInformation(int shortcutKey, String symbolURI, ShapeNode node) {\r
+                       this.symbolURI = symbolURI;\r
+                       this.node = node;\r
+                       this.shortcutKey = shortcutKey;\r
+               }\r
+       }\r
+\r
+}\r
index 1620323ba5713b7b3c622f34bb0e290d278558d4..e8f65686bf944ff12c4950aeb8546d0fb0b51b46 100644 (file)
@@ -36,8 +36,7 @@ import org.simantics.g2d.image.impl.ShapeImage;
  */\r
 public class AuxiliaryFactory extends SysdynElementFactory {\r
 \r
-    // TODO: make the static symbol image a text "AUX"\r
-    private static final Image AUX_STATIC_IMAGE = new ShapeImage(new Ellipse2D.Double(-5, -2, 10, 4), null, new BasicStroke(1), true);\r
+    public static final Image AUX_STATIC_IMAGE = new ShapeImage(new Ellipse2D.Double(-5, -2, 10, 4), null, new BasicStroke(1), true);\r
 \r
     @Override\r
     protected ElementClass compileElementClass(Resource elementType, Collection<ObjectTerminal> terminals) {\r
index 20da2813192a81cbc05162b1037abae04fa7473f..d71c579681fab91e04e68edb5b161e75363ab2bb 100644 (file)
@@ -58,7 +58,7 @@ public class CloudFactory extends SysdynElementFactory {
     public static final double       CLOUD_CURVES  = 7;\r
 \r
     private static final BasicStroke    STROKE           = new BasicStroke(1f);\r
-    private static final Image       DEFAULT_IMAGE = new ShapeImage(getCloudShape(), null, STROKE, true);\r
+    public static final Image       CLOUD_IMAGE = new ShapeImage(getCloudShape(), null, STROKE, true);\r
 \r
     static Shape getCloudShape() {\r
         Path2D path = new Path2D.Double();\r
@@ -91,7 +91,7 @@ public class CloudFactory extends SysdynElementFactory {
                 TextFontImpl.DEFAULT,\r
                 DefaultTransform.INSTANCE,\r
                 new StaticObjectAdapter(elementType),\r
-                new StaticSymbolImpl(DEFAULT_IMAGE),\r
+                new StaticSymbolImpl(CLOUD_IMAGE),\r
                 StaticSymbolImageInitializer.INSTANCE,\r
                 CloudSceneGraph.INSTANCE,\r
                 HoverImpl.INSTANCE,\r
index 43936fd3c61aa7429e443d99e0dfb24aeacaaf21..1876d3a685936dbf384d052689e69213b93d0e7a 100644 (file)
@@ -46,19 +46,19 @@ import org.simantics.utils.datastructures.hints.IHintContext.Key;
 public class InputFactory extends SysdynElementFactory {\r
 \r
     private static final BasicStroke    STROKE          = new BasicStroke(1f);\r
-    private static final Image          INPUT_IMAGE     = new ShapeImage(getInputShape(), null, STROKE, true);\r
+    public static final Image          INPUT_IMAGE     = new ShapeImage(getInputShape(), null, STROKE, true);\r
 \r
     static Shape getInputShape() {\r
         Path2D path = new Path2D.Double();\r
-        path.moveTo(0, -1);\r
-        path.lineTo(5, -1);\r
-        path.lineTo(5, -2.5);\r
-        path.lineTo(7.5, 0);\r
-        path.curveTo(7.5, -2.5, 12.5, -2.5, 12.5, 0);\r
-        path.curveTo(12.5, 2.5, 7.5, 2.5, 7.5, 0);\r
-        path.lineTo(5, 2.5);\r
-        path.lineTo(5, 1);\r
-        path.lineTo(0, 1);\r
+        path.moveTo(-6, -1);\r
+        path.lineTo(-1, -1);\r
+        path.lineTo(-1, -2.5);\r
+        path.lineTo(1.5, 0);\r
+        path.curveTo(1.5, -2.5, 6.5, -2.5, 6.5, 0);\r
+        path.curveTo(6.5, 2.5, 1.5, 2.5, 1.5, 0);\r
+        path.lineTo(-1, 2.5);\r
+        path.lineTo(-1, 1);\r
+        path.lineTo(-6, 1);\r
         path.closePath();\r
         return path;\r
     }\r
index 44476a87b8bb82604a9e14fbb306c1ab22947bb6..5e7e48e3486b3fc2e6851661de9e1c2ab2b63a2a 100644 (file)
@@ -35,7 +35,7 @@ import org.simantics.g2d.utils.Alignment;
 public class StockFactory extends SysdynElementFactory {\r
 \r
     private static final BasicStroke    STROKE           = new BasicStroke(1f);\r
-    private static final Image DEFAULT_IMAGE = new ShapeImage(new Rectangle2D.Double(-5, -2.5, 10, 5), null, STROKE, true);\r
+    public static final Image STOCK_IMAGE = new ShapeImage(new Rectangle2D.Double(-5, -2.5, 10, 5), null, STROKE, true);\r
 \r
     @Override\r
     protected ElementClass compileElementClass(Resource elementType, Collection<ObjectTerminal> terminals) {\r
@@ -47,7 +47,7 @@ public class StockFactory extends SysdynElementFactory {
                 TextFontImpl.DEFAULT,\r
                 DefaultTransform.INSTANCE,\r
                 new StaticObjectAdapter(elementType),\r
-                new StaticSymbolImpl(DEFAULT_IMAGE),\r
+                new StaticSymbolImpl(STOCK_IMAGE),\r
                 StaticSymbolImageInitializer.INSTANCE,\r
                 new HoverTextElementHandler(0, 0, Alignment.LEADING, 1f),\r
                 BoundsOutline.INSTANCE,\r
index 6c9123a80aa8cd7c42362925447133f7a1315ac8..ffcadcdb957ab3242399007193114573a2f0873b 100644 (file)
@@ -55,7 +55,7 @@ public class ValveFactory extends SysdynElementFactory {
     public static final double          VALVE_SIZE         = 2.5;\r
 \r
     private static final BasicStroke    STROKE             = new BasicStroke(1f);\r
-    private static final Image          VALVE_STATIC_IMAGE = new ShapeImage(createShape(VALVE_SIZE, false), null, STROKE, true);\r
+    public static final Image          VALVE_STATIC_IMAGE = new ShapeImage(createShape(VALVE_SIZE, false), null, STROKE, true);\r
 \r
     /* (non-Javadoc)\r
      * @see org.simantics.sysdyn.ui.elements2.SysdynElementFactory#compileElementClass(org.simantics.db.Resource, java.util.Collection)\r