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