]> gerrit.simantics Code Review - simantics/district.git/blob
2a0cb1229c783f645d416e28a646af6d22311580
[simantics/district.git] /
1 package org.simantics.district.network.ui.adapters;
2
3 import java.awt.Color;
4 import java.awt.geom.AffineTransform;
5 import java.awt.geom.Rectangle2D;
6
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;
19
20 public class DistrictNetworkVertexElement {
21
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");
24     
25     public static final ElementClass CLASS =
26             ElementClass.compile(
27                     DefaultTransform.INSTANCE,
28                     DNVertexInternalSize.INSTANCE,
29                     DNVertexSceneGraph.INSTANCE,
30                     SimpleElementLayers.INSTANCE,
31                     DistrictNetworkAdditionalColor.INSTANCE
32             ).setId(DistrictNetworkVertexElement.class.getSimpleName());
33     
34     static final class DNVertexSceneGraph implements SceneGraph {
35         
36         public static final DNVertexSceneGraph INSTANCE = new DNVertexSceneGraph();
37
38         private static final long serialVersionUID = 8894367073815556871L;
39
40         @Override
41         public void init(IElement vertexElement, G2DParentNode parent) {
42             DistrictNetworkVertex vertex = vertexElement.getHint(KEY_DN_VERTEX);
43             if (vertex == null) {
44                 cleanup(vertexElement);
45             } else {
46                 DistrictNetworkVertexNode node = vertexElement.getHint(KEY_DN_VERTEX_NODE);
47                 if (node == null) {
48                     node = parent.addNode(ElementUtils.generateNodeId(vertexElement), DistrictNetworkVertexNode.class);
49                     vertexElement.setHint(KEY_DN_VERTEX_NODE, node);
50                 }
51                 node.setVertex(vertex);
52                 
53                 node.setColor(ElementUtils.getAdditionalColor(vertexElement, Color.BLUE));
54                 
55                 AffineTransform at = ElementUtils.getTransform(vertexElement);
56                 if(at != null)
57                     node.setTransform(at);
58             }
59         }
60
61         @Override
62         public void cleanup(IElement edge) {
63             ElementUtils.removePossibleNode(edge, KEY_DN_VERTEX_NODE);
64             edge.removeHint(KEY_DN_VERTEX_NODE);
65         }
66     }
67     
68     static final class DNVertexInternalSize implements InternalSize {
69
70         public static final DNVertexInternalSize INSTANCE = new DNVertexInternalSize();
71         
72         private static final long serialVersionUID = 2625702114278956461L;
73
74         @Override
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);
79             return size;
80         }
81         
82     }
83 }