]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLCheckedStateRule.java
Playground for Antti.
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / adapters / SCLCheckedStateRule.java
1 package org.simantics.modeling.adapters;
2
3 import org.simantics.browsing.ui.CheckedState;
4 import org.simantics.browsing.ui.model.check.CheckedStateRule;
5 import org.simantics.db.ReadGraph;
6 import org.simantics.db.Resource;
7 import org.simantics.db.exception.DatabaseException;
8 import org.simantics.db.layer0.variable.Variable;
9 import org.simantics.db.layer0.variable.VariableOrResource;
10 import org.simantics.db.layer0.variable.Variables;
11 import org.simantics.modeling.ModelingResources;
12 import org.simantics.scl.runtime.SCLContext;
13 import org.simantics.scl.runtime.function.Function1;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 public class SCLCheckedStateRule implements CheckedStateRule {
18     
19     private static final Logger LOGGER = LoggerFactory.getLogger(SCLCheckedStateRule.class);
20
21     private Resource rule;
22     
23     public SCLCheckedStateRule(ReadGraph graph, Resource rule) {
24         this.rule = rule;
25     }
26     
27     @Override
28     public boolean isCompatible(Class<?> contentType) {
29         return contentType.equals(Resource.class) || contentType.equals(Variable.class);
30     }
31
32     @Override
33     public CheckedState getCheckedState(ReadGraph graph, Object content) throws DatabaseException {
34     
35         ModelingResources MOD = ModelingResources.getInstance(graph);
36         
37         Variable ruleVariable = Variables.getVariable(graph, rule);
38         
39         Function1<Object,CheckedState> getLabels = ruleVariable.getPossiblePropertyValue(graph, MOD.SCLCheckedStateRule_getState);
40         if(getLabels == null) {
41             LOGGER.warn("Didn't find value for subject={}, predicate={}.", rule, ModelingResources.URIs.SCLCheckedStateRule_getState);
42             return CheckedState.NOT_CHECKED;
43         }
44
45         SCLContext sclContext = SCLContext.getCurrent();
46         Object oldGraph = sclContext.get("graph");
47         try {
48             sclContext.put("graph", graph);
49             return getLabels.apply(VariableOrResource.make(content));
50         } catch (Throwable t) {
51             LOGGER.error("Calculating checked state failed.", t);
52                 throw new DatabaseException(t);
53         } finally {
54             sclContext.put("graph", oldGraph);
55         }
56         
57     }
58
59 }