1 package org.simantics.selectionview;
3 import java.util.Collection;
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;
17 abstract public class AbstractVariableTabContribution implements TabContribution<Object> {
19 final private String uri;
20 final protected Resource configuration;
22 public AbstractVariableTabContribution(ReadGraph graph, Resource configuration) throws DatabaseException {
23 this.configuration = configuration;
24 this.uri = graph.getURI(configuration);
27 public abstract void getContributors(ReadGraph graph, Variable variable, Integer priority, String label, Collection<ComparableTabContributor> result) throws DatabaseException;
30 public boolean accept(ReadGraph graph, Object input) throws DatabaseException {
34 public boolean accept(ReadGraph graph, Variable variable, Object selection) throws DatabaseException {
36 if(selection instanceof SelectionInput) {
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;
44 Variable configurationVariable = Variables.getVariable(graph, configuration);
45 Function f = configurationVariable.getPossiblePropertyValue(graph, SEL.AbstractVariableTabContribution_testFunction);
47 Boolean pass = Simantics.<Boolean>applySCLRead(graph, f, variable);
48 if(!pass) return false;
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());
72 final public void contribute(ReadGraph graph, final Object selection, Collection<ComparableTabContributor> result) throws DatabaseException {
74 Variable variable = getPossibleVariable(graph, selection);
75 if(variable == null) return;
77 Layer0 L0 = Layer0.getInstance(graph);
78 SelectionViewResources SEL = SelectionViewResources.getInstance(graph);
80 Integer priority = graph.getPossibleRelatedValue(configuration, SEL.AbstractVariableTabContribution_HasPriority, Bindings.INTEGER);
81 String label = graph.getPossibleRelatedValue(configuration, L0.HasLabel);
83 if(!accept(graph, variable, selection)) return;
85 getContributors(graph, variable, priority, label, result);
90 public String toString() {
91 return "AbstractVariableTabContribution[" + uri + "]";