]> gerrit.simantics Code Review - simantics/district.git/blob
339d51d4149d8f2664a2b154d87c4b48a5bd2a0e
[simantics/district.git] /
1 package org.simantics.district.network.ui.adapters;\r
2 \r
3 import java.awt.Dimension;\r
4 import java.awt.geom.AffineTransform;\r
5 import java.awt.geom.Rectangle2D;\r
6 \r
7 import org.simantics.district.network.ui.nodes.DistrictNetworkVertexNode;\r
8 import org.simantics.g2d.element.ElementClass;\r
9 import org.simantics.g2d.element.ElementUtils;\r
10 import org.simantics.g2d.element.IElement;\r
11 import org.simantics.g2d.element.SceneGraphNodeKey;\r
12 import org.simantics.g2d.element.handler.InternalSize;\r
13 import org.simantics.g2d.element.handler.SceneGraph;\r
14 import org.simantics.g2d.element.handler.impl.DefaultTransform;\r
15 import org.simantics.scenegraph.g2d.G2DParentNode;\r
16 import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
17 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
18 \r
19 public class DistrictNetworkVertexElement {\r
20 \r
21     public static final Key KEY_DN_VERTEX = new KeyOf(DistrictNetworkVertex.class, "DN_VERTEX");\r
22     public static final Key KEY_DN_VERTEX_NODE = new SceneGraphNodeKey(DistrictNetworkVertexNode.class, "DN_VERTEX_NODE");\r
23     \r
24     public static final ElementClass CLASS =\r
25             ElementClass.compile(\r
26                     DefaultTransform.INSTANCE,\r
27                     DNVertexInternalSize.INSTANCE,\r
28                     DNVertexSceneGraph.INSTANCE\r
29             ).setId(DistrictNetworkVertexElement.class.getSimpleName());\r
30     \r
31     static final class DNVertexSceneGraph implements SceneGraph {\r
32         \r
33         public static final DNVertexSceneGraph INSTANCE = new DNVertexSceneGraph();\r
34 \r
35         private static final long serialVersionUID = 8894367073815556871L;\r
36 \r
37         @Override\r
38         public void init(IElement edgeElement, G2DParentNode parent) {\r
39             DistrictNetworkVertex vertex = edgeElement.getHint(KEY_DN_VERTEX);\r
40             if (vertex == null) {\r
41                 cleanup(edgeElement);\r
42             } else {\r
43                 DistrictNetworkVertexNode node = edgeElement.getHint(KEY_DN_VERTEX_NODE);\r
44                 if (node == null) {\r
45                     node = parent.addNode(ElementUtils.generateNodeId(edgeElement), DistrictNetworkVertexNode.class);\r
46                     edgeElement.setHint(KEY_DN_VERTEX_NODE, node);\r
47                 }\r
48                 node.setVertex(vertex);\r
49                 \r
50                 AffineTransform at = ElementUtils.getTransform(edgeElement);\r
51                 if(at != null)\r
52                     node.setTransform(at);\r
53             }\r
54         }\r
55 \r
56         @Override\r
57         public void cleanup(IElement edge) {\r
58             ElementUtils.removePossibleNode(edge, KEY_DN_VERTEX_NODE);\r
59             edge.removeHint(KEY_DN_VERTEX_NODE);\r
60         }\r
61     }\r
62     \r
63     static final class DNVertexInternalSize implements InternalSize {\r
64 \r
65         public static final DNVertexInternalSize INSTANCE = new DNVertexInternalSize();\r
66         \r
67         private static final long serialVersionUID = 2625702114278956461L;\r
68 \r
69         @Override\r
70         public Rectangle2D getBounds(IElement e, Rectangle2D size) {\r
71             if (size == null)\r
72                 size = new Rectangle2D.Double();\r
73             size.setFrame(-0.5, -0.5, 1, 1);\r
74             return size;\r
75         }\r
76         \r
77     }\r
78 }\r