]> gerrit.simantics Code Review - simantics/district.git/blob
77be9c90cc392a37df569b5c917b31e6ad61b76f
[simantics/district.git] /
1 package org.simantics.district.network.ui.styles;
2
3 import java.awt.geom.Point2D;
4 import java.util.Collections;
5 import java.util.List;
6
7 import org.simantics.Simantics;
8 import org.simantics.databoard.Bindings;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
12 import org.simantics.db.common.request.ResourceRead;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.db.layer0.exception.MissingVariableException;
15 import org.simantics.db.layer0.exception.MissingVariableValueException;
16 import org.simantics.db.layer0.exception.PendingVariableException;
17 import org.simantics.db.layer0.util.Layer0Utils;
18 import org.simantics.db.layer0.variable.Variable;
19 import org.simantics.db.layer0.variable.Variables;
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.DeferredRenderingNode;
25 import org.simantics.district.network.ui.nodes.DistrictNetworkHoverInfoNode;
26 import org.simantics.district.network.ui.nodes.DistrictNetworkNodeUtils;
27 import org.simantics.layer0.Layer0;
28 import org.simantics.modeling.ModelingResources;
29 import org.simantics.scenegraph.INode;
30 import org.simantics.scenegraph.ParentNode;
31 import org.simantics.scenegraph.g2d.nodes.spatial.RTreeNode;
32 import org.simantics.scenegraph.profile.EvaluationContext;
33 import org.simantics.scenegraph.profile.common.ProfileVariables;
34 import org.simantics.scenegraph.utils.NodeUtil;
35 import org.simantics.scl.compiler.top.ValueNotFound;
36 import org.simantics.scl.osgi.SCLOsgi;
37 import org.simantics.scl.runtime.SCLContext;
38 import org.simantics.scl.runtime.function.Function1;
39 import org.simantics.scl.runtime.tuple.Tuple3;
40 import org.simantics.structural.stubs.StructuralResource2;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 public class DistrictNetworkHoverInfoStyle extends StyleBase<DistrictNetworkHoverInfoStyle.StyleResult> {
45
46     public static final String HOVER_INFO_DEFERRED = "hoverInfo";
47
48         private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkHoverInfoStyle.class);
49
50         private static final String ACTIONS_MODULE = "Actions";
51         private static final String HOVER_CONTRIBUTION = "hoverContribution";
52
53         public class StyleResult {
54                 Point2D origin;
55                 List<Tuple3> labels;
56
57                 public StyleResult(Point2D origin, List<Tuple3> labels) {
58                         this.origin = origin;
59                         this.labels = labels;
60                 }
61
62                 public Point2D getOrigin() {
63                         return origin;
64                 }
65
66                 public List<Tuple3> getLabels() {
67                         return labels;
68                 }
69         }
70         
71         public DistrictNetworkHoverInfoStyle(Resource style) throws DatabaseException {
72                 super(style);
73         }
74         
75         String currentRowKey;
76         
77         protected Resource getConfigurationComponent(ReadGraph graph, Resource element) throws DatabaseException {
78                 ModelingResources MOD = ModelingResources.getInstance(graph);
79                 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
80                 
81                 Resource mappedElement = graph.getPossibleObject(element, DN.MappedComponent);
82                 return mappedElement != null ? graph.getPossibleObject(mappedElement, MOD.ElementToComponent) : null;
83         }
84         
85         @Override
86         public StyleResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry,
87                         Resource mapElement, Variable configuration) throws DatabaseException {
88                 DiagramResource DIA = DiagramResource.getInstance(graph);
89                 StructuralResource2 STR = StructuralResource2.getInstance(graph);
90
91                 String variableURI = graph.getPossibleRelatedValue(runtimeDiagram, DIA.RuntimeDiagram_HasVariable, Bindings.STRING);
92                 Variable activeVariable = org.simantics.db.layer0.variable.Variables.getPossibleVariable(graph, variableURI);
93                 if (activeVariable == null)
94                         return null;
95
96                 Resource module = DistrictNetworkUtil.getMappedComponentCached(graph, mapElement);
97                 if (module == null)
98                         return null;
99
100                 Resource moduleType = graph.getPossibleType(module, STR.Component);
101                 if (moduleType == null)
102                         return null;
103                 
104                 Function1<Variable, List<Tuple3>> function = getUCTextGridFunctionCached(graph, moduleType);
105                 if (function == null)
106                         return null;
107                 
108                 List<Tuple3> result;
109                 try {
110                         Variable variable = Variables.getVariable(graph, module);
111                         Variable moduleVariable = Variables.possibleActiveVariable(graph, variable);
112                         if (moduleVariable == null)
113                                 moduleVariable = variable;
114
115                         result = Simantics.applySCLRead(graph, function, moduleVariable);
116                 } catch (PendingVariableException | MissingVariableValueException e) {
117                         result = Collections.singletonList(new Tuple3("<pending>", "", ""));
118                 } catch (MissingVariableException e) {
119             // the requested variable is missing from the UC
120             String message = e.getMessage();
121             LOGGER.warn("Missing variable for calculating style with function {} {}", function, message);
122             result = Collections.singletonList(new Tuple3("<" + message +">", "", ""));
123         }
124                 
125                 Point2D point;
126                 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
127                 if (graph.isInstanceOf(mapElement, DN.Vertex)) {
128                         double[] coords = graph.getRelatedValue(mapElement, DIA.HasLocation);
129                         point = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double(coords[0], coords[1]), null);
130                 }
131                 else if (graph.isInstanceOf(mapElement, DN.Edge)) {
132                         Resource v1 = graph.getSingleObject(mapElement, DN.HasStartVertex);
133                         double[] coords1 = graph.getRelatedValue(v1, DIA.HasLocation);
134                         Resource v2 = graph.getSingleObject(mapElement, DN.HasEndVertex);
135                         double[] coords2 = graph.getRelatedValue(v2, DIA.HasLocation);
136                         point = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double((coords1[0] + coords2[0]) / 2, (coords1[1] + coords2[1]) / 2), null);
137                 }
138                 else {
139                         return null;
140                 }
141                 
142                 return new StyleResult(point, result);
143         }
144         
145         @Override
146         public void applyStyleForNode(EvaluationContext observer, INode parent, StyleResult results) {
147                 if (results == null) {
148                         cleanupStyleForNode(observer, parent);
149                         return;
150                 }
151                 
152                 DistrictNetworkHoverInfoNode node = ProfileVariables.claimChild(parent, "*", DistrictNetworkHoverInfoNode.NODE_KEY, DistrictNetworkHoverInfoNode.class, observer);
153                 if (node == null)
154                         return;
155                 
156                 ParentNode<?> root = (ParentNode<?>) NodeUtil.getNearestParentOfType(parent, RTreeNode.class);
157                 if (root != null) {
158                         DeferredRenderingNode deferred = ProfileVariables.claimChild(root, "", HOVER_INFO_DEFERRED, DeferredRenderingNode.class, observer);
159                         deferred.setZIndex(Integer.MAX_VALUE);
160                 }
161                 
162                 node.setLabels(results.getLabels());
163                 node.setOrigin(results.getOrigin());
164         }
165         
166         @Override
167         protected void cleanupStyleForNode(EvaluationContext observer, INode node) {
168                 ProfileVariables.denyChild(node, "*", DistrictNetworkHoverInfoNode.NODE_KEY);
169         }
170         
171         private static Function1<Variable, List<Tuple3>> getUCTextGridFunctionCached(ReadGraph graph, Resource componentType)
172                         throws DatabaseException {
173                 return graph.syncRequest(new UCTextGridFunctionRequest(componentType), TransientCacheListener.instance());
174         }
175         
176         private static final class UCTextGridFunctionRequest extends ResourceRead<Function1<Variable, List<Tuple3>>> {
177                 public UCTextGridFunctionRequest(Resource resource) {
178                         super(resource);
179                 }
180
181                 @SuppressWarnings("unchecked")
182                 @Override
183                 public Function1<Variable, List<Tuple3>> perform(ReadGraph graph) throws DatabaseException {
184                         Resource actionsModule = Layer0Utils.getPossibleChild(graph, resource, ACTIONS_MODULE);
185                         if (actionsModule == null || !graph.isInstanceOf(actionsModule, Layer0.getInstance(graph).SCLModule))
186                                 return null;
187                         
188                         String uri = graph.getURI(actionsModule);
189                         SCLContext sclContext = SCLContext.getCurrent();
190                         Object oldGraph = sclContext.get("graph");
191                         try {
192                                 sclContext.put("graph", graph);
193                                 return (Function1<Variable, List<Tuple3>>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, HOVER_CONTRIBUTION);
194                         } catch (ValueNotFound e1) {
195                                 return null;
196                         } finally {
197                                 sclContext.put("graph", oldGraph);
198                         }
199                 }
200         }
201 }