]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.selectionview/src/org/simantics/selectionview/VariableTabContribution.java
Support labels for property children
[simantics/platform.git] / bundles / org.simantics.selectionview / src / org / simantics / selectionview / VariableTabContribution.java
1 package org.simantics.selectionview;
2
3 import java.util.Collection;
4
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;
23
24 public class VariableTabContribution implements TabContribution<Variable> {
25
26         final private Resource configuration;
27
28         public VariableTabContribution(ReadGraph graph, Resource configuration) throws DatabaseException {
29                 this.configuration = configuration;
30         }
31
32         @Override
33         public boolean accept(ReadGraph graph, Object input) {
34                 return (input instanceof Variable);
35         }
36
37         @Override
38         public void contribute(ReadGraph graph, final Variable variable, Collection<ComparableTabContributor> result) throws DatabaseException {
39
40                 Layer0 L0 = Layer0.getInstance(graph);
41                 SelectionViewResources SEL = SelectionViewResources.getInstance(graph);
42
43                 Resource view = graph.getPossibleObject(configuration, SEL.VariableTabContribution_HasView);
44                 if(view == null) throw new DatabaseException("No view for " + graph.getPossibleURI(configuration));
45
46                 Integer priority = graph.getPossibleRelatedValue(configuration, SEL.VariableTabContribution_HasPriority, Bindings.INTEGER);
47                 String label = graph.getPossibleRelatedValue(configuration, L0.HasLabel);
48                 
49                 for(Resource r : graph.getObjects(configuration, SEL.VariableTabContribution_HasTest)) {
50                         Boolean pass = Functions.exec(graph, r, graph, variable);
51                         if(!pass) return;
52                 }
53                 
54                 Variable configurationVariable = Variables.getVariable(graph, configuration);
55                 Function f = configurationVariable.getPossiblePropertyValue(graph, SEL.AbstractVariableTabContribution_testFunction);
56                 if(f != null) {
57                         Boolean pass = Simantics.<Boolean>applySCLRead(graph, f, variable);
58                         if(!pass) return;
59                 }
60
61                 final String viewURI = graph.getPossibleURI(view);
62                 
63                 result.add(new ComparableTabContributor(new ModelledTabContributor() {
64
65                         @Override
66                         public void createControls(Composite body, IWorkbenchSite site) {
67
68                                 try {
69
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() {
74                                                 
75                                                 @Override
76                                                 public void handleEvent(Event event) {
77                                                         if(!loader.isDisposed())
78                                                                 loader.dispose();
79                                                 }
80                                                 
81                                         });
82
83                                 } catch (DatabaseException e) {
84
85                                         e.printStackTrace();
86                                         Logger.defaultLogError(e);
87
88                                 }
89
90                         }
91
92                 }, priority, variable, label));
93
94         }
95
96 }