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.g2d.G2DParentNode;
17 import org.simantics.utils.datastructures.hints.IHintContext.Key;
18 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
20 public class DistrictNetworkVertexElement {
22 public static final Key KEY_DN_VERTEX = new KeyOf(DistrictNetworkVertex.class, "DN_VERTEX");
23 public static final Key KEY_DN_VERTEX_NODE = new SceneGraphNodeKey(DistrictNetworkVertexNode.class, "DN_VERTEX_NODE");
25 public static final ElementClass CLASS =
27 DefaultTransform.INSTANCE,
28 DNVertexInternalSize.INSTANCE,
29 DNVertexSceneGraph.INSTANCE,
30 SimpleElementLayers.INSTANCE,
31 DistrictNetworkAdditionalColor.INSTANCE
32 ).setId(DistrictNetworkVertexElement.class.getSimpleName());
34 static final class DNVertexSceneGraph implements SceneGraph {
36 public static final DNVertexSceneGraph INSTANCE = new DNVertexSceneGraph();
38 private static final long serialVersionUID = 8894367073815556871L;
41 public void init(IElement vertexElement, G2DParentNode parent) {
42 DistrictNetworkVertex vertex = vertexElement.getHint(KEY_DN_VERTEX);
44 cleanup(vertexElement);
46 DistrictNetworkVertexNode node = vertexElement.getHint(KEY_DN_VERTEX_NODE);
48 node = parent.addNode(ElementUtils.generateNodeId(vertexElement), DistrictNetworkVertexNode.class);
49 vertexElement.setHint(KEY_DN_VERTEX_NODE, node);
51 node.setVertex(vertex);
53 node.setColor(ElementUtils.getAdditionalColor(vertexElement, Color.BLUE));
55 AffineTransform at = ElementUtils.getTransform(vertexElement);
57 node.setTransform(at);
62 public void cleanup(IElement edge) {
63 ElementUtils.removePossibleNode(edge, KEY_DN_VERTEX_NODE);
64 edge.removeHint(KEY_DN_VERTEX_NODE);
68 static final class DNVertexInternalSize implements InternalSize {
70 public static final DNVertexInternalSize INSTANCE = new DNVertexInternalSize();
72 private static final long serialVersionUID = 2625702114278956461L;
75 public Rectangle2D getBounds(IElement e, Rectangle2D size) {
76 DistrictNetworkVertexNode node = e.getHint(KEY_DN_VERTEX_NODE);
77 Rectangle2D boundsInLocal = node.getBoundsInLocal();
78 size.setFrame(boundsInLocal);