1 package org.simantics.district.network.ui.nodes;
3 import java.awt.BasicStroke;
5 import java.awt.Graphics2D;
6 import java.awt.Stroke;
7 import java.awt.geom.Path2D;
8 import java.awt.geom.Point2D;
9 import java.awt.geom.Rectangle2D;
10 import java.util.ArrayList;
11 import java.util.Iterator;
12 import java.util.List;
14 import org.simantics.Simantics;
15 import org.simantics.db.Resource;
16 import org.simantics.db.WriteGraph;
17 import org.simantics.db.common.request.WriteRequest;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.diagram.ui.DiagramModelHints;
20 import org.simantics.district.network.ui.DNEdgeBuilder;
21 import org.simantics.district.network.ui.NetworkDrawingParticipant;
22 import org.simantics.g2d.canvas.Hints;
23 import org.simantics.g2d.canvas.IToolMode;
24 import org.simantics.g2d.diagram.IDiagram;
25 import org.simantics.scenegraph.g2d.G2DNode;
26 import org.simantics.scenegraph.g2d.events.EventTypes;
27 import org.simantics.scenegraph.g2d.events.KeyEvent.KeyPressedEvent;
28 import org.simantics.scenegraph.g2d.events.MouseEvent;
29 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseClickEvent;
30 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDoubleClickedEvent;
31 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseMovedEvent;
32 import org.simantics.scenegraph.utils.GeometryUtils;
33 import org.simantics.scenegraph.utils.NodeUtil;
35 public class NetworkDrawingNode extends G2DNode {
37 private static final long serialVersionUID = -3475301184009620573L;
39 private Point2D currentMousePos = null;
41 private List<Point2D> nodes = new ArrayList<>();
43 private Resource diagramResource;
45 private boolean committed;
47 private NetworkDrawingParticipant participant;
49 private IDiagram diagram;
51 private static final Stroke DASHED_STROKE = new BasicStroke(2.0f,
52 BasicStroke.CAP_ROUND,
53 BasicStroke.JOIN_ROUND,
54 4.0f, new float[]{4.0f}, 0.0f);
56 private static final Color BLUE_ALPHA = new Color(0, 0, 255, 100);
58 private boolean scaleStroke = true;
63 addEventHandler(this);
66 public void setNetworkDrawingParticipant(NetworkDrawingParticipant participant) {
67 this.participant = participant;
70 public void setDiagram(IDiagram diagram) {
71 if (diagram != null) {
72 this.diagram = diagram;
73 this.diagramResource = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);
78 public void render(Graphics2D g2d) {
82 Path2D path = new Path2D.Double();
83 Iterator<Point2D> nodeIter = nodes.iterator();
84 if (nodeIter.hasNext()) {
85 Point2D node = nodeIter.next();
86 path.moveTo(node.getX(), node.getY());
88 while (nodeIter.hasNext()) {
89 Point2D node = nodeIter.next();
90 path.lineTo(node.getX(), node.getY());
92 if (currentMousePos != null)
93 path.lineTo(currentMousePos.getX(), currentMousePos.getY());
95 Color old = g2d.getColor();
96 Stroke oldStroke = g2d.getStroke();
98 if (DASHED_STROKE != null) {
99 if (scaleStroke && DASHED_STROKE instanceof BasicStroke) {
100 BasicStroke bs = GeometryUtils.scaleStroke(DASHED_STROKE, (float) (1.0 / GeometryUtils.getScale(g2d.getTransform())));
103 g2d.setStroke(DASHED_STROKE);
107 g2d.setColor(BLUE_ALPHA);
111 g2d.setStroke(oldStroke);
116 public Rectangle2D getBoundsInLocal() {
121 public int getEventMask() {
122 return EventTypes.AnyMask;
126 protected boolean mouseDoubleClicked(MouseDoubleClickedEvent e) {
128 IToolMode mode = getToolMode();
129 if (mode == Hints.CONNECTTOOL || e.hasAnyModifier(MouseEvent.ALT_MASK | MouseEvent.ALT_GRAPH_MASK)) {
130 Point2D start = null;
132 Iterator<Point2D> nodeIter = nodes.iterator();
133 while (nodeIter.hasNext()) {
135 start = nodeIter.next();
139 end = nodeIter.next();
141 createEdge(start, end);
151 return super.mouseDoubleClicked(e);
154 private void createEdge(Point2D start, Point2D end) {
156 double padding = GeometryUtils.getScale(getTransform());
158 double[] startCoords = new double[] { start.getX(), start.getY() };
159 double[] endCoords = new double[] { end.getX(), end.getY() };
161 DNEdgeBuilder builder = new DNEdgeBuilder(diagramResource, diagram);
162 Simantics.getSession().asyncRequest(new WriteRequest() {
165 public void perform(WriteGraph graph) throws DatabaseException {
166 builder.create(graph, startCoords, endCoords, padding);
173 protected boolean mouseClicked(MouseClickEvent e) {
175 IToolMode mode = getToolMode();
176 if (mode == Hints.CONNECTTOOL || e.hasAnyModifier(MouseEvent.ALT_MASK | MouseEvent.ALT_GRAPH_MASK)) {
181 if (e.button == MouseEvent.RIGHT_BUTTON && !nodes.isEmpty()) {
182 nodes.remove(nodes.size() - 1);
183 } else if (e.button == MouseEvent.LEFT_BUTTON) {
184 Point2D localPos = NodeUtil.worldToLocal(this, e.controlPosition, new Point2D.Double());
185 nodes.add(new Point2D.Double(localPos.getX(), localPos.getY()));
190 return super.mouseClicked(e);
193 private IToolMode getToolMode() {
194 return participant.getHint(Hints.KEY_TOOL);
198 protected boolean mouseMoved(MouseMovedEvent e) {
199 IToolMode mode = getToolMode();
200 if (mode == Hints.CONNECTTOOL || e.hasAnyModifier(MouseEvent.ALT_MASK | MouseEvent.ALT_GRAPH_MASK)) {
201 boolean repaint = false;
202 Point2D p = NodeUtil.worldToLocal(this, e.controlPosition, new Point2D.Double());
203 if (participant.pickHoveredElement(p)) {
206 if (!nodes.isEmpty()) {
212 currentMousePos = null;
216 return super.mouseMoved(e);
220 protected boolean keyPressed(KeyPressedEvent e) {
221 if (e.keyCode == java.awt.event.KeyEvent.VK_ESCAPE) {
226 return super.keyPressed(e);