]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/viewpoint/VariablePropertyRule.java
Sync git svn branch with SVN repository r33382.
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / viewpoint / VariablePropertyRule.java
1 package org.simantics.modeling.ui.viewpoint;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collection;\r
5 import java.util.Collections;\r
6 \r
7 import org.simantics.browsing.ui.model.children.ChildRule;\r
8 import org.simantics.databoard.Bindings;\r
9 import org.simantics.db.ReadGraph;\r
10 import org.simantics.db.Resource;\r
11 import org.simantics.db.exception.DatabaseException;\r
12 import org.simantics.db.layer0.variable.Variable;\r
13 import org.simantics.db.layer0.variable.Variables;\r
14 import org.simantics.db.layer0.variable.Variables.Role;\r
15 import org.simantics.layer0.Layer0;\r
16 import org.simantics.modeling.ModelingResources;\r
17 import org.simantics.selectionview.SelectionViewResources;\r
18 \r
19 public class VariablePropertyRule implements ChildRule {\r
20 \r
21         final private ArrayList<String> requiredProperties = new ArrayList<String>();\r
22         final private ArrayList<String> filteredProperties = new ArrayList<String>();\r
23         \r
24         public VariablePropertyRule(ReadGraph graph, Resource rule) throws DatabaseException {\r
25 \r
26                 ModelingResources MOD = ModelingResources.getInstance(graph);\r
27                 for(Resource r : graph.getObjects(rule, MOD.ModelingBrowseContext_VariablePropertyRule_RequireProperty)) {\r
28                         String name = graph.getValue(r, Bindings.STRING);\r
29                         requiredProperties.add(name);\r
30                 }\r
31                 for(Resource r : graph.getObjects(rule, MOD.ModelingBrowseContext_VariablePropertyRule_FilterProperty)) {\r
32                         String name = graph.getValue(r, Bindings.STRING);\r
33                         filteredProperties.add(name);\r
34                 }\r
35                 \r
36         }\r
37         \r
38     @Override\r
39     public boolean isCompatible(Class<?> contentType) {\r
40         return contentType.equals(Variable.class);\r
41     }\r
42 \r
43     private boolean validate(ReadGraph graph, Variable parent, Variable child) throws DatabaseException {\r
44         \r
45         Resource predicate = child.getPossiblePredicateResource(graph);\r
46         if(predicate == null) return true;\r
47         \r
48         Layer0 L0 = Layer0.getInstance(graph);\r
49         if(L0.HasName.equals(predicate) || L0.HasLabel.equals(predicate)) {\r
50             Role role = parent.getRole(graph);\r
51             if(role == Role.PROPERTY) return false;\r
52         }\r
53         return true;\r
54     }\r
55     \r
56     @Override\r
57     public Collection<?> getChildren(ReadGraph graph, Object parent_)\r
58             throws DatabaseException {\r
59         \r
60         Variable parent = (Variable)parent_;\r
61         \r
62         ArrayList<Variable> result = new ArrayList<Variable>();\r
63 \r
64         Collection<Variable> properties = parent.getProperties(graph);\r
65         ArrayList<Resource> propertiesPredicates = new ArrayList<Resource>();\r
66         for (Variable property : properties) {\r
67             Resource r = property.getPossiblePredicateResource(graph);\r
68             if (r != null)\r
69                 propertiesPredicates.add(r);\r
70         }\r
71         \r
72         Layer0 L0 = Layer0.getInstance(graph);\r
73         SelectionViewResources SEL = SelectionViewResources.getInstance(graph);\r
74         \r
75         props: for(Variable property : properties) {\r
76                 \r
77             if (isUnder(graph, L0, SEL, property, propertiesPredicates)) continue props;\r
78             \r
79                 for(String req : requiredProperties) if(property.getPossibleProperty(graph, req) == null) continue props;\r
80 \r
81                 for(String req : filteredProperties) if(property.getName(graph).equals(req)) continue props;\r
82 \r
83                 if(!validate(graph, parent, property)) continue;\r
84                                 \r
85                 //System.err.println("add " + property.getURI(graph));\r
86                 \r
87                 result.add(property);\r
88                                 \r
89         }\r
90         \r
91         Resource predicateResource = parent.getPossiblePredicateResource(graph);\r
92         if (predicateResource != null) {\r
93                 Collection<Resource> underOfs = graph.getObjects(predicateResource, SEL.UnderOf);\r
94                 if (!underOfs.isEmpty()) {\r
95                     Collection<Variable> siblings = parent.getParent(graph).getProperties(graph);\r
96                     for (Variable sibling : siblings) {\r
97                         Resource r = sibling.getPossiblePredicateResource(graph);\r
98                         if (r != null) {\r
99                         if (underOfs.contains(r))\r
100                             result.add(sibling);\r
101                         }\r
102                     }\r
103                 }\r
104         }\r
105         \r
106         if(isUnderProperty(graph, parent)) {\r
107                 Collection<Variable> children = parent.getChildren(graph);\r
108                 for(Variable child : children) {\r
109                     result.add(child);\r
110                 }\r
111         }\r
112         \r
113         return result;\r
114     }\r
115     \r
116     private boolean isUnderProperty(ReadGraph graph, Variable variable) throws DatabaseException {\r
117         Role role = variable.getRole(graph);\r
118         if(Role.PROPERTY.equals(role)) {\r
119                 return true;\r
120         }\r
121         Variable parent = variable.getParent(graph);\r
122         if(parent == null) return false;\r
123         else return isUnderProperty(graph, parent);\r
124         \r
125     }\r
126 \r
127     private boolean isUnder(ReadGraph graph, Layer0 L0, SelectionViewResources SEL, Variable property, ArrayList<Resource> propertiesPredicates) throws DatabaseException {\r
128         Resource predicate = property.getPossiblePredicateResource(graph);\r
129         if (predicate == null)\r
130             return false;\r
131         Collection<Resource> shownUnders = graph.getObjects(predicate, SEL.IsShownUnder);\r
132         return !Collections.disjoint(propertiesPredicates, shownUnders);\r
133     }\r
134     \r
135     @Override\r
136     public Collection<?> getParents(ReadGraph graph, Object child)\r
137             throws DatabaseException {\r
138         Variable parent = ((Variable)child).getParent(graph);\r
139         if(parent == null)\r
140             return Collections.emptyList();\r
141         else\r
142             return Collections.singleton(parent);\r
143     }\r
144 \r
145 }\r