]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.views/src/org/simantics/views/ViewUtils.java
c0a6072875046719f6b7b79864ad3df3272f41c6
[simantics/platform.git] / bundles / org.simantics.views / src / org / simantics / views / ViewUtils.java
1 package org.simantics.views;
2
3 import org.eclipse.jface.viewers.ISelection;
4 import org.eclipse.jface.viewers.ISelectionProvider;
5 import org.eclipse.ui.PlatformUI;
6 import org.simantics.Simantics;
7 import org.simantics.databoard.Bindings;
8 import org.simantics.databoard.binding.Binding;
9 import org.simantics.databoard.binding.error.BindingException;
10 import org.simantics.databoard.util.Bean;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.RequestProcessor;
13 import org.simantics.db.Resource;
14 import org.simantics.db.WriteGraph;
15 import org.simantics.db.common.request.ResourceRead;
16 import org.simantics.db.common.request.TernaryRead;
17 import org.simantics.db.common.request.WriteRequest;
18 import org.simantics.db.common.utils.Functions;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.layer0.variable.Variable;
21 import org.simantics.db.procedure.Listener;
22 import org.simantics.scl.runtime.function.Function1;
23 import org.simantics.scl.runtime.function.Function2;
24 import org.simantics.views.ontology.ViewsResources;
25
26 public class ViewUtils {
27
28         public static class ExtendedMargins {
29                 
30         public int left, right, top, bottom;
31                 
32     }
33
34     public static final Binding EXTENDED_MARGINS_BINDING = Bindings.getBindingUnchecked(ExtendedMargins.class);
35
36         public static class LayoutBean extends Bean {
37                 public int marginLeft;
38                 public int marginRight;
39                 public int marginTop;
40                 public int marginBottom;
41         }
42
43         public static class GridLayoutBean extends LayoutBean {
44                 
45                 public int numColumns;
46                 public int horizontalSpacing;
47                 public int verticalSpacing;
48                 
49         }
50
51         public static class RowLayoutBean extends LayoutBean {
52                 public int type;
53                 public int spacing;
54                 public boolean center;
55                 public boolean fill;
56                 public boolean justify;
57                 public boolean pack;
58                 public boolean wrap;
59         }
60
61         public static class LayoutDataBean extends Bean {}
62
63         public static class GridDataBean extends LayoutDataBean {
64                 
65                 public int horizontalSpan;
66                 public boolean grabExcessHorizontalSpace;
67                 public boolean grabExcessVerticalSpace;
68                 public int horizontalAlignment;
69                 public int verticalAlignment;
70                 public int widthHint;
71                 public int heightHint;
72                 
73         }
74
75         public static class RowDataBean extends LayoutDataBean {
76                 public int width;
77                 public int height;
78         }
79
80         public static class ColumnBean extends Bean {
81                 
82                 public String key;
83                 public String label;
84                 public String alignment;
85                 public int width;
86                 public String tooltip;
87                 public boolean grab;
88                 public int weight;
89                 
90         }
91         
92
93         
94         public static GridLayoutBean getLayout(RequestProcessor processor, Resource configuration) throws DatabaseException {
95
96                 return processor.sync(new ResourceRead<GridLayoutBean>(configuration) {
97
98                         @Override
99                         public GridLayoutBean perform(ReadGraph graph) throws DatabaseException {
100
101                                 ViewsResources VIEW = ViewsResources.getInstance(graph);
102
103                                 Integer columns = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_columnCount, Bindings.INTEGER);
104                                 Integer horizontalSpacing = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_horizontalSpacing, Bindings.INTEGER);
105                                 Integer verticalSpacing = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_verticalSpacing, Bindings.INTEGER);
106                                 ExtendedMargins extendedMargins = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_extendedMargins, EXTENDED_MARGINS_BINDING);
107                                 
108                                 GridLayoutBean layout = new GridLayoutBean();//.fillDefaults().numColumns(1).equalWidth(false).margins(0, 0).spacing(0, 0).create();
109                                 layout.numColumns = columns;
110                                 layout.horizontalSpacing = horizontalSpacing;
111                                 layout.verticalSpacing = verticalSpacing;
112                                 layout.marginLeft = extendedMargins.left;
113                                 layout.marginRight = extendedMargins.right;
114                                 layout.marginTop = extendedMargins.top;
115                                 layout.marginBottom = extendedMargins.bottom;
116
117                                 return layout;
118
119                         }
120
121                 });
122                 
123         }
124
125         public static GridDataBean getGridData(RequestProcessor processor, Resource configuration) throws DatabaseException {
126         
127                 return processor.sync(new ResourceRead<GridDataBean>(configuration) {
128
129                         @Override
130                         public GridDataBean perform(ReadGraph graph) throws DatabaseException {
131
132                                 ViewsResources VIEW = ViewsResources.getInstance(graph);
133                                 GridDataBean data = new GridDataBean();
134                                 
135                                 data.horizontalSpan = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_horizontalSpan, Bindings.INTEGER);
136                                 data.grabExcessHorizontalSpace = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_horizontalGrab, Bindings.BOOLEAN);
137                                 data.grabExcessVerticalSpace = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_verticalGrab, Bindings.BOOLEAN);
138                                 data.horizontalAlignment = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_horizontalAlignment, Bindings.INTEGER);
139                                 data.verticalAlignment = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_verticalAlignment, Bindings.INTEGER);
140                                 data.widthHint = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_preferredWidth, Bindings.INTEGER);
141                                 data.heightHint = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_preferredHeight, Bindings.INTEGER);
142
143                                 return data;
144                                 
145                         }
146
147                 });
148     
149         }
150
151         public static RowLayoutBean getRowLayout(RequestProcessor processor, Resource configuration) throws DatabaseException {
152             return processor.sync(new ResourceRead<RowLayoutBean>(configuration) {
153                 @Override
154                 public RowLayoutBean perform(ReadGraph graph) throws DatabaseException {
155                     ViewsResources VIEW = ViewsResources.getInstance(graph);
156                     Integer type = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_type, Bindings.INTEGER);
157                     Integer spacing = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_spacing, Bindings.INTEGER);
158                     Boolean center = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_center, Bindings.BOOLEAN);
159                 Boolean fill = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_fill, Bindings.BOOLEAN);
160                 Boolean justify = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_justify, Bindings.BOOLEAN);
161                     Boolean pack = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_pack, Bindings.BOOLEAN);
162                     Boolean wrap = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_wrap, Bindings.BOOLEAN);
163                     ExtendedMargins extendedMargins = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_extendedMargins, EXTENDED_MARGINS_BINDING);
164
165                     RowLayoutBean layout = new RowLayoutBean();
166                     layout.type = type;
167                     layout.spacing = spacing;
168                 layout.center = center;
169                 layout.fill = fill;
170                 layout.justify = justify;
171                     layout.pack = pack;
172                     layout.wrap = wrap;
173                     layout.marginLeft = extendedMargins.left;
174                     layout.marginRight = extendedMargins.right;
175                     layout.marginTop = extendedMargins.top;
176                     layout.marginBottom = extendedMargins.bottom;
177                     return layout;
178                 }
179             });
180         }
181
182         public static RowDataBean getRowData(RequestProcessor processor, Resource configuration) throws DatabaseException {
183             return processor.sync(new ResourceRead<RowDataBean>(configuration) {
184                 @Override
185                 public RowDataBean perform(ReadGraph graph) throws DatabaseException {
186                     ViewsResources VIEW = ViewsResources.getInstance(graph);
187                     RowDataBean data = new RowDataBean();
188                     data.width = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_preferredWidth, Bindings.INTEGER);
189                     data.height = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_preferredHeight, Bindings.INTEGER);
190                     return data;
191                 }
192             });
193         }
194
195         public static int getStyle(RequestProcessor processor, Resource configuration) throws DatabaseException {
196                 
197                 return processor.sync(new ResourceRead<Integer>(configuration) {
198
199                         @Override
200                         public Integer perform(ReadGraph graph) throws DatabaseException {
201
202                                 ViewsResources VIEW = ViewsResources.getInstance(graph);
203                                 
204                                 int result = 0;
205                                 for(Resource constant : graph.getObjects(resource, VIEW.Control_Style_HasConstant)) {
206                                         int value = graph.getValue(constant, Bindings.INTEGER);
207                                         result |= value;
208                                 }
209                                 
210                                 return result;
211                                 
212                         }
213
214                 });
215     
216         }
217
218         public static ColumnBean getColumn(RequestProcessor processor, Resource configuration) throws DatabaseException {
219                 
220                 return processor.sync(new ResourceRead<ColumnBean>(configuration) {
221
222                         @Override
223                         public ColumnBean perform(ReadGraph graph) throws DatabaseException {
224
225                                 ViewsResources VIEW = ViewsResources.getInstance(graph);
226                                 String key = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasKey, Bindings.STRING);
227                                 String label = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasLabel, Bindings.STRING);
228                                 String alignment = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasAlignment, Bindings.STRING);
229                                 Integer width = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasWidth, Bindings.INTEGER);
230                                 String tooltip = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasTooltip, Bindings.STRING);
231                                 Boolean grab = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasGrab, Bindings.BOOLEAN);
232                                 Integer weight = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasWeight, Bindings.INTEGER);
233                                 
234                                 ColumnBean bean = new ColumnBean();
235                                 bean.key = key;
236                                 bean.label = label;
237                                 bean.alignment = alignment;
238                                 bean.width = width;
239                                 bean.tooltip = tooltip;
240                                 bean.grab = grab;
241                                 bean.weight = weight;
242                                 
243                                 return bean;
244                                 
245                         }
246                         
247                 });
248     
249         }
250         
251         public static <T> void listen(Resource configuration, Variable context, String relationURI, final Binding binding, final Function1<T, Boolean> function) throws DatabaseException {
252
253                 Simantics.getSession().async(new TernaryRead<Resource, Variable, String, T> (configuration, context, relationURI) {
254
255                         @SuppressWarnings("unchecked")
256                         @Override
257                         public T perform(ReadGraph graph) throws DatabaseException {
258                                 Object value = graph.getRelatedValue2(parameter, graph.getResource(parameter3), parameter2);
259                                 Object result = binding.createDefaultUnchecked();
260                                 try {
261                                         binding.readFrom(Bindings.getBinding(binding.type()), value, result);
262                                 } catch (BindingException e) {
263 //                                      e.printStackTrace();
264                                         throw new DatabaseException(e);
265                                 }
266                                 return (T)result;
267                         }
268                         
269                 }, new Listener<T>() {
270
271                         private boolean disposed = false;
272                         
273             @Override
274             public void exception(Throwable t) {
275 //              t.printStackTrace();
276             }
277
278             @Override
279             public void execute(T result) {
280                 disposed = function.apply(result);
281             }
282
283             @Override
284             public boolean isDisposed() {
285                 return disposed;
286             }
287
288         });
289     
290         }
291         
292         public static <T> void listen(Resource configuration, Variable context, String relationURI, final Function1<T, Boolean> function) throws DatabaseException {
293
294                 Simantics.getSession().async(new TernaryRead<Resource, Variable, String, T> (configuration, context, relationURI) {
295
296                         @Override
297                         public T perform(ReadGraph graph) throws DatabaseException {
298                         return graph.getRelatedValue2(parameter, graph.getResource(parameter3), parameter2);
299                         }
300                         
301                 }, new Listener<T>() {
302
303                         private boolean disposed = false;
304                         
305             @Override
306             public void exception(Throwable t) {
307 //              t.printStackTrace();
308             }
309
310             @Override
311             public void execute(T result) {
312                 disposed = function.apply(result);
313             }
314
315             @Override
316             public boolean isDisposed() {
317                 return disposed;
318             }
319
320         });
321     
322         }
323         
324         public static Function2<Object, Object, Object> getActionFunctionDeprecated(final Resource configuration, final Resource runtime, final String relationURI) throws DatabaseException {
325                 
326                 return new Function2<Object, Object, Object>() {
327                         
328                         @Override
329                         public Object apply(final Object selection, final Object event) {
330                                 
331                                 Simantics.getSession().async(new WriteRequest() {
332
333                                         @Override
334                                         public void perform(WriteGraph graph) throws DatabaseException {
335
336                                         Resource relation = graph.getResource(relationURI);
337                                         
338                                         final Resource function = graph.getSingleObject(configuration, relation);
339                                                 
340                                                 Functions.exec(graph, function, graph, runtime, selection, event);
341                                                 
342                                         }
343                                         
344                                 });
345                                 
346                                 return null;
347                                 
348                         }
349                         
350                 };
351     
352         }
353         
354         public static void setWorkbenchSelection(ISelection selection) {
355                 ISelectionProvider provider = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart().getSite().getSelectionProvider();
356         provider.setSelection(selection);
357         }
358         
359 }