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.Rectangle2D;
8 import java.util.Collection;
9 import java.util.Collections;
11 import org.simantics.district.network.ModelledCRS;
12 import org.simantics.district.network.ui.DistrictNetworkEdge;
13 import org.simantics.district.network.ui.nodes.DistrictNetworkEdgeNode;
14 import org.simantics.g2d.connection.handler.ConnectionHandler;
15 import org.simantics.g2d.diagram.handler.PickRequest.PickPolicy;
16 import org.simantics.g2d.diagram.handler.Topology.Connection;
17 import org.simantics.g2d.element.ElementClass;
18 import org.simantics.g2d.element.ElementUtils;
19 import org.simantics.g2d.element.IElement;
20 import org.simantics.g2d.element.SceneGraphNodeKey;
21 import org.simantics.g2d.element.handler.InternalSize;
22 import org.simantics.g2d.element.handler.Outline;
23 import org.simantics.g2d.element.handler.Pick;
24 import org.simantics.g2d.element.handler.SceneGraph;
25 import org.simantics.g2d.element.handler.SelectionOutline;
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);
118 private Shape getSelectionShape(IElement e) {
119 for (SelectionOutline so : e.getElementClass().getItemsByClass(SelectionOutline.class)) {
120 Shape shape = so.getSelectionShape(e);
124 // Using on-diagram coordinates because neither connections nor
125 // edges have a non-identity transform which means that
126 // coordinates are always absolute. Therefore branch point
127 // shape also needs to be calculated in absolute coordinates.
128 Shape shape = ElementUtils.getElementShapeOrBoundsOnDiagram(e);
133 public boolean pickTest(IElement e, Shape s, PickPolicy policy) {
134 DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);
136 Rectangle2D bounds = getBounds(s);
139 case PICK_CONTAINED_OBJECTS:
140 Shape selectionShape = getSelectionShape(e);
141 return bounds.contains(selectionShape.getBounds2D());
143 case PICK_INTERSECTING_OBJECTS:
144 double tolerance = (bounds.getHeight() + bounds.getHeight()) * 0.25 / MapScalingTransform.getScaleX();
145 Line2D line = new Line2D.Double(edge.getStartPoint(), edge.getEndPoint());
146 double sx = bounds.getCenterX() / MapScalingTransform.getScaleX();
147 double sy = bounds.getCenterY() / MapScalingTransform.getScaleY();
148 double ssx = ModelledCRS.xToLongitude(sx);
149 double ssy = ModelledCRS.yToLatitude(-sy); // Invert for Simantics diagram coordinate system
150 double distSq = line.ptSegDistSq(ssx, ssy);
151 // System.out.println("s: " + sx + ", " + sy);
152 // System.out.println("ss: " + ssx + ", " + ssy);
153 // System.out.println("p1: " + edge.getStartPoint());
154 // System.out.println("p2: " + edge.getEndPoint());
155 // System.out.println("line: " + "(" + line.getX1() + ", " + line.getY1() + ", " + line.getX2() + ", " + line.getY2() + ")");
156 // System.out.println("distance from line is " + Math.sqrt(distSq) + " with tolerance " + tolerance);
157 if (distSq <= tolerance * tolerance) {
167 private Rectangle2D getBounds(Shape shape) {
168 if (shape instanceof Rectangle2D)
169 return (Rectangle2D) shape;
170 return shape.getBounds2D();
175 static class DNEdgeConnectionHandler implements ConnectionHandler {
177 private static final long serialVersionUID = -6882671891381761687L;
179 public static final DNEdgeConnectionHandler INSTANCE = new DNEdgeConnectionHandler();
182 public Collection<IElement> getChildren(IElement connection, Collection<IElement> result) {
183 return Collections.emptyList();
187 public Collection<IElement> getBranchPoints(IElement connection, Collection<IElement> result) {
188 return Collections.emptyList();
192 public Collection<IElement> getSegments(IElement connection, Collection<IElement> result) {
193 return Collections.emptyList();
197 public Collection<Connection> getTerminalConnections(IElement connection, Collection<Connection> result) {
198 return Collections.emptyList();