]> gerrit.simantics Code Review - simantics/platform.git/blob
68d6cdae48e66f1b0657ee605a022014cc573e89
[simantics/platform.git] /
1 package org.simantics.selectionview;
2
3 import java.util.Collection;
4 import java.util.Collections;
5
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;
23
24 public class TypedVariableTabContribution implements TabContribution<Object> {
25
26         final private Resource configuration;
27         
28         public TypedVariableTabContribution(ReadGraph graph, Resource configuration) throws DatabaseException {
29                 this.configuration = configuration;
30         }
31         
32         static class Contribution extends ModelledTabContributor {
33
34                 private String viewURI;
35                 
36                 public Contribution(String viewURI) {
37                         this.viewURI = viewURI;
38                 }
39                 
40                 @Override
41                 public void createControls(Composite body, IWorkbenchSite site) {
42
43                         try {
44
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() {
49                                         
50                                         @Override
51                                         public void handleEvent(Event event) {
52                                                 if(!loader.isDisposed())
53                                                         loader.dispose();
54                                         }
55                                         
56                                 });
57                                 
58                         } catch (DatabaseException e) {
59
60                                 e.printStackTrace();
61                                 Logger.defaultLogError(e);
62
63                         }
64
65                 }
66                 
67             @Override
68             public boolean equals(Object obj) {
69                 if (this == obj)
70                     return true;
71                 else if (obj == null)
72                     return false;
73                 else if (!(obj instanceof Contribution))
74                     return false;
75                 Contribution c = (Contribution)obj;
76                 return viewURI.equals(c.viewURI);
77             }
78             
79             @Override
80             public int hashCode() {
81                 return viewURI.hashCode();
82             }
83                 
84         }
85         
86
87         @Override
88         public boolean accept(ReadGraph graph, Object input) {
89                 return true;
90 //              return (input instanceof Variable);
91         }
92         
93         private Variable getPossibleVariable(ReadGraph graph, Object input) throws DatabaseException {
94
95                 if(input instanceof Variable) {
96                         return (Variable)input;
97                 } else {
98                         return WorkbenchSelectionUtils.getPossibleVariable(graph, input);
99                 }
100                 
101         }
102         
103         @Override
104         public void contribute(ReadGraph graph, final Object object, Collection<ComparableTabContributor> result) throws DatabaseException {
105                 
106                 Variable variable = getPossibleVariable(graph, object);
107                 if(variable == null) return;
108                 
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);
114                 if(view == null) {
115                         throw new DatabaseException("No view for " + graph.getPossibleURI(configuration));
116                 }
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);
120
121         Resource resource = variable.getPossibleRepresents(graph);
122         if (resource == null)
123             return;
124
125         Collection<Resource> ts = graph.getTypes(resource);
126         
127         if (!Collections.disjoint(types, ts)) {
128
129                 for(Resource r : graph.getObjects(configuration, SEL.VariableTabContribution_HasTest)) {
130                         Boolean pass = Functions.exec(graph, r, graph, variable);
131                         if(!pass) return;
132                 }
133                 
134                 try {
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
139                 }
140                 
141                 result.add(new ComparableTabContributor(new Contribution(viewURI), priority, variable, label));
142
143         }
144
145         }
146
147 }