]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/ge/ObjectChildRule.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.document.linking.ui / src / org / simantics / document / linking / ge / ObjectChildRule.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.document.linking.utils.SourceLinkUtil;
13
14 /**
15  * A rule for browsing document links of an object. Properties are not included.
16  * 
17  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
18  *
19  */
20 public class ObjectChildRule implements org.simantics.browsing.ui.model.children.ChildRule{
21         
22         private boolean showOnlyCheckable = false;
23         
24         @Override
25         public boolean isCompatible(Class<?> contentType) {
26                 return contentType.equals(Resource.class) || contentType.equals(Variable.class);
27         }
28         
29         public void setShowOnlyCheckable(boolean showOnlyCheckable) {
30                 this.showOnlyCheckable = showOnlyCheckable;
31         }
32         
33         @Override
34         public Collection<?> getChildren(ReadGraph graph, Object obj)
35                         throws DatabaseException {
36
37                 ArrayList<Variable> children = new ArrayList<Variable>();
38                 
39
40                 Resource resource = null;
41                 Variable variable = null;
42                 if (obj instanceof Resource) {
43                         resource = (Resource)obj;
44                         try {
45                                 variable = graph.adapt(resource, Variable.class);
46                         } catch (Throwable t) {
47                                 return children;
48                         }
49                         children.add(new StandardGraphChildVariable(variable, null, resource));
50                         return children;
51                 } else {
52                         variable = (Variable)obj;
53                         resource = variable.getPossibleRepresents(graph);
54                 }
55                 
56         
57                 DocumentLink sl = DocumentLink.getInstance(graph);
58                 if (graph.isInstanceOf(resource, sl.Source))
59                         return children;
60                 
61                 
62                 //find sources declared on parentResource using consernsRelation
63                 Variable parentVariable = variable.getParent(graph);
64                 Resource parentRes = null;
65                 Resource relation = null;
66                 if (parentVariable != null) {
67                         parentRes = parentVariable.getPossibleRepresents(graph);
68                         relation = variable.getPossiblePredicateResource(graph);
69                 }
70                 
71                 if (parentRes != null && relation != null) {
72                         Collection<Resource> sources = graph.getObjects(parentRes, sl.hasSource);
73                         for (Resource source : sources) {
74                                 Resource rel = graph.getPossibleObject(source, sl.consernsRelation);
75                                 if (rel != null && rel.equals(relation)) {
76                                         if (showOnlyCheckable && 
77                                                 (SourceLinkUtil.isValidSource(graph, source) &&
78                                                  SourceLinkUtil.isUpToDateSource(graph, source)))
79                                                 continue;
80                                         children.add(new StandardGraphChildVariable(variable, null, source));
81                                 }
82                         }
83                 }
84                 //find sources declared on this resource, ie. sources that do not have consernsRelation.
85                 Collection<Resource> sources = graph.getObjects(resource, sl.hasSource);
86                 for (Resource source : sources) {
87                         Resource rel = graph.getPossibleObject(source, sl.consernsRelation);
88                         if (rel == null) {
89                                 if (showOnlyCheckable && 
90                                    (SourceLinkUtil.isValidSource(graph, source) &&
91                                     SourceLinkUtil.isUpToDateSource(graph, source)))
92                                         continue;
93                                 children.add(new StandardGraphChildVariable(variable, null, source));
94                         }
95                 }
96                 while (children.remove(variable)) {
97                         
98                 }
99                 
100
101                 return children;
102         }
103         
104         @Override
105         public Collection<?> getParents(ReadGraph graph, Object child)
106                         throws DatabaseException {
107                 return new ArrayList<Resource>();
108         }
109
110 }