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 private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkEdgeElement.class);
44 public static final Key KEY_DN_EDGE = new KeyOf(DistrictNetworkEdge.class, "DN_EDGE");
45 public static final Key KEY_DN_EDGE_NODE = new SceneGraphNodeKey(DistrictNetworkEdgeNode.class, "DN_EDGE_NODE");
46 public static final Key KEY_DN_EDGE_SYMBOL_NODE = new SceneGraphNodeKey(DistrictNetworkEdgeNode.class, "DN_EDGE_SYMBOL_NODE");
48 public static final ElementClass CLASS =
50 DefaultTransform.INSTANCE,
51 DNEdgeInternalSize.INSTANCE,
52 DNEdgeSceneGraph.INSTANCE,
53 DNEdgeConnectionHandler.INSTANCE,
54 SimpleElementLayers.INSTANCE,
55 // TODO: do we need this and does it work?
56 ConnectionSelectionOutline.INSTANCE,
57 DistrictNetworkAdditionalColor.INSTANCE
58 ).setId(DistrictNetworkEdgeElement.class.getSimpleName());
60 static final class DNEdgeSceneGraph implements SceneGraph {
62 public static final DNEdgeSceneGraph INSTANCE = new DNEdgeSceneGraph();
64 private static final long serialVersionUID = 68135568495835923L;
67 public void init(IElement edgeElement, G2DParentNode parent) {
69 DistrictNetworkEdge edge = edgeElement.getHint(KEY_DN_EDGE);
73 DistrictNetworkEdgeNode node = edgeElement.getHint(KEY_DN_EDGE_NODE);
75 node = parent.addNode(ElementUtils.generateNodeId(edgeElement), DistrictNetworkEdgeNode.class);
76 edgeElement.setHint(KEY_DN_EDGE_NODE, node);
78 SVGNode symbol = node.addNode(ElementUtils.generateNodeId(edgeElement), SVGNode.class);
79 edgeElement.setHint(KEY_DN_EDGE_SYMBOL_NODE, symbol);
82 node.setColor(ElementUtils.getAdditionalColor(edgeElement, Color.BLUE));
85 AffineTransform at = ElementUtils.getTransform(edgeElement);
87 node.setTransform(at);
89 } catch (Exception e) {
90 LOGGER.error("Could not initialize element {} for parent {}", edgeElement, parent, e);
95 public void cleanup(IElement edge) {
96 ElementUtils.removePossibleNode(edge, KEY_DN_EDGE_SYMBOL_NODE);
97 ElementUtils.removePossibleNode(edge, KEY_DN_EDGE_NODE);
98 edge.removeHint(KEY_DN_EDGE_NODE);
99 edge.removeHint(KEY_DN_EDGE_SYMBOL_NODE);
103 static final class DNEdgeInternalSize implements InternalSize, Outline, Pick {
105 private static final Logger LOGGER = LoggerFactory.getLogger(DNEdgeInternalSize.class);
107 private static final long serialVersionUID = -7346653820911240628L;
109 public static final DNEdgeInternalSize INSTANCE = new DNEdgeInternalSize();
112 public Rectangle2D getBounds(IElement e, Rectangle2D size) {
113 DistrictNetworkEdgeNode edgeNode = e.getHint(KEY_DN_EDGE_NODE);
115 size = new Rectangle2D.Double();
116 if (edgeNode != null)
117 size.setFrame(edgeNode.getBoundsInLocal());
119 LOGGER.debug("Element {} does not have edge!", e);
125 public Shape getElementShape(IElement e) {
126 DistrictNetworkEdgeNode edgeNode = e.getHint(KEY_DN_EDGE_NODE);
127 if (edgeNode != null) {
128 return edgeNode.getPath();
130 return getBounds(e, null);
135 public boolean pickTest(IElement e, Shape s, PickPolicy policy) {
136 DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);
138 Rectangle2D bounds = getBounds(s);
140 case PICK_CONTAINED_OBJECTS: return pickContainedObjects(edge, bounds);
141 case PICK_INTERSECTING_OBJECTS: return pickIntersectingObjects(e, edge, bounds);
149 private boolean pickContainedObjects(DistrictNetworkEdge edge, Rectangle2D bounds) {
150 double bminx = bounds.getMinX() / MapScalingTransform.getScaleX();
151 double bminy = bounds.getMinY() / MapScalingTransform.getScaleY();
152 double bmaxx = bounds.getMaxX() / MapScalingTransform.getScaleX();
153 double bmaxy = bounds.getMaxY() / MapScalingTransform.getScaleY();
155 double bsminx = ModelledCRS.xToLongitude(bminx);
156 double bsminy = ModelledCRS.yToLatitude(-bminy); // Invert for Simantics diagram coordinate system
157 double bsmaxx = ModelledCRS.xToLongitude(bmaxx);
158 double bsmaxy = ModelledCRS.yToLatitude(-bmaxy); // Invert for Simantics diagram coordinate system
160 double boundsMinY = Math.min(bsminy, bsmaxy);
161 double boundsMaxY = Math.max(bsminy, bsmaxy);
163 Point2D start = edge.getStartPoint();
164 Point2D end = edge.getEndPoint();
166 double eminx = Math.min(start.getX(), end.getX());
167 double eminy = Math.min(start.getY(), end.getY());
168 double emaxx = Math.max(start.getX(), end.getX());
169 double emaxy = Math.max(start.getY(), end.getY());
171 return eminx >= bsminx && eminy >= boundsMinY && emaxx <= bsmaxx && emaxy <= boundsMaxY;
174 private boolean pickIntersectingObjects(IElement e, DistrictNetworkEdge edge, Rectangle2D bounds) {
175 double dx = bounds.getWidth() / MapScalingTransform.getScaleX();
176 double dy = bounds.getHeight() / MapScalingTransform.getScaleY();
178 // Half the diagonal + half of the line width
179 DistrictNetworkEdgeNode node = e.getHint(KEY_DN_EDGE_NODE);
180 AffineTransform at = NodeUtil.getLocalToGlobalTransform(node);
182 Path2D path = node.getPath();
186 double lineWidth = node.getStrokeWidth(at, true);
187 double tolerance = Math.sqrt(dx * dx + dy * dy) / 2 + lineWidth / 2;
189 double sx = bounds.getCenterX() / MapScalingTransform.getScaleX();
190 double sy = bounds.getCenterY() / MapScalingTransform.getScaleY();
192 double coords[] = new double[6];
193 Point2D prevPoint = new Point2D.Double(), curPoint = new Point2D.Double();
194 Line2D line = new Line2D.Double();
195 for (PathIterator it = path.getPathIterator(null); !it.isDone(); it.next()) {
196 int type = it.currentSegment(coords);
198 case PathIterator.SEG_MOVETO:
199 curPoint.setLocation(coords[0], coords[1]);
201 case PathIterator.SEG_LINETO:
202 prevPoint.setLocation(curPoint);
203 curPoint.setLocation(coords[0], coords[1]);
204 line.setLine(prevPoint, curPoint);
205 double distSq = line.ptSegDistSq(sx, sy);
206 // System.out.println("s: " + sx + ", " + sy);
207 // System.out.println("ss: " + ssx + ", " + ssy);
208 // System.out.println("p1: " + edge.getStartPoint());
209 // System.out.println("p2: " + edge.getEndPoint());
210 // System.out.println("line: " + "(" + line.getX1() + ", " + line.getY1() + ", " + line.getX2() + ", " + line.getY2() + ")");
211 // System.out.println("distance from line is " + Math.sqrt(distSq) + " with tolerance " + tolerance);
212 if (distSq <= tolerance * tolerance)
216 LOGGER.error("Invalid edge path", new IllegalStateException());
224 private Rectangle2D getBounds(Shape shape) {
225 if (shape instanceof Rectangle2D)
226 return (Rectangle2D) shape;
227 return shape.getBounds2D();
232 static class DNEdgeConnectionHandler implements ConnectionHandler {
234 private static final long serialVersionUID = -6882671891381761687L;
236 public static final DNEdgeConnectionHandler INSTANCE = new DNEdgeConnectionHandler();
239 public Collection<IElement> getChildren(IElement connection, Collection<IElement> result) {
240 return Collections.emptyList();
244 public Collection<IElement> getBranchPoints(IElement connection, Collection<IElement> result) {
245 return Collections.emptyList();
249 public Collection<IElement> getSegments(IElement connection, Collection<IElement> result) {
250 return Collections.emptyList();
254 public Collection<Connection> getTerminalConnections(IElement connection, Collection<Connection> result) {
255 return Collections.emptyList();