]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/profile/VertexSymbolStyle.java
26a5564d5fade5e47180b437fd12b5c56f66032f
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / profile / VertexSymbolStyle.java
1 package org.simantics.district.network.profile;
2
3 import org.simantics.Simantics;
4 import org.simantics.db.ReadGraph;
5 import org.simantics.db.Resource;
6 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
7 import org.simantics.db.common.request.ResourceRead;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.district.network.ontology.DistrictNetworkResource;
10 import org.simantics.layer0.Layer0;
11 import org.simantics.scenegraph.INode;
12 import org.simantics.scenegraph.g2d.G2DSceneGraph;
13 import org.simantics.scenegraph.g2d.nodes.SingleElementNode;
14 import org.simantics.scenegraph.profile.EvaluationContext;
15 import org.simantics.scenegraph.profile.common.ProfileVariables;
16 import org.simantics.scl.runtime.function.Function;
17 import org.simantics.scl.runtime.function.FunctionImpl1;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public class VertexSymbolStyle extends ThrottledStyleBase<String> {
22
23         private static final Logger LOGGER = LoggerFactory.getLogger(VertexSymbolStyle.class);
24         
25         @SuppressWarnings({ "rawtypes", "unchecked" })
26         @Override
27         public String calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException {
28                 // Prevent PendingVariableExceptions from coming through
29                 boolean wasSynchronous = graph.setSynchronous(true);
30                 try {
31                         Function symbolFunction = getSymbolFunction(graph, entry);
32                         if (symbolFunction == null)
33                                 return null;
34                         
35                         try {
36                                 return (String) Simantics.applySCLRead(graph, symbolFunction, groupItem);
37                         } catch (Exception e) {
38                                 LOGGER.error("Getting dynamic symbol for " + groupItem + " (" + graph.getPossibleRelatedValue(groupItem, Layer0.getInstance(graph).HasName) + ") failed", e);
39                                 return null;
40                         }
41                 }
42                 finally {
43                         graph.setSynchronous(wasSynchronous);
44                 }
45         }
46
47         @SuppressWarnings("rawtypes")
48         protected static Function getSymbolFunction(ReadGraph graph, Resource entry) throws DatabaseException {
49                 // Cache function read for profile entry
50                 return graph.syncRequest(new SymbolFunctionRequest(entry), TransientCacheListener.<Function>instance());
51         }
52
53         @Override
54         public void applyThrottledStyleForNode(EvaluationContext observer, INode node, String value) {
55                 SingleElementNode n = (SingleElementNode) node;
56                 for (INode nn : n.getNodes())
57                         ProfileVariables.claimNodeProperty(nn, "SVG", value, observer);
58         }
59
60         @Override
61         protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode node) {
62                 ((G2DSceneGraph)node.getRootNode()).clearPending(node);
63                 SingleElementNode n = (SingleElementNode) node;
64                 for (INode nn : n.getNodes())
65                         ProfileVariables.claimNodeProperty(nn, "SVG", null, evaluationContext);
66         }
67
68         @SuppressWarnings("rawtypes")
69         protected static final class SymbolFunctionRequest extends ResourceRead<Function> {
70                 protected static final Function CONST_NULL = new FunctionImpl1<Resource, String>() {
71                         @Override
72                         public String apply(Resource p0) {
73                                 return null;
74                         }
75                 };
76                 
77                 public SymbolFunctionRequest(Resource entry) {
78                         super(entry);
79                 }
80
81                 @Override
82                 public Function perform(ReadGraph graph) throws DatabaseException {
83                         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
84                         
85                         Function symbolFunction = (Function) graph.getPossibleRelatedValue2(resource, DN.HasSymbolFunction, resource);
86                         if (symbolFunction == null)
87                                 symbolFunction = CONST_NULL;
88                         
89                         return symbolFunction;
90                 }
91         }
92
93 }