]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/viewpoint/VariablePropertyRule.java
Merge commit 'b3da313'
[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             Boolean hidden = property.getPossiblePropertyValue(graph, SEL.hidden, Bindings.BOOLEAN);\r
80             if(hidden != null && hidden) continue props;\r
81             \r
82                 for(String req : requiredProperties) if(property.getPossibleProperty(graph, req) == null) continue props;\r
83 \r
84                 for(String req : filteredProperties) if(property.getName(graph).equals(req)) continue props;\r
85 \r
86                 if(!validate(graph, parent, property)) continue;\r
87                                 \r
88                 //System.err.println("add " + property.getURI(graph));\r
89                 \r
90                 result.add(property);\r
91                                 \r
92         }\r
93         \r
94         Resource predicateResource = parent.getPossiblePredicateResource(graph);\r
95         if (predicateResource != null) {\r
96                 Collection<Resource> underOfs = graph.getObjects(predicateResource, SEL.UnderOf);\r
97                 if (!underOfs.isEmpty()) {\r
98                     Collection<Variable> siblings = parent.getParent(graph).getProperties(graph);\r
99                     for (Variable sibling : siblings) {\r
100                         Resource r = sibling.getPossiblePredicateResource(graph);\r
101                         if (r != null) {\r
102                         if (underOfs.contains(r))\r
103                             result.add(sibling);\r
104                         }\r
105                     }\r
106                 }\r
107         }\r
108         \r
109         if(isUnderProperty(graph, parent)) {\r
110                 Collection<Variable> children = parent.getChildren(graph);\r
111                 for(Variable child : children) {\r
112                     result.add(child);\r
113                 }\r
114         }\r
115         \r
116         return result;\r
117     }\r
118     \r
119     private boolean isUnderProperty(ReadGraph graph, Variable variable) throws DatabaseException {\r
120         Role role = variable.getRole(graph);\r
121         if(Role.PROPERTY.equals(role)) {\r
122                 return true;\r
123         }\r
124         Variable parent = variable.getParent(graph);\r
125         if(parent == null) return false;\r
126         else return isUnderProperty(graph, parent);\r
127         \r
128     }\r
129 \r
130     private boolean isUnder(ReadGraph graph, Layer0 L0, SelectionViewResources SEL, Variable property, ArrayList<Resource> propertiesPredicates) throws DatabaseException {\r
131         Resource predicate = property.getPossiblePredicateResource(graph);\r
132         if (predicate == null)\r
133             return false;\r
134         Collection<Resource> shownUnders = graph.getObjects(predicate, SEL.IsShownUnder);\r
135         return !Collections.disjoint(propertiesPredicates, shownUnders);\r
136     }\r
137     \r
138     @Override\r
139     public Collection<?> getParents(ReadGraph graph, Object child)\r
140             throws DatabaseException {\r
141         Variable parent = ((Variable)child).getParent(graph);\r
142         if(parent == null)\r
143             return Collections.emptyList();\r
144         else\r
145             return Collections.singleton(parent);\r
146     }\r
147 \r
148 }\r