]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLCheckedStateRule.java
Improvements to SCL browse context contributions
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / adapters / SCLCheckedStateRule.java
1 /*******************************************************************************
2  * Copyright (c) 2019 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.adapters;
13
14 import org.simantics.browsing.ui.CheckedState;
15 import org.simantics.browsing.ui.model.check.CheckedStateRule;
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.layer0.variable.Variable;
20 import org.simantics.db.layer0.variable.VariableOrResource;
21 import org.simantics.db.layer0.variable.Variables;
22 import org.simantics.modeling.ModelingResources;
23 import org.simantics.scl.runtime.SCLContext;
24 import org.simantics.scl.runtime.function.Function1;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class SCLCheckedStateRule implements CheckedStateRule {
29
30     private static final Logger LOGGER = LoggerFactory.getLogger(SCLCheckedStateRule.class);
31
32     private Resource rule;
33
34     public SCLCheckedStateRule(ReadGraph graph, Resource rule) {
35         this.rule = rule;
36     }
37
38     @Override
39     public boolean isCompatible(Class<?> contentType) {
40         return contentType.equals(Resource.class) || contentType.equals(Variable.class);
41     }
42
43     @Override
44     public CheckedState getCheckedState(ReadGraph graph, Object content) throws DatabaseException {
45
46         ModelingResources MOD = ModelingResources.getInstance(graph);
47
48         Variable ruleVariable = Variables.getVariable(graph, rule);
49
50         Function1<Object,CheckedState> getLabels = ruleVariable.getPossiblePropertyValue(graph, MOD.SCLCheckedStateRule_getState);
51         if(getLabels == null) {
52             LOGGER.warn("Didn't find value for subject={}, predicate={}.", rule, ModelingResources.URIs.SCLCheckedStateRule_getState);
53             return CheckedState.NOT_CHECKED;
54         }
55
56         SCLContext sclContext = SCLContext.getCurrent();
57         Object oldGraph = sclContext.get("graph");
58         try {
59             sclContext.put("graph", graph);
60             return getLabels.apply(VariableOrResource.make(content));
61         } catch (Throwable t) {
62             LOGGER.error("Calculating checked state failed.", t);
63             throw new DatabaseException(t);
64         } finally {
65             sclContext.put("graph", oldGraph);
66         }
67
68     }
69
70 }