]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElement.java
Hide "enabled" column for non-component type tech type tables
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / adapters / DistrictNetworkEdgeElement.java
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.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;
13
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;
39
40 public class DistrictNetworkEdgeElement {
41
42     private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkEdgeElement.class);
43
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");
47
48     public static final ElementClass CLASS =
49             ElementClass.compile(
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());
59
60     static final class DNEdgeSceneGraph implements SceneGraph {
61
62         public static final DNEdgeSceneGraph INSTANCE = new DNEdgeSceneGraph();
63
64         private static final long serialVersionUID = 68135568495835923L;
65
66         @Override
67         public void init(IElement edgeElement, G2DParentNode parent) {
68             try {
69                 DistrictNetworkEdge edge = edgeElement.getHint(KEY_DN_EDGE);
70                 if (edge == null) {
71                     cleanup(edgeElement);
72                 } else {
73                     DistrictNetworkEdgeNode node = edgeElement.getHint(KEY_DN_EDGE_NODE);
74                     if (node == null) {
75                         node = parent.addNode(ElementUtils.generateNodeId(edgeElement), DistrictNetworkEdgeNode.class);
76                         edgeElement.setHint(KEY_DN_EDGE_NODE, node);
77                         
78                         SVGNode symbol = node.addNode(ElementUtils.generateNodeId(edgeElement), SVGNode.class);
79                         edgeElement.setHint(KEY_DN_EDGE_SYMBOL_NODE, symbol);
80                     }
81                     
82                     node.setColor(ElementUtils.getAdditionalColor(edgeElement, Color.BLUE));
83                     
84                     node.setDNEdge(edge);
85                     AffineTransform at = ElementUtils.getTransform(edgeElement);
86                     if (at != null)
87                         node.setTransform(at);
88                 }
89             } catch (Exception e) {
90                 LOGGER.error("Could not initialize element {} for parent {}", edgeElement, parent, e);
91             }
92         }
93
94         @Override
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);
100         }
101     }
102
103     static final class DNEdgeInternalSize implements InternalSize, Outline, Pick {
104
105         private static final Logger LOGGER = LoggerFactory.getLogger(DNEdgeInternalSize.class);
106
107         private static final long serialVersionUID = -7346653820911240628L;
108
109         public static final DNEdgeInternalSize INSTANCE = new DNEdgeInternalSize();
110
111         @Override
112         public Rectangle2D getBounds(IElement e, Rectangle2D size) {
113             DistrictNetworkEdgeNode edgeNode = e.getHint(KEY_DN_EDGE_NODE);
114             if (size == null)
115                 size = new Rectangle2D.Double();
116             if (edgeNode != null)
117                 size.setFrame(edgeNode.getBoundsInLocal());
118             else
119                 LOGGER.debug("Element {} does not have edge!", e);
120
121             return size;
122         }
123
124         @Override
125         public Shape getElementShape(IElement e) {
126             DistrictNetworkEdgeNode edgeNode = e.getHint(KEY_DN_EDGE_NODE);
127             if (edgeNode != null) {
128                 return edgeNode.getPath();
129             } else {
130                 return getBounds(e, null);
131             }
132         }
133
134         @Override
135         public boolean pickTest(IElement e, Shape s, PickPolicy policy) {
136             DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);
137             if (edge != null) {
138                 Rectangle2D bounds = getBounds(s);
139                 switch (policy) {
140                 case PICK_CONTAINED_OBJECTS:    return pickContainedObjects(edge, bounds);
141                 case PICK_INTERSECTING_OBJECTS: return pickIntersectingObjects(e, edge, bounds);
142                 }
143                 return false;
144             }
145
146             return false;
147         }
148
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();
154
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
159
160             double boundsMinY = Math.min(bsminy, bsmaxy);
161             double boundsMaxY = Math.max(bsminy, bsmaxy);
162
163             Point2D start = edge.getStartPoint();
164             Point2D end = edge.getEndPoint();
165
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());
170
171             return eminx >= bsminx && eminy >= boundsMinY && emaxx <= bsmaxx && emaxy <= boundsMaxY;
172         }
173
174         private boolean pickIntersectingObjects(IElement e, DistrictNetworkEdge edge, Rectangle2D bounds) {
175             double dx = bounds.getWidth() / MapScalingTransform.getScaleX();
176             double dy = bounds.getHeight() / MapScalingTransform.getScaleY();
177             
178             // Half the diagonal + half of the line width
179             DistrictNetworkEdgeNode node = e.getHint(KEY_DN_EDGE_NODE);
180             AffineTransform at = NodeUtil.getLocalToGlobalTransform(node);
181             
182             Path2D path = node.getPath();
183             if (path == null)
184                 return false;
185             
186             double lineWidth = node.getStrokeWidth(at, true);
187             double tolerance = Math.sqrt(dx * dx + dy * dy) / 2 + lineWidth / 2;
188             
189             double sx = bounds.getCenterX() / MapScalingTransform.getScaleX();
190             double sy = bounds.getCenterY() / MapScalingTransform.getScaleY();
191             
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);
197                 switch (type) {
198                 case PathIterator.SEG_MOVETO:
199                     curPoint.setLocation(coords[0], coords[1]);
200                     break;
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)
213                         return true; 
214                     break;
215                 default:
216                     LOGGER.error("Invalid edge path", new IllegalStateException());
217                     return false;
218                 }
219             }
220             
221             return false;
222         }
223
224         private Rectangle2D getBounds(Shape shape) {
225             if (shape instanceof Rectangle2D)
226                 return (Rectangle2D) shape;
227             return shape.getBounds2D();
228         }
229
230     }
231
232     static class DNEdgeConnectionHandler implements ConnectionHandler {
233
234         private static final long serialVersionUID = -6882671891381761687L;
235
236         public static final DNEdgeConnectionHandler INSTANCE = new DNEdgeConnectionHandler();
237
238         @Override
239         public Collection<IElement> getChildren(IElement connection, Collection<IElement> result) {
240             return Collections.emptyList();
241         }
242
243         @Override
244         public Collection<IElement> getBranchPoints(IElement connection, Collection<IElement> result) {
245             return Collections.emptyList();
246         }
247
248         @Override
249         public Collection<IElement> getSegments(IElement connection, Collection<IElement> result) {
250             return Collections.emptyList();
251         }
252
253         @Override
254         public Collection<Connection> getTerminalConnections(IElement connection, Collection<Connection> result) {
255             return Collections.emptyList();
256         }
257     }
258
259 }