1 package org.simantics.selectionview;
3 import java.util.Collection;
4 import java.util.Collections;
6 import org.eclipse.swt.SWT;
7 import org.eclipse.swt.widgets.Composite;
8 import org.eclipse.swt.widgets.Event;
9 import org.eclipse.swt.widgets.Listener;
10 import org.eclipse.ui.IWorkbenchSite;
11 import org.simantics.Simantics;
12 import org.simantics.databoard.Bindings;
13 import org.simantics.db.ReadGraph;
14 import org.simantics.db.Resource;
15 import org.simantics.db.common.utils.Functions;
16 import org.simantics.db.common.utils.Logger;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.layer0.variable.Variable;
19 import org.simantics.layer0.Layer0;
20 import org.simantics.ui.selection.WorkbenchSelectionUtils;
21 import org.simantics.views.swt.SWTViewLoaderProcess;
22 import org.simantics.views.swt.client.base.SWTRoot;
24 public class TypedVariableTabContribution implements TabContribution<Object> {
26 final private Resource configuration;
28 public TypedVariableTabContribution(ReadGraph graph, Resource configuration) throws DatabaseException {
29 this.configuration = configuration;
32 static class Contribution extends ModelledTabContributor {
34 private String viewURI;
36 public Contribution(String viewURI) {
37 this.viewURI = viewURI;
41 public void createControls(Composite body, IWorkbenchSite site) {
45 final SWTViewLoaderProcess loader = new SWTViewLoaderProcess(null, site);
46 final SWTRoot root = loader.load(viewURI, runtime);
47 root.createControls(body);
48 body.addListener(SWT.Dispose, new Listener() {
51 public void handleEvent(Event event) {
52 if(!loader.isDisposed())
58 } catch (DatabaseException e) {
61 Logger.defaultLogError(e);
68 public boolean equals(Object obj) {
73 else if (!(obj instanceof Contribution))
75 Contribution c = (Contribution)obj;
76 return viewURI.equals(c.viewURI);
80 public int hashCode() {
81 return viewURI.hashCode();
88 public boolean accept(ReadGraph graph, Object input) {
90 // return (input instanceof Variable);
93 private Variable getPossibleVariable(ReadGraph graph, Object input) throws DatabaseException {
95 if(input instanceof Variable) {
96 return (Variable)input;
98 return WorkbenchSelectionUtils.getPossibleVariable(graph, input);
104 public void contribute(ReadGraph graph, final Object object, Collection<ComparableTabContributor> result) throws DatabaseException {
106 Variable variable = getPossibleVariable(graph, object);
107 if(variable == null) return;
109 Layer0 L0 = Layer0.getInstance(graph);
110 SelectionViewResources SEL = SelectionViewResources.getInstance(graph);
111 Collection<Resource> types = graph.getObjects(configuration, SEL.TypedVariableTabContribution_HasType);
112 if(types.isEmpty()) throw new DatabaseException("No type for " + graph.getPossibleURI(configuration));
113 Resource view = graph.getPossibleObject(configuration, SEL.VariableTabContribution_HasView);
115 throw new DatabaseException("No view for " + graph.getPossibleURI(configuration));
117 Integer priority = graph.getPossibleRelatedValue(configuration, SEL.VariableTabContribution_HasPriority, Bindings.INTEGER);
118 String label = graph.getPossibleRelatedValue(configuration, L0.HasLabel);
119 final String viewURI = graph.getPossibleURI(view);
121 Resource resource = variable.getPossibleRepresents(graph);
122 if (resource == null)
125 Collection<Resource> ts = graph.getTypes(resource);
127 if (!Collections.disjoint(types, ts)) {
129 for(Resource r : graph.getObjects(configuration, SEL.VariableTabContribution_HasTest)) {
130 Boolean pass = Functions.exec(graph, r, graph, variable);
135 Variable transformed = Simantics.tryInvokeSCL(graph, configuration, SEL.VariableTabContribution_transformation, variable);
136 if(transformed != null) variable = transformed;
137 } catch (DatabaseException e) {
138 // Errors are just ignored
141 result.add(new ComparableTabContributor(new Contribution(viewURI), priority, variable, label));