1 package org.simantics.modeling.adapters;
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;
17 public class SCLCheckedStateRule implements CheckedStateRule {
19 private static final Logger LOGGER = LoggerFactory.getLogger(SCLCheckedStateRule.class);
21 private Resource rule;
23 public SCLCheckedStateRule(ReadGraph graph, Resource rule) {
28 public boolean isCompatible(Class<?> contentType) {
29 return contentType.equals(Resource.class) || contentType.equals(Variable.class);
33 public CheckedState getCheckedState(ReadGraph graph, Object content) throws DatabaseException {
35 ModelingResources MOD = ModelingResources.getInstance(graph);
37 Variable ruleVariable = Variables.getVariable(graph, rule);
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;
45 SCLContext sclContext = SCLContext.getCurrent();
46 Object oldGraph = sclContext.get("graph");
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);
54 sclContext.put("graph", oldGraph);