-package org.simantics.district.network.ui.adapters;\r
-\r
-import java.awt.geom.Rectangle2D;\r
-\r
-import org.simantics.district.network.ui.DistrictNetworkEdge;\r
-import org.simantics.district.network.ui.nodes.DistrictNetworkEdgeNode;\r
-import org.simantics.g2d.element.ElementClass;\r
-import org.simantics.g2d.element.ElementUtils;\r
-import org.simantics.g2d.element.IElement;\r
-import org.simantics.g2d.element.SceneGraphNodeKey;\r
-import org.simantics.g2d.element.handler.InternalSize;\r
-import org.simantics.g2d.element.handler.SceneGraph;\r
-import org.simantics.g2d.element.handler.impl.DefaultTransform;\r
-import org.simantics.scenegraph.g2d.G2DParentNode;\r
-import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
-import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
-\r
-public class DistrictNetworkEdgeElement {\r
-\r
- public static final Key KEY_DN_EDGE = new KeyOf(DistrictNetworkEdge.class, "DN_EDGE");\r
- public static final Key KEY_DN_EDGE_NODE = new SceneGraphNodeKey(DistrictNetworkEdgeNode.class, "DN_EDGE_NODE");\r
- \r
- public static final ElementClass CLASS =\r
- ElementClass.compile(\r
- DefaultTransform.INSTANCE,\r
- DNEdgeInternalSize.INSTANCE,\r
- DNEdgeSceneGraph.INSTANCE\r
- ).setId(DistrictNetworkEdgeElement.class.getSimpleName());\r
- \r
- static final class DNEdgeSceneGraph implements SceneGraph {\r
- \r
- public static final DNEdgeSceneGraph INSTANCE = new DNEdgeSceneGraph();\r
-\r
- private static final long serialVersionUID = 8894367073815556871L;\r
-\r
- @Override\r
- public void init(IElement edgeElement, G2DParentNode parent) {\r
- DistrictNetworkEdge edge = edgeElement.getHint(KEY_DN_EDGE);\r
- if (edge == null) {\r
- cleanup(edgeElement);\r
- } else {\r
- DistrictNetworkEdgeNode node = edgeElement.getHint(KEY_DN_EDGE_NODE);\r
- if (node == null) {\r
- node = parent.addNode(ElementUtils.generateNodeId(edgeElement), DistrictNetworkEdgeNode.class);\r
- edgeElement.setHint(KEY_DN_EDGE_NODE, node);\r
- }\r
- node.setDNEdge(edge);\r
- }\r
- }\r
-\r
- @Override\r
- public void cleanup(IElement edge) {\r
- ElementUtils.removePossibleNode(edge, KEY_DN_EDGE_NODE);\r
- edge.removeHint(KEY_DN_EDGE_NODE);\r
- }\r
- }\r
- \r
- static final class DNEdgeInternalSize implements InternalSize {\r
-\r
- private static final long serialVersionUID = -2725017034692179676L;\r
- \r
- public static final DNEdgeInternalSize INSTANCE = new DNEdgeInternalSize();\r
-\r
- @Override\r
- public Rectangle2D getBounds(IElement e, Rectangle2D size) {\r
- DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);\r
- if (size == null)\r
- size = new Rectangle2D.Double();\r
- edge.getBounds(size);\r
-\r
- return size;\r
- }\r
- \r
- }\r
-}\r
+package org.simantics.district.network.ui.adapters;
+
+import java.awt.Color;
+import java.awt.Shape;
+import java.awt.geom.AffineTransform;
+import java.awt.geom.Line2D;
+import java.awt.geom.Path2D;
+import java.awt.geom.PathIterator;
+import java.awt.geom.Point2D;
+import java.awt.geom.Rectangle2D;
+import java.util.Collection;
+import java.util.Collections;
+
+import org.simantics.district.network.ModelledCRS;
+import org.simantics.district.network.ui.DistrictNetworkEdge;
+import org.simantics.district.network.ui.nodes.DistrictNetworkEdgeNode;
+import org.simantics.g2d.connection.handler.ConnectionHandler;
+import org.simantics.g2d.diagram.handler.PickRequest.PickPolicy;
+import org.simantics.g2d.diagram.handler.Topology.Connection;
+import org.simantics.g2d.element.ElementClass;
+import org.simantics.g2d.element.ElementUtils;
+import org.simantics.g2d.element.IElement;
+import org.simantics.g2d.element.SceneGraphNodeKey;
+import org.simantics.g2d.element.handler.InternalSize;
+import org.simantics.g2d.element.handler.Outline;
+import org.simantics.g2d.element.handler.Pick;
+import org.simantics.g2d.element.handler.SceneGraph;
+import org.simantics.g2d.element.handler.impl.ConnectionSelectionOutline;
+import org.simantics.g2d.element.handler.impl.DefaultTransform;
+import org.simantics.g2d.element.handler.impl.SimpleElementLayers;
+import org.simantics.maps.MapScalingTransform;
+import org.simantics.scenegraph.g2d.G2DParentNode;
+import org.simantics.scenegraph.g2d.nodes.SVGNode;
+import org.simantics.scenegraph.utils.NodeUtil;
+import org.simantics.utils.datastructures.hints.IHintContext.Key;
+import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class DistrictNetworkEdgeElement {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkEdgeElement.class);
+
+ public static final Key KEY_DN_EDGE = new KeyOf(DistrictNetworkEdge.class, "DN_EDGE");
+ public static final Key KEY_DN_EDGE_NODE = new SceneGraphNodeKey(DistrictNetworkEdgeNode.class, "DN_EDGE_NODE");
+ public static final Key KEY_DN_EDGE_SYMBOL_NODE = new SceneGraphNodeKey(DistrictNetworkEdgeNode.class, "DN_EDGE_SYMBOL_NODE");
+
+ public static final ElementClass CLASS =
+ ElementClass.compile(
+ DefaultTransform.INSTANCE,
+ DNEdgeInternalSize.INSTANCE,
+ DNEdgeSceneGraph.INSTANCE,
+ DNEdgeConnectionHandler.INSTANCE,
+ SimpleElementLayers.INSTANCE,
+ // TODO: do we need this and does it work?
+ ConnectionSelectionOutline.INSTANCE,
+ DistrictNetworkAdditionalColor.INSTANCE
+ ).setId(DistrictNetworkEdgeElement.class.getSimpleName());
+
+ static final class DNEdgeSceneGraph implements SceneGraph {
+
+ public static final DNEdgeSceneGraph INSTANCE = new DNEdgeSceneGraph();
+
+ private static final long serialVersionUID = 68135568495835923L;
+
+ @Override
+ public void init(IElement edgeElement, G2DParentNode parent) {
+ try {
+ DistrictNetworkEdge edge = edgeElement.getHint(KEY_DN_EDGE);
+ if (edge == null) {
+ cleanup(edgeElement);
+ } else {
+ DistrictNetworkEdgeNode node = edgeElement.getHint(KEY_DN_EDGE_NODE);
+ if (node == null) {
+ node = parent.addNode(ElementUtils.generateNodeId(edgeElement), DistrictNetworkEdgeNode.class);
+ edgeElement.setHint(KEY_DN_EDGE_NODE, node);
+
+ SVGNode symbol = node.addNode(ElementUtils.generateNodeId(edgeElement), SVGNode.class);
+ edgeElement.setHint(KEY_DN_EDGE_SYMBOL_NODE, symbol);
+ }
+
+ node.setColor(ElementUtils.getAdditionalColor(edgeElement, Color.BLUE));
+
+ node.setDNEdge(edge);
+ AffineTransform at = ElementUtils.getTransform(edgeElement);
+ if (at != null)
+ node.setTransform(at);
+ }
+ } catch (Exception e) {
+ LOGGER.error("Could not initialize element {} for parent {}", edgeElement, parent, e);
+ }
+ }
+
+ @Override
+ public void cleanup(IElement edge) {
+ ElementUtils.removePossibleNode(edge, KEY_DN_EDGE_SYMBOL_NODE);
+ ElementUtils.removePossibleNode(edge, KEY_DN_EDGE_NODE);
+ edge.removeHint(KEY_DN_EDGE_NODE);
+ edge.removeHint(KEY_DN_EDGE_SYMBOL_NODE);
+ }
+ }
+
+ static final class DNEdgeInternalSize implements InternalSize, Outline, Pick {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(DNEdgeInternalSize.class);
+
+ private static final long serialVersionUID = -7346653820911240628L;
+
+ public static final DNEdgeInternalSize INSTANCE = new DNEdgeInternalSize();
+
+ @Override
+ public Rectangle2D getBounds(IElement e, Rectangle2D size) {
+ DistrictNetworkEdgeNode edgeNode = e.getHint(KEY_DN_EDGE_NODE);
+ if (size == null)
+ size = new Rectangle2D.Double();
+ if (edgeNode != null)
+ size.setFrame(edgeNode.getBoundsInLocal());
+ else
+ LOGGER.debug("Element {} does not have edge!", e);
+
+ return size;
+ }
+
+ @Override
+ public Shape getElementShape(IElement e) {
+ DistrictNetworkEdgeNode edgeNode = e.getHint(KEY_DN_EDGE_NODE);
+ if (edgeNode != null) {
+ return edgeNode.getPath();
+ } else {
+ return getBounds(e, null);
+ }
+ }
+
+ @Override
+ public boolean pickTest(IElement e, Shape s, PickPolicy policy) {
+ DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);
+ if (edge != null) {
+ Rectangle2D bounds = getBounds(s);
+ switch (policy) {
+ case PICK_CONTAINED_OBJECTS: return pickContainedObjects(edge, bounds);
+ case PICK_INTERSECTING_OBJECTS: return pickIntersectingObjects(e, edge, bounds);
+ }
+ return false;
+ }
+
+ return false;
+ }
+
+ private boolean pickContainedObjects(DistrictNetworkEdge edge, Rectangle2D bounds) {
+ double bminx = bounds.getMinX() / MapScalingTransform.getScaleX();
+ double bminy = bounds.getMinY() / MapScalingTransform.getScaleY();
+ double bmaxx = bounds.getMaxX() / MapScalingTransform.getScaleX();
+ double bmaxy = bounds.getMaxY() / MapScalingTransform.getScaleY();
+
+ double bsminx = ModelledCRS.xToLongitude(bminx);
+ double bsminy = ModelledCRS.yToLatitude(-bminy); // Invert for Simantics diagram coordinate system
+ double bsmaxx = ModelledCRS.xToLongitude(bmaxx);
+ double bsmaxy = ModelledCRS.yToLatitude(-bmaxy); // Invert for Simantics diagram coordinate system
+
+ double boundsMinY = Math.min(bsminy, bsmaxy);
+ double boundsMaxY = Math.max(bsminy, bsmaxy);
+
+ Point2D start = edge.getStartPoint();
+ Point2D end = edge.getEndPoint();
+
+ double eminx = Math.min(start.getX(), end.getX());
+ double eminy = Math.min(start.getY(), end.getY());
+ double emaxx = Math.max(start.getX(), end.getX());
+ double emaxy = Math.max(start.getY(), end.getY());
+
+ return eminx >= bsminx && eminy >= boundsMinY && emaxx <= bsmaxx && emaxy <= boundsMaxY;
+ }
+
+ private boolean pickIntersectingObjects(IElement e, DistrictNetworkEdge edge, Rectangle2D bounds) {
+ double dx = bounds.getWidth() / MapScalingTransform.getScaleX();
+ double dy = bounds.getHeight() / MapScalingTransform.getScaleY();
+
+ // Half the diagonal + half of the line width
+ DistrictNetworkEdgeNode node = e.getHint(KEY_DN_EDGE_NODE);
+ AffineTransform at = NodeUtil.getLocalToGlobalTransform(node);
+
+ Path2D path = node.getPath();
+ if (path == null)
+ return false;
+
+ double lineWidth = node.getStrokeWidth(at, true);
+ double tolerance = Math.sqrt(dx * dx + dy * dy) / 2 + lineWidth / 2;
+
+ double sx = bounds.getCenterX() / MapScalingTransform.getScaleX();
+ double sy = bounds.getCenterY() / MapScalingTransform.getScaleY();
+
+ double coords[] = new double[6];
+ Point2D prevPoint = new Point2D.Double(), curPoint = new Point2D.Double();
+ Line2D line = new Line2D.Double();
+ for (PathIterator it = path.getPathIterator(null); !it.isDone(); it.next()) {
+ int type = it.currentSegment(coords);
+ switch (type) {
+ case PathIterator.SEG_MOVETO:
+ curPoint.setLocation(coords[0], coords[1]);
+ break;
+ case PathIterator.SEG_LINETO:
+ prevPoint.setLocation(curPoint);
+ curPoint.setLocation(coords[0], coords[1]);
+ line.setLine(prevPoint, curPoint);
+ double distSq = line.ptSegDistSq(sx, sy);
+// System.out.println("s: " + sx + ", " + sy);
+// System.out.println("ss: " + ssx + ", " + ssy);
+// System.out.println("p1: " + edge.getStartPoint());
+// System.out.println("p2: " + edge.getEndPoint());
+// System.out.println("line: " + "(" + line.getX1() + ", " + line.getY1() + ", " + line.getX2() + ", " + line.getY2() + ")");
+// System.out.println("distance from line is " + Math.sqrt(distSq) + " with tolerance " + tolerance);
+ if (distSq <= tolerance * tolerance)
+ return true;
+ break;
+ default:
+ LOGGER.error("Invalid edge path", new IllegalStateException());
+ return false;
+ }
+ }
+
+ return false;
+ }
+
+ private Rectangle2D getBounds(Shape shape) {
+ if (shape instanceof Rectangle2D)
+ return (Rectangle2D) shape;
+ return shape.getBounds2D();
+ }
+
+ }
+
+ static class DNEdgeConnectionHandler implements ConnectionHandler {
+
+ private static final long serialVersionUID = -6882671891381761687L;
+
+ public static final DNEdgeConnectionHandler INSTANCE = new DNEdgeConnectionHandler();
+
+ @Override
+ public Collection<IElement> getChildren(IElement connection, Collection<IElement> result) {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public Collection<IElement> getBranchPoints(IElement connection, Collection<IElement> result) {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public Collection<IElement> getSegments(IElement connection, Collection<IElement> result) {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public Collection<Connection> getTerminalConnections(IElement connection, Collection<Connection> result) {
+ return Collections.emptyList();
+ }
+ }
+
+}