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.Point2D;
8 import java.awt.geom.Rectangle2D;
9 import java.util.Collection;
10 import java.util.Collections;
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.scenegraph.g2d.nodes.SVGNode;
32 import org.simantics.utils.datastructures.hints.IHintContext.Key;
33 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class DistrictNetworkEdgeElement {
39 public static final Key KEY_DN_EDGE = new KeyOf(DistrictNetworkEdge.class, "DN_EDGE");
40 public static final Key KEY_DN_EDGE_NODE = new SceneGraphNodeKey(DistrictNetworkEdgeNode.class, "DN_EDGE_NODE");
41 public static final Key KEY_DN_EDGE_SYMBOL_NODE = new SceneGraphNodeKey(DistrictNetworkEdgeNode.class, "DN_EDGE_SYMBOL_NODE");
43 public static final ElementClass CLASS =
45 DefaultTransform.INSTANCE,
46 DNEdgeInternalSize.INSTANCE,
47 DNEdgeSceneGraph.INSTANCE,
48 DNEdgeConnectionHandler.INSTANCE,
49 SimpleElementLayers.INSTANCE,
50 // TODO: do we need this and does it work?
51 ConnectionSelectionOutline.INSTANCE,
52 DistrictNetworkAdditionalColor.INSTANCE
53 ).setId(DistrictNetworkEdgeElement.class.getSimpleName());
55 static final class DNEdgeSceneGraph implements SceneGraph {
57 public static final DNEdgeSceneGraph INSTANCE = new DNEdgeSceneGraph();
59 private static final long serialVersionUID = 68135568495835923L;
62 public void init(IElement edgeElement, G2DParentNode parent) {
63 DistrictNetworkEdge edge = edgeElement.getHint(KEY_DN_EDGE);
67 DistrictNetworkEdgeNode node = edgeElement.getHint(KEY_DN_EDGE_NODE);
69 node = parent.addNode(ElementUtils.generateNodeId(edgeElement), DistrictNetworkEdgeNode.class);
70 edgeElement.setHint(KEY_DN_EDGE_NODE, node);
72 SVGNode symbol = node.addNode(ElementUtils.generateNodeId(edgeElement), SVGNode.class);
73 edgeElement.setHint(KEY_DN_EDGE_SYMBOL_NODE, symbol);
76 node.setColor(ElementUtils.getAdditionalColor(edgeElement, Color.BLUE));
79 AffineTransform at = ElementUtils.getTransform(edgeElement);
81 node.setTransform(at);
86 public void cleanup(IElement edge) {
87 ElementUtils.removePossibleNode(edge, KEY_DN_EDGE_SYMBOL_NODE);
88 ElementUtils.removePossibleNode(edge, KEY_DN_EDGE_NODE);
89 edge.removeHint(KEY_DN_EDGE_NODE);
90 edge.removeHint(KEY_DN_EDGE_SYMBOL_NODE);
94 static final class DNEdgeInternalSize implements InternalSize, Outline, Pick {
96 private static final Logger LOGGER = LoggerFactory.getLogger(DNEdgeInternalSize.class);
98 private static final long serialVersionUID = -7346653820911240628L;
100 public static final DNEdgeInternalSize INSTANCE = new DNEdgeInternalSize();
103 public Rectangle2D getBounds(IElement e, Rectangle2D size) {
104 DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);
106 size = new Rectangle2D.Double();
108 size.setFrame(DistrictNetworkEdgeNode.calculatePath(edge, null).getBounds2D());
110 LOGGER.debug("Element {} does not have edge!", e);
116 public Shape getElementShape(IElement e) {
117 DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);
119 return DistrictNetworkEdgeNode.calculatePath(edge, null);
121 return getBounds(e, null);
126 public boolean pickTest(IElement e, Shape s, PickPolicy policy) {
127 DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);
129 Rectangle2D bounds = getBounds(s);
131 case PICK_CONTAINED_OBJECTS: return pickContainedObjects(edge, bounds);
132 case PICK_INTERSECTING_OBJECTS: return pickIntersectingObjects(edge, bounds);
140 private boolean pickContainedObjects(DistrictNetworkEdge edge, Rectangle2D bounds) {
141 double bminx = bounds.getMinX() / MapScalingTransform.getScaleX();
142 double bminy = bounds.getMinY() / MapScalingTransform.getScaleY();
143 double bmaxx = bounds.getMaxX() / MapScalingTransform.getScaleX();
144 double bmaxy = bounds.getMaxY() / MapScalingTransform.getScaleY();
146 double bsminx = ModelledCRS.xToLongitude(bminx);
147 double bsminy = ModelledCRS.yToLatitude(-bminy); // Invert for Simantics diagram coordinate system
148 double bsmaxx = ModelledCRS.xToLongitude(bmaxx);
149 double bsmaxy = ModelledCRS.yToLatitude(-bmaxy); // Invert for Simantics diagram coordinate system
151 double boundsMinY = Math.min(bsminy, bsmaxy);
152 double boundsMaxY = Math.max(bsminy, bsmaxy);
154 Point2D start = edge.getStartPoint();
155 Point2D end = edge.getEndPoint();
157 double eminx = Math.min(start.getX(), end.getX());
158 double eminy = Math.min(start.getY(), end.getY());
159 double emaxx = Math.max(start.getX(), end.getX());
160 double emaxy = Math.max(start.getY(), end.getY());
162 return eminx >= bsminx && eminy >= boundsMinY && emaxx <= bsmaxx && emaxy <= boundsMaxY;
165 private boolean pickIntersectingObjects(DistrictNetworkEdge edge, Rectangle2D bounds) {
166 double tolerance = (bounds.getHeight() + bounds.getHeight()) * 0.25 / MapScalingTransform.getScaleX();
167 Line2D line = new Line2D.Double(edge.getStartPoint(), edge.getEndPoint());
168 double sx = bounds.getCenterX() / MapScalingTransform.getScaleX();
169 double sy = bounds.getCenterY() / MapScalingTransform.getScaleY();
170 double ssx = ModelledCRS.xToLongitude(sx);
171 double ssy = ModelledCRS.yToLatitude(-sy); // Invert for Simantics diagram coordinate system
172 double distSq = line.ptSegDistSq(ssx, ssy);
173 // System.out.println("s: " + sx + ", " + sy);
174 // System.out.println("ss: " + ssx + ", " + ssy);
175 // System.out.println("p1: " + edge.getStartPoint());
176 // System.out.println("p2: " + edge.getEndPoint());
177 // System.out.println("line: " + "(" + line.getX1() + ", " + line.getY1() + ", " + line.getX2() + ", " + line.getY2() + ")");
178 // System.out.println("distance from line is " + Math.sqrt(distSq) + " with tolerance " + tolerance);
179 return distSq <= tolerance * tolerance;
182 private Rectangle2D getBounds(Shape shape) {
183 if (shape instanceof Rectangle2D)
184 return (Rectangle2D) shape;
185 return shape.getBounds2D();
190 static class DNEdgeConnectionHandler implements ConnectionHandler {
192 private static final long serialVersionUID = -6882671891381761687L;
194 public static final DNEdgeConnectionHandler INSTANCE = new DNEdgeConnectionHandler();
197 public Collection<IElement> getChildren(IElement connection, Collection<IElement> result) {
198 return Collections.emptyList();
202 public Collection<IElement> getBranchPoints(IElement connection, Collection<IElement> result) {
203 return Collections.emptyList();
207 public Collection<IElement> getSegments(IElement connection, Collection<IElement> result) {
208 return Collections.emptyList();
212 public Collection<Connection> getTerminalConnections(IElement connection, Collection<Connection> result) {
213 return Collections.emptyList();