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