X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.document.linking.ui%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Flinking%2Fge%2FObjectChildRule.java;fp=bundles%2Forg.simantics.document.linking.ui%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Flinking%2Fge%2FObjectChildRule.java;h=76e3fe1cf2c0e4ba44b6420bb463919527866612;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/ge/ObjectChildRule.java b/bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/ge/ObjectChildRule.java new file mode 100644 index 000000000..76e3fe1cf --- /dev/null +++ b/bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/ge/ObjectChildRule.java @@ -0,0 +1,110 @@ +package org.simantics.document.linking.ge; + +import java.util.ArrayList; +import java.util.Collection; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.variable.StandardGraphChildVariable; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.document.linking.ontology.DocumentLink; +import org.simantics.document.linking.utils.SourceLinkUtil; + +/** + * A rule for browsing document links of an object. Properties are not included. + * + * @author Marko Luukkainen + * + */ +public class ObjectChildRule implements org.simantics.browsing.ui.model.children.ChildRule{ + + private boolean showOnlyCheckable = false; + + @Override + public boolean isCompatible(Class contentType) { + return contentType.equals(Resource.class) || contentType.equals(Variable.class); + } + + public void setShowOnlyCheckable(boolean showOnlyCheckable) { + this.showOnlyCheckable = showOnlyCheckable; + } + + @Override + public Collection getChildren(ReadGraph graph, Object obj) + throws DatabaseException { + + ArrayList children = new ArrayList(); + + + Resource resource = null; + Variable variable = null; + if (obj instanceof Resource) { + resource = (Resource)obj; + try { + variable = graph.adapt(resource, Variable.class); + } catch (Throwable t) { + return children; + } + children.add(new StandardGraphChildVariable(variable, null, resource)); + return children; + } else { + variable = (Variable)obj; + resource = variable.getPossibleRepresents(graph); + } + + + DocumentLink sl = DocumentLink.getInstance(graph); + if (graph.isInstanceOf(resource, sl.Source)) + return children; + + + //find sources declared on parentResource using consernsRelation + Variable parentVariable = variable.getParent(graph); + Resource parentRes = null; + Resource relation = null; + if (parentVariable != null) { + parentRes = parentVariable.getPossibleRepresents(graph); + relation = variable.getPossiblePredicateResource(graph); + } + + if (parentRes != null && relation != null) { + Collection sources = graph.getObjects(parentRes, sl.hasSource); + for (Resource source : sources) { + Resource rel = graph.getPossibleObject(source, sl.consernsRelation); + if (rel != null && rel.equals(relation)) { + if (showOnlyCheckable && + (SourceLinkUtil.isValidSource(graph, source) && + SourceLinkUtil.isUpToDateSource(graph, source))) + continue; + children.add(new StandardGraphChildVariable(variable, null, source)); + } + } + } + //find sources declared on this resource, ie. sources that do not have consernsRelation. + Collection sources = graph.getObjects(resource, sl.hasSource); + for (Resource source : sources) { + Resource rel = graph.getPossibleObject(source, sl.consernsRelation); + if (rel == null) { + if (showOnlyCheckable && + (SourceLinkUtil.isValidSource(graph, source) && + SourceLinkUtil.isUpToDateSource(graph, source))) + continue; + children.add(new StandardGraphChildVariable(variable, null, source)); + } + } + while (children.remove(variable)) { + + } + + + return children; + } + + @Override + public Collection getParents(ReadGraph graph, Object child) + throws DatabaseException { + return new ArrayList(); + } + +}