package org.simantics.district.network.ui.adapters; import java.awt.Color; import java.awt.Shape; import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; import org.simantics.diagram.elements.DiagramNodeUtil; import org.simantics.district.network.ui.nodes.DistrictNetworkVertexNode; import org.simantics.g2d.canvas.Hints; import org.simantics.g2d.canvas.ICanvasContext; import org.simantics.g2d.diagram.handler.PickRequest.PickPolicy; 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.Pick; import org.simantics.g2d.element.handler.SceneGraph; import org.simantics.g2d.element.handler.impl.DefaultTransform; import org.simantics.g2d.element.handler.impl.SimpleElementLayers; import org.simantics.scenegraph.INode; import org.simantics.scenegraph.g2d.G2DParentNode; import org.simantics.scenegraph.g2d.nodes.SVGNode; import org.simantics.utils.datastructures.hints.IHintContext.Key; import org.simantics.utils.datastructures.hints.IHintContext.KeyOf; public class DistrictNetworkVertexElement { public static final Key KEY_DN_VERTEX = new KeyOf(DistrictNetworkVertex.class, "DN_VERTEX"); public static final Key KEY_DN_VERTEX_NODE = new SceneGraphNodeKey(DistrictNetworkVertexNode.class, "DN_VERTEX_NODE"); public static final Key KEY_DN_VERTEX_SYMBOL_NODE = new SceneGraphNodeKey(SVGNode.class, "DN_VERTEX_SYMBOL_NODE"); public static final ElementClass CLASS = ElementClass.compile( DefaultTransform.INSTANCE, DNVertexInternalSize.INSTANCE, DNVertexSceneGraph.INSTANCE, SimpleElementLayers.INSTANCE, DistrictNetworkAdditionalColor.INSTANCE ).setId(DistrictNetworkVertexElement.class.getSimpleName()); static final class DNVertexSceneGraph implements SceneGraph { public static final DNVertexSceneGraph INSTANCE = new DNVertexSceneGraph(); private static final long serialVersionUID = 8894367073815556871L; @Override public void init(IElement vertexElement, G2DParentNode parent) { DistrictNetworkVertex vertex = vertexElement.getHint(KEY_DN_VERTEX); if (vertex == null) { cleanup(vertexElement); } else { DistrictNetworkVertexNode node = vertexElement.getHint(KEY_DN_VERTEX_NODE); if (node == null) { node = parent.addNode(ElementUtils.generateNodeId(vertexElement), DistrictNetworkVertexNode.class); vertexElement.setHint(KEY_DN_VERTEX_NODE, node); SVGNode symbol = node.addNode(ElementUtils.generateNodeId(vertexElement), SVGNode.class); vertexElement.setHint(KEY_DN_VERTEX_SYMBOL_NODE, symbol); } node.setVertex(vertex); node.setColor(ElementUtils.getAdditionalColor(vertexElement, Color.BLUE)); AffineTransform at = ElementUtils.getTransform(vertexElement); if(at != null) node.setTransform(at); } } @Override public void cleanup(IElement edge) { ElementUtils.removePossibleNode(edge, KEY_DN_VERTEX_SYMBOL_NODE); INode node = ElementUtils.removePossibleNode(edge, KEY_DN_VERTEX_NODE); if (node != null) node.getParent().remove(); edge.removeHint(KEY_DN_VERTEX_NODE); edge.removeHint(KEY_DN_VERTEX_SYMBOL_NODE); } } static final class DNVertexInternalSize implements InternalSize, Pick { public static final DNVertexInternalSize INSTANCE = new DNVertexInternalSize(); private static final long serialVersionUID = 2625702114278956461L; @Override public Rectangle2D getBounds(IElement e, Rectangle2D size) { DistrictNetworkVertexNode node = e.getHint(KEY_DN_VERTEX_NODE); Rectangle2D boundsInLocal = node.getBoundsInLocal(); ICanvasContext ctx = DiagramNodeUtil.getCanvasContext(node); AffineTransform canvasTransform = ctx.getHintStack().getHint(Hints.KEY_CANVAS_TRANSFORM); // for some reason PickContextImpl expands the rectangle by 0.001 (too much) - let's counter it double x = boundsInLocal.getX(); double y = boundsInLocal.getY(); double scaledWidth = boundsInLocal.getWidth(); double scaledHeight = boundsInLocal.getHeight(); if (canvasTransform != null) { scaledWidth = boundsInLocal.getWidth() / canvasTransform.getScaleX(); scaledHeight= boundsInLocal.getHeight() / canvasTransform.getScaleY(); } double width = scaledWidth; double height = scaledHeight; size.setFrame(x, y, width, height); return size; } @Override public boolean pickTest(IElement e, Shape s, PickPolicy policy) { DistrictNetworkVertexNode node = e.getHint(KEY_DN_VERTEX_NODE); Rectangle2D boundsInLocal = node.getBounds(); Rectangle2D bounds = getBounds(s); switch (policy) { case PICK_CONTAINED_OBJECTS: return org.simantics.g2d.utils.GeometryUtils.contains(bounds, boundsInLocal); case PICK_INTERSECTING_OBJECTS: return org.simantics.g2d.utils.GeometryUtils.intersects(boundsInLocal, bounds); } return false; } private Rectangle2D getBounds(Shape shape) { if (shape instanceof Rectangle2D) return (Rectangle2D) shape; return shape.getBounds2D(); } } }