1 package org.simantics.district.network.ui.styles;
3 import java.awt.geom.Point2D;
4 import java.util.Collections;
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;
44 public class DistrictNetworkHoverInfoStyle extends StyleBase<DistrictNetworkHoverInfoStyle.StyleResult> {
46 public static final String HOVER_INFO_DEFERRED = "hoverInfo";
48 private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkHoverInfoStyle.class);
50 private static final String ACTIONS_MODULE = "Actions";
51 private static final String HOVER_CONTRIBUTION = "hoverContribution";
53 public class StyleResult {
57 public StyleResult(Point2D origin, List<Tuple3> labels) {
62 public Point2D getOrigin() {
66 public List<Tuple3> getLabels() {
71 public DistrictNetworkHoverInfoStyle(Resource style) throws DatabaseException {
77 protected Resource getConfigurationComponent(ReadGraph graph, Resource element) throws DatabaseException {
78 ModelingResources MOD = ModelingResources.getInstance(graph);
79 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
81 Resource mappedElement = graph.getPossibleObject(element, DN.MappedComponent);
82 return mappedElement != null ? graph.getPossibleObject(mappedElement, MOD.ElementToComponent) : null;
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);
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)
96 Resource module = DistrictNetworkUtil.getMappedComponentCached(graph, mapElement);
100 Resource moduleType = graph.getPossibleType(module, STR.Component);
101 if (moduleType == null)
104 Function1<Variable, List<Tuple3>> function = getUCTextGridFunctionCached(graph, moduleType);
105 if (function == null)
110 Variable variable = Variables.getVariable(graph, module);
111 Variable moduleVariable = Variables.possibleActiveVariable(graph, variable);
112 if (moduleVariable == null)
113 moduleVariable = variable;
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 +">", "", ""));
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);
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);
142 return new StyleResult(point, result);
146 public void applyStyleForNode(EvaluationContext observer, INode parent, StyleResult results) {
147 if (results == null) {
148 cleanupStyleForNode(observer, parent);
152 DistrictNetworkHoverInfoNode node = ProfileVariables.claimChild(parent, "*", DistrictNetworkHoverInfoNode.NODE_KEY, DistrictNetworkHoverInfoNode.class, observer);
156 ParentNode<?> root = (ParentNode<?>) NodeUtil.getNearestParentOfType(parent, RTreeNode.class);
158 DeferredRenderingNode deferred = ProfileVariables.claimChild(root, "", HOVER_INFO_DEFERRED, DeferredRenderingNode.class, observer);
159 deferred.setZIndex(Integer.MAX_VALUE);
162 node.setLabels(results.getLabels());
163 node.setOrigin(results.getOrigin());
167 protected void cleanupStyleForNode(EvaluationContext observer, INode node) {
168 ProfileVariables.denyChild(node, "*", DistrictNetworkHoverInfoNode.NODE_KEY);
171 private static Function1<Variable, List<Tuple3>> getUCTextGridFunctionCached(ReadGraph graph, Resource componentType)
172 throws DatabaseException {
173 return graph.syncRequest(new UCTextGridFunctionRequest(componentType), TransientCacheListener.instance());
176 private static final class UCTextGridFunctionRequest extends ResourceRead<Function1<Variable, List<Tuple3>>> {
177 public UCTextGridFunctionRequest(Resource resource) {
181 @SuppressWarnings("unchecked")
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))
188 String uri = graph.getURI(actionsModule);
189 SCLContext sclContext = SCLContext.getCurrent();
190 Object oldGraph = sclContext.get("graph");
192 sclContext.put("graph", graph);
193 return (Function1<Variable, List<Tuple3>>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, HOVER_CONTRIBUTION);
194 } catch (ValueNotFound e1) {
197 sclContext.put("graph", oldGraph);