1 package org.simantics.district.network.ui.nodes;
\r
3 import java.awt.Color;
\r
4 import java.awt.Graphics2D;
\r
5 import java.awt.geom.Path2D;
\r
6 import java.awt.geom.Point2D;
\r
7 import java.awt.geom.Rectangle2D;
\r
8 import java.util.ArrayList;
\r
9 import java.util.HashSet;
\r
10 import java.util.Iterator;
\r
11 import java.util.List;
\r
12 import java.util.Set;
\r
14 import org.simantics.Simantics;
\r
15 import org.simantics.db.Resource;
\r
16 import org.simantics.db.WriteGraph;
\r
17 import org.simantics.db.common.request.WriteRequest;
\r
18 import org.simantics.db.exception.DatabaseException;
\r
19 import org.simantics.diagram.ui.DiagramModelHints;
\r
20 import org.simantics.district.network.ui.DNEdgeBuilder;
\r
21 import org.simantics.district.network.ui.NetworkDrawingParticipant;
\r
22 import org.simantics.g2d.canvas.Hints;
\r
23 import org.simantics.g2d.canvas.IToolMode;
\r
24 import org.simantics.g2d.diagram.IDiagram;
\r
25 import org.simantics.scenegraph.g2d.G2DNode;
\r
26 import org.simantics.scenegraph.g2d.events.EventTypes;
\r
27 import org.simantics.scenegraph.g2d.events.MouseEvent;
\r
28 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseClickEvent;
\r
29 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDoubleClickedEvent;
\r
30 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseMovedEvent;
\r
31 import org.simantics.scenegraph.utils.NodeUtil;
\r
33 public class NetworkDrawingNode extends G2DNode {
\r
35 private static final long serialVersionUID = -3475301184009620573L;
\r
37 private List<Point2D> nodes = new ArrayList<>();
\r
39 private Rectangle2D rect = new Rectangle2D.Double(10, 10, 10, 10);
\r
41 private Set<Path2D> paths = new HashSet<>();
\r
43 private Resource diagramResource;
\r
45 private boolean committed;
\r
47 private NetworkDrawingParticipant participant;
\r
49 private IDiagram diagram;
\r
52 public void init() {
\r
54 addEventHandler(this);
\r
57 public void setNetworkDrawingParticipant(NetworkDrawingParticipant participant) {
\r
58 this.participant = participant;
\r
61 public void setDiagram(IDiagram diagram) {
\r
62 if (diagram != null) {
\r
63 this.diagram = diagram;
\r
64 this.diagramResource = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);
\r
69 public void render(Graphics2D g2d) {
\r
70 Color old = g2d.getColor();
\r
71 g2d.setColor(Color.BLUE);
\r
73 paths.forEach(p -> {
\r
81 public Rectangle2D getBoundsInLocal() {
\r
82 return rect.getBounds2D();
\r
86 public int getEventMask() {
\r
87 return EventTypes.MouseMask;
\r
91 protected boolean mouseDoubleClicked(MouseDoubleClickedEvent e) {
\r
93 IToolMode mode = getToolMode();
\r
94 if (mode == Hints.CONNECTTOOL || e.hasAnyModifier(MouseEvent.ALT_MASK | MouseEvent.ALT_GRAPH_MASK)) {
\r
95 Point2D start = null;
\r
97 Iterator<Point2D> nodeIter = nodes.iterator();
\r
98 while (nodeIter.hasNext()) {
\r
100 start = nodeIter.next();
\r
104 end = nodeIter.next();
\r
106 createEdge(start, end);
\r
116 return super.mouseDoubleClicked(e);
\r
119 private void createEdge(Point2D start, Point2D end) {
\r
120 double[] startCoords = new double[] { start.getX(), start.getY() };
\r
121 double[] endCoords = new double[] { end.getX(), end.getY() };
\r
123 DNEdgeBuilder builder = new DNEdgeBuilder(diagramResource, diagram);
\r
124 Simantics.getSession().asyncRequest(new WriteRequest() {
\r
127 public void perform(WriteGraph graph) throws DatabaseException {
\r
128 builder.create(graph, startCoords, endCoords);
\r
135 protected boolean mouseClicked(MouseClickEvent e) {
\r
137 IToolMode mode = getToolMode();
\r
138 if (mode == Hints.CONNECTTOOL || e.hasAnyModifier(MouseEvent.ALT_MASK | MouseEvent.ALT_GRAPH_MASK)) {
\r
143 Point2D localPos = NodeUtil.worldToLocal(this, e.controlPosition, new Point2D.Double());
\r
144 nodes.add(new Point2D.Double(localPos.getX(), localPos.getY()));
\r
146 return super.mouseClicked(e);
\r
149 private IToolMode getToolMode() {
\r
150 return participant.getHint(Hints.KEY_TOOL);
\r
154 protected boolean mouseMoved(MouseMovedEvent e) {
\r
155 return super.mouseMoved(e);
\r