]> gerrit.simantics Code Review - simantics/district.git/blob
f01fa2aeece9cdd63dc1e340908307f98158399d
[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.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.ui.nodes.DistrictNetworkHoverInfoNode;
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.profile.EvaluationContext;
29 import org.simantics.scenegraph.profile.common.ProfileVariables;
30 import org.simantics.scl.compiler.top.ValueNotFound;
31 import org.simantics.scl.osgi.SCLOsgi;
32 import org.simantics.scl.runtime.SCLContext;
33 import org.simantics.scl.runtime.function.Function1;
34 import org.simantics.scl.runtime.tuple.Tuple3;
35 import org.simantics.structural.stubs.StructuralResource2;
36
37 public class DistrictNetworkHoverInfoStyle extends StyleBase<DistrictNetworkHoverInfoStyle.StyleResult> {
38
39         private static final String ACTIONS_MODULE = "Actions";
40         private static final String HOVER_CONTRIBUTION = "hoverContribution";
41
42         public class StyleResult {
43                 Point2D origin;
44                 List<Tuple3> labels;
45
46                 public StyleResult(Point2D origin, List<Tuple3> labels) {
47                         this.origin = origin;
48                         this.labels = labels;
49                 }
50
51                 public Point2D getOrigin() {
52                         return origin;
53                 }
54
55                 public List<Tuple3> getLabels() {
56                         return labels;
57                 }
58         }
59         
60         public DistrictNetworkHoverInfoStyle(Resource style) throws DatabaseException {
61                 super(style);
62         }
63         
64         String currentRowKey;
65         
66         protected Resource getConfigurationComponent(ReadGraph graph, Resource element) throws DatabaseException {
67                 ModelingResources MOD = ModelingResources.getInstance(graph);
68                 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
69                 
70                 Resource mappedElement = graph.getPossibleObject(element, DN.MappedComponent);
71                 return mappedElement != null ? graph.getPossibleObject(mappedElement, MOD.ElementToComponent) : null;
72         }
73         
74         @Override
75         public StyleResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry,
76                         Resource mapElement, Variable configuration) throws DatabaseException {
77                 DiagramResource DIA = DiagramResource.getInstance(graph);
78                 StructuralResource2 STR = StructuralResource2.getInstance(graph);
79
80                 String variableURI = graph.getPossibleRelatedValue(runtimeDiagram, DIA.RuntimeDiagram_HasVariable, Bindings.STRING);
81                 Variable activeVariable = org.simantics.db.layer0.variable.Variables.getPossibleVariable(graph, variableURI);
82                 if (activeVariable == null)
83                         return null;
84
85                 Resource module = DistrictNetworkUtil.getMappedComponentCached(graph, mapElement);
86                 if (module == null)
87                         return null;
88
89                 Resource moduleType = graph.getPossibleType(module, STR.Component);
90                 if (moduleType == null)
91                         return null;
92                 
93                 Function1<Variable, List<Tuple3>> function = getUCTextGridFunctionCached(graph, moduleType);
94                 if (function == null)
95                         return null;
96                 
97                 List<Tuple3> result;
98                 try {
99                         Variable variable = Variables.getVariable(graph, module);
100                         Variable moduleVariable = Variables.possibleActiveVariable(graph, variable);
101                         if (moduleVariable == null)
102                                 moduleVariable = variable;
103
104                         result = Simantics.applySCLRead(graph, function, moduleVariable);
105                 } catch (PendingVariableException | MissingVariableValueException e) {
106                         result = Collections.singletonList(new Tuple3("<pending>", "", ""));
107                 }
108                 
109                 Point2D point;
110                 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
111                 if (graph.isInstanceOf(mapElement, DN.Vertex)) {
112                         double[] coords = graph.getRelatedValue(mapElement, DIA.HasLocation);
113                         point = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double(coords[0], coords[1]), null);
114                 }
115                 else if (graph.isInstanceOf(mapElement, DN.Edge)) {
116                         Resource v1 = graph.getSingleObject(mapElement, DN.HasStartVertex);
117                         double[] coords1 = graph.getRelatedValue(v1, DIA.HasLocation);
118                         Resource v2 = graph.getSingleObject(mapElement, DN.HasEndVertex);
119                         double[] coords2 = graph.getRelatedValue(v2, DIA.HasLocation);
120                         point = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double((coords1[0] + coords2[0]) / 2, (coords1[1] + coords2[1]) / 2), null);
121                 }
122                 else {
123                         return null;
124                 }
125                 
126                 return new StyleResult(point, result);
127         }
128         
129         @Override
130         public void applyStyleForNode(EvaluationContext observer, INode parent, StyleResult results) {
131                 if (results == null) {
132                         cleanupStyleForNode(observer, parent);
133                         return;
134                 }
135                 
136                 DistrictNetworkHoverInfoNode node = ProfileVariables.claimChild(parent, "*", DistrictNetworkHoverInfoNode.NODE_KEY, DistrictNetworkHoverInfoNode.class, observer);
137                 if (node == null)
138                         return;
139                 
140                 node.setLabels(results.getLabels());
141                 node.setOrigin(results.getOrigin());
142         }
143         
144         @Override
145         protected void cleanupStyleForNode(EvaluationContext observer, INode node) {
146                 ProfileVariables.denyChild(node, "*", DistrictNetworkHoverInfoNode.NODE_KEY);
147         }
148         
149         private static Function1<Variable, List<Tuple3>> getUCTextGridFunctionCached(ReadGraph graph, Resource componentType)
150                         throws DatabaseException {
151                 return graph.syncRequest(new UCTextGridFunctionRequest(componentType), TransientCacheListener.instance());
152         }
153         
154         private static final class UCTextGridFunctionRequest extends ResourceRead<Function1<Variable, List<Tuple3>>> {
155                 public UCTextGridFunctionRequest(Resource resource) {
156                         super(resource);
157                 }
158
159                 @SuppressWarnings("unchecked")
160                 @Override
161                 public Function1<Variable, List<Tuple3>> perform(ReadGraph graph) throws DatabaseException {
162                         Resource actionsModule = Layer0Utils.getPossibleChild(graph, resource, ACTIONS_MODULE);
163                         if (actionsModule == null || !graph.isInstanceOf(actionsModule, Layer0.getInstance(graph).SCLModule))
164                                 return null;
165                         
166                         String uri = graph.getURI(actionsModule);
167                         SCLContext sclContext = SCLContext.getCurrent();
168                         Object oldGraph = sclContext.get("graph");
169                         try {
170                                 sclContext.put("graph", graph);
171                                 return (Function1<Variable, List<Tuple3>>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, HOVER_CONTRIBUTION);
172                         } catch (ValueNotFound e1) {
173                                 return null;
174                         } finally {
175                                 sclContext.put("graph", oldGraph);
176                         }
177                 }
178         }
179 }