]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.views/src/org/simantics/views/ViewUtils.java
Prevent NPEs in ViewUtils layout loading code
[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         private static void readMargins(ExtendedMargins extendedMargins, LayoutBean layout) {
93                 if (extendedMargins != null) {
94                         layout.marginLeft = extendedMargins.left;
95                         layout.marginRight = extendedMargins.right;
96                         layout.marginTop = extendedMargins.top;
97                         layout.marginBottom = extendedMargins.bottom;
98                 } else {
99                         layout.marginLeft = 3; 
100                         layout.marginRight = 3;
101                         layout.marginTop = 3;
102                         layout.marginBottom = 3;
103                 }
104         }
105
106         public static GridLayoutBean getLayout(RequestProcessor processor, Resource configuration) throws DatabaseException {
107
108                 return processor.sync(new ResourceRead<GridLayoutBean>(configuration) {
109
110                         @Override
111                         public GridLayoutBean perform(ReadGraph graph) throws DatabaseException {
112
113                                 ViewsResources VIEW = ViewsResources.getInstance(graph);
114
115                                 Integer columns = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_columnCount, Bindings.INTEGER);
116                                 Integer horizontalSpacing = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_horizontalSpacing, Bindings.INTEGER);
117                                 Integer verticalSpacing = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_verticalSpacing, Bindings.INTEGER);
118                                 ExtendedMargins extendedMargins = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_extendedMargins, EXTENDED_MARGINS_BINDING);
119                                 
120                                 GridLayoutBean layout = new GridLayoutBean();//.fillDefaults().numColumns(1).equalWidth(false).margins(0, 0).spacing(0, 0).create();
121                                 layout.numColumns = columns != null ? columns : 1;
122                                 layout.horizontalSpacing = horizontalSpacing != null ? horizontalSpacing : 5;
123                                 layout.verticalSpacing = verticalSpacing != null ? verticalSpacing : 5;
124                                 readMargins(extendedMargins, layout);
125
126                                 return layout;
127
128                         }
129
130                 });
131                 
132         }
133
134         public static GridDataBean getGridData(RequestProcessor processor, Resource configuration) throws DatabaseException {
135         
136                 return processor.sync(new ResourceRead<GridDataBean>(configuration) {
137
138                         @Override
139                         public GridDataBean perform(ReadGraph graph) throws DatabaseException {
140
141                                 ViewsResources VIEW = ViewsResources.getInstance(graph);
142                                 GridDataBean data = new GridDataBean();
143                                 
144                                 data.horizontalSpan = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_horizontalSpan, Bindings.INTEGER);
145                                 data.grabExcessHorizontalSpace = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_horizontalGrab, Bindings.BOOLEAN);
146                                 data.grabExcessVerticalSpace = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_verticalGrab, Bindings.BOOLEAN);
147                                 data.horizontalAlignment = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_horizontalAlignment, Bindings.INTEGER);
148                                 data.verticalAlignment = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_verticalAlignment, Bindings.INTEGER);
149                                 data.widthHint = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_preferredWidth, Bindings.INTEGER);
150                                 data.heightHint = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_preferredHeight, Bindings.INTEGER);
151
152                                 return data;
153                                 
154                         }
155
156                 });
157     
158         }
159
160         public static RowLayoutBean getRowLayout(RequestProcessor processor, Resource configuration) throws DatabaseException {
161             return processor.sync(new ResourceRead<RowLayoutBean>(configuration) {
162                 @Override
163                 public RowLayoutBean perform(ReadGraph graph) throws DatabaseException {
164                     ViewsResources VIEW = ViewsResources.getInstance(graph);
165                     Integer type = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_type, Bindings.INTEGER);
166                     Integer spacing = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_spacing, Bindings.INTEGER);
167                     Boolean center = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_center, Bindings.BOOLEAN);
168                 Boolean fill = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_fill, Bindings.BOOLEAN);
169                 Boolean justify = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_justify, Bindings.BOOLEAN);
170                     Boolean pack = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_pack, Bindings.BOOLEAN);
171                     Boolean wrap = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_wrap, Bindings.BOOLEAN);
172                     ExtendedMargins extendedMargins = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_extendedMargins, EXTENDED_MARGINS_BINDING);
173
174                     RowLayoutBean layout = new RowLayoutBean();
175                     layout.type = type != null ? type : 256;
176                     layout.spacing = spacing != null ? spacing : 3;
177                 layout.center = center != null ? center : false;
178                 layout.fill = fill != null ? fill : false;
179                 layout.justify = justify != null ? justify : false;
180                     layout.pack = pack != null ? pack : true;
181                     layout.wrap = wrap != null ? wrap : true;
182                     readMargins(extendedMargins, layout);
183                     return layout;
184                 }
185             });
186         }
187
188         public static RowDataBean getRowData(RequestProcessor processor, Resource configuration) throws DatabaseException {
189             return processor.sync(new ResourceRead<RowDataBean>(configuration) {
190                 @Override
191                 public RowDataBean perform(ReadGraph graph) throws DatabaseException {
192                     ViewsResources VIEW = ViewsResources.getInstance(graph);
193                     RowDataBean data = new RowDataBean();
194                     Integer width = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_preferredWidth, Bindings.INTEGER);
195                     Integer height = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_preferredHeight, Bindings.INTEGER);
196                     data.width = width != null ? width : -1;
197                     data.height = height != null ? height : -1;
198                     return data;
199                 }
200             });
201         }
202
203         public static int getStyle(RequestProcessor processor, Resource configuration) throws DatabaseException {
204                 
205                 return processor.sync(new ResourceRead<Integer>(configuration) {
206
207                         @Override
208                         public Integer perform(ReadGraph graph) throws DatabaseException {
209
210                                 ViewsResources VIEW = ViewsResources.getInstance(graph);
211                                 
212                                 int result = 0;
213                                 for(Resource constant : graph.getObjects(resource, VIEW.Control_Style_HasConstant)) {
214                                         int value = graph.getValue(constant, Bindings.INTEGER);
215                                         result |= value;
216                                 }
217                                 
218                                 return result;
219                                 
220                         }
221
222                 });
223     
224         }
225
226         public static ColumnBean getColumn(RequestProcessor processor, Resource configuration) throws DatabaseException {
227                 
228                 return processor.sync(new ResourceRead<ColumnBean>(configuration) {
229
230                         @Override
231                         public ColumnBean perform(ReadGraph graph) throws DatabaseException {
232
233                                 ViewsResources VIEW = ViewsResources.getInstance(graph);
234                                 String key = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasKey, Bindings.STRING);
235                                 String label = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasLabel, Bindings.STRING);
236                                 String alignment = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasAlignment, Bindings.STRING);
237                                 Integer width = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasWidth, Bindings.INTEGER);
238                                 String tooltip = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasTooltip, Bindings.STRING);
239                                 Boolean grab = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasGrab, Bindings.BOOLEAN);
240                                 Integer weight = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasWeight, Bindings.INTEGER);
241                                 
242                                 ColumnBean bean = new ColumnBean();
243                                 bean.key = key;
244                                 bean.label = label;
245                                 bean.alignment = alignment;
246                                 bean.width = width != null ? width : -1;
247                                 bean.tooltip = tooltip;
248                                 bean.grab = grab != null ? grab : false;
249                                 bean.weight = weight != null ? weight : 0;
250                                 
251                                 return bean;
252                                 
253                         }
254                         
255                 });
256     
257         }
258         
259         public static <T> void listen(Resource configuration, Variable context, String relationURI, final Binding binding, final Function1<T, Boolean> function) throws DatabaseException {
260
261                 Simantics.getSession().async(new TernaryRead<Resource, Variable, String, T> (configuration, context, relationURI) {
262
263                         @SuppressWarnings("unchecked")
264                         @Override
265                         public T perform(ReadGraph graph) throws DatabaseException {
266                                 Object value = graph.getRelatedValue2(parameter, graph.getResource(parameter3), parameter2);
267                                 Object result = binding.createDefaultUnchecked();
268                                 try {
269                                         binding.readFrom(Bindings.getBinding(binding.type()), value, result);
270                                 } catch (BindingException e) {
271 //                                      e.printStackTrace();
272                                         throw new DatabaseException(e);
273                                 }
274                                 return (T)result;
275                         }
276                         
277                 }, new Listener<T>() {
278
279                         private boolean disposed = false;
280                         
281             @Override
282             public void exception(Throwable t) {
283 //              t.printStackTrace();
284             }
285
286             @Override
287             public void execute(T result) {
288                 disposed = function.apply(result);
289             }
290
291             @Override
292             public boolean isDisposed() {
293                 return disposed;
294             }
295
296         });
297     
298         }
299         
300         public static <T> void listen(Resource configuration, Variable context, String relationURI, final Function1<T, Boolean> function) throws DatabaseException {
301
302                 Simantics.getSession().async(new TernaryRead<Resource, Variable, String, T> (configuration, context, relationURI) {
303
304                         @Override
305                         public T perform(ReadGraph graph) throws DatabaseException {
306                         return graph.getRelatedValue2(parameter, graph.getResource(parameter3), parameter2);
307                         }
308                         
309                 }, new Listener<T>() {
310
311                         private boolean disposed = false;
312                         
313             @Override
314             public void exception(Throwable t) {
315 //              t.printStackTrace();
316             }
317
318             @Override
319             public void execute(T result) {
320                 disposed = function.apply(result);
321             }
322
323             @Override
324             public boolean isDisposed() {
325                 return disposed;
326             }
327
328         });
329     
330         }
331         
332         public static Function2<Object, Object, Object> getActionFunctionDeprecated(final Resource configuration, final Resource runtime, final String relationURI) throws DatabaseException {
333                 
334                 return new Function2<Object, Object, Object>() {
335                         
336                         @Override
337                         public Object apply(final Object selection, final Object event) {
338                                 
339                                 Simantics.getSession().async(new WriteRequest() {
340
341                                         @Override
342                                         public void perform(WriteGraph graph) throws DatabaseException {
343
344                                         Resource relation = graph.getResource(relationURI);
345                                         
346                                         final Resource function = graph.getSingleObject(configuration, relation);
347                                                 
348                                                 Functions.exec(graph, function, graph, runtime, selection, event);
349                                                 
350                                         }
351                                         
352                                 });
353                                 
354                                 return null;
355                                 
356                         }
357                         
358                 };
359     
360         }
361         
362         public static void setWorkbenchSelection(ISelection selection) {
363                 ISelectionProvider provider = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart().getSite().getSelectionProvider();
364         provider.setSelection(selection);
365         }
366         
367 }