]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/examples/org/simantics/databoard/example/DataboardFormDialogExample.java
Fixed ComponentTypeCommands.setUnit to support unit == null
[simantics/platform.git] / bundles / org.simantics.databoard / examples / org / simantics / databoard / example / DataboardFormDialogExample.java
1 package org.simantics.databoard.example;
2
3 import java.util.List;
4 import java.util.Random;
5
6 import org.eclipse.jface.window.Window;
7 import org.eclipse.swt.SWT;
8 import org.eclipse.swt.custom.ScrolledComposite;
9 import org.eclipse.swt.events.SelectionAdapter;
10 import org.eclipse.swt.events.SelectionEvent;
11 import org.eclipse.swt.layout.FillLayout;
12 import org.eclipse.swt.layout.GridData;
13 import org.eclipse.swt.layout.GridLayout;
14 import org.eclipse.swt.widgets.Button;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.Display;
17 import org.eclipse.swt.widgets.Event;
18 import org.eclipse.swt.widgets.Listener;
19 import org.eclipse.swt.widgets.MessageBox;
20 import org.eclipse.swt.widgets.Shell;
21 import org.simantics.databoard.Bindings;
22 import org.simantics.databoard.Datatypes;
23 import org.simantics.databoard.accessor.error.AccessorConstructionException;
24 import org.simantics.databoard.accessor.error.AccessorException;
25 import org.simantics.databoard.binding.RecordBinding;
26 import org.simantics.databoard.binding.error.BindingException;
27 import org.simantics.databoard.forms.DataboardDialog;
28 import org.simantics.databoard.forms.DataboardForm;
29 import org.simantics.databoard.forms.DataboardForm.Problem;
30 import org.simantics.databoard.type.Datatype;
31 import org.simantics.databoard.type.DoubleType;
32 import org.simantics.databoard.type.RecordType;
33 import org.simantics.databoard.type.StringType;
34 import org.simantics.databoard.type.UnionType;
35
36
37 public class DataboardFormDialogExample {
38
39         public static void main(String [] args) throws BindingException, AccessorConstructionException, AccessorException {
40             final Display display = new Display ();
41             final Shell shell = new Shell (display);
42             shell.setLayout(new FillLayout());
43
44             UnionType experiments = UnionType.newEnum("Result 1 - 5.11.2011 12:50",     "Result 2 - 5.11.2011 13:40", "Result 3 - 5.11.2011 15:40" ); 
45             //UnionType mergeType = UnionType.newEnum("One file", "Many files");
46             
47             
48             //mergeType = UnionType.newEnum("Merge to one file", "Separate files");
49
50             Datatype second = new DoubleType("s");
51             
52             // Diagram layers
53             RecordType diagramOptions = new RecordType();
54             
55             RecordType layers = new RecordType();
56             layers.metadata.put("style", "Dialog");
57             layers.addComponent("Layer 1", Datatypes.BOOLEAN);
58             layers.addComponent("Layer 2", Datatypes.BOOLEAN);
59             layers.addComponent("Layer 3", Datatypes.BOOLEAN);
60             diagramOptions.addComponent("Layers", layers);
61             
62             // Dialog
63             {
64                 RecordBinding b = Bindings.getMutableBinding(layers);
65                 Object o = b.createDefault();
66                     DataboardDialog dialog = new DataboardDialog(
67                                 display.getActiveShell(),
68                                 "Layer Selection",
69                                 b, 
70                                 o);
71                     int code = dialog.open();
72                     if ( code == Window.OK ) {
73                         Object result = dialog.getResult();
74                         System.out.println( b.toString(result, true) );
75                     }
76             }
77             
78             // Diagram PDF Export Format options
79             diagramOptions.addComponent("Fit to content", Datatypes.BOOLEAN);
80
81             // Experiment options
82             RecordType experimentOptions = new RecordType();        
83             experimentOptions.addComponent("Start Time", second);           
84             experimentOptions.addComponent("End Time", second);     
85             experimentOptions.addComponent("Experiment", experiments);      
86             
87             // CSV Export
88             RecordType csvOptions = new RecordType();       
89             csvOptions.addComponent("Step Size", second);
90
91             // Chart options
92             RecordType chartOptions = new RecordType();     
93             chartOptions.addComponent("Autoscale", UnionType.newEnum("Stacked", "Overlapping"));
94             chartOptions.addComponent("TimeFormat", UnionType.newEnum("Decimal", "Time"));
95             chartOptions.addComponent("ValueFormat", UnionType.newEnum("Currency", "Scientific", "Engineering", "Default"));
96             
97             // PDF options
98             RecordType pdfOptions = new RecordType();
99             // PDF metadata         
100             pdfOptions.addComponent("Title", Datatypes.STRING );
101             pdfOptions.addComponent("Author", Datatypes.STRING );
102             pdfOptions.addComponent("Subject", Datatypes.STRING );
103             pdfOptions.addComponent("Keywords", DataboardForm.TEXTBOX );            
104             // PDF Encryption
105             StringType privatekeys = DataboardForm.fileOpenDialog("PKCS#12 keystore (.p12)", ".p12", "PFX (.pfx)", ".pfx"); 
106             pdfOptions.addComponent("Private Key", privatekeys);
107             pdfOptions.addComponent("Keystore Password", DataboardForm.PASSWORD);
108             pdfOptions.addComponent("Private Key Password", DataboardForm.PASSWORD);        
109             // Formatter options 
110             pdfOptions.addComponent("Compression", UnionType.newEnum("0 (No compression)", "1", "2", "3", "4", "5", "6", "7", "8", "9 (Best)"));
111
112             // Output format
113             RecordType outputOptions = new RecordType(); 
114             UnionType mergeType = new UnionType();
115             mergeType.metadata.put("style", "no-border");
116             mergeType.addComponent("One file", DataboardForm.fileSaveDialog("Comma Separated Value (.csv)", ".csv"));
117             mergeType.addComponent("Separate files", DataboardForm.directoryDialog());
118             outputOptions.addComponent("Output", mergeType);
119
120             
121             ScrolledComposite scroll = new ScrolledComposite(shell, SWT.V_SCROLL);
122             final Composite composite = new Composite(scroll, 0);
123         scroll.setContent(composite);
124         scroll.setExpandHorizontal(true);
125         scroll.setExpandVertical(false);
126             final DataboardForm form = new DataboardForm();
127             form.setFirstColumnWidth(150);          
128             
129                 composite.setLayout( new GridLayout(3, false) );                        
130                 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 3, 1));                   
131             
132                 form.addField(composite, "Diagram", diagramOptions);
133                 form.addField(composite, "Experiment", experimentOptions);
134                 form.addField(composite, "Comma Separated Value (CSV)", csvOptions);
135                 form.addField(composite, "Chart", chartOptions);
136                 form.addField(composite, "Portable Document Format (PDF)", pdfOptions);
137                 form.addField(composite, "Output Options", outputOptions);
138                 
139                 form.addListener(composite, form.type(), new Listener() {
140                         public void handleEvent(Event event) {
141                                 System.out.println(event);
142                         } } );
143                 
144                 // Button that validates all fields
145                 Button validate = new Button(composite, SWT.DEFAULT);
146                 validate.setText( "Validate" );
147                 validate.addSelectionListener(new SelectionAdapter() {
148                         public void widgetSelected(SelectionEvent e) {
149                                 List<Problem> problems = form.validate(composite);
150                                 StringBuilder sb = new StringBuilder();
151                                 if ( problems.isEmpty() ) {
152                                         sb.append("No problemo!");
153                                 } else {
154                                         for (Problem problem : problems) {
155                                                 sb.append(problem.fieldReference+": "+problem.error);
156                                                 sb.append("\n");
157                                         }
158                                 }
159                                         
160                                 MessageBox dialog = new MessageBox(shell, SWT.ICON_QUESTION | SWT.OK| SWT.CANCEL);
161                                 dialog.setText("Form");
162                                 dialog.setMessage( sb.toString() );
163                                 dialog.open();                                                                          
164                         }
165                 });
166                 
167                 
168                 // Button that reads all fields
169                 Button read = new Button(composite, SWT.DEFAULT);
170                 read.setText( "Read" );
171                 read.addSelectionListener(new SelectionAdapter() {
172                         public void widgetSelected(SelectionEvent e) {
173                             // Read result values
174                                 try {
175                                     RecordBinding binding = (RecordBinding) Bindings.getBinding( form.type() );
176                                     Object formValues = binding.createDefault();
177                                         form.readFields(composite, binding, formValues);
178                                         String txt = binding.toString(formValues, false);
179                                         
180                                         MessageBox dialog = new MessageBox(shell, SWT.ICON_QUESTION | SWT.OK| SWT.CANCEL);
181                                         dialog.setText("Form");
182                                         dialog.setMessage(txt);
183                                         dialog.open();                                  
184                                         
185                                 } catch (AccessorConstructionException e1) {
186                                         e1.printStackTrace();
187                                 } catch (AccessorException e1) {
188                                         e1.printStackTrace();
189                                 } catch (BindingException e1) {
190                                         e1.printStackTrace();
191                                 }
192                         }
193                 });
194
195                 // Button that writes fields
196                 Button write = new Button(composite, SWT.DEFAULT);
197                 write.setText( "Write" );
198                 write.addSelectionListener(new SelectionAdapter() {
199                         public void widgetSelected(SelectionEvent e) {
200                                 try {
201                                         RecordType type = form.type();
202                                     RecordBinding binding = (RecordBinding) Bindings.getBinding( type );                                    
203                                     Object values = binding.createRandom( new Random() );
204                                         form.writeFields(composite, binding, values);
205                                 } catch (AccessorConstructionException e1) {
206                                         e1.printStackTrace();
207                                 } catch (AccessorException e1) {
208                                         e1.printStackTrace();
209                                 } catch (BindingException e1) {
210                                         e1.printStackTrace();
211                                 }
212                                 
213                         }
214                 });
215                 
216                 Button modal = new Button(composite, SWT.DEFAULT);
217                 modal.setText("Modal");
218                 modal.addSelectionListener( new SelectionAdapter() {
219                         @Override
220                         public void widgetSelected(SelectionEvent e) {
221                                 Shell dialogShell = new Shell(display, SWT.DIALOG_TRIM);
222                                 // populate dialogShell
223                                 dialogShell.open();
224                                 while (!dialogShell.isDisposed()) {
225                                     if (!display.readAndDispatch()) {
226                                         display.sleep();
227                                     }
228                                 }
229                         }
230                 });
231                                         
232             composite.pack();
233             
234             shell.setSize(800,400);
235             //shell.layout();
236             shell.open ();
237             while (!shell.isDisposed()) {
238                 if (!display.readAndDispatch ()) display.sleep ();
239             }
240             
241             display.dispose ();
242         }
243                 
244 }