]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLChildRule.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / adapters / SCLChildRule.java
1 package org.simantics.modeling.adapters;
2
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.List;
6
7 import org.simantics.browsing.ui.model.children.ChildRule;
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.Resource;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.layer0.variable.Variable;
12 import org.simantics.db.layer0.variable.Variables;
13 import org.simantics.modeling.ModelingResources;
14 import org.simantics.scl.runtime.SCLContext;
15 import org.simantics.scl.runtime.function.Function1;
16
17 public class SCLChildRule implements ChildRule {
18
19     private Resource rule;
20     
21     public SCLChildRule(ReadGraph graph, Resource rule) {
22         this.rule = rule;
23     }
24     
25     @Override
26     public boolean isCompatible(Class<?> contentType) {
27         return contentType.equals(Resource.class) || contentType.equals(Variable.class);
28     }
29
30     @Override
31     public Collection<?> getChildren(ReadGraph graph, Object parent) throws DatabaseException {
32         
33         ModelingResources MOD = ModelingResources.getInstance(graph);
34         
35         Variable ruleVariable = Variables.getVariable(graph, rule);
36
37         Function1<Object,List<?>> getChildren = ruleVariable.getPossiblePropertyValue(graph, MOD.SCLChildRule_getChildren);
38         if(getChildren == null) return Collections.emptyList();
39
40         SCLContext sclContext = SCLContext.getCurrent();
41         Object oldGraph = sclContext.get("graph");
42         try {
43             sclContext.put("graph", graph);
44             Object value = getChildren.apply(parent);
45             return (Collection<?>)value;
46         } catch (Throwable t) {
47                 throw new DatabaseException(t);
48         } finally {
49             sclContext.put("graph", oldGraph);
50         }
51
52     }
53
54     @Override
55     public Collection<?> getParents(ReadGraph graph, Object child) throws DatabaseException {
56         return null;
57     }
58
59 }