]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/styles/DistrictNetworkStaticInfoStyle.java
89e48813d5551130b6e37dc5a4bb66d7bbbb48c7
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / styles / DistrictNetworkStaticInfoStyle.java
1 package org.simantics.district.network.ui.styles;
2
3 import java.awt.geom.Point2D;
4 import java.util.Objects;
5 import java.util.Set;
6
7 import org.simantics.Simantics;
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.Resource;
10 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
11 import org.simantics.db.common.request.ResourceRead;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.layer0.exception.MissingVariableException;
14 import org.simantics.db.layer0.exception.MissingVariableValueException;
15 import org.simantics.db.layer0.exception.PendingVariableException;
16 import org.simantics.db.layer0.util.Layer0Utils;
17 import org.simantics.db.layer0.variable.Variable;
18 import org.simantics.db.layer0.variable.Variables;
19 import org.simantics.diagram.profile.StyleBase;
20 import org.simantics.diagram.stubs.DiagramResource;
21 import org.simantics.district.network.DistrictNetworkUtil;
22 import org.simantics.district.network.ontology.DistrictNetworkResource;
23 import org.simantics.district.network.profile.MidBranchEdgeSetRequest;
24 import org.simantics.district.network.ui.nodes.DeferredRenderingNode;
25 import org.simantics.district.network.ui.nodes.DistrictNetworkEdgeNode;
26 import org.simantics.district.network.ui.nodes.DistrictNetworkNodeUtils;
27 import org.simantics.district.network.ui.nodes.DistrictNetworkStaticInfoNode;
28 import org.simantics.layer0.Layer0;
29 import org.simantics.scenegraph.INode;
30 import org.simantics.scenegraph.ParentNode;
31 import org.simantics.scenegraph.g2d.IG2DNode;
32 import org.simantics.scenegraph.g2d.nodes.ConnectionNode;
33 import org.simantics.scenegraph.g2d.nodes.spatial.RTreeNode;
34 import org.simantics.scenegraph.profile.EvaluationContext;
35 import org.simantics.scenegraph.profile.common.ProfileVariables;
36 import org.simantics.scenegraph.utils.NodeUtil;
37 import org.simantics.scl.compiler.top.ValueNotFound;
38 import org.simantics.scl.osgi.SCLOsgi;
39 import org.simantics.scl.runtime.SCLContext;
40 import org.simantics.scl.runtime.function.Function1;
41 import org.simantics.structural.stubs.StructuralResource2;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 public class DistrictNetworkStaticInfoStyle extends StyleBase<DistrictNetworkStaticInfoStyle.StyleResult> {
46
47     private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkStaticInfoStyle.class);
48
49         private static final String ACTIONS_MODULE = "Actions";
50         private static final String PIPELINE_INFO = "pipelineInfo";
51
52         public static final String STATIC_INFO_DEFERRED = "staticInfo";
53
54         public static class StyleResult {
55                 public StyleResult(Resource r, Point2D p1, String info) {
56                         this.r = r;
57                         this.p1 = p1;
58                         this.info = info;
59                 }
60
61                 public final Resource r;
62                 public final Point2D p1;
63                 public final String info;
64
65                 @Override
66                 public int hashCode() {
67                         final int prime = 31;
68                         int result = 1;
69                         result = prime * result + r.hashCode();
70                         result = prime * result + ((info == null) ? 0 : info.hashCode());
71                         result = prime * result + ((p1 == null) ? 0 : p1.hashCode());
72                         return result;
73                 }
74                 @Override
75                 public boolean equals(Object obj) {
76                         if (this == obj)
77                                 return true;
78                         if (obj == null)
79                                 return false;
80                         if (getClass() != obj.getClass())
81                                 return false;
82                         StyleResult other = (StyleResult) obj;
83                         return r.equals(other.r) && Objects.equals(info, other.info) && Objects.equals(p1, other.p1);
84                 }
85         }
86
87         public DistrictNetworkStaticInfoStyle(Resource style) {
88                 super();
89         }
90
91         private static final Point2D EDGE = new Point2D.Double();
92
93         @Override
94         public StyleResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource mapElement)
95                         throws DatabaseException {
96                 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
97                 Set<Resource> types = graph.getTypes(mapElement);
98                 boolean isEdge = types.contains(DN.Edge);
99                 boolean isVertex = types.contains(DN.Vertex);
100                 if (!isEdge && !isVertex)
101                         return null;
102
103                 if (isEdge) {
104                         Resource diagram = graph.getSingleObject(mapElement, Layer0.getInstance(graph).PartOf);
105                         Set<Resource> edgesToUse = graph.syncRequest(new MidBranchEdgeSetRequest(diagram), TransientCacheListener.instance());
106                         if (!edgesToUse.contains(mapElement))
107                                 return null;
108                 }
109
110                 Resource module = DistrictNetworkUtil.getMappedComponentCached(graph, mapElement);
111                 if (module == null)
112                         return null;
113
114                 StructuralResource2 STR = StructuralResource2.getInstance(graph);
115                 Resource moduleType = graph.getPossibleType(module, STR.Component);
116                 if (moduleType == null)
117                         return null;
118
119                 Function1<Variable, String> function = getUCPipelineInfoFunctionCached(graph, moduleType);
120                 if (function == null)
121                         return null;
122
123                 String result;
124                 try {
125                         Variable variable = Variables.getVariable(graph, module);
126                         Variable moduleVariable = Variables.possibleActiveVariable(graph, variable);
127                         if (moduleVariable == null)
128                                 moduleVariable = variable;
129
130                         result = Simantics.applySCLRead(graph, function, moduleVariable);
131                 } catch (PendingVariableException | MissingVariableValueException e) {
132                         result = null;
133                 } catch (MissingVariableException e) {
134                         // the requested variable is missing from the UC
135                         String message = e.getMessage();
136                         LOGGER.warn("Missing variable for calculating style with function {} {}", function, message);
137                         result = "<" + message + ">";
138                 }
139
140                 if (isVertex) {
141                         DiagramResource DIA = DiagramResource.getInstance(graph);
142                         double[] coords = graph.getRelatedValue(mapElement, DIA.HasLocation);
143                         Point2D p = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double(coords[0], coords[1]), null);
144                         return new StyleResult(mapElement, p, result);
145                 } else if (isEdge) {
146                         return new StyleResult(mapElement, EDGE, result);
147                 }
148                 return null;
149         }
150
151         @Override
152         public void applyStyleForNode(EvaluationContext evaluationContext, INode parent, StyleResult result) {
153                 if (result == null) {
154                         cleanupStyleForNode(evaluationContext, parent);
155                         return;
156                 }
157
158                 ParentNode<?> root = (ParentNode<?>) NodeUtil.getNearestParentOfType(parent, RTreeNode.class);
159                 if (root != null) {
160                         DeferredRenderingNode deferred = ProfileVariables.claimChild(root, "", STATIC_INFO_DEFERRED, DeferredRenderingNode.class, evaluationContext);
161                         deferred.setZIndex(Integer.MAX_VALUE-1);
162                 }
163
164                 DistrictNetworkStaticInfoNode node = ProfileVariables.claimChild(parent, "*", DistrictNetworkStaticInfoNode.NODE_KEY, DistrictNetworkStaticInfoNode.class, evaluationContext);
165                 if (node == null)
166                         return;
167
168                 Point2D p1 = result.p1;
169                 if (p1 != EDGE) {
170                         node.setLocation(p1, new Point2D.Double(1.0, 0.0));
171                 } else {
172                         // StaticInfoNode takes care of positioning the text during render
173                         // based on the found DistrictNetworkEdgeNode's information.
174                         for (IG2DNode n : ((ConnectionNode)parent).getNodes()) {
175                                 if (n instanceof DistrictNetworkEdgeNode) {
176                                         node.setEdgeNode((DistrictNetworkEdgeNode) n);
177                                 }
178                         }
179                 }
180
181                 node.setInfo(result.info);
182         }
183
184         private static Function1<Variable, String> getUCPipelineInfoFunctionCached(ReadGraph graph, Resource componentType)
185                         throws DatabaseException {
186                 return graph.syncRequest(new UCPipelineInfoRequest(componentType), TransientCacheListener.instance());
187         }
188
189         private static final class UCPipelineInfoRequest extends ResourceRead<Function1<Variable, String>> {
190                 public UCPipelineInfoRequest(Resource resource) {
191                         super(resource);
192                 }
193
194                 @SuppressWarnings("unchecked")
195                 @Override
196                 public Function1<Variable, String> perform(ReadGraph graph) throws DatabaseException {
197                         Resource actionsModule = Layer0Utils.getPossibleChild(graph, resource, ACTIONS_MODULE);
198                         if (actionsModule == null || !graph.isInstanceOf(actionsModule, Layer0.getInstance(graph).SCLModule))
199                                 return null;
200
201                         String uri = graph.getURI(actionsModule);
202                         SCLContext sclContext = SCLContext.getCurrent();
203                         Object oldGraph = sclContext.get("graph");
204                         try {
205                                 sclContext.put("graph", graph);
206                                 return (Function1<Variable, String>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, PIPELINE_INFO);
207                         } catch (ValueNotFound e1) {
208                                 return null;
209                         } finally {
210                                 sclContext.put("graph", oldGraph);
211                         }
212                 }
213         }
214
215         @Override
216         protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode node) {
217                 ProfileVariables.denyChild(node, "*", DistrictNetworkStaticInfoNode.NODE_KEY);
218         }
219
220 }