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