1 /*******************************************************************************
2 * Copyright (c) 2019 Association for Decentralized Information Management
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
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.adapters;
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;
28 public class SCLCheckedStateRule implements CheckedStateRule {
30 private static final Logger LOGGER = LoggerFactory.getLogger(SCLCheckedStateRule.class);
32 private Resource rule;
34 public SCLCheckedStateRule(ReadGraph graph, Resource rule) {
39 public boolean isCompatible(Class<?> contentType) {
40 return contentType.equals(Resource.class) || contentType.equals(Variable.class);
44 public CheckedState getCheckedState(ReadGraph graph, Object content) throws DatabaseException {
46 ModelingResources MOD = ModelingResources.getInstance(graph);
48 Variable ruleVariable = Variables.getVariable(graph, rule);
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;
56 SCLContext sclContext = SCLContext.getCurrent();
57 Object oldGraph = sclContext.get("graph");
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);
65 sclContext.put("graph", oldGraph);