]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/forms/DataboardDialog.java
Improve Databoard's dynamically typed data capabilities.
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / forms / DataboardDialog.java
1 package org.simantics.databoard.forms;\r
2 \r
3 import org.eclipse.jface.dialogs.Dialog;\r
4 import org.eclipse.jface.dialogs.IDialogConstants;\r
5 import org.eclipse.swt.SWT;\r
6 import org.eclipse.swt.custom.ScrolledComposite;\r
7 import org.eclipse.swt.layout.GridData;\r
8 import org.eclipse.swt.layout.GridLayout;\r
9 import org.eclipse.swt.widgets.Composite;\r
10 import org.eclipse.swt.widgets.Control;\r
11 import org.eclipse.swt.widgets.Shell;\r
12 import org.simantics.databoard.accessor.error.AccessorConstructionException;\r
13 import org.simantics.databoard.accessor.error.AccessorException;\r
14 import org.simantics.databoard.binding.RecordBinding;\r
15 import org.simantics.databoard.binding.error.BindingException;\r
16 \r
17 public class DataboardDialog extends Dialog {\r
18         \r
19         RecordBinding formBinding;\r
20         Object initialValues;\r
21         String title;\r
22         Object result;\r
23     DataboardForm form;\r
24     Composite composite;\r
25     \r
26     /**\r
27      * Create dialog window\r
28      * \r
29      * @param parentShell\r
30      * @param title\r
31      * @param formBinding\r
32      * @param initialValues values according to formBinding or null\r
33      */\r
34         public DataboardDialog(Shell parentShell, String title, RecordBinding formBinding, Object initialValues) {              \r
35                 super(parentShell);\r
36                 this.formBinding = formBinding;\r
37                 this.initialValues = initialValues;\r
38                 this.title = title;\r
39                 \r
40                 setBlockOnOpen(true);\r
41                 setShellStyle(SWT.RESIZE | SWT.TITLE | SWT.CLOSE | SWT.BORDER);\r
42         }\r
43 \r
44         @Override\r
45         protected void createButtonsForButtonBar(Composite parent) {\r
46                 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);\r
47                 createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);\r
48         }\r
49 \r
50         @Override\r
51         protected Control createDialogArea(Composite parent) {\r
52             ScrolledComposite scroll = new ScrolledComposite(parent, SWT.V_SCROLL);\r
53             composite = new Composite(scroll, 0);\r
54         scroll.setContent(composite);\r
55         scroll.setExpandHorizontal(true);\r
56         scroll.setExpandVertical(false);\r
57                 form = new DataboardForm();\r
58             form.setFirstColumnWidth(0);\r
59             form.addFields(composite, formBinding.type());\r
60             \r
61                 composite.setLayout( new GridLayout(3, false) );                        \r
62                 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 3, 1));\r
63                 \r
64                 if ( initialValues != null ) {\r
65                         try {\r
66                                 form.writeFields(composite, formBinding, initialValues);\r
67                         } catch (AccessorException e) {\r
68                                 e.printStackTrace();\r
69                         } catch (BindingException e) {\r
70                                 e.printStackTrace();\r
71                         } catch (AccessorConstructionException e) {\r
72                                 e.printStackTrace();\r
73                         }                       \r
74                 }\r
75                 \r
76             composite.pack();\r
77                 \r
78                 return scroll;\r
79         }\r
80 \r
81     protected void configureShell(Shell newShell) {\r
82         super.configureShell(newShell);\r
83         newShell.setText(title);\r
84     }\r
85     \r
86         @Override\r
87         protected void okPressed() {\r
88                 try {\r
89                         result = formBinding.createDefault();\r
90                         form.readFields(composite, formBinding, result);\r
91                 } catch (BindingException e) {\r
92                         e.printStackTrace();\r
93                         result = null;\r
94                 } catch (AccessorConstructionException e) {\r
95                         e.printStackTrace();\r
96                         result = null;\r
97                 } catch (AccessorException e) {\r
98                         e.printStackTrace();\r
99                         result = null;\r
100                 }\r
101                 super.okPressed();\r
102         }\r
103         \r
104         @Override\r
105         protected void cancelPressed() {\r
106                 result = null;\r
107                 super.cancelPressed();\r
108         }\r
109 \r
110         public Object getResult() {\r
111                 return result;\r
112         }\r
113         \r
114 }