]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/ConnectionLineStyle.java
Connection lines for control inputs.
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / nodes / ConnectionLineStyle.java
1 package org.simantics.district.network.ui.nodes;
2
3 import java.awt.BasicStroke;
4 import java.awt.Color;
5 import java.awt.Graphics2D;
6 import java.awt.geom.Line2D;
7 import java.awt.geom.Point2D;
8 import java.awt.geom.Rectangle2D;
9 import java.util.ArrayList;
10 import java.util.Collections;
11 import java.util.List;
12
13 import org.simantics.Simantics;
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
17 import org.simantics.db.common.request.ResourceRead;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.layer0.util.Layer0Utils;
20 import org.simantics.diagram.profile.StyleBase;
21 import org.simantics.diagram.stubs.DiagramResource;
22 import org.simantics.district.network.ontology.DistrictNetworkResource;
23 import org.simantics.layer0.Layer0;
24 import org.simantics.modeling.ModelingResources;
25 import org.simantics.scenegraph.INode;
26 import org.simantics.scenegraph.g2d.G2DNode;
27 import org.simantics.scenegraph.profile.EvaluationContext;
28 import org.simantics.scenegraph.profile.common.ProfileVariables;
29 import org.simantics.scenegraph.utils.GeometryUtils;
30 import org.simantics.structural.stubs.StructuralResource2;
31
32 public class ConnectionLineStyle extends StyleBase<List<Point2D>> {
33         
34         public static class ConnectionLineNode extends G2DNode {
35                 private static final BasicStroke STROKE = new BasicStroke(1.f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1.f, new float[] {4.f, 2.f}, 0.f);
36                 private static final Color[] colors = { Color.RED, Color.GREEN, Color.BLUE, Color.ORANGE, Color.CYAN, Color.PINK };
37                 
38                 private float strokeWidth;
39                 private Line2D[] lines;
40
41                 public ConnectionLineNode() {
42                         super();
43                 }
44                 
45                 private static final long serialVersionUID = 1L;
46
47                 @Override
48                 public Rectangle2D getBoundsInLocal() {
49                         return null;
50                 }
51
52                 @Override
53                 public Rectangle2D getBoundsInLocal(boolean b) {
54                         return null;
55                 }
56
57                 @Override
58                 public Rectangle2D getBounds() {
59                         return null;
60                 }
61                 
62                 public void setStrokeWidth(float w) {
63                         strokeWidth = w;
64                 }
65                 
66                 public void setPoints(List<Point2D> result) {
67                         Point2D p0 = DistrictNetworkNodeUtils.calculatePoint2D(result.get(0), null);
68                         lines = new Line2D[result.size() - 1];
69                         for (int i = 1; i < result.size(); i++) 
70                         {
71                                 Point2D p = result.get(i);
72                                 lines[i-1] = p != null ? new Line2D.Double(p0, DistrictNetworkNodeUtils.calculatePoint2D(p, null)) : null;
73                         }
74                 }
75                 
76                 @Override
77                 public void render(Graphics2D g2d) {
78                         if (lines == null || lines.length == 0)
79                                 return;
80                         
81                         // Keep fixed line width on screen
82                         float scaleRecip = (float) GeometryUtils.getScale(g2d.getTransform());
83                         g2d.setStroke(GeometryUtils.scaleStroke(STROKE, strokeWidth / scaleRecip));
84                         
85                         for (int i = 0; i < lines.length; i++) {
86                                 if (lines[i] != null) {
87                                         g2d.setColor(colors[i % colors.length]);
88                                         g2d.draw(lines[i]);
89                                 }
90                         }
91                 }
92         }
93         
94         @Override
95         public List<Point2D> calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem)
96                         throws DatabaseException {
97                 return graph.syncRequest(new ElementConnectionRequest(groupItem), TransientCacheListener.instance());
98         }
99         
100         @Override
101         public void applyStyleForNode(EvaluationContext observer, INode parent, List<Point2D> result) {
102                 if (result == null || result.size() < 2) {
103                         ProfileVariables.denyChild(parent, "*", "districtNetworkConnection");
104                         return;
105                 }
106                 
107                 ConnectionLineNode node = ProfileVariables.claimChild(parent, "*", "districtNetworkConnection", ConnectionLineNode.class, observer);
108                 if (node == null)
109                         return;
110                 
111                 node.setPoints(result);
112                 node.setZIndex(0);
113                 node.setStrokeWidth(2.f);
114         }
115
116         @Override
117         protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode parent) {
118                 ProfileVariables.denyChild(parent, "*", "districtNetworkConnection");
119         }
120         
121         private static final class ElementConnectionRequest extends ResourceRead<List<Point2D>> {
122                 private ElementConnectionRequest(Resource resource) {
123                         super(resource);
124                 }
125         
126                 @Override
127                 public List<Point2D> perform(ReadGraph graph) throws DatabaseException {
128                         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
129                         ModelingResources MOD = ModelingResources.getInstance(graph);
130                         StructuralResource2 STR = StructuralResource2.getInstance(graph);
131                         
132                         Resource vertex = resource;
133                         if (!graph.isInstanceOf(vertex, DN.Vertex))
134                                 return Collections.emptyList();
135                         
136                         double[] coords = graph.getRelatedValue(vertex, DiagramResource.getInstance(graph).HasLocation);
137                         
138                         Resource element = graph.getPossibleObject(vertex, DN.MappedComponent);
139                         if (element == null)
140                                 return Collections.emptyList();
141                         Resource component = graph.getPossibleObject(element, MOD.ElementToComponent);
142                         if (component == null)
143                                 return Collections.emptyList();
144                         Resource componentType = graph.getPossibleType(component, STR.Component);
145                         if (componentType == null)
146                                 return Collections.emptyList();
147                         
148                         Resource actionsModule = Layer0Utils.getPossibleChild(graph, componentType, "Actions");
149                         if (actionsModule == null || !graph.isInstanceOf(actionsModule, Layer0.getInstance(graph).SCLModule))
150                                 return Collections.emptyList();
151                         
152                         List<Resource> components;
153                         try {
154                                 components = Simantics.applySCL(graph.getURI(actionsModule), "getConnectedComponents", graph, component);
155                         }
156                         catch (DatabaseException e) {
157                                 return Collections.emptyList();
158                         }
159                         
160                         if (components == null || components.isEmpty())
161                                 return Collections.emptyList();
162                         
163                         List<Point2D> result = new ArrayList<>(components.size() + 1);
164                         result.add(new Point2D.Double(coords[0], coords[1]));
165                         for (Resource comp : components) {
166                                 Resource e = comp != null ? graph.getPossibleObject(comp, MOD.ComponentToElement) : null;
167                                 Resource mappingElement = e != null ? graph.getPossibleObject(e, DN.MappedFromElement) : null;
168                                 if (mappingElement != null) {
169                                         double[] coords2 = graph.getRelatedValue(mappingElement, DiagramResource.getInstance(graph).HasLocation);
170                                         result.add(new Point2D.Double(coords2[0], coords2[1]));
171                                 }
172                                 else {
173                                         result.add(null);
174                                 }
175                         }
176                         
177                         return result;
178                 }
179         }
180 }