From: niemisto Date: Tue, 1 Dec 2009 15:44:12 +0000 (+0000) Subject: git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@13203 ac1ea38d-2e2b... X-Git-Tag: simantics-1.0~117 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=fe7170d189c80c949830968a9a7c2e27da28baed;p=simantics%2Fsysdyn.git git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@13203 ac1ea38d-2e2b-0410-8846-a27921b304fc --- 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 index 00000000..8d8c1733 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/actions/ConnectBase.java @@ -0,0 +1,108 @@ +package org.simantics.sysdyn.ui.actions; + +import java.awt.geom.Rectangle2D; + +import org.simantics.h2d.editor.IDiagramEditor; +import org.simantics.h2d.element.IElement; +import org.simantics.h2d.element.handler.Connectable; +import org.simantics.h2d.event.DragEvent; +import org.simantics.h2d.event.handler.DragEventHandler; +import org.simantics.h2d.node.RectangleNode; +import org.simantics.scenegraph.g2d.G2DParentNode; + +public abstract class ConnectBase extends DragEventHandler { + + protected Connectable from; + protected Connectable to; + + RectangleNode fromNode = new RectangleNode(); + RectangleNode toNode = new RectangleNode(); + + /** + * This is called when starting to make a connection. + */ + protected abstract boolean isAllowedTail(Connectable tail); + + /** + * This is called when the mouse hovers over another connection point. + */ + protected abstract boolean isAllowedHead(Connectable head); + + protected abstract void connect(IDiagramEditor editor); + + @Override + protected boolean begin(IDiagramEditor editor, DragEvent event) { + for(IElement element : event.pickedElements) { + Connectable connectable = element.getInterface(Connectable.class); + if(connectable != null && isAllowedTail(connectable)) { + System.out.println("allowed"); + this.from = connectable; + return true; + } + } + System.out.println("not allowed"); + return false; + } + + @Override + protected void update(IDiagramEditor editor, DragEvent event) { + for(IElement element : editor.pickElements(event.current)) { + Connectable connectable = element.getInterface(Connectable.class); + if(connectable != null && connectable != from) { + if(connectable == to) + return; + if(!isAllowedHead(connectable)) + continue; + to = connectable; + + Rectangle2D bounds = new Rectangle2D.Double(); + to.getBounds(bounds); + bounds.setFrame( + bounds.getX()-2.0, + bounds.getY()-2.0, + bounds.getWidth()+4.0, + bounds.getHeight()+4.0); + toNode.init(bounds); + + editor.requestRepaint(); + return; + } + } + to = null; + toNode.init(fromNode.getBounds()); + editor.requestRepaint(); + } + + @Override + protected void end(IDiagramEditor editor, DragEvent event) { + if(to != null) + connect(editor); + + from = null; + to = null; + } + + @Override + public void init(G2DParentNode parent) { + fromNode = parent.addNode(RectangleNode.class); + toNode = parent.addNode(RectangleNode.class); + + Rectangle2D bounds = new Rectangle2D.Double(); + from.getBounds(bounds); + bounds.setFrame( + bounds.getX()-2.0, + bounds.getY()-2.0, + bounds.getWidth()+4.0, + bounds.getHeight()+4.0); + + fromNode.init(bounds); + toNode.init(bounds); + } + + @Override + public void remove() { + fromNode.remove(); + toNode.remove(); + } + +} 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 index 00000000..765ff397 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/actions/ConnectDependency.java @@ -0,0 +1,36 @@ +package org.simantics.sysdyn.ui.actions; + +import org.simantics.h2d.editor.IDiagramEditor; +import org.simantics.h2d.element.handler.Connectable; +import org.simantics.sysdyn.representation.Auxiliary; +import org.simantics.sysdyn.representation.Stock; +import org.simantics.sysdyn.representation.Valve; +import org.simantics.sysdyn.ui.elements.AuxiliaryElement; +import org.simantics.sysdyn.ui.elements.DependencyElement; +import org.simantics.sysdyn.ui.elements.StockElement; +import org.simantics.sysdyn.ui.elements.ValveElement; + +public class ConnectDependency extends ConnectBase { + + @Override + protected boolean isAllowedTail(Connectable tail) { + return tail instanceof AuxiliaryElement + || tail instanceof StockElement + || tail instanceof ValveElement + ; + } + + @Override + protected boolean isAllowedHead(Connectable head) { + return head instanceof AuxiliaryElement + || head instanceof ValveElement + ; + } + + @Override + protected void connect(IDiagramEditor editor) { + editor.getDiagram().addElement(new DependencyElement(from, to)); + } + + +} 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 index 00000000..cca8cdec --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/actions/ConnectFlow.java @@ -0,0 +1,53 @@ +package org.simantics.sysdyn.ui.actions; + +import java.awt.geom.Point2D; + +import org.simantics.h2d.editor.IDiagramEditor; +import org.simantics.h2d.element.handler.Connectable; +import org.simantics.sysdyn.representation.Cloud; +import org.simantics.sysdyn.representation.Stock; +import org.simantics.sysdyn.representation.Valve; +import org.simantics.sysdyn.ui.elements.CloudElement; +import org.simantics.sysdyn.ui.elements.FlowElement; +import org.simantics.sysdyn.ui.elements.StockElement; +import org.simantics.sysdyn.ui.elements.ValveElement; + +public class ConnectFlow extends ConnectBase { + + @Override + protected boolean isAllowedTail(Connectable tail) { + return tail instanceof CloudElement + || tail instanceof StockElement + || tail instanceof ValveElement + ; + } + + @Override + protected boolean isAllowedHead(Connectable head) { + return head instanceof CloudElement + || head instanceof StockElement + || (head instanceof ValveElement && !(from instanceof ValveElement)) + ; + } + + @Override + protected void connect(IDiagramEditor editor) { + if(from instanceof Valve || to instanceof Valve) + editor.getDiagram().addElement(new FlowElement(from, to)); + else { + Point2D fromOrigo = from.getOrigo(); + Point2D toOrigo = to.getOrigo(); + ValveElement valveElement = new ValveElement( + 0.5 * (fromOrigo.getX() + toOrigo.getX()), + 0.5 * (fromOrigo.getY() + toOrigo.getY()) + ); + editor.getDiagram().addElement(valveElement); + editor.getDiagram().addElement(new FlowElement(from, valveElement)); + editor.getDiagram().addElement(new FlowElement(valveElement, to)); + + valveElement.beginRenameAction(editor); + } + } + + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SysdynDiagramEditor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SysdynDiagramEditor.java index 006093c5..ef60abab 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SysdynDiagramEditor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SysdynDiagramEditor.java @@ -32,7 +32,8 @@ import org.simantics.objmap.IMapping; import org.simantics.objmap.IMappingListener; import org.simantics.objmap.MappingException; import org.simantics.objmap.Mappings; -import org.simantics.sysdyn.ui.actions.Connect; +import org.simantics.sysdyn.ui.actions.ConnectDependency; +import org.simantics.sysdyn.ui.actions.ConnectFlow; import org.simantics.sysdyn.ui.actions.CreateAuxiliary; import org.simantics.sysdyn.ui.actions.CreateCloud; import org.simantics.sysdyn.ui.actions.CreateStock; @@ -164,7 +165,8 @@ public class SysdynDiagramEditor extends ResourceEditorPart { editor.addEventHandler(1, "key(S)", new CreateStock()); editor.addEventHandler(1, "key(V)", new CreateValve()); editor.addEventHandler(1, "key(C)", new CreateCloud()); - editor.addEventHandler(1, "drag(alt+left)", new Connect()); + editor.addEventHandler(1, "drag(alt+left)", new ConnectDependency()); + editor.addEventHandler(1, "drag(alt+right)", new ConnectFlow()); canvas = new EditorCanvas(editor); frame.add(canvas); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/SysdynSchema.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/SysdynSchema.java index eccb7c9d..58be1de4 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/SysdynSchema.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/SysdynSchema.java @@ -16,6 +16,7 @@ public class SysdynSchema extends SimpleSchema { addLinkType(MappingSchemas.fromAnnotations(g, Flow.class)); addLinkType(MappingSchemas.fromAnnotations(g, Stock.class)); addLinkType(MappingSchemas.fromAnnotations(g, Valve.class)); + addLinkType(MappingSchemas.fromAnnotations(g, NormalExpression.class)); } catch (DatabaseException e) { // TODO Auto-generated catch block e.printStackTrace();