]> gerrit.simantics Code Review - simantics/district.git/blob
49d524a4b2f9525b9fcde4e4f95c96f466a5e59b
[simantics/district.git] /
1 package org.simantics.district.network.ui.nodes;
2
3 import java.awt.geom.Point2D;
4 import java.util.Set;
5
6 import org.simantics.Simantics;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
10 import org.simantics.db.common.request.ResourceRead;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.exception.MissingVariableValueException;
13 import org.simantics.db.layer0.exception.PendingVariableException;
14 import org.simantics.db.layer0.util.Layer0Utils;
15 import org.simantics.db.layer0.variable.Variable;
16 import org.simantics.db.layer0.variable.Variables;
17 import org.simantics.diagram.profile.StyleBase;
18 import org.simantics.diagram.stubs.DiagramResource;
19 import org.simantics.district.network.DistrictNetworkUtil;
20 import org.simantics.district.network.ontology.DistrictNetworkResource;
21 import org.simantics.district.network.profile.MidBranchEdgeSetRequest;
22 import org.simantics.layer0.Layer0;
23 import org.simantics.scenegraph.INode;
24 import org.simantics.scenegraph.profile.EvaluationContext;
25 import org.simantics.scenegraph.profile.common.ProfileVariables;
26 import org.simantics.scl.compiler.top.ValueNotFound;
27 import org.simantics.scl.osgi.SCLOsgi;
28 import org.simantics.scl.runtime.SCLContext;
29 import org.simantics.scl.runtime.function.Function1;
30 import org.simantics.structural.stubs.StructuralResource2;
31
32 public class DistrictNetworkStaticInfoStyle extends StyleBase<DistrictNetworkStaticInfoStyle.StyleResult> {
33
34         private static final String ACTIONS_MODULE = "Actions";
35         private static final String PIPELINE_INFO = "pipelineInfo";
36         
37         public static class StyleResult {
38                 public StyleResult(Point2D p1, Point2D p2, String info) {
39                         this.p1 = p1;
40                         this.p2 = p2;
41                         this.info = info;
42                 }
43                 
44                 public Point2D p1;
45                 public Point2D p2;
46                 public String info;
47         }
48         
49         public DistrictNetworkStaticInfoStyle(Resource style) {
50                 super(style);
51         }
52         
53         @Override
54         public StyleResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource mapElement)
55                         throws DatabaseException {
56                 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
57                 
58                 if (!graph.isInstanceOf(mapElement, DN.Edge))
59                         return null;
60                 
61                 Resource diagram = graph.getSingleObject(mapElement, Layer0.getInstance(graph).PartOf);
62                 Set<Resource> edgesToUse = graph.syncRequest(new MidBranchEdgeSetRequest(diagram), TransientCacheListener.instance());
63                 if (!edgesToUse.contains(mapElement))
64                         return null;
65                 
66                 DiagramResource DIA = DiagramResource.getInstance(graph);
67                 StructuralResource2 STR = StructuralResource2.getInstance(graph);
68                 
69                 Resource module = DistrictNetworkUtil.getMappedComponentCached(graph, mapElement);
70                 if (module == null)
71                         return null;
72
73                 Resource moduleType = graph.getPossibleType(module, STR.Component);
74                 if (moduleType == null)
75                         return null;
76                 
77                 Function1<Variable, String> function = getUCPipelineInfoFunctionCached(graph, moduleType);
78                 if (function == null)
79                         return null;
80                 
81                 String result;
82                 try {
83                         Variable variable = Variables.getVariable(graph, module);
84                         Variable moduleVariable = Variables.possibleActiveVariable(graph, variable);
85                         if (moduleVariable == null)
86                                 moduleVariable = variable;
87
88                         result = Simantics.applySCLRead(graph, function, moduleVariable);
89                 } catch (PendingVariableException | MissingVariableValueException e) {
90                         result = null;
91                 }
92                 
93                 Resource v1 = graph.getSingleObject(mapElement, DN.HasStartVertex);
94                 double[] coords1 = graph.getRelatedValue(v1, DIA.HasLocation);
95                 Resource v2 = graph.getSingleObject(mapElement, DN.HasEndVertex);
96                 double[] coords2 = graph.getRelatedValue(v2, DIA.HasLocation);
97                 Point2D p1 = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double(coords1[0], coords1[1]), null);
98                 Point2D p2 = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double(coords2[0], coords2[1]), null);
99                 
100                 return new StyleResult(p1, p2, result);
101         }
102         
103         @Override
104         public void applyStyleForNode(EvaluationContext evaluationContext, INode parent, StyleResult result) {
105                 if (result == null) {
106                         cleanupStyleForNode(evaluationContext, parent);
107                         return;
108                 }
109                 
110                 DistrictNetworkStaticInfoNode node = ProfileVariables.claimChild(parent, "*", DistrictNetworkStaticInfoNode.NODE_KEY, DistrictNetworkStaticInfoNode.class, evaluationContext);
111                 if (node == null)
112                         return;
113
114                 Point2D p1 = result.p1;
115                 Point2D p2 = result.p2;
116                 
117                 double sign = Math.signum(p1.getX() - p2.getX());
118                 Point2D.Double origin = new Point2D.Double(0.5 * (p1.getX() + p2.getX()), 0.5 * (p1.getY() + p2.getY()));
119                 Point2D direction = new Point2D.Double(0.5 * sign * (p1.getX() - p2.getX()), 0.5 * sign * (p1.getY() - p2.getY()));
120                 
121                 node.setLocation(origin, direction);
122                 node.setInfo(result.info);
123         }
124         
125         private static Function1<Variable, String> getUCPipelineInfoFunctionCached(ReadGraph graph, Resource componentType)
126                         throws DatabaseException {
127                 return graph.syncRequest(new UCPipelineInfoRequest(componentType), TransientCacheListener.instance());
128         }
129         
130         private static final class UCPipelineInfoRequest extends ResourceRead<Function1<Variable, String>> {
131                 public UCPipelineInfoRequest(Resource resource) {
132                         super(resource);
133                 }
134
135                 @SuppressWarnings("unchecked")
136                 @Override
137                 public Function1<Variable, String> perform(ReadGraph graph) throws DatabaseException {
138                         Resource actionsModule = Layer0Utils.getPossibleChild(graph, resource, ACTIONS_MODULE);
139                         if (actionsModule == null || !graph.isInstanceOf(actionsModule, Layer0.getInstance(graph).SCLModule))
140                                 return null;
141                         
142                         String uri = graph.getURI(actionsModule);
143                         SCLContext sclContext = SCLContext.getCurrent();
144                         Object oldGraph = sclContext.get("graph");
145                         try {
146                                 sclContext.put("graph", graph);
147                                 return (Function1<Variable, String>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, PIPELINE_INFO);
148                         } catch (ValueNotFound e1) {
149                                 return null;
150                         } finally {
151                                 sclContext.put("graph", oldGraph);
152                         }
153                 }
154         }
155         
156         @Override
157         protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode node) {
158                 ProfileVariables.denyChild(node, DistrictNetworkStaticInfoNode.NODE_KEY);
159         }
160 }