1 package org.simantics.modeling.adapters;
3 import java.util.Collections;
4 import java.util.HashMap;
8 import org.simantics.browsing.ui.model.labels.LabelRule;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.variable.Variable;
13 import org.simantics.db.layer0.variable.Variables;
14 import org.simantics.modeling.ModelingResources;
15 import org.simantics.scl.runtime.SCLContext;
16 import org.simantics.scl.runtime.function.Function1;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 public class SCLLabelRule implements LabelRule {
21 private static final Logger LOGGER = LoggerFactory.getLogger(SCLLabelRule.class);
23 private Resource rule;
25 public SCLLabelRule(ReadGraph graph, Resource rule) {
30 public boolean isCompatible(Class<?> contentType) {
31 return contentType.equals(Resource.class) || contentType.equals(Variable.class);
35 public Map<String, String> getLabel(ReadGraph graph, Object content) throws DatabaseException {
36 ModelingResources MOD = ModelingResources.getInstance(graph);
38 Variable ruleVariable = Variables.getVariable(graph, rule);
40 Function1<Object,List<String>> getLabels = ruleVariable.getPossiblePropertyValue(graph, MOD.SCLLabelRule_getLabels);
41 if(getLabels == null) {
42 LOGGER.warn("Didn't find value for subject={}, predicate={}.", rule, ModelingResources.URIs.SCLLabelRule_getLabels);
43 return Collections.emptyMap();
46 SCLContext sclContext = SCLContext.getCurrent();
47 Object oldGraph = sclContext.get("graph");
49 sclContext.put("graph", graph);
50 List<String> value = getLabels.apply(content);
51 Map<String,String> result = new HashMap<>();
52 for(int i=0;i<value.size();i+=2) {
53 result.put(value.get(i), value.get(i+1));
56 } catch (Throwable t) {
57 LOGGER.error("Calculating label failed.", t);
58 throw new DatabaseException(t);
60 sclContext.put("graph", oldGraph);