--- /dev/null
+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
--- /dev/null
+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
--- /dev/null
+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
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
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
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