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