]> gerrit.simantics Code Review - simantics/district.git/blob
12f26182dca15fe03cd4cf02ff6588a2a3954f3f
[simantics/district.git] /
1 package org.simantics.district.network.ui.adapters;
2
3 import java.awt.geom.Rectangle2D;
4 import java.util.Collection;
5 import java.util.Collections;
6
7 import org.simantics.district.network.ui.DistrictNetworkEdge;
8 import org.simantics.district.network.ui.nodes.DistrictNetworkEdgeNode;
9 import org.simantics.g2d.connection.handler.ConnectionHandler;
10 import org.simantics.g2d.diagram.handler.Topology.Connection;
11 import org.simantics.g2d.element.ElementClass;
12 import org.simantics.g2d.element.ElementUtils;
13 import org.simantics.g2d.element.IElement;
14 import org.simantics.g2d.element.SceneGraphNodeKey;
15 import org.simantics.g2d.element.handler.InternalSize;
16 import org.simantics.g2d.element.handler.SceneGraph;
17 import org.simantics.g2d.element.handler.impl.DefaultTransform;
18 import org.simantics.g2d.element.handler.impl.SimpleElementLayers;
19 import org.simantics.scenegraph.g2d.G2DParentNode;
20 import org.simantics.utils.datastructures.hints.IHintContext.Key;
21 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class DistrictNetworkEdgeElement {
26
27     public static final Key KEY_DN_EDGE = new KeyOf(DistrictNetworkEdge.class, "DN_EDGE");
28     public static final Key KEY_DN_EDGE_NODE = new SceneGraphNodeKey(DistrictNetworkEdgeNode.class, "DN_EDGE_NODE");
29     
30     public static final ElementClass CLASS =
31             ElementClass.compile(
32                     DefaultTransform.INSTANCE,
33                     DNEdgeInternalSize.INSTANCE,
34                     DNEdgeSceneGraph.INSTANCE,
35                     DNEdgeConnectionHandler.INSTANCE,
36                     SimpleElementLayers.INSTANCE
37             ).setId(DistrictNetworkEdgeElement.class.getSimpleName());
38     
39     static final class DNEdgeSceneGraph implements SceneGraph {
40         
41         public static final DNEdgeSceneGraph INSTANCE = new DNEdgeSceneGraph();
42
43         private static final long serialVersionUID = 8894367073815556871L;
44
45         @Override
46         public void init(IElement edgeElement, G2DParentNode parent) {
47             DistrictNetworkEdge edge = edgeElement.getHint(KEY_DN_EDGE);
48             if (edge == null) {
49                 cleanup(edgeElement);
50             } else {
51                 DistrictNetworkEdgeNode node = edgeElement.getHint(KEY_DN_EDGE_NODE);
52                 if (node == null) {
53                     node = parent.addNode(ElementUtils.generateNodeId(edgeElement), DistrictNetworkEdgeNode.class);
54                     edgeElement.setHint(KEY_DN_EDGE_NODE, node);
55                 }
56                 node.setDNEdge(edge);
57             }
58         }
59
60         @Override
61         public void cleanup(IElement edge) {
62             ElementUtils.removePossibleNode(edge, KEY_DN_EDGE_NODE);
63             edge.removeHint(KEY_DN_EDGE_NODE);
64         }
65     }
66     
67     static final class DNEdgeInternalSize implements InternalSize {
68
69         private static final Logger LOGGER = LoggerFactory.getLogger(DNEdgeInternalSize.class);
70         
71         private static final long serialVersionUID = -2725017034692179676L;
72         
73         public static final DNEdgeInternalSize INSTANCE = new DNEdgeInternalSize();
74
75         @Override
76         public Rectangle2D getBounds(IElement e, Rectangle2D size) {
77             DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);
78             if (size == null)
79                 size = new Rectangle2D.Double();
80             if (edge != null)
81                 edge.getBounds(size);
82             else
83                 LOGGER.debug("Element {} does not have edge!", e);
84
85             return size;
86         }
87         
88     }
89     
90     static class DNEdgeConnectionHandler implements ConnectionHandler {
91
92         private static final long serialVersionUID = -410377314637446238L;
93         
94         public static final DNEdgeConnectionHandler INSTANCE = new DNEdgeConnectionHandler();
95
96         @Override
97         public Collection<IElement> getChildren(IElement connection, Collection<IElement> result) {
98             return Collections.emptyList();
99         }
100
101         @Override
102         public Collection<IElement> getBranchPoints(IElement connection, Collection<IElement> result) {
103             return Collections.emptyList();
104         }
105
106         @Override
107         public Collection<IElement> getSegments(IElement connection, Collection<IElement> result) {
108             return Collections.emptyList();
109         }
110
111         @Override
112         public Collection<Connection> getTerminalConnections(IElement connection, Collection<Connection> result) {
113             return Collections.emptyList();
114         }
115     }
116 }