]> gerrit.simantics Code Review - simantics/platform.git/blob
8063eb3c1489d206d3f21650ba3a2e10ec95fc31
[simantics/platform.git] /
1 package org.simantics.selectionview;
2
3 import java.util.Collection;
4
5 import org.simantics.Simantics;
6 import org.simantics.databoard.Bindings;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.common.utils.Functions;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.layer0.variable.Variable;
12 import org.simantics.db.layer0.variable.Variables;
13 import org.simantics.layer0.Layer0;
14 import org.simantics.scl.runtime.function.Function;
15 import org.simantics.ui.selection.WorkbenchSelectionUtils;
16
17 abstract public class AbstractVariableTabContribution implements TabContribution<Object> {
18
19         final private String uri;
20         final protected Resource configuration;
21
22         public AbstractVariableTabContribution(ReadGraph graph, Resource configuration) throws DatabaseException {
23                 this.configuration = configuration;
24                 this.uri = graph.getURI(configuration);
25         }
26
27         public abstract void getContributors(ReadGraph graph, Variable variable, Integer priority, String label, Collection<ComparableTabContributor> result) throws DatabaseException;
28
29         @Override
30         public boolean accept(ReadGraph graph, Object input) throws DatabaseException {
31                 return true;
32         }
33
34         public boolean accept(ReadGraph graph, Variable variable, Object selection) throws DatabaseException {
35
36                 if(selection instanceof SelectionInput) {
37                 
38                         SelectionViewResources SEL = SelectionViewResources.getInstance(graph);
39                         for(Resource r : graph.getObjects(configuration, SEL.AbstractVariableTabContribution_HasTest)) {
40                                 Boolean pass = Functions.exec(graph, r, graph, (SelectionInput)selection);
41                                 if(!pass) return false;
42                         }
43                 
44                         Variable configurationVariable = Variables.getVariable(graph, configuration);
45                         Function f = configurationVariable.getPossiblePropertyValue(graph, SEL.AbstractVariableTabContribution_testFunction);
46                         if(f != null) {
47                                 Boolean pass = Simantics.<Boolean>applySCLRead(graph, f, variable);
48                                 if(!pass) return false;
49                         }
50                         
51                 } else {
52                         
53                         return false;
54                         
55                 }
56                 
57                 return true;
58                 
59         }
60         
61         final private Variable getPossibleVariable(ReadGraph graph, Object selection) throws DatabaseException {
62                 if(selection instanceof SelectionInput) {
63                         SelectionInput si = (SelectionInput)selection;
64                         if(si.getElements().size() == 1) {
65                                 return WorkbenchSelectionUtils.getPossibleVariable(graph, si.getElements().iterator().next());
66                         }
67                 }
68                 return null;
69         } 
70         
71         @Override
72         final public void contribute(ReadGraph graph, final Object selection, Collection<ComparableTabContributor> result) throws DatabaseException {
73                 
74                 Variable variable = getPossibleVariable(graph, selection);
75                 if(variable == null) return;
76
77                 Layer0 L0 = Layer0.getInstance(graph);
78                 SelectionViewResources SEL = SelectionViewResources.getInstance(graph);
79
80                 Integer priority = graph.getPossibleRelatedValue(configuration, SEL.AbstractVariableTabContribution_HasPriority, Bindings.INTEGER);
81                 String label = graph.getPossibleRelatedValue(configuration, L0.HasLabel);
82                 
83                 if(!accept(graph, variable, selection)) return;
84
85                 getContributors(graph, variable, priority, label, result);              
86
87         }
88         
89         @Override
90         public String toString() {
91                 return "AbstractVariableTabContribution[" + uri + "]";
92         }
93
94 }