]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLChildRule.java
a04b9315f544d698913c655ce084e485ec05224a
[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<Resource> getChildren(ReadGraph graph, Object parent) throws DatabaseException {
32         Resource parentResource;
33         
34         if (parent instanceof Variable) {
35             parentResource = ((Variable)parent).getRepresents(graph);
36         } else {
37             parentResource = (Resource)parent;
38         }
39         
40         ModelingResources MOD = ModelingResources.getInstance(graph);
41         
42         Variable ruleVariable = Variables.getVariable(graph, rule);
43
44         Function1<Resource,List<Resource>> getChildren = ruleVariable.getPossiblePropertyValue(graph, MOD.SCLChildRule_getChildren);
45         if(getChildren == null) return Collections.emptyList();
46
47         SCLContext sclContext = SCLContext.getCurrent();
48         Object oldGraph = sclContext.get("graph");
49         try {
50             sclContext.put("graph", graph);
51             return getChildren.apply(parentResource);
52         } catch (Throwable t) {
53                 throw new DatabaseException(t);
54         } finally {
55             sclContext.put("graph", oldGraph);
56         }
57
58     }
59
60     @Override
61     public Collection<?> getParents(ReadGraph graph, Object child) throws DatabaseException {
62         return null;
63     }
64
65 }