]> gerrit.simantics Code Review - simantics/district.git/blob
7be6a14faa659abb28df213e694b14e9094dd848
[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.INode;
17 import org.simantics.scenegraph.g2d.G2DParentNode;
18 import org.simantics.scenegraph.g2d.nodes.SVGNode;
19 import org.simantics.utils.datastructures.hints.IHintContext.Key;
20 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
21
22 public class DistrictNetworkVertexElement {
23
24     public static final Key KEY_DN_VERTEX = new KeyOf(DistrictNetworkVertex.class, "DN_VERTEX");
25     public static final Key KEY_DN_VERTEX_NODE = new SceneGraphNodeKey(DistrictNetworkVertexNode.class, "DN_VERTEX_NODE");
26     public static final Key KEY_DN_VERTEX_SYMBOL_NODE = new SceneGraphNodeKey(SVGNode.class, "DN_VERTEX_SYMBOL_NODE");
27     
28     public static final ElementClass CLASS =
29             ElementClass.compile(
30                     DefaultTransform.INSTANCE,
31                     DNVertexInternalSize.INSTANCE,
32                     DNVertexSceneGraph.INSTANCE,
33                     SimpleElementLayers.INSTANCE,
34                     DistrictNetworkAdditionalColor.INSTANCE
35             ).setId(DistrictNetworkVertexElement.class.getSimpleName());
36     
37     static final class DNVertexSceneGraph implements SceneGraph {
38         
39         public static final DNVertexSceneGraph INSTANCE = new DNVertexSceneGraph();
40
41         private static final long serialVersionUID = 8894367073815556871L;
42
43         @Override
44         public void init(IElement vertexElement, G2DParentNode parent) {
45             DistrictNetworkVertex vertex = vertexElement.getHint(KEY_DN_VERTEX);
46             if (vertex == null) {
47                 cleanup(vertexElement);
48             } else {
49                 DistrictNetworkVertexNode node = vertexElement.getHint(KEY_DN_VERTEX_NODE);
50                 if (node == null) {
51                     node = parent.addNode(ElementUtils.generateNodeId(vertexElement), DistrictNetworkVertexNode.class);
52                     vertexElement.setHint(KEY_DN_VERTEX_NODE, node);
53                     
54                     SVGNode symbol = node.addNode(ElementUtils.generateNodeId(vertexElement), SVGNode.class);
55                     vertexElement.setHint(KEY_DN_VERTEX_SYMBOL_NODE, symbol);
56                 }
57                 
58                 node.setVertex(vertex);
59                 
60                 node.setColor(ElementUtils.getAdditionalColor(vertexElement, Color.BLUE));
61                 
62                 AffineTransform at = ElementUtils.getTransform(vertexElement);
63                 if(at != null)
64                     node.setTransform(at);
65             }
66         }
67
68         @Override
69         public void cleanup(IElement edge) {
70             ElementUtils.removePossibleNode(edge, KEY_DN_VERTEX_SYMBOL_NODE);
71             INode node = ElementUtils.removePossibleNode(edge, KEY_DN_VERTEX_NODE);
72             if (node != null)
73                 node.getParent().remove();
74             
75             edge.removeHint(KEY_DN_VERTEX_NODE);
76             edge.removeHint(KEY_DN_VERTEX_SYMBOL_NODE);
77         }
78     }
79     
80     static final class DNVertexInternalSize implements InternalSize {
81
82         public static final DNVertexInternalSize INSTANCE = new DNVertexInternalSize();
83         
84         private static final long serialVersionUID = 2625702114278956461L;
85
86         @Override
87         public Rectangle2D getBounds(IElement e, Rectangle2D size) {
88             DistrictNetworkVertexNode node = e.getHint(KEY_DN_VERTEX_NODE);
89             Rectangle2D boundsInLocal = node.getBoundsInLocal();
90             size.setFrame(boundsInLocal);
91             return size;
92         }
93         
94     }
95 }