1 package org.simantics.selectionview;
3 import java.util.Collection;
5 import org.eclipse.swt.SWT;
6 import org.eclipse.swt.widgets.Composite;
7 import org.eclipse.swt.widgets.Event;
8 import org.eclipse.swt.widgets.Listener;
9 import org.eclipse.ui.IWorkbenchSite;
10 import org.simantics.Simantics;
11 import org.simantics.databoard.Bindings;
12 import org.simantics.db.ReadGraph;
13 import org.simantics.db.Resource;
14 import org.simantics.db.common.utils.Functions;
15 import org.simantics.db.common.utils.Logger;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.layer0.variable.Variable;
18 import org.simantics.db.layer0.variable.Variables;
19 import org.simantics.layer0.Layer0;
20 import org.simantics.scl.runtime.function.Function;
21 import org.simantics.views.swt.SWTViewLoaderProcess;
22 import org.simantics.views.swt.client.base.SWTRoot;
24 public class VariableTabContribution implements TabContribution<Variable> {
26 final private Resource configuration;
28 public VariableTabContribution(ReadGraph graph, Resource configuration) throws DatabaseException {
29 this.configuration = configuration;
33 public boolean accept(ReadGraph graph, Object input) {
34 return (input instanceof Variable);
38 public void contribute(ReadGraph graph, final Variable variable, Collection<ComparableTabContributor> result) throws DatabaseException {
40 Layer0 L0 = Layer0.getInstance(graph);
41 SelectionViewResources SEL = SelectionViewResources.getInstance(graph);
43 Resource view = graph.getPossibleObject(configuration, SEL.VariableTabContribution_HasView);
44 if(view == null) throw new DatabaseException("No view for " + graph.getPossibleURI(configuration));
46 Integer priority = graph.getPossibleRelatedValue(configuration, SEL.VariableTabContribution_HasPriority, Bindings.INTEGER);
47 String label = graph.getPossibleRelatedValue(configuration, L0.HasLabel);
49 for(Resource r : graph.getObjects(configuration, SEL.VariableTabContribution_HasTest)) {
50 Boolean pass = Functions.exec(graph, r, graph, variable);
54 Variable configurationVariable = Variables.getVariable(graph, configuration);
55 Function f = configurationVariable.getPossiblePropertyValue(graph, SEL.AbstractVariableTabContribution_testFunction);
57 Boolean pass = Simantics.<Boolean>applySCLRead(graph, f, variable);
61 final String viewURI = graph.getPossibleURI(view);
63 result.add(new ComparableTabContributor(new ModelledTabContributor() {
66 public void createControls(Composite body, IWorkbenchSite site) {
70 final SWTViewLoaderProcess loader = new SWTViewLoaderProcess(null, site);
71 SWTRoot root = loader.load(viewURI, runtime);
72 root.createControls(body);
73 body.addListener(SWT.Dispose, new Listener() {
76 public void handleEvent(Event event) {
77 if(!loader.isDisposed())
83 } catch (DatabaseException e) {
86 Logger.defaultLogError(e);
92 }, priority, variable, label));