]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/ge/ModelChildRule.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.document.linking.ui / src / org / simantics / document / linking / ge / ModelChildRule.java
1 package org.simantics.document.linking.ge;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collection;\r
5 \r
6 import org.simantics.db.ReadGraph;\r
7 import org.simantics.db.Resource;\r
8 import org.simantics.db.exception.DatabaseException;\r
9 import org.simantics.db.layer0.variable.StandardGraphChildVariable;\r
10 import org.simantics.db.layer0.variable.Variable;\r
11 import org.simantics.document.linking.ontology.DocumentLink;\r
12 import org.simantics.layer0.Layer0;\r
13 \r
14 /**\r
15  * Rule for browsing all document links in a model.\r
16  * \r
17  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
18  *\r
19  */\r
20 public class ModelChildRule implements org.simantics.browsing.ui.model.children.ChildRule{\r
21         \r
22         private ObjectChildRule objectChildRule;\r
23         private PropertyChildRule propertyChildRule;\r
24         private boolean showOnlyCheckable = false; // show only removed old old links\r
25         \r
26         public ModelChildRule(ReadGraph graph, Resource r) {\r
27                 DocumentLink sl = DocumentLink.getInstance(graph);\r
28                 showOnlyCheckable = sl.ModelViewpointBrowseContext2_ChildRule.equals(r);\r
29                 objectChildRule = new ObjectChildRule();\r
30                 propertyChildRule = new PropertyChildRule(false);\r
31                 objectChildRule.setShowOnlyCheckable(showOnlyCheckable);\r
32                 propertyChildRule.setShowOnlyCheckable(showOnlyCheckable);\r
33         }\r
34         \r
35         @Override\r
36         public boolean isCompatible(Class<?> contentType) {\r
37                 return contentType.equals(Resource.class) || contentType.equals(Variable.class);\r
38         }\r
39         \r
40         @Override\r
41         public Collection<?> getChildren(ReadGraph graph, Object obj)\r
42                         throws DatabaseException {\r
43 \r
44                 ArrayList<Object> children = new ArrayList<Object>();\r
45                 \r
46 \r
47                 Resource resource = null;\r
48                 Variable variable = null;\r
49                 if (obj instanceof Resource) {\r
50                         resource = (Resource)obj;\r
51                         try {\r
52                                 variable = graph.adapt(resource, Variable.class);\r
53                         } catch (Throwable t) {\r
54                                 return children;\r
55                         }\r
56                         children.add(new StandardGraphChildVariable(variable,null,  resource));\r
57                         return children;\r
58                 } else {\r
59                         variable = (Variable)obj;\r
60                         resource = variable.getPossibleRepresents(graph);\r
61                 }\r
62                 \r
63         \r
64                 DocumentLink sl = DocumentLink.getInstance(graph);\r
65                 if (graph.isInstanceOf(resource, sl.Source))\r
66                         return children;\r
67                 Layer0 l0 = Layer0.getInstance(graph);\r
68 //              children.addAll(graph.getObjects(resource, l0.ConsistsOf));\r
69                 for (Resource  r : graph.getObjects(resource, l0.ConsistsOf)) {\r
70                         Variable v = new StandardGraphChildVariable(variable,null,  r);\r
71                         if (hasLinkedChildren(graph, v))\r
72                                 children.add(v);\r
73                 }\r
74                 children.addAll(objectChildRule.getChildren(graph, variable));\r
75                 children.addAll(propertyChildRule.getChildren(graph, variable));\r
76                 \r
77 \r
78                 return children;\r
79         }\r
80         \r
81         private boolean hasLinkedChildren(ReadGraph graph, Variable variable) throws DatabaseException {\r
82                 if (getLinkChildren(graph, variable).size() > 0)\r
83                         return true;\r
84                 Collection<Variable> children = getObjectChildren(graph, variable);\r
85                 if (children.size() == 0)\r
86                         return false;\r
87                 \r
88                 for (Variable child : children) {\r
89                         if (hasLinkedChildren(graph, child))\r
90                                 return true;\r
91                 }\r
92                 return false;\r
93                         \r
94         }\r
95         \r
96         private Collection<Variable> getObjectChildren(ReadGraph graph, Variable variable) throws DatabaseException{\r
97                 ArrayList<Variable> children = new ArrayList<Variable>();\r
98                 Layer0 l0 = Layer0.getInstance(graph);\r
99                 for (Resource  r : graph.getObjects(variable.getRepresents(graph), l0.ConsistsOf)) {\r
100                         children.add(new StandardGraphChildVariable(variable,null, r));\r
101                 }\r
102                 return children;\r
103         }\r
104         \r
105         private Collection<?> getLinkChildren(ReadGraph graph, Variable variable) throws DatabaseException{\r
106                 ArrayList<Object> children = new ArrayList<Object>();\r
107                 children.addAll(objectChildRule.getChildren(graph, variable));\r
108                 children.addAll(propertyChildRule.getChildren(graph, variable));\r
109                 return children;\r
110         }\r
111         \r
112         @Override\r
113         public Collection<?> getParents(ReadGraph graph, Object child)\r
114                         throws DatabaseException {\r
115                 return new ArrayList<Resource>();\r
116         }\r
117 \r
118 }\r