1 package org.simantics.district.network.ui.adapters;
4 import java.awt.geom.AffineTransform;
5 import java.awt.geom.Rectangle2D;
7 import org.simantics.district.network.ui.nodes.DistrictNetworkVertexNode;
8 import org.simantics.g2d.element.ElementClass;
9 import org.simantics.g2d.element.ElementUtils;
10 import org.simantics.g2d.element.IElement;
11 import org.simantics.g2d.element.SceneGraphNodeKey;
12 import org.simantics.g2d.element.handler.InternalSize;
13 import org.simantics.g2d.element.handler.SceneGraph;
14 import org.simantics.g2d.element.handler.impl.DefaultTransform;
15 import org.simantics.g2d.element.handler.impl.SimpleElementLayers;
16 import org.simantics.scenegraph.INode;
17 import org.simantics.scenegraph.g2d.G2DParentNode;
18 import org.simantics.scenegraph.g2d.nodes.SVGNode;
19 import org.simantics.utils.datastructures.hints.IHintContext.Key;
20 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
22 public class DistrictNetworkVertexElement {
24 public static final Key KEY_DN_VERTEX = new KeyOf(DistrictNetworkVertex.class, "DN_VERTEX");
25 public static final Key KEY_DN_VERTEX_NODE = new SceneGraphNodeKey(DistrictNetworkVertexNode.class, "DN_VERTEX_NODE");
26 public static final Key KEY_DN_VERTEX_SYMBOL_NODE = new SceneGraphNodeKey(SVGNode.class, "DN_VERTEX_SYMBOL_NODE");
28 public static final ElementClass CLASS =
30 DefaultTransform.INSTANCE,
31 DNVertexInternalSize.INSTANCE,
32 DNVertexSceneGraph.INSTANCE,
33 SimpleElementLayers.INSTANCE,
34 DistrictNetworkAdditionalColor.INSTANCE
35 ).setId(DistrictNetworkVertexElement.class.getSimpleName());
37 static final class DNVertexSceneGraph implements SceneGraph {
39 public static final DNVertexSceneGraph INSTANCE = new DNVertexSceneGraph();
41 private static final long serialVersionUID = 8894367073815556871L;
44 public void init(IElement vertexElement, G2DParentNode parent) {
45 DistrictNetworkVertex vertex = vertexElement.getHint(KEY_DN_VERTEX);
47 cleanup(vertexElement);
49 DistrictNetworkVertexNode node = vertexElement.getHint(KEY_DN_VERTEX_NODE);
51 node = parent.addNode(ElementUtils.generateNodeId(vertexElement), DistrictNetworkVertexNode.class);
52 vertexElement.setHint(KEY_DN_VERTEX_NODE, node);
54 SVGNode symbol = node.addNode(ElementUtils.generateNodeId(vertexElement), SVGNode.class);
55 vertexElement.setHint(KEY_DN_VERTEX_SYMBOL_NODE, symbol);
58 node.setVertex(vertex);
60 node.setColor(ElementUtils.getAdditionalColor(vertexElement, Color.BLUE));
62 AffineTransform at = ElementUtils.getTransform(vertexElement);
64 node.setTransform(at);
69 public void cleanup(IElement edge) {
70 ElementUtils.removePossibleNode(edge, KEY_DN_VERTEX_SYMBOL_NODE);
71 INode node = ElementUtils.removePossibleNode(edge, KEY_DN_VERTEX_NODE);
73 node.getParent().remove();
75 edge.removeHint(KEY_DN_VERTEX_NODE);
76 edge.removeHint(KEY_DN_VERTEX_SYMBOL_NODE);
80 static final class DNVertexInternalSize implements InternalSize {
82 public static final DNVertexInternalSize INSTANCE = new DNVertexInternalSize();
84 private static final long serialVersionUID = 2625702114278956461L;
87 public Rectangle2D getBounds(IElement e, Rectangle2D size) {
88 DistrictNetworkVertexNode node = e.getHint(KEY_DN_VERTEX_NODE);
89 Rectangle2D boundsInLocal = node.getBoundsInLocal();
90 size.setFrame(boundsInLocal);