]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/ge/ModelChildRule.java b/bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/ge/ModelChildRule.java
new file mode 100644 (file)
index 0000000..b8c5b84
--- /dev/null
@@ -0,0 +1,118 @@
+package org.simantics.document.linking.ge;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.StandardGraphChildVariable;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.document.linking.ontology.DocumentLink;\r
+import org.simantics.layer0.Layer0;\r
+\r
+/**\r
+ * Rule for browsing all document links in a model.\r
+ * \r
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
+ *\r
+ */\r
+public class ModelChildRule implements org.simantics.browsing.ui.model.children.ChildRule{\r
+       \r
+       private ObjectChildRule objectChildRule;\r
+       private PropertyChildRule propertyChildRule;\r
+       private boolean showOnlyCheckable = false; // show only removed old old links\r
+       \r
+       public ModelChildRule(ReadGraph graph, Resource r) {\r
+               DocumentLink sl = DocumentLink.getInstance(graph);\r
+               showOnlyCheckable = sl.ModelViewpointBrowseContext2_ChildRule.equals(r);\r
+               objectChildRule = new ObjectChildRule();\r
+               propertyChildRule = new PropertyChildRule(false);\r
+               objectChildRule.setShowOnlyCheckable(showOnlyCheckable);\r
+               propertyChildRule.setShowOnlyCheckable(showOnlyCheckable);\r
+       }\r
+       \r
+       @Override\r
+       public boolean isCompatible(Class<?> contentType) {\r
+               return contentType.equals(Resource.class) || contentType.equals(Variable.class);\r
+       }\r
+       \r
+       @Override\r
+       public Collection<?> getChildren(ReadGraph graph, Object obj)\r
+                       throws DatabaseException {\r
+\r
+               ArrayList<Object> children = new ArrayList<Object>();\r
+               \r
+\r
+               Resource resource = null;\r
+               Variable variable = null;\r
+               if (obj instanceof Resource) {\r
+                       resource = (Resource)obj;\r
+                       try {\r
+                               variable = graph.adapt(resource, Variable.class);\r
+                       } catch (Throwable t) {\r
+                               return children;\r
+                       }\r
+                       children.add(new StandardGraphChildVariable(variable,null,  resource));\r
+                       return children;\r
+               } else {\r
+                       variable = (Variable)obj;\r
+                       resource = variable.getPossibleRepresents(graph);\r
+               }\r
+               \r
+       \r
+               DocumentLink sl = DocumentLink.getInstance(graph);\r
+               if (graph.isInstanceOf(resource, sl.Source))\r
+                       return children;\r
+               Layer0 l0 = Layer0.getInstance(graph);\r
+//             children.addAll(graph.getObjects(resource, l0.ConsistsOf));\r
+               for (Resource  r : graph.getObjects(resource, l0.ConsistsOf)) {\r
+                       Variable v = new StandardGraphChildVariable(variable,null,  r);\r
+                       if (hasLinkedChildren(graph, v))\r
+                               children.add(v);\r
+               }\r
+               children.addAll(objectChildRule.getChildren(graph, variable));\r
+               children.addAll(propertyChildRule.getChildren(graph, variable));\r
+               \r
+\r
+               return children;\r
+       }\r
+       \r
+       private boolean hasLinkedChildren(ReadGraph graph, Variable variable) throws DatabaseException {\r
+               if (getLinkChildren(graph, variable).size() > 0)\r
+                       return true;\r
+               Collection<Variable> children = getObjectChildren(graph, variable);\r
+               if (children.size() == 0)\r
+                       return false;\r
+               \r
+               for (Variable child : children) {\r
+                       if (hasLinkedChildren(graph, child))\r
+                               return true;\r
+               }\r
+               return false;\r
+                       \r
+       }\r
+       \r
+       private Collection<Variable> getObjectChildren(ReadGraph graph, Variable variable) throws DatabaseException{\r
+               ArrayList<Variable> children = new ArrayList<Variable>();\r
+               Layer0 l0 = Layer0.getInstance(graph);\r
+               for (Resource  r : graph.getObjects(variable.getRepresents(graph), l0.ConsistsOf)) {\r
+                       children.add(new StandardGraphChildVariable(variable,null, r));\r
+               }\r
+               return children;\r
+       }\r
+       \r
+       private Collection<?> getLinkChildren(ReadGraph graph, Variable variable) throws DatabaseException{\r
+               ArrayList<Object> children = new ArrayList<Object>();\r
+               children.addAll(objectChildRule.getChildren(graph, variable));\r
+               children.addAll(propertyChildRule.getChildren(graph, variable));\r
+               return children;\r
+       }\r
+       \r
+       @Override\r
+       public Collection<?> getParents(ReadGraph graph, Object child)\r
+                       throws DatabaseException {\r
+               return new ArrayList<Resource>();\r
+       }\r
+\r
+}\r