]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/viewpoint/VariablePropertyRule.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / viewpoint / VariablePropertyRule.java
diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/viewpoint/VariablePropertyRule.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/viewpoint/VariablePropertyRule.java
new file mode 100644 (file)
index 0000000..76ec9b9
--- /dev/null
@@ -0,0 +1,145 @@
+package org.simantics.modeling.ui.viewpoint;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+\r
+import org.simantics.browsing.ui.model.children.ChildRule;\r
+import org.simantics.databoard.Bindings;\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.Variable;\r
+import org.simantics.db.layer0.variable.Variables;\r
+import org.simantics.db.layer0.variable.Variables.Role;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.selectionview.SelectionViewResources;\r
+\r
+public class VariablePropertyRule implements ChildRule {\r
+\r
+       final private ArrayList<String> requiredProperties = new ArrayList<String>();\r
+       final private ArrayList<String> filteredProperties = new ArrayList<String>();\r
+       \r
+       public VariablePropertyRule(ReadGraph graph, Resource rule) throws DatabaseException {\r
+\r
+               ModelingResources MOD = ModelingResources.getInstance(graph);\r
+               for(Resource r : graph.getObjects(rule, MOD.ModelingBrowseContext_VariablePropertyRule_RequireProperty)) {\r
+                       String name = graph.getValue(r, Bindings.STRING);\r
+                       requiredProperties.add(name);\r
+               }\r
+               for(Resource r : graph.getObjects(rule, MOD.ModelingBrowseContext_VariablePropertyRule_FilterProperty)) {\r
+                       String name = graph.getValue(r, Bindings.STRING);\r
+                       filteredProperties.add(name);\r
+               }\r
+               \r
+       }\r
+       \r
+    @Override\r
+    public boolean isCompatible(Class<?> contentType) {\r
+        return contentType.equals(Variable.class);\r
+    }\r
+\r
+    private boolean validate(ReadGraph graph, Variable parent, Variable child) throws DatabaseException {\r
+       \r
+       Resource predicate = child.getPossiblePredicateResource(graph);\r
+       if(predicate == null) return true;\r
+       \r
+       Layer0 L0 = Layer0.getInstance(graph);\r
+       if(L0.HasName.equals(predicate) || L0.HasLabel.equals(predicate)) {\r
+            Role role = parent.getRole(graph);\r
+            if(role == Role.PROPERTY) return false;\r
+       }\r
+       return true;\r
+    }\r
+    \r
+    @Override\r
+    public Collection<?> getChildren(ReadGraph graph, Object parent_)\r
+            throws DatabaseException {\r
+       \r
+       Variable parent = (Variable)parent_;\r
+       \r
+       ArrayList<Variable> result = new ArrayList<Variable>();\r
+\r
+       Collection<Variable> properties = parent.getProperties(graph);\r
+       ArrayList<Resource> propertiesPredicates = new ArrayList<Resource>();\r
+       for (Variable property : properties) {\r
+           Resource r = property.getPossiblePredicateResource(graph);\r
+           if (r != null)\r
+               propertiesPredicates.add(r);\r
+       }\r
+       \r
+       Layer0 L0 = Layer0.getInstance(graph);\r
+       SelectionViewResources SEL = SelectionViewResources.getInstance(graph);\r
+       \r
+       props: for(Variable property : properties) {\r
+               \r
+           if (isUnder(graph, L0, SEL, property, propertiesPredicates)) continue props;\r
+           \r
+               for(String req : requiredProperties) if(property.getPossibleProperty(graph, req) == null) continue props;\r
+\r
+               for(String req : filteredProperties) if(property.getName(graph).equals(req)) continue props;\r
+\r
+               if(!validate(graph, parent, property)) continue;\r
+                               \r
+               //System.err.println("add " + property.getURI(graph));\r
+               \r
+               result.add(property);\r
+                               \r
+       }\r
+       \r
+       Resource predicateResource = parent.getPossiblePredicateResource(graph);\r
+       if (predicateResource != null) {\r
+               Collection<Resource> underOfs = graph.getObjects(predicateResource, SEL.UnderOf);\r
+               if (!underOfs.isEmpty()) {\r
+                   Collection<Variable> siblings = parent.getParent(graph).getProperties(graph);\r
+                   for (Variable sibling : siblings) {\r
+                       Resource r = sibling.getPossiblePredicateResource(graph);\r
+                       if (r != null) {\r
+                        if (underOfs.contains(r))\r
+                            result.add(sibling);\r
+                       }\r
+                   }\r
+               }\r
+       }\r
+       \r
+       if(isUnderProperty(graph, parent)) {\r
+               Collection<Variable> children = parent.getChildren(graph);\r
+               for(Variable child : children) {\r
+                   result.add(child);\r
+               }\r
+       }\r
+       \r
+        return result;\r
+    }\r
+    \r
+    private boolean isUnderProperty(ReadGraph graph, Variable variable) throws DatabaseException {\r
+       Role role = variable.getRole(graph);\r
+       if(Role.PROPERTY.equals(role)) {\r
+               return true;\r
+       }\r
+       Variable parent = variable.getParent(graph);\r
+       if(parent == null) return false;\r
+       else return isUnderProperty(graph, parent);\r
+       \r
+    }\r
+\r
+    private boolean isUnder(ReadGraph graph, Layer0 L0, SelectionViewResources SEL, Variable property, ArrayList<Resource> propertiesPredicates) throws DatabaseException {\r
+        Resource predicate = property.getPossiblePredicateResource(graph);\r
+        if (predicate == null)\r
+            return false;\r
+        Collection<Resource> shownUnders = graph.getObjects(predicate, SEL.IsShownUnder);\r
+        return !Collections.disjoint(propertiesPredicates, shownUnders);\r
+    }\r
+    \r
+    @Override\r
+    public Collection<?> getParents(ReadGraph graph, Object child)\r
+            throws DatabaseException {\r
+        Variable parent = ((Variable)child).getParent(graph);\r
+        if(parent == null)\r
+            return Collections.emptyList();\r
+        else\r
+            return Collections.singleton(parent);\r
+    }\r
+\r
+}\r