]> gerrit.simantics Code Review - simantics/district.git/blob
524bf7221e42133f76b0644130760af8fd43842a
[simantics/district.git] /
1 package org.simantics.district.network.ui.nodes;
2
3 import java.awt.BasicStroke;
4 import java.awt.Color;
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
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;
41
42 public class NetworkDrawingNode extends G2DNode {
43
44     static class DrawingNode {
45
46         private List<Point2D> routeNodes = new ArrayList<>();
47     }
48
49     private static final long serialVersionUID = -3475301184009620573L;
50     
51     private Point2D currentMousePos = null;
52
53     private List<DrawingNode> nodes = new ArrayList<>();
54     private DrawingNode currentRouteNode = null;
55
56     private Resource diagramResource;
57
58     private boolean committed;
59
60     private NetworkDrawingParticipant participant;
61
62     private IDiagram diagram;
63
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);
68
69     private static final Color BLUE_ALPHA = new Color(0, 0, 255, 100);
70
71     private boolean scaleStroke = true;
72     
73     @Override
74     public void init() {
75         super.init();
76         addEventHandler(this);
77     }
78     
79     public void setNetworkDrawingParticipant(NetworkDrawingParticipant participant) {
80         this.participant = participant;
81     }
82     
83     public void setDiagram(IDiagram diagram) {
84         if (diagram != null) {
85             this.diagram = diagram;
86             this.diagramResource = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);
87         }
88     }
89
90     @Override
91     public void render(Graphics2D g2d) {
92         if (nodes.isEmpty())
93             return;
94
95         Color old = g2d.getColor();
96         Stroke oldStroke = g2d.getStroke();
97
98         Iterator<DrawingNode> dnodeIterator = nodes.iterator();
99         while (dnodeIterator.hasNext()) {
100             Path2D path = new Path2D.Double();
101             DrawingNode dnode = dnodeIterator.next();
102             Iterator<Point2D> nodeIter = dnode.routeNodes.iterator();
103             if (nodeIter.hasNext()) {
104                 Point2D node = nodeIter.next();
105                 path.moveTo(node.getX(), node.getY());
106             }
107             while (nodeIter.hasNext()) {
108                 Point2D node = nodeIter.next();
109                 path.lineTo(node.getX(), node.getY());
110             }
111             if (!dnodeIterator.hasNext()) {
112                 if (currentMousePos != null)
113                     path.lineTo(currentMousePos.getX(), currentMousePos.getY());
114             }
115             
116             if (DASHED_STROKE != null) {
117                 if (scaleStroke && DASHED_STROKE instanceof BasicStroke) {
118                     BasicStroke bs = GeometryUtils.scaleStroke(DASHED_STROKE, (float) (1.0 / GeometryUtils.getScale(g2d.getTransform())));
119                     g2d.setStroke(bs);
120                 } else {
121                     g2d.setStroke(DASHED_STROKE);
122                 }
123             }
124             
125             g2d.setColor(BLUE_ALPHA);
126
127             g2d.draw(path);
128         }
129         
130         g2d.setStroke(oldStroke);
131         g2d.setColor(old);
132     }
133
134     @Override
135     public Rectangle2D getBoundsInLocal() {
136         return null;
137     }
138     
139     @Override
140     public int getEventMask() {
141         return EventTypes.AnyMask;
142     }
143     
144     @Override
145     protected boolean mouseDoubleClicked(MouseDoubleClickedEvent e) {
146         // nodes to path2d
147         IToolMode mode = getToolMode();
148         if (mode == Hints.CONNECTTOOL || e.hasAnyModifier(MouseEvent.ALT_MASK | MouseEvent.ALT_GRAPH_MASK)) {
149             // ok, new routenode starts from here
150             Point2D localPos = NodeUtil.worldToLocal(this, e.controlPosition, new Point2D.Double());
151             Point2D.Double pos = new Point2D.Double(localPos.getX(), localPos.getY());
152             if (currentRouteNode != null) {
153                 //currentRouteNode.routeNodes.add(pos);
154                 currentRouteNode = new DrawingNode();
155                 currentRouteNode.routeNodes.add(pos);
156                 nodes.add(currentRouteNode);
157             } else {
158                 // ok, this must be creation of dh_point
159                 double scale = getTransform().getScaleY();
160                 double x = ModelledCRS.xToLongitude(pos.getX() / scale);
161                 double y = ModelledCRS.yToLatitude(-pos.getY() / scale);
162                 Simantics.getSession().asyncRequest(new Write() {
163                     
164                     @Override
165                     public void perform(WriteGraph graph) throws DatabaseException {
166                         graph.markUndoPoint();
167                         Resource defaultMapping = graph.getSingleObject(diagramResource, DistrictNetworkResource.getInstance(graph).VertexDefaultMapping);
168                         DistrictNetworkUtil.createVertex(graph, diagramResource, new double[] { x, y }, 0, defaultMapping); // TODO: elevation can be fetched from e.g. elevation API
169                     }
170                 });
171             }
172             repaint();
173             return true;
174         }
175         return super.mouseDoubleClicked(e);
176     }
177
178     private void createEdge(DrawingNode node) {
179         
180         Point2D start = node.routeNodes.get(0);
181         Point2D end = node.routeNodes.get(node.routeNodes.size() - 1);
182         
183         double currentPadding = DistrictNetworkVertexNode.width;
184         AffineTransform test = getTransform();
185         ICanvasContext ctx = DiagramNodeUtil.getCanvasContext(this);
186         AffineTransform tr = ctx.getHintStack().getHint(Hints.KEY_CANVAS_TRANSFORM);
187         AffineTransform testing = new AffineTransform(tr); 
188         testing.concatenate(test);
189         double calculateScaleRecip = DistrictNetworkNodeUtils.calculateScaleRecip(testing);
190         double padding = currentPadding * calculateScaleRecip;
191         /*
192          *  To convert y-coordinates to map coordinates in ruler, use:
193          *    double val = (y-offsetY)/scaleY;
194          *    val = Math.toDegrees(Math.atan(Math.sinh(Math.toRadians(val))));
195          *    String str = formatValue(val);
196          */
197         // TODO: fix scale
198         double scaleY = getTransform().getScaleY();
199         double scaleX = getTransform().getScaleX();
200         
201         double startLat = ModelledCRS.yToLatitude(-start.getY() / scaleY);
202         double startLon = ModelledCRS.xToLongitude(start.getX() / scaleX);
203         
204         double endLat = ModelledCRS.yToLatitude(-end.getY() / scaleY);
205         double endLon = ModelledCRS.xToLongitude(end.getX() / scaleX);
206         
207         double[] startCoords = new double[] { startLon, startLat };
208         double[] endCoords = new double[] { endLon, endLat };
209         
210         double[] detailedGeometryCoords = new double[node.routeNodes.size() * 2];
211         int i = 0;
212         for (Point2D p : node.routeNodes) {
213             double lat = ModelledCRS.yToLatitude(-p.getY() / scaleY);
214             double lon = ModelledCRS.xToLongitude(p.getX() / scaleX);
215             detailedGeometryCoords[i++] = lon;
216             detailedGeometryCoords[i++] = lat;
217         }
218         
219         DNEdgeBuilder builder = new DNEdgeBuilder(diagramResource, diagram);
220         Simantics.getSession().asyncRequest(new WriteRequest() {
221             
222             @Override
223             public void perform(WriteGraph graph) throws DatabaseException {
224                 builder.create(graph, startCoords, 0, endCoords, 0, detailedGeometryCoords, padding);
225             }
226         });
227         
228     }
229
230     @Override
231     protected boolean mouseClicked(MouseClickEvent e) {
232         // check ToolMode
233         IToolMode mode = getToolMode();
234         if (mode == Hints.CONNECTTOOL || e.hasAnyModifier(MouseEvent.ALT_MASK | MouseEvent.ALT_GRAPH_MASK)) {
235             if (committed) {
236                 committed = false;
237                 return false;
238             }
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());
254                     }
255                     currentRouteNode = null;
256                     nodes.clear();
257                     committed = true;
258                 } else if (currentRouteNode != null) {
259                     currentRouteNode.routeNodes.add(new Point2D.Double(localPos.getX(), localPos.getY()));
260                 }
261             }
262             repaint();
263             return true;
264         }
265         return super.mouseClicked(e);
266     }
267
268     private boolean canStartEdge(Point2D currentPos) {
269         return participant.isHoveringOverNode(currentPos);
270     }
271
272     private IToolMode getToolMode() {
273         return participant.getHint(Hints.KEY_TOOL);
274     }
275
276     @Override
277     protected boolean mouseMoved(MouseMovedEvent e) {
278         IToolMode mode = getToolMode();
279         if (mode == Hints.CONNECTTOOL || e.hasAnyModifier(MouseEvent.ALT_MASK | MouseEvent.ALT_GRAPH_MASK)) {
280             boolean repaint = false;
281             Point2D p = NodeUtil.worldToLocal(this, e.controlPosition, new Point2D.Double());
282             if (participant.pickHoveredElement(p)) {
283                 repaint = true;
284             }
285             if (!nodes.isEmpty()) {
286                 currentMousePos = p;
287                 
288                 repaint();
289                 return true;
290             }
291             currentMousePos = null;
292             if (repaint == true)
293                 repaint();
294         }
295         return super.mouseMoved(e);
296     }
297     
298     @Override
299     protected boolean keyPressed(KeyPressedEvent e) {
300         if (e.keyCode == java.awt.event.KeyEvent.VK_ESCAPE) {
301             currentRouteNode = null;
302             nodes.clear();
303             repaint();
304             return true;
305         }
306         return super.keyPressed(e);
307             
308     }
309 }