]> gerrit.simantics Code Review - simantics/district.git/blob
e452a17b1f52188d8ab43b48de2a4302c5ad2a8a
[simantics/district.git] /
1 package org.simantics.district.network.ui.styles;
2
3 import java.awt.geom.Point2D;
4 import java.util.Set;
5
6 import org.simantics.Simantics;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
10 import org.simantics.db.common.request.ResourceRead;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.exception.MissingVariableValueException;
13 import org.simantics.db.layer0.exception.PendingVariableException;
14 import org.simantics.db.layer0.util.Layer0Utils;
15 import org.simantics.db.layer0.variable.Variable;
16 import org.simantics.db.layer0.variable.Variables;
17 import org.simantics.diagram.profile.StyleBase;
18 import org.simantics.diagram.stubs.DiagramResource;
19 import org.simantics.district.network.DistrictNetworkUtil;
20 import org.simantics.district.network.ontology.DistrictNetworkResource;
21 import org.simantics.district.network.profile.MidBranchEdgeSetRequest;
22 import org.simantics.district.network.ui.nodes.DistrictNetworkNodeUtils;
23 import org.simantics.district.network.ui.nodes.DistrictNetworkStaticInfoNode;
24 import org.simantics.layer0.Layer0;
25 import org.simantics.scenegraph.INode;
26 import org.simantics.scenegraph.profile.EvaluationContext;
27 import org.simantics.scenegraph.profile.common.ProfileVariables;
28 import org.simantics.scl.compiler.top.ValueNotFound;
29 import org.simantics.scl.osgi.SCLOsgi;
30 import org.simantics.scl.runtime.SCLContext;
31 import org.simantics.scl.runtime.function.Function1;
32 import org.simantics.structural.stubs.StructuralResource2;
33
34 public class DistrictNetworkStaticInfoStyle extends StyleBase<DistrictNetworkStaticInfoStyle.StyleResult> {
35
36         private static final String ACTIONS_MODULE = "Actions";
37         private static final String PIPELINE_INFO = "pipelineInfo";
38         
39         public static class StyleResult {
40                 public StyleResult(Point2D p1, Point2D p2, String info) {
41                         this.p1 = p1;
42                         this.p2 = p2;
43                         this.info = info;
44                 }
45                 
46                 public Point2D p1;
47                 public Point2D p2;
48                 public String info;
49         }
50         
51         public DistrictNetworkStaticInfoStyle(Resource style) {
52                 super(style);
53         }
54         
55         @Override
56         public StyleResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource mapElement)
57                         throws DatabaseException {
58                 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
59                 
60                 boolean isEdge = graph.isInstanceOf(mapElement, DN.Edge);
61                 boolean isVertex = graph.isInstanceOf(mapElement, DN.Vertex);
62                 if (!isEdge && !isVertex)
63                         return null;
64                 
65                 if (isEdge) {
66                         Resource diagram = graph.getSingleObject(mapElement, Layer0.getInstance(graph).PartOf);
67                         Set<Resource> edgesToUse = graph.syncRequest(new MidBranchEdgeSetRequest(diagram), TransientCacheListener.instance());
68                         if (!edgesToUse.contains(mapElement))
69                                 return null;
70                 }
71                 
72                 DiagramResource DIA = DiagramResource.getInstance(graph);
73                 StructuralResource2 STR = StructuralResource2.getInstance(graph);
74                 
75                 Resource module = DistrictNetworkUtil.getMappedComponentCached(graph, mapElement);
76                 if (module == null)
77                         return null;
78
79                 Resource moduleType = graph.getPossibleType(module, STR.Component);
80                 if (moduleType == null)
81                         return null;
82                 
83                 Function1<Variable, String> function = getUCPipelineInfoFunctionCached(graph, moduleType);
84                 if (function == null)
85                         return null;
86                 
87                 String result;
88                 try {
89                         Variable variable = Variables.getVariable(graph, module);
90                         Variable moduleVariable = Variables.possibleActiveVariable(graph, variable);
91                         if (moduleVariable == null)
92                                 moduleVariable = variable;
93
94                         result = Simantics.applySCLRead(graph, function, moduleVariable);
95                 } catch (PendingVariableException | MissingVariableValueException e) {
96                         result = null;
97                 }
98                 
99                 if (isVertex) {
100                         double[] coords = graph.getRelatedValue(mapElement, DIA.HasLocation);
101                         Point2D p = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double(coords[0], coords[1]), null);
102                         return new StyleResult(p, p, result);
103                 }
104                 else if (isEdge) {
105                         Resource v1 = graph.getSingleObject(mapElement, DN.HasStartVertex);
106                         double[] coords1 = graph.getRelatedValue(v1, DIA.HasLocation);
107                         Resource v2 = graph.getSingleObject(mapElement, DN.HasEndVertex);
108                         double[] coords2 = graph.getRelatedValue(v2, DIA.HasLocation);
109                         Point2D p1 = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double(coords1[0], coords1[1]), null);
110                         Point2D p2 = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double(coords2[0], coords2[1]), null);
111                         
112                         return new StyleResult(p1, p2, result);
113                 }
114                 
115                 return null;
116         }
117         
118         @Override
119         public void applyStyleForNode(EvaluationContext evaluationContext, INode parent, StyleResult result) {
120                 if (result == null) {
121                         cleanupStyleForNode(evaluationContext, parent);
122                         return;
123                 }
124                 
125                 DistrictNetworkStaticInfoNode node = ProfileVariables.claimChild(parent, "*", DistrictNetworkStaticInfoNode.NODE_KEY, DistrictNetworkStaticInfoNode.class, evaluationContext);
126                 if (node == null)
127                         return;
128
129                 Point2D p1 = result.p1;
130                 Point2D p2 = result.p2;
131                 
132                 if (p1.equals(p2)) {
133                         node.setLocation(p1, new Point2D.Double(1.0, 0.0));
134                 }
135                 else {
136                         double sign = Math.signum(p1.getX() - p2.getX());
137                         Point2D.Double origin = new Point2D.Double(0.5 * (p1.getX() + p2.getX()), 0.5 * (p1.getY() + p2.getY()));
138                         Point2D direction = new Point2D.Double(0.5 * sign * (p1.getX() - p2.getX()), 0.5 * sign * (p1.getY() - p2.getY()));
139                         
140                         node.setLocation(origin, direction);
141                 }
142                 
143                 node.setInfo(result.info);
144         }
145         
146         private static Function1<Variable, String> getUCPipelineInfoFunctionCached(ReadGraph graph, Resource componentType)
147                         throws DatabaseException {
148                 return graph.syncRequest(new UCPipelineInfoRequest(componentType), TransientCacheListener.instance());
149         }
150         
151         private static final class UCPipelineInfoRequest extends ResourceRead<Function1<Variable, String>> {
152                 public UCPipelineInfoRequest(Resource resource) {
153                         super(resource);
154                 }
155
156                 @SuppressWarnings("unchecked")
157                 @Override
158                 public Function1<Variable, String> perform(ReadGraph graph) throws DatabaseException {
159                         Resource actionsModule = Layer0Utils.getPossibleChild(graph, resource, ACTIONS_MODULE);
160                         if (actionsModule == null || !graph.isInstanceOf(actionsModule, Layer0.getInstance(graph).SCLModule))
161                                 return null;
162                         
163                         String uri = graph.getURI(actionsModule);
164                         SCLContext sclContext = SCLContext.getCurrent();
165                         Object oldGraph = sclContext.get("graph");
166                         try {
167                                 sclContext.put("graph", graph);
168                                 return (Function1<Variable, String>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, PIPELINE_INFO);
169                         } catch (ValueNotFound e1) {
170                                 return null;
171                         } finally {
172                                 sclContext.put("graph", oldGraph);
173                         }
174                 }
175         }
176         
177         @Override
178         protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode node) {
179                 ProfileVariables.denyChild(node, "*", DistrictNetworkStaticInfoNode.NODE_KEY);
180         }
181 }