]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/styles/ConnectionLineStyle.java
Fix dynamic visualisations UI color bars disappearing
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / styles / ConnectionLineStyle.java
1 package org.simantics.district.network.ui.styles;
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.DistrictNetworkUtil;
23 import org.simantics.district.network.ontology.DistrictNetworkResource;
24 import org.simantics.district.network.ui.nodes.DistrictNetworkNodeUtils;
25 import org.simantics.layer0.Layer0;
26 import org.simantics.modeling.ModelingResources;
27 import org.simantics.scenegraph.INode;
28 import org.simantics.scenegraph.g2d.G2DNode;
29 import org.simantics.scenegraph.profile.EvaluationContext;
30 import org.simantics.scenegraph.profile.common.ProfileVariables;
31 import org.simantics.scenegraph.utils.GeometryUtils;
32 import org.simantics.scl.compiler.top.ValueNotFound;
33 import org.simantics.scl.osgi.SCLOsgi;
34 import org.simantics.scl.runtime.SCLContext;
35 import org.simantics.scl.runtime.function.Function1;
36 import org.simantics.structural.stubs.StructuralResource2;
37
38 public class ConnectionLineStyle extends StyleBase<List<Point2D>> {
39         
40         public static class ConnectionLineNode extends G2DNode {
41                 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);
42                 private static final Color[] colors = { Color.RED, Color.GREEN, Color.BLUE, Color.ORANGE, Color.CYAN, Color.PINK };
43                 
44                 private float strokeWidth;
45                 private Line2D[] lines;
46
47                 public ConnectionLineNode() {
48                         super();
49                 }
50                 
51                 private static final long serialVersionUID = 1L;
52
53                 @Override
54                 public Rectangle2D getBoundsInLocal() {
55                         return null;
56                 }
57
58                 @Override
59                 public Rectangle2D getBoundsInLocal(boolean b) {
60                         return null;
61                 }
62
63                 @Override
64                 public Rectangle2D getBounds() {
65                         return null;
66                 }
67                 
68                 public void setStrokeWidth(float w) {
69                         strokeWidth = w;
70                 }
71                 
72                 public void setPoints(List<Point2D> result) {
73                         Point2D p0 = DistrictNetworkNodeUtils.calculatePoint2D(result.get(0), null);
74                         lines = new Line2D[result.size() - 1];
75                         for (int i = 1; i < result.size(); i++) 
76                         {
77                                 Point2D p = result.get(i);
78                                 lines[i-1] = p != null ? new Line2D.Double(p0, DistrictNetworkNodeUtils.calculatePoint2D(p, null)) : null;
79                         }
80                 }
81                 
82                 @Override
83                 public void render(Graphics2D g2d) {
84                         if (lines == null || lines.length == 0)
85                                 return;
86                         
87                         // Keep fixed line width on screen
88                         float scaleRecip = (float) GeometryUtils.getScale(g2d.getTransform());
89                         g2d.setStroke(GeometryUtils.scaleStroke(STROKE, strokeWidth / scaleRecip));
90                         
91                         for (int i = 0; i < lines.length; i++) {
92                                 if (lines[i] != null) {
93                                         g2d.setColor(colors[i % colors.length]);
94                                         g2d.draw(lines[i]);
95                                 }
96                         }
97                 }
98         }
99         
100         @Override
101         public List<Point2D> calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem)
102                         throws DatabaseException {
103                 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
104                 ModelingResources MOD = ModelingResources.getInstance(graph);
105                 StructuralResource2 STR = StructuralResource2.getInstance(graph);
106                 
107                 Resource vertex = groupItem;
108                 if (!graph.isInstanceOf(vertex, DN.Vertex))
109                         return Collections.emptyList();
110                 
111                 double[] coords = graph.getRelatedValue(vertex, DiagramResource.getInstance(graph).HasLocation);
112                 
113                 Resource component = DistrictNetworkUtil.getMappedComponentCached(graph, vertex);
114                 if (component == null)
115                         return Collections.emptyList();
116                 
117                 Resource componentType = graph.getPossibleType(component, STR.Component);
118                 if (componentType == null)
119                         return Collections.emptyList();
120                 
121                 Function1<Resource, List<Resource>> fun = getConnectedComponentsFunctionCached(graph, componentType);
122                 if (fun == null)
123                         return Collections.emptyList();
124
125                 List<Resource> components = Simantics.applySCLRead(graph, fun, component);
126                 
127                 if (components == null || components.isEmpty())
128                         return Collections.emptyList();
129                 
130                 List<Point2D> result = new ArrayList<>(components.size() + 1);
131                 result.add(new Point2D.Double(coords[0], coords[1]));
132                 for (Resource comp : components) {
133                         Resource e = comp != null ? graph.getPossibleObject(comp, MOD.ComponentToElement) : null;
134                         Resource mappingElement = e != null ? graph.getPossibleObject(e, DN.MappedFromElement) : null;
135                         if (mappingElement != null) {
136                                 double[] coords2 = graph.getRelatedValue(mappingElement, DiagramResource.getInstance(graph).HasLocation);
137                                 result.add(new Point2D.Double(coords2[0], coords2[1]));
138                         }
139                         else {
140                                 result.add(null);
141                         }
142                 }
143                 
144                 return result;
145         }
146         
147         @Override
148         public void applyStyleForNode(EvaluationContext observer, INode parent, List<Point2D> result) {
149                 if (result == null || result.size() < 2) {
150                         ProfileVariables.denyChild(parent, "*", "districtNetworkConnection");
151                         return;
152                 }
153                 
154                 ConnectionLineNode node = ProfileVariables.claimChild(parent, "*", "districtNetworkConnection", ConnectionLineNode.class, observer);
155                 if (node == null)
156                         return;
157                 
158                 node.setPoints(result);
159                 node.setZIndex(0);
160                 node.setStrokeWidth(2.f);
161         }
162
163         @Override
164         protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode parent) {
165                 ProfileVariables.denyChild(parent, "*", "districtNetworkConnection");
166         }
167         
168         private static Function1<Resource, List<Resource>> getConnectedComponentsFunctionCached(ReadGraph graph, Resource componentType)
169                         throws DatabaseException {
170                 return graph.syncRequest(new ConnectedComponentsFunctionRequest(componentType), TransientCacheListener.instance());
171         }
172         
173         private static final class ConnectedComponentsFunctionRequest
174                         extends ResourceRead<Function1<Resource, List<Resource>>> {
175                 public ConnectedComponentsFunctionRequest(Resource resource) {
176                         super(resource);
177                 }
178
179                 @SuppressWarnings("unchecked")
180                 @Override
181                 public Function1<Resource, List<Resource>> perform(ReadGraph graph) throws DatabaseException {
182                         Resource actionsModule = Layer0Utils.getPossibleChild(graph, resource, "Actions");
183                         if (actionsModule == null || !graph.isInstanceOf(actionsModule, Layer0.getInstance(graph).SCLModule))
184                                 return null;
185                         
186                         String uri = graph.getURI(actionsModule);
187                         SCLContext sclContext = SCLContext.getCurrent();
188                         Object oldGraph = sclContext.get("graph");
189                         try {
190                                 sclContext.put("graph", graph);
191                                 return (Function1<Resource, List<Resource>>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, "getConnectedComponents");
192                         } catch (ValueNotFound e1) {
193                                 return null;
194                         } finally {
195                                 sclContext.put("graph", oldGraph);
196                         }
197                 }
198         }
199 }