1 package org.simantics.district.network.ui.adapters;
5 import java.awt.geom.AffineTransform;
6 import java.awt.geom.Line2D;
7 import java.awt.geom.Path2D;
8 import java.awt.geom.PathIterator;
9 import java.awt.geom.Point2D;
10 import java.awt.geom.Rectangle2D;
11 import java.util.Collection;
12 import java.util.Collections;
14 import org.simantics.district.network.ModelledCRS;
15 import org.simantics.district.network.ui.DistrictNetworkEdge;
16 import org.simantics.district.network.ui.nodes.DistrictNetworkEdgeNode;
17 import org.simantics.g2d.connection.handler.ConnectionHandler;
18 import org.simantics.g2d.diagram.handler.PickRequest.PickPolicy;
19 import org.simantics.g2d.diagram.handler.Topology.Connection;
20 import org.simantics.g2d.element.ElementClass;
21 import org.simantics.g2d.element.ElementUtils;
22 import org.simantics.g2d.element.IElement;
23 import org.simantics.g2d.element.SceneGraphNodeKey;
24 import org.simantics.g2d.element.handler.InternalSize;
25 import org.simantics.g2d.element.handler.Outline;
26 import org.simantics.g2d.element.handler.Pick;
27 import org.simantics.g2d.element.handler.SceneGraph;
28 import org.simantics.g2d.element.handler.impl.ConnectionSelectionOutline;
29 import org.simantics.g2d.element.handler.impl.DefaultTransform;
30 import org.simantics.g2d.element.handler.impl.SimpleElementLayers;
31 import org.simantics.maps.MapScalingTransform;
32 import org.simantics.scenegraph.g2d.G2DParentNode;
33 import org.simantics.scenegraph.g2d.nodes.SVGNode;
34 import org.simantics.scenegraph.utils.NodeUtil;
35 import org.simantics.utils.datastructures.hints.IHintContext.Key;
36 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class DistrictNetworkEdgeElement {
42 public static final Key KEY_DN_EDGE = new KeyOf(DistrictNetworkEdge.class, "DN_EDGE");
43 public static final Key KEY_DN_EDGE_NODE = new SceneGraphNodeKey(DistrictNetworkEdgeNode.class, "DN_EDGE_NODE");
44 public static final Key KEY_DN_EDGE_SYMBOL_NODE = new SceneGraphNodeKey(DistrictNetworkEdgeNode.class, "DN_EDGE_SYMBOL_NODE");
46 public static final ElementClass CLASS =
48 DefaultTransform.INSTANCE,
49 DNEdgeInternalSize.INSTANCE,
50 DNEdgeSceneGraph.INSTANCE,
51 DNEdgeConnectionHandler.INSTANCE,
52 SimpleElementLayers.INSTANCE,
53 // TODO: do we need this and does it work?
54 ConnectionSelectionOutline.INSTANCE,
55 DistrictNetworkAdditionalColor.INSTANCE
56 ).setId(DistrictNetworkEdgeElement.class.getSimpleName());
58 static final class DNEdgeSceneGraph implements SceneGraph {
60 public static final DNEdgeSceneGraph INSTANCE = new DNEdgeSceneGraph();
62 private static final long serialVersionUID = 68135568495835923L;
65 public void init(IElement edgeElement, G2DParentNode parent) {
66 DistrictNetworkEdge edge = edgeElement.getHint(KEY_DN_EDGE);
70 DistrictNetworkEdgeNode node = edgeElement.getHint(KEY_DN_EDGE_NODE);
72 node = parent.addNode(ElementUtils.generateNodeId(edgeElement), DistrictNetworkEdgeNode.class);
73 edgeElement.setHint(KEY_DN_EDGE_NODE, node);
75 SVGNode symbol = node.addNode(ElementUtils.generateNodeId(edgeElement), SVGNode.class);
76 edgeElement.setHint(KEY_DN_EDGE_SYMBOL_NODE, symbol);
79 node.setColor(ElementUtils.getAdditionalColor(edgeElement, Color.BLUE));
82 AffineTransform at = ElementUtils.getTransform(edgeElement);
84 node.setTransform(at);
89 public void cleanup(IElement edge) {
90 ElementUtils.removePossibleNode(edge, KEY_DN_EDGE_SYMBOL_NODE);
91 ElementUtils.removePossibleNode(edge, KEY_DN_EDGE_NODE);
92 edge.removeHint(KEY_DN_EDGE_NODE);
93 edge.removeHint(KEY_DN_EDGE_SYMBOL_NODE);
97 static final class DNEdgeInternalSize implements InternalSize, Outline, Pick {
99 private static final Logger LOGGER = LoggerFactory.getLogger(DNEdgeInternalSize.class);
101 private static final long serialVersionUID = -7346653820911240628L;
103 public static final DNEdgeInternalSize INSTANCE = new DNEdgeInternalSize();
106 public Rectangle2D getBounds(IElement e, Rectangle2D size) {
107 DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);
109 size = new Rectangle2D.Double();
111 size.setFrame(DistrictNetworkEdgeNode.calculatePath(edge, null, false).getBounds2D());
113 LOGGER.debug("Element {} does not have edge!", e);
119 public Shape getElementShape(IElement e) {
120 DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);
122 return DistrictNetworkEdgeNode.calculatePath(edge, null, false);
124 return getBounds(e, null);
129 public boolean pickTest(IElement e, Shape s, PickPolicy policy) {
130 DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);
132 Rectangle2D bounds = getBounds(s);
134 case PICK_CONTAINED_OBJECTS: return pickContainedObjects(edge, bounds);
135 case PICK_INTERSECTING_OBJECTS: return pickIntersectingObjects(e, edge, bounds);
143 private boolean pickContainedObjects(DistrictNetworkEdge edge, Rectangle2D bounds) {
144 double bminx = bounds.getMinX() / MapScalingTransform.getScaleX();
145 double bminy = bounds.getMinY() / MapScalingTransform.getScaleY();
146 double bmaxx = bounds.getMaxX() / MapScalingTransform.getScaleX();
147 double bmaxy = bounds.getMaxY() / MapScalingTransform.getScaleY();
149 double bsminx = ModelledCRS.xToLongitude(bminx);
150 double bsminy = ModelledCRS.yToLatitude(-bminy); // Invert for Simantics diagram coordinate system
151 double bsmaxx = ModelledCRS.xToLongitude(bmaxx);
152 double bsmaxy = ModelledCRS.yToLatitude(-bmaxy); // Invert for Simantics diagram coordinate system
154 double boundsMinY = Math.min(bsminy, bsmaxy);
155 double boundsMaxY = Math.max(bsminy, bsmaxy);
157 Point2D start = edge.getStartPoint();
158 Point2D end = edge.getEndPoint();
160 double eminx = Math.min(start.getX(), end.getX());
161 double eminy = Math.min(start.getY(), end.getY());
162 double emaxx = Math.max(start.getX(), end.getX());
163 double emaxy = Math.max(start.getY(), end.getY());
165 return eminx >= bsminx && eminy >= boundsMinY && emaxx <= bsmaxx && emaxy <= boundsMaxY;
168 private boolean pickIntersectingObjects(IElement e, DistrictNetworkEdge edge, Rectangle2D bounds) {
169 double dx = bounds.getWidth() / MapScalingTransform.getScaleX();
170 double dy = bounds.getHeight() / MapScalingTransform.getScaleY();
172 // Half the diagonal + half of the line width
173 DistrictNetworkEdgeNode node = e.getHint(KEY_DN_EDGE_NODE);
174 AffineTransform at = NodeUtil.getLocalToGlobalTransform(node);
176 Path2D path = node.getPath();
180 double lineWidth = node.getStrokeWidth(at, true);
181 double tolerance = Math.sqrt(dx * dx + dy * dy) / 2 + lineWidth / 2;
183 double sx = bounds.getCenterX() / MapScalingTransform.getScaleX();
184 double sy = bounds.getCenterY() / MapScalingTransform.getScaleY();
186 double coords[] = new double[6];
187 Point2D prevPoint = new Point2D.Double(), curPoint = new Point2D.Double();
188 Line2D line = new Line2D.Double();
189 for (PathIterator it = path.getPathIterator(null); !it.isDone(); it.next()) {
190 int type = it.currentSegment(coords);
192 case PathIterator.SEG_MOVETO:
193 curPoint.setLocation(coords[0], coords[1]);
195 case PathIterator.SEG_LINETO:
196 prevPoint.setLocation(curPoint);
197 curPoint.setLocation(coords[0], coords[1]);
198 line.setLine(prevPoint, curPoint);
199 double distSq = line.ptSegDistSq(sx, sy);
200 // System.out.println("s: " + sx + ", " + sy);
201 // System.out.println("ss: " + ssx + ", " + ssy);
202 // System.out.println("p1: " + edge.getStartPoint());
203 // System.out.println("p2: " + edge.getEndPoint());
204 // System.out.println("line: " + "(" + line.getX1() + ", " + line.getY1() + ", " + line.getX2() + ", " + line.getY2() + ")");
205 // System.out.println("distance from line is " + Math.sqrt(distSq) + " with tolerance " + tolerance);
206 if (distSq <= tolerance * tolerance)
210 LOGGER.error("Invalid edge path", new IllegalStateException());
218 private Rectangle2D getBounds(Shape shape) {
219 if (shape instanceof Rectangle2D)
220 return (Rectangle2D) shape;
221 return shape.getBounds2D();
226 static class DNEdgeConnectionHandler implements ConnectionHandler {
228 private static final long serialVersionUID = -6882671891381761687L;
230 public static final DNEdgeConnectionHandler INSTANCE = new DNEdgeConnectionHandler();
233 public Collection<IElement> getChildren(IElement connection, Collection<IElement> result) {
234 return Collections.emptyList();
238 public Collection<IElement> getBranchPoints(IElement connection, Collection<IElement> result) {
239 return Collections.emptyList();
243 public Collection<IElement> getSegments(IElement connection, Collection<IElement> result) {
244 return Collections.emptyList();
248 public Collection<Connection> getTerminalConnections(IElement connection, Collection<Connection> result) {
249 return Collections.emptyList();