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.utils.datastructures.hints.IHintContext.Key;
32 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class DistrictNetworkEdgeElement {
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");
41 public static final ElementClass CLASS =
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());
53 static final class DNEdgeSceneGraph implements SceneGraph {
55 public static final DNEdgeSceneGraph INSTANCE = new DNEdgeSceneGraph();
57 private static final long serialVersionUID = 68135568495835923L;
60 public void init(IElement edgeElement, G2DParentNode parent) {
61 DistrictNetworkEdge edge = edgeElement.getHint(KEY_DN_EDGE);
65 DistrictNetworkEdgeNode node = edgeElement.getHint(KEY_DN_EDGE_NODE);
67 node = parent.addNode(ElementUtils.generateNodeId(edgeElement), DistrictNetworkEdgeNode.class);
68 edgeElement.setHint(KEY_DN_EDGE_NODE, node);
71 node.setColor(ElementUtils.getAdditionalColor(edgeElement, Color.BLUE));
74 AffineTransform at = ElementUtils.getTransform(edgeElement);
76 node.setTransform(at);
81 public void cleanup(IElement edge) {
82 ElementUtils.removePossibleNode(edge, KEY_DN_EDGE_NODE);
83 edge.removeHint(KEY_DN_EDGE_NODE);
87 static final class DNEdgeInternalSize implements InternalSize, Outline, Pick {
89 private static final Logger LOGGER = LoggerFactory.getLogger(DNEdgeInternalSize.class);
91 private static final long serialVersionUID = -7346653820911240628L;
93 public static final DNEdgeInternalSize INSTANCE = new DNEdgeInternalSize();
96 public Rectangle2D getBounds(IElement e, Rectangle2D size) {
97 DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);
99 size = new Rectangle2D.Double();
101 size.setFrame(DistrictNetworkEdgeNode.calculatePath(edge, null).getBounds2D());
103 LOGGER.debug("Element {} does not have edge!", e);
109 public Shape getElementShape(IElement e) {
110 DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);
112 return DistrictNetworkEdgeNode.calculatePath(edge, null);
114 return getBounds(e, null);
119 public boolean pickTest(IElement e, Shape s, PickPolicy policy) {
120 DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);
122 Rectangle2D bounds = getBounds(s);
124 case PICK_CONTAINED_OBJECTS: return pickContainedObjects(edge, bounds);
125 case PICK_INTERSECTING_OBJECTS: return pickIntersectingObjects(edge, bounds);
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();
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
144 double boundsMinY = Math.min(bsminy, bsmaxy);
145 double boundsMaxY = Math.max(bsminy, bsmaxy);
147 Point2D start = edge.getStartPoint();
148 Point2D end = edge.getEndPoint();
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());
155 return eminx >= bsminx && eminy >= boundsMinY && emaxx <= bsmaxx && emaxy <= boundsMaxY;
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;
175 private Rectangle2D getBounds(Shape shape) {
176 if (shape instanceof Rectangle2D)
177 return (Rectangle2D) shape;
178 return shape.getBounds2D();
183 static class DNEdgeConnectionHandler implements ConnectionHandler {
185 private static final long serialVersionUID = -6882671891381761687L;
187 public static final DNEdgeConnectionHandler INSTANCE = new DNEdgeConnectionHandler();
190 public Collection<IElement> getChildren(IElement connection, Collection<IElement> result) {
191 return Collections.emptyList();
195 public Collection<IElement> getBranchPoints(IElement connection, Collection<IElement> result) {
196 return Collections.emptyList();
200 public Collection<IElement> getSegments(IElement connection, Collection<IElement> result) {
201 return Collections.emptyList();
205 public Collection<Connection> getTerminalConnections(IElement connection, Collection<Connection> result) {
206 return Collections.emptyList();