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.ModelledCRS;
21 import org.simantics.district.network.ui.DNEdgeBuilder;
22 import org.simantics.district.network.ui.NetworkDrawingParticipant;
23 import org.simantics.g2d.canvas.Hints;
24 import org.simantics.g2d.canvas.IToolMode;
25 import org.simantics.g2d.diagram.IDiagram;
26 import org.simantics.scenegraph.g2d.G2DNode;
27 import org.simantics.scenegraph.g2d.events.EventTypes;
28 import org.simantics.scenegraph.g2d.events.KeyEvent.KeyPressedEvent;
29 import org.simantics.scenegraph.g2d.events.MouseEvent;
30 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseClickEvent;
31 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDoubleClickedEvent;
32 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseMovedEvent;
33 import org.simantics.scenegraph.utils.GeometryUtils;
34 import org.simantics.scenegraph.utils.NodeUtil;
36 public class NetworkDrawingNode extends G2DNode {
38 private static final long serialVersionUID = -3475301184009620573L;
40 private Point2D currentMousePos = null;
42 private List<Point2D> nodes = new ArrayList<>();
44 private Resource diagramResource;
46 private boolean committed;
48 private NetworkDrawingParticipant participant;
50 private IDiagram diagram;
52 private static final Stroke DASHED_STROKE = new BasicStroke(2.0f,
53 BasicStroke.CAP_ROUND,
54 BasicStroke.JOIN_ROUND,
55 4.0f, new float[]{4.0f}, 0.0f);
57 private static final Color BLUE_ALPHA = new Color(0, 0, 255, 100);
59 private boolean scaleStroke = true;
64 addEventHandler(this);
67 public void setNetworkDrawingParticipant(NetworkDrawingParticipant participant) {
68 this.participant = participant;
71 public void setDiagram(IDiagram diagram) {
72 if (diagram != null) {
73 this.diagram = diagram;
74 this.diagramResource = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);
79 public void render(Graphics2D g2d) {
83 Path2D path = new Path2D.Double();
84 Iterator<Point2D> nodeIter = nodes.iterator();
85 if (nodeIter.hasNext()) {
86 Point2D node = nodeIter.next();
87 path.moveTo(node.getX(), node.getY());
89 while (nodeIter.hasNext()) {
90 Point2D node = nodeIter.next();
91 path.lineTo(node.getX(), node.getY());
93 if (currentMousePos != null)
94 path.lineTo(currentMousePos.getX(), currentMousePos.getY());
96 Color old = g2d.getColor();
97 Stroke oldStroke = g2d.getStroke();
99 if (DASHED_STROKE != null) {
100 if (scaleStroke && DASHED_STROKE instanceof BasicStroke) {
101 BasicStroke bs = GeometryUtils.scaleStroke(DASHED_STROKE, (float) (1.0 / GeometryUtils.getScale(g2d.getTransform())));
104 g2d.setStroke(DASHED_STROKE);
108 g2d.setColor(BLUE_ALPHA);
112 g2d.setStroke(oldStroke);
117 public Rectangle2D getBoundsInLocal() {
122 public int getEventMask() {
123 return EventTypes.AnyMask;
127 protected boolean mouseDoubleClicked(MouseDoubleClickedEvent e) {
129 IToolMode mode = getToolMode();
130 if (mode == Hints.CONNECTTOOL || e.hasAnyModifier(MouseEvent.ALT_MASK | MouseEvent.ALT_GRAPH_MASK)) {
131 Point2D start = null;
133 Iterator<Point2D> nodeIter = nodes.iterator();
134 while (nodeIter.hasNext()) {
136 start = nodeIter.next();
137 if (!nodeIter.hasNext()) {
144 end = nodeIter.next();
146 createEdge(start, end);
156 return super.mouseDoubleClicked(e);
159 private void createEdge(Point2D start, Point2D end) {
161 double padding = GeometryUtils.getScale(getTransform());
164 * To convert y-coordinates to map coordinates in ruler, use:
165 * double val = (y-offsetY)/scaleY;
166 * val = Math.toDegrees(Math.atan(Math.sinh(Math.toRadians(val))));
167 * String str = formatValue(val);
170 double scaleY = getTransform().getScaleY();
171 double scaleX = getTransform().getScaleX();
173 double startLat = ModelledCRS.yToLatitude(-start.getY() / scaleY);
174 double startLon = ModelledCRS.xToLongitude(start.getX() / scaleX);
176 double endLat = ModelledCRS.yToLatitude(-end.getY() / scaleY);
177 double endLon = ModelledCRS.xToLongitude(end.getX() / scaleX);
179 double[] startCoords = new double[] { startLon, startLat };
180 double[] endCoords = new double[] { endLon, endLat };
182 DNEdgeBuilder builder = new DNEdgeBuilder(diagramResource, diagram);
183 Simantics.getSession().asyncRequest(new WriteRequest() {
186 public void perform(WriteGraph graph) throws DatabaseException {
187 builder.create(graph, startCoords, endCoords, 1 / padding);
194 protected boolean mouseClicked(MouseClickEvent e) {
196 IToolMode mode = getToolMode();
197 if (mode == Hints.CONNECTTOOL || e.hasAnyModifier(MouseEvent.ALT_MASK | MouseEvent.ALT_GRAPH_MASK)) {
202 if (e.button == MouseEvent.RIGHT_BUTTON && !nodes.isEmpty()) {
203 nodes.remove(nodes.size() - 1);
204 } else if (e.button == MouseEvent.LEFT_BUTTON) {
205 Point2D localPos = NodeUtil.worldToLocal(this, e.controlPosition, new Point2D.Double());
206 nodes.add(new Point2D.Double(localPos.getX(), localPos.getY()));
211 return super.mouseClicked(e);
214 private IToolMode getToolMode() {
215 return participant.getHint(Hints.KEY_TOOL);
219 protected boolean mouseMoved(MouseMovedEvent e) {
220 IToolMode mode = getToolMode();
221 if (mode == Hints.CONNECTTOOL || e.hasAnyModifier(MouseEvent.ALT_MASK | MouseEvent.ALT_GRAPH_MASK)) {
222 boolean repaint = false;
223 Point2D p = NodeUtil.worldToLocal(this, e.controlPosition, new Point2D.Double());
224 if (participant.pickHoveredElement(p)) {
227 if (!nodes.isEmpty()) {
233 currentMousePos = null;
237 return super.mouseMoved(e);
241 protected boolean keyPressed(KeyPressedEvent e) {
242 if (e.keyCode == java.awt.event.KeyEvent.VK_ESCAPE) {
247 return super.keyPressed(e);