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