]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/ge/ModelChildRule.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.document.linking.ui / src / org / simantics / document / linking / ge / ModelChildRule.java
1 package org.simantics.document.linking.ge;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5
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;
13
14 /**
15  * Rule for browsing all document links in a model.
16  * 
17  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
18  *
19  */
20 public class ModelChildRule implements org.simantics.browsing.ui.model.children.ChildRule{
21         
22         private ObjectChildRule objectChildRule;
23         private PropertyChildRule propertyChildRule;
24         private boolean showOnlyCheckable = false; // show only removed old old links
25         
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);
33         }
34         
35         @Override
36         public boolean isCompatible(Class<?> contentType) {
37                 return contentType.equals(Resource.class) || contentType.equals(Variable.class);
38         }
39         
40         @Override
41         public Collection<?> getChildren(ReadGraph graph, Object obj)
42                         throws DatabaseException {
43
44                 ArrayList<Object> children = new ArrayList<Object>();
45                 
46
47                 Resource resource = null;
48                 Variable variable = null;
49                 if (obj instanceof Resource) {
50                         resource = (Resource)obj;
51                         try {
52                                 variable = graph.adapt(resource, Variable.class);
53                         } catch (Throwable t) {
54                                 return children;
55                         }
56                         children.add(new StandardGraphChildVariable(variable,null,  resource));
57                         return children;
58                 } else {
59                         variable = (Variable)obj;
60                         resource = variable.getPossibleRepresents(graph);
61                 }
62                 
63         
64                 DocumentLink sl = DocumentLink.getInstance(graph);
65                 if (graph.isInstanceOf(resource, sl.Source))
66                         return children;
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))
72                                 children.add(v);
73                 }
74                 children.addAll(objectChildRule.getChildren(graph, variable));
75                 children.addAll(propertyChildRule.getChildren(graph, variable));
76                 
77
78                 return children;
79         }
80         
81         private boolean hasLinkedChildren(ReadGraph graph, Variable variable) throws DatabaseException {
82                 if (getLinkChildren(graph, variable).size() > 0)
83                         return true;
84                 Collection<Variable> children = getObjectChildren(graph, variable);
85                 if (children.size() == 0)
86                         return false;
87                 
88                 for (Variable child : children) {
89                         if (hasLinkedChildren(graph, child))
90                                 return true;
91                 }
92                 return false;
93                         
94         }
95         
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));
101                 }
102                 return children;
103         }
104         
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));
109                 return children;
110         }
111         
112         @Override
113         public Collection<?> getParents(ReadGraph graph, Object child)
114                         throws DatabaseException {
115                 return new ArrayList<Resource>();
116         }
117
118 }