]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLLabelRule.java
Log some information about missing property in SCLLabelRule
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / adapters / SCLLabelRule.java
1 package org.simantics.modeling.adapters;
2
3 import java.util.Collections;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
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;
19
20 public class SCLLabelRule implements LabelRule {
21     private static final Logger LOGGER = LoggerFactory.getLogger(SCLLabelRule.class);
22
23     private Resource rule;
24     
25     public SCLLabelRule(ReadGraph graph, Resource rule) {
26         this.rule = rule;
27     }
28     
29     @Override
30     public boolean isCompatible(Class<?> contentType) {
31         return contentType.equals(Resource.class) || contentType.equals(Variable.class);
32     }
33
34     @Override
35     public Map<String, String> getLabel(ReadGraph graph, Object content) throws DatabaseException {
36         ModelingResources MOD = ModelingResources.getInstance(graph);
37         
38         Variable ruleVariable = Variables.getVariable(graph, rule);
39
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();
44         }
45
46         SCLContext sclContext = SCLContext.getCurrent();
47         Object oldGraph = sclContext.get("graph");
48         try {
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));
54             }
55             return result;
56         } catch (Throwable t) {
57             LOGGER.error("Calculating label failed.", t);
58                 throw new DatabaseException(t);
59         } finally {
60             sclContext.put("graph", oldGraph);
61         }
62     }
63
64 }