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