]> gerrit.simantics Code Review - simantics/district.git/blob
9595f232debb6339459ff11203b5e2f56139bed4
[simantics/district.git] /
1 package org.simantics.district.network.ui.adapters;
2
3 import java.awt.Color;
4 import java.awt.Shape;
5 import java.awt.geom.AffineTransform;
6 import java.awt.geom.Line2D;
7 import java.awt.geom.Point2D;
8 import java.awt.geom.Rectangle2D;
9 import java.util.Collection;
10 import java.util.Collections;
11
12 import org.simantics.district.network.ModelledCRS;
13 import org.simantics.district.network.ui.DistrictNetworkEdge;
14 import org.simantics.district.network.ui.nodes.DistrictNetworkEdgeNode;
15 import org.simantics.g2d.connection.handler.ConnectionHandler;
16 import org.simantics.g2d.diagram.handler.PickRequest.PickPolicy;
17 import org.simantics.g2d.diagram.handler.Topology.Connection;
18 import org.simantics.g2d.element.ElementClass;
19 import org.simantics.g2d.element.ElementUtils;
20 import org.simantics.g2d.element.IElement;
21 import org.simantics.g2d.element.SceneGraphNodeKey;
22 import org.simantics.g2d.element.handler.InternalSize;
23 import org.simantics.g2d.element.handler.Outline;
24 import org.simantics.g2d.element.handler.Pick;
25 import org.simantics.g2d.element.handler.SceneGraph;
26 import org.simantics.g2d.element.handler.impl.ConnectionSelectionOutline;
27 import org.simantics.g2d.element.handler.impl.DefaultTransform;
28 import org.simantics.g2d.element.handler.impl.SimpleElementLayers;
29 import org.simantics.maps.MapScalingTransform;
30 import org.simantics.scenegraph.g2d.G2DParentNode;
31 import org.simantics.utils.datastructures.hints.IHintContext.Key;
32 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class DistrictNetworkEdgeElement {
37
38     public static final Key KEY_DN_EDGE = new KeyOf(DistrictNetworkEdge.class, "DN_EDGE");
39     public static final Key KEY_DN_EDGE_NODE = new SceneGraphNodeKey(DistrictNetworkEdgeNode.class, "DN_EDGE_NODE");
40
41     public static final ElementClass CLASS =
42             ElementClass.compile(
43                     DefaultTransform.INSTANCE,
44                     DNEdgeInternalSize.INSTANCE,
45                     DNEdgeSceneGraph.INSTANCE,
46                     DNEdgeConnectionHandler.INSTANCE,
47                     SimpleElementLayers.INSTANCE,
48                     // TODO: do we need this and does it work?
49                     ConnectionSelectionOutline.INSTANCE,
50                     DistrictNetworkAdditionalColor.INSTANCE
51             ).setId(DistrictNetworkEdgeElement.class.getSimpleName());
52
53     static final class DNEdgeSceneGraph implements SceneGraph {
54
55         public static final DNEdgeSceneGraph INSTANCE = new DNEdgeSceneGraph();
56
57         private static final long serialVersionUID = 68135568495835923L;
58
59         @Override
60         public void init(IElement edgeElement, G2DParentNode parent) {
61             DistrictNetworkEdge edge = edgeElement.getHint(KEY_DN_EDGE);
62             if (edge == null) {
63                 cleanup(edgeElement);
64             } else {
65                 DistrictNetworkEdgeNode node = edgeElement.getHint(KEY_DN_EDGE_NODE);
66                 if (node == null) {
67                     node = parent.addNode(ElementUtils.generateNodeId(edgeElement), DistrictNetworkEdgeNode.class);
68                     edgeElement.setHint(KEY_DN_EDGE_NODE, node);
69                 }
70                 
71                 node.setColor(ElementUtils.getAdditionalColor(edgeElement, Color.BLUE));
72                 
73                 node.setDNEdge(edge);
74                 AffineTransform at = ElementUtils.getTransform(edgeElement);
75                 if (at != null)
76                     node.setTransform(at);
77             }
78         }
79
80         @Override
81         public void cleanup(IElement edge) {
82             ElementUtils.removePossibleNode(edge, KEY_DN_EDGE_NODE);
83             edge.removeHint(KEY_DN_EDGE_NODE);
84         }
85     }
86
87     static final class DNEdgeInternalSize implements InternalSize, Outline, Pick {
88
89         private static final Logger LOGGER = LoggerFactory.getLogger(DNEdgeInternalSize.class);
90
91         private static final long serialVersionUID = -7346653820911240628L;
92
93         public static final DNEdgeInternalSize INSTANCE = new DNEdgeInternalSize();
94
95         @Override
96         public Rectangle2D getBounds(IElement e, Rectangle2D size) {
97             DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);
98             if (size == null)
99                 size = new Rectangle2D.Double();
100             if (edge != null)
101                 size.setFrame(DistrictNetworkEdgeNode.calculatePath(edge, null).getBounds2D());
102             else
103                 LOGGER.debug("Element {} does not have edge!", e);
104
105             return size;
106         }
107
108         @Override
109         public Shape getElementShape(IElement e) {
110             DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);
111             if (edge != null) {
112                 return DistrictNetworkEdgeNode.calculatePath(edge, null);
113             } else {
114                 return getBounds(e, null);
115             }
116         }
117
118         @Override
119         public boolean pickTest(IElement e, Shape s, PickPolicy policy) {
120             DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);
121             if (edge != null) {
122                 Rectangle2D bounds = getBounds(s);
123                 switch (policy) {
124                 case PICK_CONTAINED_OBJECTS:    return pickContainedObjects(edge, bounds);
125                 case PICK_INTERSECTING_OBJECTS: return pickIntersectingObjects(edge, bounds);
126                 }
127                 return false;
128             }
129
130             return false;
131         }
132
133         private boolean pickContainedObjects(DistrictNetworkEdge edge, Rectangle2D bounds) {
134             double bminx = bounds.getMinX() / MapScalingTransform.getScaleX();
135             double bminy = bounds.getMinY() / MapScalingTransform.getScaleY();
136             double bmaxx = bounds.getMaxX() / MapScalingTransform.getScaleX();
137             double bmaxy = bounds.getMaxY() / MapScalingTransform.getScaleY();
138
139             double bsminx = ModelledCRS.xToLongitude(bminx);
140             double bsminy = ModelledCRS.yToLatitude(-bminy); // Invert for Simantics diagram coordinate system
141             double bsmaxx = ModelledCRS.xToLongitude(bmaxx);
142             double bsmaxy = ModelledCRS.yToLatitude(-bmaxy); // Invert for Simantics diagram coordinate system
143
144             double boundsMinY = Math.min(bsminy, bsmaxy);
145             double boundsMaxY = Math.max(bsminy, bsmaxy);
146
147             Point2D start = edge.getStartPoint();
148             Point2D end = edge.getEndPoint();
149
150             double eminx = Math.min(start.getX(), end.getX());
151             double eminy = Math.min(start.getY(), end.getY());
152             double emaxx = Math.max(start.getX(), end.getX());
153             double emaxy = Math.max(start.getY(), end.getY());
154
155             return eminx >= bsminx && eminy >= boundsMinY && emaxx <= bsmaxx && emaxy <= boundsMaxY;
156         }
157
158         private boolean pickIntersectingObjects(DistrictNetworkEdge edge, Rectangle2D bounds) {
159             double tolerance = (bounds.getHeight() + bounds.getHeight()) * 0.25 / MapScalingTransform.getScaleX();
160             Line2D line = new Line2D.Double(edge.getStartPoint(), edge.getEndPoint());
161             double sx = bounds.getCenterX() / MapScalingTransform.getScaleX();
162             double sy = bounds.getCenterY() / MapScalingTransform.getScaleY();
163             double ssx = ModelledCRS.xToLongitude(sx);
164             double ssy = ModelledCRS.yToLatitude(-sy); // Invert for Simantics diagram coordinate system
165             double distSq = line.ptSegDistSq(ssx, ssy);
166 //            System.out.println("s: " + sx + ", " + sy);
167 //            System.out.println("ss: " + ssx + ", " + ssy);
168 //            System.out.println("p1: " + edge.getStartPoint());
169 //            System.out.println("p2: " + edge.getEndPoint());
170 //            System.out.println("line: " + "(" + line.getX1() + ", " + line.getY1() + ", " + line.getX2() + ", " + line.getY2() + ")");
171 //            System.out.println("distance from line is " + Math.sqrt(distSq) + " with tolerance " + tolerance);
172             return distSq <= tolerance * tolerance;
173         }
174
175         private Rectangle2D getBounds(Shape shape) {
176             if (shape instanceof Rectangle2D)
177                 return (Rectangle2D) shape;
178             return shape.getBounds2D();
179         }
180
181     }
182
183     static class DNEdgeConnectionHandler implements ConnectionHandler {
184
185         private static final long serialVersionUID = -6882671891381761687L;
186
187         public static final DNEdgeConnectionHandler INSTANCE = new DNEdgeConnectionHandler();
188
189         @Override
190         public Collection<IElement> getChildren(IElement connection, Collection<IElement> result) {
191             return Collections.emptyList();
192         }
193
194         @Override
195         public Collection<IElement> getBranchPoints(IElement connection, Collection<IElement> result) {
196             return Collections.emptyList();
197         }
198
199         @Override
200         public Collection<IElement> getSegments(IElement connection, Collection<IElement> result) {
201             return Collections.emptyList();
202         }
203
204         @Override
205         public Collection<Connection> getTerminalConnections(IElement connection, Collection<Connection> result) {
206             return Collections.emptyList();
207         }
208     }
209
210 }