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