]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@13203 ac1ea38d-2e2b...
authorniemisto <niemisto@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 1 Dec 2009 15:44:12 +0000 (15:44 +0000)
committerniemisto <niemisto@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 1 Dec 2009 15:44:12 +0000 (15:44 +0000)
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/actions/ConnectBase.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/actions/ConnectDependency.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/actions/ConnectFlow.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SysdynDiagramEditor.java
org.simantics.sysdyn/src/org/simantics/sysdyn/representation/SysdynSchema.java

diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/actions/ConnectBase.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/actions/ConnectBase.java
new file mode 100644 (file)
index 0000000..8d8c173
--- /dev/null
@@ -0,0 +1,108 @@
+package org.simantics.sysdyn.ui.actions;\r
+\r
+import java.awt.geom.Rectangle2D;\r
+\r
+import org.simantics.h2d.editor.IDiagramEditor;\r
+import org.simantics.h2d.element.IElement;\r
+import org.simantics.h2d.element.handler.Connectable;\r
+import org.simantics.h2d.event.DragEvent;\r
+import org.simantics.h2d.event.handler.DragEventHandler;\r
+import org.simantics.h2d.node.RectangleNode;\r
+import org.simantics.scenegraph.g2d.G2DParentNode;\r
+\r
+public abstract class ConnectBase extends DragEventHandler {\r
+\r
+       protected Connectable from;\r
+       protected Connectable to;\r
+       \r
+       RectangleNode fromNode = new RectangleNode();   \r
+       RectangleNode toNode = new RectangleNode();\r
+       \r
+       /**\r
+        * This is called when starting to make a connection.\r
+        */\r
+       protected abstract boolean isAllowedTail(Connectable tail);\r
+       \r
+       /**\r
+        * This is called when the mouse hovers over another connection point.\r
+        */\r
+       protected abstract boolean isAllowedHead(Connectable head);\r
+       \r
+       protected abstract void connect(IDiagramEditor editor);\r
+       \r
+       @Override\r
+       protected boolean begin(IDiagramEditor editor, DragEvent event) {\r
+               for(IElement element : event.pickedElements) {\r
+                       Connectable connectable = element.getInterface(Connectable.class);\r
+                       if(connectable != null && isAllowedTail(connectable)) {\r
+                           System.out.println("allowed");\r
+                               this.from = connectable;\r
+                               return true;\r
+                       }\r
+               }\r
+               System.out.println("not allowed");\r
+               return false;\r
+       }\r
+       \r
+       @Override\r
+       protected void update(IDiagramEditor editor, DragEvent event) {\r
+               for(IElement element : editor.pickElements(event.current)) {\r
+                       Connectable connectable = element.getInterface(Connectable.class);                      \r
+                       if(connectable != null && connectable != from) {\r
+                               if(connectable == to)\r
+                                       return;\r
+                               if(!isAllowedHead(connectable))\r
+                                   continue;\r
+                               to = connectable;\r
+                               \r
+                               Rectangle2D bounds = new Rectangle2D.Double();          \r
+                               to.getBounds(bounds);\r
+                               bounds.setFrame(\r
+                                               bounds.getX()-2.0, \r
+                                               bounds.getY()-2.0, \r
+                                               bounds.getWidth()+4.0, \r
+                                               bounds.getHeight()+4.0);                                \r
+                               toNode.init(bounds);\r
+                               \r
+                               editor.requestRepaint();                                \r
+                               return;\r
+                       }\r
+               }\r
+               to = null;\r
+               toNode.init(fromNode.getBounds());\r
+               editor.requestRepaint();\r
+       }\r
+       \r
+       @Override\r
+       protected void end(IDiagramEditor editor, DragEvent event) {\r
+               if(to != null)\r
+                       connect(editor);\r
+               \r
+               from = null;\r
+               to = null;\r
+       }\r
+       \r
+       @Override\r
+       public void init(G2DParentNode parent) {\r
+               fromNode = parent.addNode(RectangleNode.class);\r
+               toNode = parent.addNode(RectangleNode.class);\r
+               \r
+               Rectangle2D bounds = new Rectangle2D.Double();          \r
+               from.getBounds(bounds);\r
+               bounds.setFrame(\r
+                               bounds.getX()-2.0, \r
+                               bounds.getY()-2.0, \r
+                               bounds.getWidth()+4.0, \r
+                               bounds.getHeight()+4.0);\r
+               \r
+               fromNode.init(bounds);\r
+               toNode.init(bounds);\r
+       }\r
+       \r
+       @Override\r
+       public void remove() {\r
+               fromNode.remove();\r
+               toNode.remove();\r
+       }\r
+\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/actions/ConnectDependency.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/actions/ConnectDependency.java
new file mode 100644 (file)
index 0000000..765ff39
--- /dev/null
@@ -0,0 +1,36 @@
+package org.simantics.sysdyn.ui.actions;\r
+\r
+import org.simantics.h2d.editor.IDiagramEditor;\r
+import org.simantics.h2d.element.handler.Connectable;\r
+import org.simantics.sysdyn.representation.Auxiliary;\r
+import org.simantics.sysdyn.representation.Stock;\r
+import org.simantics.sysdyn.representation.Valve;\r
+import org.simantics.sysdyn.ui.elements.AuxiliaryElement;\r
+import org.simantics.sysdyn.ui.elements.DependencyElement;\r
+import org.simantics.sysdyn.ui.elements.StockElement;\r
+import org.simantics.sysdyn.ui.elements.ValveElement;\r
+\r
+public class ConnectDependency extends ConnectBase {\r
+\r
+    @Override\r
+    protected boolean isAllowedTail(Connectable tail) {\r
+        return tail instanceof AuxiliaryElement \r
+            || tail instanceof StockElement\r
+            || tail instanceof ValveElement \r
+             ;\r
+    }\r
+    \r
+    @Override\r
+    protected boolean isAllowedHead(Connectable head) {\r
+        return head instanceof AuxiliaryElement \r
+            || head instanceof ValveElement\r
+             ;            \r
+    }\r
+\r
+    @Override\r
+    protected void connect(IDiagramEditor editor) {\r
+        editor.getDiagram().addElement(new DependencyElement(from, to));        \r
+    }\r
+\r
+\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/actions/ConnectFlow.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/actions/ConnectFlow.java
new file mode 100644 (file)
index 0000000..cca8cde
--- /dev/null
@@ -0,0 +1,53 @@
+package org.simantics.sysdyn.ui.actions;\r
+\r
+import java.awt.geom.Point2D;\r
+\r
+import org.simantics.h2d.editor.IDiagramEditor;\r
+import org.simantics.h2d.element.handler.Connectable;\r
+import org.simantics.sysdyn.representation.Cloud;\r
+import org.simantics.sysdyn.representation.Stock;\r
+import org.simantics.sysdyn.representation.Valve;\r
+import org.simantics.sysdyn.ui.elements.CloudElement;\r
+import org.simantics.sysdyn.ui.elements.FlowElement;\r
+import org.simantics.sysdyn.ui.elements.StockElement;\r
+import org.simantics.sysdyn.ui.elements.ValveElement;\r
+\r
+public class ConnectFlow extends ConnectBase {\r
+\r
+    @Override\r
+    protected boolean isAllowedTail(Connectable tail) {\r
+        return tail instanceof CloudElement \r
+            || tail instanceof StockElement\r
+            || tail instanceof ValveElement\r
+             ;\r
+    }\r
+    \r
+    @Override\r
+    protected boolean isAllowedHead(Connectable head) {\r
+        return head instanceof CloudElement \r
+            || head instanceof StockElement \r
+            || (head instanceof ValveElement && !(from instanceof ValveElement))\r
+             ;          \r
+    }\r
+\r
+    @Override\r
+    protected void connect(IDiagramEditor editor) {\r
+        if(from instanceof Valve || to instanceof Valve)\r
+            editor.getDiagram().addElement(new FlowElement(from, to));\r
+        else {\r
+            Point2D fromOrigo = from.getOrigo();\r
+            Point2D toOrigo = to.getOrigo();\r
+            ValveElement valveElement = new ValveElement(\r
+                    0.5 * (fromOrigo.getX() + toOrigo.getX()),\r
+                    0.5 * (fromOrigo.getY() + toOrigo.getY())\r
+                    );\r
+            editor.getDiagram().addElement(valveElement);\r
+            editor.getDiagram().addElement(new FlowElement(from, valveElement));\r
+            editor.getDiagram().addElement(new FlowElement(valveElement, to));\r
+            \r
+            valveElement.beginRenameAction(editor);\r
+        }\r
+    }\r
+\r
+\r
+}\r
index 006093c5d90ac480bf57e29ff662de8ee8931dbc..ef60abab626053e19e986b32c8c478b6d310792c 100644 (file)
@@ -32,7 +32,8 @@ import org.simantics.objmap.IMapping;
 import org.simantics.objmap.IMappingListener;\r
 import org.simantics.objmap.MappingException;\r
 import org.simantics.objmap.Mappings;\r
-import org.simantics.sysdyn.ui.actions.Connect;\r
+import org.simantics.sysdyn.ui.actions.ConnectDependency;\r
+import org.simantics.sysdyn.ui.actions.ConnectFlow;\r
 import org.simantics.sysdyn.ui.actions.CreateAuxiliary;\r
 import org.simantics.sysdyn.ui.actions.CreateCloud;\r
 import org.simantics.sysdyn.ui.actions.CreateStock;\r
@@ -164,7 +165,8 @@ public class SysdynDiagramEditor extends ResourceEditorPart {
                        editor.addEventHandler(1, "key(S)", new CreateStock());\r
                        editor.addEventHandler(1, "key(V)", new CreateValve());\r
                        editor.addEventHandler(1, "key(C)", new CreateCloud());\r
-                       editor.addEventHandler(1, "drag(alt+left)", new Connect());\r
+                       editor.addEventHandler(1, "drag(alt+left)", new ConnectDependency());\r
+                       editor.addEventHandler(1, "drag(alt+right)", new ConnectFlow());\r
                        \r
                canvas = new EditorCanvas(editor);\r
                 frame.add(canvas);\r
index eccb7c9d31bf48f35ed92897ce3e767d21986500..58be1de4684c3e16a2d90cb2d8990ca96ddea815 100644 (file)
@@ -16,6 +16,7 @@ public class SysdynSchema extends SimpleSchema {
             addLinkType(MappingSchemas.fromAnnotations(g, Flow.class));\r
             addLinkType(MappingSchemas.fromAnnotations(g, Stock.class));\r
             addLinkType(MappingSchemas.fromAnnotations(g, Valve.class));\r
+            addLinkType(MappingSchemas.fromAnnotations(g, NormalExpression.class));\r
         } catch (DatabaseException e) {\r
             // TODO Auto-generated catch block\r
             e.printStackTrace();\r