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;
15 import org.simantics.Simantics;
16 import org.simantics.db.Resource;
17 import org.simantics.db.WriteGraph;
18 import org.simantics.db.common.request.WriteRequest;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.request.Write;
21 import org.simantics.diagram.elements.DiagramNodeUtil;
22 import org.simantics.diagram.ui.DiagramModelHints;
23 import org.simantics.district.network.DistrictNetworkUtil;
24 import org.simantics.district.network.ModelledCRS;
25 import org.simantics.district.network.ontology.DistrictNetworkResource;
26 import org.simantics.district.network.ui.DNEdgeBuilder;
27 import org.simantics.district.network.ui.NetworkDrawingParticipant;
28 import org.simantics.g2d.canvas.Hints;
29 import org.simantics.g2d.canvas.ICanvasContext;
30 import org.simantics.g2d.canvas.IToolMode;
31 import org.simantics.g2d.diagram.IDiagram;
32 import org.simantics.scenegraph.g2d.G2DNode;
33 import org.simantics.scenegraph.g2d.events.EventTypes;
34 import org.simantics.scenegraph.g2d.events.KeyEvent.KeyPressedEvent;
35 import org.simantics.scenegraph.g2d.events.MouseEvent;
36 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseClickEvent;
37 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDoubleClickedEvent;
38 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseMovedEvent;
39 import org.simantics.scenegraph.utils.GeometryUtils;
40 import org.simantics.scenegraph.utils.NodeUtil;
42 public class NetworkDrawingNode extends G2DNode {
44 static class DrawingNode {
46 private List<Point2D> routeNodes = new ArrayList<>();
49 private static final long serialVersionUID = -3475301184009620573L;
51 private Point2D currentMousePos = null;
53 private List<DrawingNode> nodes = new ArrayList<>();
54 private DrawingNode currentRouteNode = null;
56 private Resource diagramResource;
58 private NetworkDrawingParticipant participant;
60 private IDiagram diagram;
62 private static final Stroke DASHED_STROKE = new BasicStroke(2.0f,
63 BasicStroke.CAP_ROUND,
64 BasicStroke.JOIN_ROUND,
65 4.0f, new float[]{4.0f}, 0.0f);
67 private static final Color BLUE_ALPHA = new Color(0, 0, 255, 100);
68 private static final Color RED_ALPHA = new Color(255, 0, 0, 100);
70 private boolean scaleStroke = true;
75 addEventHandler(this);
78 public void setNetworkDrawingParticipant(NetworkDrawingParticipant participant) {
79 this.participant = participant;
82 public void setDiagram(IDiagram diagram) {
83 if (diagram != null) {
84 this.diagram = diagram;
85 this.diagramResource = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);
90 public void render(Graphics2D g2d) {
94 Color old = g2d.getColor();
95 Stroke oldStroke = g2d.getStroke();
97 Iterator<DrawingNode> dnodeIterator = nodes.iterator();
98 while (dnodeIterator.hasNext()) {
99 Path2D path = new Path2D.Double();
100 DrawingNode dnode = dnodeIterator.next();
101 Iterator<Point2D> nodeIter = dnode.routeNodes.iterator();
102 if (nodeIter.hasNext()) {
103 Point2D node = nodeIter.next();
104 path.moveTo(node.getX(), node.getY());
106 while (nodeIter.hasNext()) {
107 Point2D node = nodeIter.next();
108 path.lineTo(node.getX(), node.getY());
110 if (!dnodeIterator.hasNext()) {
111 if (currentMousePos != null)
112 path.lineTo(currentMousePos.getX(), currentMousePos.getY());
115 if (DASHED_STROKE != null) {
116 if (scaleStroke && DASHED_STROKE instanceof BasicStroke) {
117 BasicStroke bs = GeometryUtils.scaleStroke(DASHED_STROKE, (float) (1.0 / GeometryUtils.getScale(g2d.getTransform())));
120 g2d.setStroke(DASHED_STROKE);
124 g2d.setColor(BLUE_ALPHA);
127 g2d.setColor(RED_ALPHA);
128 BasicStroke stroke = GeometryUtils.scaleStroke(DASHED_STROKE, (float) (1.0 / GeometryUtils.getScale(g2d.getTransform())));
129 g2d.setStroke(stroke);
130 Point2D currentPoint = path.getCurrentPoint();
131 g2d.draw(new Rectangle2D.Double(currentPoint.getX() - 0.0001 / 2, currentPoint.getY() - 0.0001 / 2, 0.0001, 0.0001));
134 g2d.setStroke(oldStroke);
139 public Rectangle2D getBoundsInLocal() {
144 public int getEventMask() {
145 return EventTypes.AnyMask;
149 protected boolean mouseDoubleClicked(MouseDoubleClickedEvent e) {
151 IToolMode mode = getToolMode();
152 if (mode == Hints.CONNECTTOOL || e.hasAnyModifier(MouseEvent.ALT_MASK | MouseEvent.ALT_GRAPH_MASK)) {
153 // ok, new routenode starts from here
154 Point2D localPos = NodeUtil.worldToLocal(this, e.controlPosition, new Point2D.Double());
155 Point2D.Double pos = new Point2D.Double(localPos.getX(), localPos.getY());
156 if (currentRouteNode != null) {
157 //currentRouteNode.routeNodes.add(pos);
158 currentRouteNode = new DrawingNode();
159 currentRouteNode.routeNodes.add(pos);
160 nodes.add(currentRouteNode);
162 // ok, this must be creation of dh_point
163 double scale = getTransform().getScaleY();
164 double x = ModelledCRS.xToLongitude(pos.getX() / scale);
165 double y = ModelledCRS.yToLatitude(-pos.getY() / scale);
166 Simantics.getSession().asyncRequest(new Write() {
169 public void perform(WriteGraph graph) throws DatabaseException {
170 graph.markUndoPoint();
171 Resource defaultMapping = graph.getSingleObject(diagramResource, DistrictNetworkResource.getInstance(graph).VertexDefaultMapping);
172 DistrictNetworkUtil.createVertex(graph, diagramResource, new double[] { x, y }, 0, defaultMapping); // TODO: elevation can be fetched from e.g. elevation API
179 return super.mouseDoubleClicked(e);
182 private void createEdge(DrawingNode node) {
184 Point2D start = node.routeNodes.get(0);
185 Point2D end = node.routeNodes.get(node.routeNodes.size() - 1);
187 double currentPadding = DistrictNetworkVertexNode.width;
188 AffineTransform test = getTransform();
189 ICanvasContext ctx = DiagramNodeUtil.getCanvasContext(this);
190 AffineTransform tr = ctx.getHintStack().getHint(Hints.KEY_CANVAS_TRANSFORM);
191 AffineTransform testing = new AffineTransform(tr);
192 testing.concatenate(test);
193 double calculateScaleRecip = DistrictNetworkNodeUtils.calculateScaleRecip(testing);
194 double padding = currentPadding * calculateScaleRecip;
196 * To convert y-coordinates to map coordinates in ruler, use:
197 * double val = (y-offsetY)/scaleY;
198 * val = Math.toDegrees(Math.atan(Math.sinh(Math.toRadians(val))));
199 * String str = formatValue(val);
202 double scaleY = getTransform().getScaleY();
203 double scaleX = getTransform().getScaleX();
205 double startLat = ModelledCRS.yToLatitude(-start.getY() / scaleY);
206 double startLon = ModelledCRS.xToLongitude(start.getX() / scaleX);
208 double endLat = ModelledCRS.yToLatitude(-end.getY() / scaleY);
209 double endLon = ModelledCRS.xToLongitude(end.getX() / scaleX);
211 double[] startCoords = new double[] { startLon, startLat };
212 double[] endCoords = new double[] { endLon, endLat };
214 double[] detailedGeometryCoords = new double[node.routeNodes.size() * 2];
216 for (Point2D p : node.routeNodes) {
217 double lat = ModelledCRS.yToLatitude(-p.getY() / scaleY);
218 double lon = ModelledCRS.xToLongitude(p.getX() / scaleX);
219 detailedGeometryCoords[i++] = lon;
220 detailedGeometryCoords[i++] = lat;
223 DNEdgeBuilder builder = new DNEdgeBuilder(diagramResource, diagram);
224 Simantics.getSession().asyncRequest(new WriteRequest() {
227 public void perform(WriteGraph graph) throws DatabaseException {
228 builder.create(graph, startCoords, 0, endCoords, 0, detailedGeometryCoords, padding);
235 protected boolean mouseClicked(MouseClickEvent e) {
237 IToolMode mode = getToolMode();
238 if (mode == Hints.CONNECTTOOL || e.hasAnyModifier(MouseEvent.ALT_MASK | MouseEvent.ALT_GRAPH_MASK)) {
239 if (e.button == MouseEvent.RIGHT_BUTTON && !nodes.isEmpty()) {
240 nodes.remove(nodes.size() - 1);
241 } else if (e.button == MouseEvent.LEFT_BUTTON) {
242 Point2D localPos = NodeUtil.worldToLocal(this, e.controlPosition, new Point2D.Double());
243 if (currentRouteNode == null && canStartEdge(localPos)) {
244 // ok, we can start from here
245 currentRouteNode = new DrawingNode();
246 currentRouteNode.routeNodes.add(new Point2D.Double(localPos.getX(), localPos.getY()));
247 nodes.add(currentRouteNode);
248 } else if (currentRouteNode != null && canStartEdge(localPos)) {
249 // let's commit our new routenode
250 currentRouteNode.routeNodes.add(new Point2D.Double(localPos.getX(), localPos.getY()));
251 Iterator<DrawingNode> nodeIter = nodes.iterator();
252 while (nodeIter.hasNext()) {
253 createEdge(nodeIter.next());
255 currentRouteNode = null;
257 } else if (currentRouteNode != null) {
258 currentRouteNode.routeNodes.add(new Point2D.Double(localPos.getX(), localPos.getY()));
264 return super.mouseClicked(e);
267 private boolean canStartEdge(Point2D currentPos) {
268 return participant.isHoveringOverNode(currentPos);
271 private IToolMode getToolMode() {
272 return participant.getHint(Hints.KEY_TOOL);
276 protected boolean mouseMoved(MouseMovedEvent e) {
277 IToolMode mode = getToolMode();
278 boolean repaint = false;
279 Point2D p = NodeUtil.worldToLocal(this, e.controlPosition, new Point2D.Double());
280 boolean isConnectionTool = mode == Hints.CONNECTTOOL || e.hasAnyModifier(MouseEvent.ALT_MASK | MouseEvent.ALT_GRAPH_MASK);
281 if (participant.pickHoveredElement(p, isConnectionTool)) {
284 if (!nodes.isEmpty()) {
290 currentMousePos = null;
293 return super.mouseMoved(e);
297 protected boolean keyPressed(KeyPressedEvent e) {
298 if (e.keyCode == java.awt.event.KeyEvent.VK_ESCAPE) {
299 currentRouteNode = null;
304 return super.keyPressed(e);