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