]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLChildRule.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / adapters / SCLChildRule.java
diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLChildRule.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLChildRule.java
new file mode 100644 (file)
index 0000000..8ca1b9a
--- /dev/null
@@ -0,0 +1,59 @@
+package org.simantics.modeling.adapters;\r
+\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.List;\r
+\r
+import org.simantics.browsing.ui.model.children.ChildRule;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.layer0.variable.Variables;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.scl.runtime.SCLContext;\r
+import org.simantics.scl.runtime.function.Function1;\r
+\r
+public class SCLChildRule implements ChildRule {\r
+\r
+    private Resource rule;\r
+    \r
+    public SCLChildRule(ReadGraph graph, Resource rule) {\r
+        this.rule = rule;\r
+    }\r
+    \r
+    @Override\r
+    public boolean isCompatible(Class<?> contentType) {\r
+        return contentType.equals(Resource.class) || contentType.equals(Variable.class);\r
+    }\r
+\r
+    @Override\r
+    public Collection<?> getChildren(ReadGraph graph, Object parent) throws DatabaseException {\r
+        \r
+        ModelingResources MOD = ModelingResources.getInstance(graph);\r
+        \r
+        Variable ruleVariable = Variables.getVariable(graph, rule);\r
+\r
+        Function1<Object,List<?>> getChildren = ruleVariable.getPossiblePropertyValue(graph, MOD.SCLChildRule_getChildren);\r
+        if(getChildren == null) return Collections.emptyList();\r
+\r
+        SCLContext sclContext = SCLContext.getCurrent();\r
+        Object oldGraph = sclContext.get("graph");\r
+        try {\r
+            sclContext.put("graph", graph);\r
+            Object value = getChildren.apply(parent);\r
+            return (Collection<?>)value;\r
+        } catch (Throwable t) {\r
+               throw new DatabaseException(t);\r
+        } finally {\r
+            sclContext.put("graph", oldGraph);\r
+        }\r
+\r
+    }\r
+\r
+    @Override\r
+    public Collection<?> getParents(ReadGraph graph, Object child) throws DatabaseException {\r
+        return null;\r
+    }\r
+\r
+}\r