1 package org.simantics.district.network.ui.adapters;
\r
3 import java.awt.Dimension;
\r
4 import java.awt.geom.Rectangle2D;
\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
18 public class DistrictNetworkVertexElement {
\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
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
30 static final class DNVertexSceneGraph implements SceneGraph {
\r
32 public static final DNVertexSceneGraph INSTANCE = new DNVertexSceneGraph();
\r
34 private static final long serialVersionUID = 8894367073815556871L;
\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
42 DistrictNetworkVertexNode node = edgeElement.getHint(KEY_DN_VERTEX_NODE);
\r
44 node = parent.addNode(ElementUtils.generateNodeId(edgeElement), DistrictNetworkVertexNode.class);
\r
45 edgeElement.setHint(KEY_DN_VERTEX_NODE, node);
\r
47 node.setVertex(vertex);
\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
58 static final class DNVertexInternalSize implements InternalSize {
\r
60 public static final DNVertexInternalSize INSTANCE = new DNVertexInternalSize();
\r
62 private static final long serialVersionUID = 2625702114278956461L;
\r
65 public Rectangle2D getBounds(IElement e, Rectangle2D size) {
\r
66 DistrictNetworkVertex vertex = e.getHint(KEY_DN_VERTEX);
\r
68 size = new Rectangle2D.Double();
\r
69 size.setFrame(vertex.getPoint(), new Dimension(1, 1));
\r