]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/profile/VertexSymbolStyle.java
Set license/copyright information for district features
[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 @Deprecated
22 public class VertexSymbolStyle extends ThrottledStyleBase<String> {
23
24         private static final Logger LOGGER = LoggerFactory.getLogger(VertexSymbolStyle.class);
25
26     @Override
27     public String calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException {
28         return calculateStyle(graph, entry, groupItem);
29     }
30
31     @SuppressWarnings({ "rawtypes", "unchecked" })
32     public static String calculateStyle(ReadGraph graph, Resource entry, Resource groupItem) throws DatabaseException {
33         // Prevent PendingVariableExceptions from coming through
34         boolean wasSynchronous = graph.setSynchronous(true);
35         try {
36             Function symbolFunction = getSymbolFunction(graph, entry);
37             if (symbolFunction == null)
38                 return null;
39
40             try {
41                 return (String) Simantics.applySCLRead(graph, symbolFunction, groupItem);
42             } catch (Exception e) {
43                 LOGGER.error("Getting dynamic symbol for " + groupItem + " ("
44                         + graph.getPossibleRelatedValue(groupItem, Layer0.getInstance(graph).HasName) + ") failed", e);
45                 return null;
46             }
47         } finally {
48             graph.setSynchronous(wasSynchronous);
49         }
50     }
51
52         @SuppressWarnings("rawtypes")
53         public static Function getSymbolFunction(ReadGraph graph, Resource entry) throws DatabaseException {
54                 // Cache function read for profile entry
55                 return graph.syncRequest(new SymbolFunctionRequest(entry), TransientCacheListener.<Function>instance());
56         }
57
58         @Override
59         public void applyThrottledStyleForNode(EvaluationContext observer, INode node, String value) {
60                 SingleElementNode n = (SingleElementNode) node;
61                 for (INode nn : n.getNodes())
62                         ProfileVariables.claimNodeProperty(nn, "SVG", value, observer);
63         }
64
65         @Override
66         protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode node) {
67                 ((G2DSceneGraph)node.getRootNode()).clearPending(node);
68                 SingleElementNode n = (SingleElementNode) node;
69                 for (INode nn : n.getNodes())
70                         ProfileVariables.claimNodeProperty(nn, "SVG", null, evaluationContext);
71         }
72
73         @SuppressWarnings("rawtypes")
74         protected static final class SymbolFunctionRequest extends ResourceRead<Function> {
75                 protected static final Function CONST_NULL = new FunctionImpl1<Resource, String>() {
76                         @Override
77                         public String apply(Resource p0) {
78                                 return null;
79                         }
80                 };
81                 
82                 public SymbolFunctionRequest(Resource entry) {
83                         super(entry);
84                 }
85
86                 @Override
87                 public Function perform(ReadGraph graph) throws DatabaseException {
88                         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
89                         
90                         Function symbolFunction = (Function) graph.getPossibleRelatedValue2(resource, DN.HasSymbolFunction, resource);
91                         if (symbolFunction == null)
92                                 symbolFunction = CONST_NULL;
93                         
94                         return symbolFunction;
95                 }
96         }
97
98 }