]> gerrit.simantics Code Review - simantics/district.git/blob
cbc6a578638e9a1d679915613af759de27b751be
[simantics/district.git] /
1 package org.simantics.district.network.ui.adapters;
2
3 import java.awt.Color;
4 import java.awt.Shape;
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;
10
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;
35
36 public class DistrictNetworkEdgeElement {
37
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");
40
41     public static final ElementClass CLASS =
42             ElementClass.compile(
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());
52
53     static final class DNEdgeSceneGraph implements SceneGraph {
54
55         public static final DNEdgeSceneGraph INSTANCE = new DNEdgeSceneGraph();
56
57         private static final long serialVersionUID = 68135568495835923L;
58
59         @Override
60         public void init(IElement edgeElement, G2DParentNode parent) {
61             DistrictNetworkEdge edge = edgeElement.getHint(KEY_DN_EDGE);
62             if (edge == null) {
63                 cleanup(edgeElement);
64             } else {
65                 DistrictNetworkEdgeNode node = edgeElement.getHint(KEY_DN_EDGE_NODE);
66                 if (node == null) {
67                     node = parent.addNode(ElementUtils.generateNodeId(edgeElement), DistrictNetworkEdgeNode.class);
68                     edgeElement.setHint(KEY_DN_EDGE_NODE, node);
69                 }
70                 
71                 node.setColor(ElementUtils.getAdditionalColor(edgeElement, Color.BLUE));
72                 
73                 node.setDNEdge(edge);
74                 AffineTransform at = ElementUtils.getTransform(edgeElement);
75                 if (at != null)
76                     node.setTransform(at);
77             }
78         }
79
80         @Override
81         public void cleanup(IElement edge) {
82             ElementUtils.removePossibleNode(edge, KEY_DN_EDGE_NODE);
83             edge.removeHint(KEY_DN_EDGE_NODE);
84         }
85     }
86
87     static final class DNEdgeInternalSize implements InternalSize, Outline, Pick {
88
89         private static final Logger LOGGER = LoggerFactory.getLogger(DNEdgeInternalSize.class);
90
91         private static final long serialVersionUID = -7346653820911240628L;
92
93         public static final DNEdgeInternalSize INSTANCE = new DNEdgeInternalSize();
94
95         @Override
96         public Rectangle2D getBounds(IElement e, Rectangle2D size) {
97             DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);
98             if (size == null)
99                 size = new Rectangle2D.Double();
100             if (edge != null)
101                 size.setFrame(DistrictNetworkEdgeNode.calculatePath(edge, null).getBounds2D());
102             else
103                 LOGGER.debug("Element {} does not have edge!", e);
104
105             return size;
106         }
107
108         @Override
109         public Shape getElementShape(IElement e) {
110             DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);
111             if (edge != null) {
112                 return DistrictNetworkEdgeNode.calculatePath(edge, null);
113             } else {
114                 return getBounds(e, null);
115             }
116         }
117
118         private Shape getSelectionShape(IElement e) {
119             for (SelectionOutline so : e.getElementClass().getItemsByClass(SelectionOutline.class)) {
120                 Shape shape = so.getSelectionShape(e);
121                 if (shape != null)
122                     return shape;
123             }
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);
129             return shape;
130         }
131
132         @Override
133         public boolean pickTest(IElement e, Shape s, PickPolicy policy) {
134             DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);
135             if (edge != null) {
136                 Rectangle2D bounds = getBounds(s);
137
138                 switch (policy) {
139                 case PICK_CONTAINED_OBJECTS:
140                     Shape selectionShape = getSelectionShape(e);
141                     return bounds.contains(selectionShape.getBounds2D());
142
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) {
158                         return true;
159                     }
160                 }
161                 return false;
162             }
163
164             return false;
165         }
166
167         private Rectangle2D getBounds(Shape shape) {
168             if (shape instanceof Rectangle2D)
169                 return (Rectangle2D) shape;
170             return shape.getBounds2D();
171         }
172
173     }
174
175     static class DNEdgeConnectionHandler implements ConnectionHandler {
176
177         private static final long serialVersionUID = -6882671891381761687L;
178
179         public static final DNEdgeConnectionHandler INSTANCE = new DNEdgeConnectionHandler();
180
181         @Override
182         public Collection<IElement> getChildren(IElement connection, Collection<IElement> result) {
183             return Collections.emptyList();
184         }
185
186         @Override
187         public Collection<IElement> getBranchPoints(IElement connection, Collection<IElement> result) {
188             return Collections.emptyList();
189         }
190
191         @Override
192         public Collection<IElement> getSegments(IElement connection, Collection<IElement> result) {
193             return Collections.emptyList();
194         }
195
196         @Override
197         public Collection<Connection> getTerminalConnections(IElement connection, Collection<Connection> result) {
198             return Collections.emptyList();
199         }
200     }
201
202 }