1 package org.simantics.document.linking.ge;
3 import java.util.ArrayList;
4 import java.util.Collection;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.db.layer0.variable.StandardGraphChildVariable;
10 import org.simantics.db.layer0.variable.Variable;
11 import org.simantics.document.linking.ontology.DocumentLink;
12 import org.simantics.layer0.Layer0;
15 * Rule for browsing all document links in a model.
17 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
20 public class ModelChildRule implements org.simantics.browsing.ui.model.children.ChildRule{
22 private ObjectChildRule objectChildRule;
23 private PropertyChildRule propertyChildRule;
24 private boolean showOnlyCheckable = false; // show only removed old old links
26 public ModelChildRule(ReadGraph graph, Resource r) {
27 DocumentLink sl = DocumentLink.getInstance(graph);
28 showOnlyCheckable = sl.ModelViewpointBrowseContext2_ChildRule.equals(r);
29 objectChildRule = new ObjectChildRule();
30 propertyChildRule = new PropertyChildRule(false);
31 objectChildRule.setShowOnlyCheckable(showOnlyCheckable);
32 propertyChildRule.setShowOnlyCheckable(showOnlyCheckable);
36 public boolean isCompatible(Class<?> contentType) {
37 return contentType.equals(Resource.class) || contentType.equals(Variable.class);
41 public Collection<?> getChildren(ReadGraph graph, Object obj)
42 throws DatabaseException {
44 ArrayList<Object> children = new ArrayList<Object>();
47 Resource resource = null;
48 Variable variable = null;
49 if (obj instanceof Resource) {
50 resource = (Resource)obj;
52 variable = graph.adapt(resource, Variable.class);
53 } catch (Throwable t) {
56 children.add(new StandardGraphChildVariable(variable,null, resource));
59 variable = (Variable)obj;
60 resource = variable.getPossibleRepresents(graph);
64 DocumentLink sl = DocumentLink.getInstance(graph);
65 if (graph.isInstanceOf(resource, sl.Source))
67 Layer0 l0 = Layer0.getInstance(graph);
68 // children.addAll(graph.getObjects(resource, l0.ConsistsOf));
69 for (Resource r : graph.getObjects(resource, l0.ConsistsOf)) {
70 Variable v = new StandardGraphChildVariable(variable,null, r);
71 if (hasLinkedChildren(graph, v))
74 children.addAll(objectChildRule.getChildren(graph, variable));
75 children.addAll(propertyChildRule.getChildren(graph, variable));
81 private boolean hasLinkedChildren(ReadGraph graph, Variable variable) throws DatabaseException {
82 if (getLinkChildren(graph, variable).size() > 0)
84 Collection<Variable> children = getObjectChildren(graph, variable);
85 if (children.size() == 0)
88 for (Variable child : children) {
89 if (hasLinkedChildren(graph, child))
96 private Collection<Variable> getObjectChildren(ReadGraph graph, Variable variable) throws DatabaseException{
97 ArrayList<Variable> children = new ArrayList<Variable>();
98 Layer0 l0 = Layer0.getInstance(graph);
99 for (Resource r : graph.getObjects(variable.getRepresents(graph), l0.ConsistsOf)) {
100 children.add(new StandardGraphChildVariable(variable,null, r));
105 private Collection<?> getLinkChildren(ReadGraph graph, Variable variable) throws DatabaseException{
106 ArrayList<Object> children = new ArrayList<Object>();
107 children.addAll(objectChildRule.getChildren(graph, variable));
108 children.addAll(propertyChildRule.getChildren(graph, variable));
113 public Collection<?> getParents(ReadGraph graph, Object child)
114 throws DatabaseException {
115 return new ArrayList<Resource>();