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.AffineTransform;
8 import java.awt.geom.Path2D;
9 import java.awt.geom.Point2D;
10 import java.awt.geom.Rectangle2D;
11 import java.util.ArrayList;
12 import java.util.Iterator;
13 import java.util.List;
14 import java.util.concurrent.TimeUnit;
16 import org.simantics.Simantics;
17 import org.simantics.db.Resource;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.common.request.WriteRequest;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.request.Write;
22 import org.simantics.diagram.elements.DiagramNodeUtil;
23 import org.simantics.diagram.ui.DiagramModelHints;
24 import org.simantics.district.network.DistrictNetworkUtil;
25 import org.simantics.district.network.ModelledCRS;
26 import org.simantics.district.network.ontology.DistrictNetworkResource;
27 import org.simantics.district.network.ui.DNEdgeBuilder;
28 import org.simantics.district.network.ui.NetworkDrawingParticipant;
29 import org.simantics.g2d.canvas.Hints;
30 import org.simantics.g2d.canvas.ICanvasContext;
31 import org.simantics.g2d.canvas.IToolMode;
32 import org.simantics.g2d.diagram.IDiagram;
33 import org.simantics.scenegraph.g2d.G2DNode;
34 import org.simantics.scenegraph.g2d.events.EventTypes;
35 import org.simantics.scenegraph.g2d.events.KeyEvent.KeyPressedEvent;
36 import org.simantics.scenegraph.g2d.events.MouseEvent;
37 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseClickEvent;
38 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDoubleClickedEvent;
39 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseMovedEvent;
40 import org.simantics.scenegraph.utils.GeometryUtils;
41 import org.simantics.scenegraph.utils.NodeUtil;
42 import org.simantics.utils.threads.ThreadUtils;
44 public class NetworkDrawingNode extends G2DNode {
46 static class DrawingNode {
48 private List<Point2D> routeNodes = new ArrayList<>();
51 private static final long serialVersionUID = -3475301184009620573L;
53 private Point2D currentMousePos = null;
55 private List<DrawingNode> nodes = new ArrayList<>();
56 private DrawingNode currentRouteNode = null;
58 private Resource diagramResource;
60 private NetworkDrawingParticipant participant;
62 private IDiagram diagram;
64 private static final Stroke DASHED_STROKE = new BasicStroke(2.0f,
65 BasicStroke.CAP_ROUND,
66 BasicStroke.JOIN_ROUND,
67 4.0f, new float[]{4.0f}, 0.0f);
69 private static final Color BLUE_ALPHA = new Color(0, 0, 255, 100);
70 private static final Color RED_ALPHA = new Color(255, 0, 0, 100);
72 private boolean scaleStroke = true;
77 addEventHandler(this);
80 public void setNetworkDrawingParticipant(NetworkDrawingParticipant participant) {
81 this.participant = participant;
84 public void setDiagram(IDiagram diagram) {
85 if (diagram != null) {
86 this.diagram = diagram;
87 this.diagramResource = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);
92 public void render(Graphics2D g2d) {
96 Color old = g2d.getColor();
97 Stroke oldStroke = g2d.getStroke();
99 Iterator<DrawingNode> dnodeIterator = nodes.iterator();
100 while (dnodeIterator.hasNext()) {
101 Path2D path = new Path2D.Double();
102 DrawingNode dnode = dnodeIterator.next();
103 Iterator<Point2D> nodeIter = dnode.routeNodes.iterator();
104 if (nodeIter.hasNext()) {
105 Point2D node = nodeIter.next();
106 path.moveTo(node.getX(), node.getY());
108 while (nodeIter.hasNext()) {
109 Point2D node = nodeIter.next();
110 path.lineTo(node.getX(), node.getY());
112 if (!dnodeIterator.hasNext()) {
113 if (currentMousePos != null)
114 path.lineTo(currentMousePos.getX(), currentMousePos.getY());
117 if (DASHED_STROKE != null) {
118 if (scaleStroke && DASHED_STROKE instanceof BasicStroke) {
119 BasicStroke bs = GeometryUtils.scaleStroke(DASHED_STROKE, (float) (1.0 / GeometryUtils.getScale(g2d.getTransform())));
122 g2d.setStroke(DASHED_STROKE);
126 g2d.setColor(BLUE_ALPHA);
129 g2d.setColor(RED_ALPHA);
130 BasicStroke stroke = GeometryUtils.scaleStroke(DASHED_STROKE, (float) (1.0 / GeometryUtils.getScale(g2d.getTransform())));
131 g2d.setStroke(stroke);
132 Point2D currentPoint = path.getCurrentPoint();
133 g2d.draw(new Rectangle2D.Double(currentPoint.getX() - 0.0001 / 2, currentPoint.getY() - 0.0001 / 2, 0.0001, 0.0001));
136 g2d.setStroke(oldStroke);
141 public Rectangle2D getBoundsInLocal() {
146 public int getEventMask() {
147 return EventTypes.AnyMask;
151 protected boolean mouseDoubleClicked(MouseDoubleClickedEvent e) {
153 IToolMode mode = getToolMode();
154 if (mode == Hints.CONNECTTOOL || e.hasAnyModifier(MouseEvent.ALT_MASK | MouseEvent.ALT_GRAPH_MASK)) {
155 // ok, new routenode starts from here
156 Point2D localPos = NodeUtil.worldToLocal(this, e.controlPosition, new Point2D.Double());
157 Point2D.Double pos = new Point2D.Double(localPos.getX(), localPos.getY());
158 if (currentRouteNode != null) {
159 //currentRouteNode.routeNodes.add(pos);
160 currentRouteNode = new DrawingNode();
161 currentRouteNode.routeNodes.add(pos);
162 nodes.add(currentRouteNode);
164 // ok, this must be creation of dh_point
165 double scale = getTransform().getScaleY();
166 double x = ModelledCRS.xToLongitude(pos.getX() / scale);
167 double y = ModelledCRS.yToLatitude(-pos.getY() / scale);
169 boolean leftButton = e.button == MouseEvent.LEFT_BUTTON;
171 ThreadUtils.getNonBlockingWorkExecutor().schedule(() -> {
172 Simantics.getSession().asyncRequest(new Write() {
175 public void perform(WriteGraph graph) throws DatabaseException {
176 graph.markUndoPoint();
177 Resource mapping = null;
179 mapping = graph.getSingleObject(diagramResource, DistrictNetworkResource.getInstance(graph).LeftClickDefaultMapping);
181 mapping = graph.getSingleObject(diagramResource, DistrictNetworkResource.getInstance(graph).RightClickDefaultMapping);
183 if (mapping == null) {
184 mapping = graph.getSingleObject(diagramResource, DistrictNetworkResource.getInstance(graph).VertexDefaultMapping);
186 DistrictNetworkUtil.createVertex(graph, diagramResource, new double[] { x, y }, 0, mapping); // TODO: elevation can be fetched from e.g. elevation API
189 }, 100, TimeUnit.MILLISECONDS);
194 return super.mouseDoubleClicked(e);
197 private void createEdge(DrawingNode node) {
199 Point2D start = node.routeNodes.get(0);
200 Point2D end = node.routeNodes.get(node.routeNodes.size() - 1);
202 double currentPadding = DistrictNetworkVertexNode.width;
203 AffineTransform test = getTransform();
204 ICanvasContext ctx = DiagramNodeUtil.getCanvasContext(this);
205 AffineTransform tr = ctx.getHintStack().getHint(Hints.KEY_CANVAS_TRANSFORM);
206 AffineTransform testing = new AffineTransform(tr);
207 testing.concatenate(test);
208 double calculateScaleRecip = DistrictNetworkNodeUtils.calculateScaleRecip(testing);
209 double padding = currentPadding * calculateScaleRecip;
211 * To convert y-coordinates to map coordinates in ruler, use:
212 * double val = (y-offsetY)/scaleY;
213 * val = Math.toDegrees(Math.atan(Math.sinh(Math.toRadians(val))));
214 * String str = formatValue(val);
217 double scaleY = getTransform().getScaleY();
218 double scaleX = getTransform().getScaleX();
220 double startLat = ModelledCRS.yToLatitude(-start.getY() / scaleY);
221 double startLon = ModelledCRS.xToLongitude(start.getX() / scaleX);
223 double endLat = ModelledCRS.yToLatitude(-end.getY() / scaleY);
224 double endLon = ModelledCRS.xToLongitude(end.getX() / scaleX);
226 double[] startCoords = new double[] { startLon, startLat };
227 double[] endCoords = new double[] { endLon, endLat };
229 double[] detailedGeometryCoords = new double[node.routeNodes.size() * 2];
231 for (Point2D p : node.routeNodes) {
232 double lat = ModelledCRS.yToLatitude(-p.getY() / scaleY);
233 double lon = ModelledCRS.xToLongitude(p.getX() / scaleX);
234 detailedGeometryCoords[i++] = lon;
235 detailedGeometryCoords[i++] = lat;
238 DNEdgeBuilder builder = new DNEdgeBuilder(diagramResource, diagram);
239 Simantics.getSession().asyncRequest(new WriteRequest() {
242 public void perform(WriteGraph graph) throws DatabaseException {
243 builder.create(graph, startCoords, 0, endCoords, 0, detailedGeometryCoords, padding);
250 protected boolean mouseClicked(MouseClickEvent e) {
252 IToolMode mode = getToolMode();
253 if (mode == Hints.CONNECTTOOL || e.hasAnyModifier(MouseEvent.ALT_MASK | MouseEvent.ALT_GRAPH_MASK)) {
254 if (e.button == MouseEvent.RIGHT_BUTTON && !nodes.isEmpty()) {
255 nodes.remove(nodes.size() - 1);
256 } else if (e.button == MouseEvent.LEFT_BUTTON) {
257 Point2D localPos = NodeUtil.worldToLocal(this, e.controlPosition, new Point2D.Double());
258 if (currentRouteNode == null && canStartEdge(localPos)) {
259 // ok, we can start from here
260 currentRouteNode = new DrawingNode();
261 currentRouteNode.routeNodes.add(new Point2D.Double(localPos.getX(), localPos.getY()));
262 nodes.add(currentRouteNode);
263 } else if (currentRouteNode != null && canStartEdge(localPos)) {
264 // let's commit our new routenode
265 currentRouteNode.routeNodes.add(new Point2D.Double(localPos.getX(), localPos.getY()));
266 Iterator<DrawingNode> nodeIter = nodes.iterator();
267 while (nodeIter.hasNext()) {
268 createEdge(nodeIter.next());
270 currentRouteNode = null;
272 } else if (currentRouteNode != null) {
273 currentRouteNode.routeNodes.add(new Point2D.Double(localPos.getX(), localPos.getY()));
279 return super.mouseClicked(e);
282 private boolean canStartEdge(Point2D currentPos) {
283 return participant.isHoveringOverNode(currentPos);
286 private IToolMode getToolMode() {
287 return participant.getHint(Hints.KEY_TOOL);
291 protected boolean mouseMoved(MouseMovedEvent e) {
292 IToolMode mode = getToolMode();
293 boolean repaint = false;
294 Point2D p = NodeUtil.worldToLocal(this, e.controlPosition, new Point2D.Double());
295 boolean isConnectionTool = mode == Hints.CONNECTTOOL || e.hasAnyModifier(MouseEvent.ALT_MASK | MouseEvent.ALT_GRAPH_MASK);
296 if (participant.pickHoveredElement(p, isConnectionTool)) {
299 if (!nodes.isEmpty()) {
305 currentMousePos = null;
308 return super.mouseMoved(e);
312 protected boolean keyPressed(KeyPressedEvent e) {
313 if (e.keyCode == java.awt.event.KeyEvent.VK_ESCAPE) {
314 currentRouteNode = null;
319 return super.keyPressed(e);