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