package org.simantics.modeling.adapters; import java.util.Collection; import java.util.Collections; import java.util.List; import org.simantics.browsing.ui.model.children.ChildRule; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.variable.Variable; import org.simantics.db.layer0.variable.Variables; import org.simantics.modeling.ModelingResources; import org.simantics.scl.runtime.SCLContext; import org.simantics.scl.runtime.function.Function1; public class SCLChildRule implements ChildRule { private Resource rule; public SCLChildRule(ReadGraph graph, Resource rule) { this.rule = rule; } @Override public boolean isCompatible(Class contentType) { return contentType.equals(Resource.class) || contentType.equals(Variable.class); } @Override public Collection getChildren(ReadGraph graph, Object parent) throws DatabaseException { Resource parentResource; if (parent instanceof Variable) { parentResource = ((Variable)parent).getRepresents(graph); } else { parentResource = (Resource)parent; } ModelingResources MOD = ModelingResources.getInstance(graph); Variable ruleVariable = Variables.getVariable(graph, rule); Function1> getChildren = ruleVariable.getPossiblePropertyValue(graph, MOD.SCLChildRule_getChildren); if(getChildren == null) return Collections.emptyList(); SCLContext sclContext = SCLContext.getCurrent(); Object oldGraph = sclContext.get("graph"); try { sclContext.put("graph", graph); return getChildren.apply(parentResource); } catch (Throwable t) { throw new DatabaseException(t); } finally { sclContext.put("graph", oldGraph); } } @Override public Collection getParents(ReadGraph graph, Object child) throws DatabaseException { return null; } }