]> gerrit.simantics Code Review - simantics/district.git/blob
a53c02d2e0ba98708b8661cf64379f4f6a232545
[simantics/district.git] /
1 package org.simantics.district.network.ui.adapters;\r
2 \r
3 import java.awt.geom.Rectangle2D;\r
4 \r
5 import org.simantics.district.network.ui.DistrictNetworkEdge;\r
6 import org.simantics.district.network.ui.nodes.DistrictNetworkEdgeNode;\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.element.handler.impl.DefaultTransform;\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 DistrictNetworkEdgeElement {\r
19 \r
20     public static final Key KEY_DN_EDGE = new KeyOf(DistrictNetworkEdge.class, "DN_EDGE");\r
21     public static final Key KEY_DN_EDGE_NODE = new SceneGraphNodeKey(DistrictNetworkEdgeNode.class, "DN_EDGE_NODE");\r
22     \r
23     public static final ElementClass CLASS =\r
24             ElementClass.compile(\r
25                     DefaultTransform.INSTANCE,\r
26                     DNEdgeInternalSize.INSTANCE,\r
27                     DNEdgeSceneGraph.INSTANCE\r
28             ).setId(DistrictNetworkEdgeElement.class.getSimpleName());\r
29     \r
30     static final class DNEdgeSceneGraph implements SceneGraph {\r
31         \r
32         public static final DNEdgeSceneGraph INSTANCE = new DNEdgeSceneGraph();\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             DistrictNetworkEdge edge = edgeElement.getHint(KEY_DN_EDGE);\r
39             if (edge == null) {\r
40                 cleanup(edgeElement);\r
41             } else {\r
42                 DistrictNetworkEdgeNode node = edgeElement.getHint(KEY_DN_EDGE_NODE);\r
43                 if (node == null) {\r
44                     node = parent.addNode(ElementUtils.generateNodeId(edgeElement), DistrictNetworkEdgeNode.class);\r
45                     edgeElement.setHint(KEY_DN_EDGE_NODE, node);\r
46                 }\r
47                 node.setDNEdge(edge);\r
48             }\r
49         }\r
50 \r
51         @Override\r
52         public void cleanup(IElement edge) {\r
53             ElementUtils.removePossibleNode(edge, KEY_DN_EDGE_NODE);\r
54             edge.removeHint(KEY_DN_EDGE_NODE);\r
55         }\r
56     }\r
57     \r
58     static final class DNEdgeInternalSize implements InternalSize {\r
59 \r
60         private static final long serialVersionUID = -2725017034692179676L;\r
61         \r
62         public static final DNEdgeInternalSize INSTANCE = new DNEdgeInternalSize();\r
63 \r
64         @Override\r
65         public Rectangle2D getBounds(IElement e, Rectangle2D size) {\r
66             DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);\r
67             if (size == null)\r
68                 size = new Rectangle2D.Double();\r
69             edge.getBounds(size);\r
70 \r
71             return size;\r
72         }\r
73         \r
74     }\r
75 }\r