]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/preferences/CSVPreferencePage.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / preferences / CSVPreferencePage.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.modeling.ui.preferences;\r
13 \r
14 import org.eclipse.core.runtime.preferences.InstanceScope;\r
15 import org.eclipse.jface.layout.GridDataFactory;\r
16 import org.eclipse.jface.layout.GridLayoutFactory;\r
17 import org.eclipse.jface.preference.BooleanFieldEditor;\r
18 import org.eclipse.jface.preference.ComboFieldEditor;\r
19 import org.eclipse.jface.preference.FieldEditor;\r
20 import org.eclipse.jface.preference.FieldEditorPreferencePage;\r
21 import org.eclipse.jface.preference.IPreferenceStore;\r
22 import org.eclipse.jface.preference.IntegerFieldEditor;\r
23 import org.eclipse.jface.preference.StringFieldEditor;\r
24 import org.eclipse.jface.util.PropertyChangeEvent;\r
25 import org.eclipse.swt.SWT;\r
26 import org.eclipse.swt.widgets.Composite;\r
27 import org.eclipse.swt.widgets.Group;\r
28 import org.eclipse.ui.IWorkbench;\r
29 import org.eclipse.ui.IWorkbenchPreferencePage;\r
30 import org.eclipse.ui.preferences.ScopedPreferenceStore;\r
31 import org.simantics.history.csv.ColumnSeparator;\r
32 import org.simantics.history.csv.DecimalSeparator;\r
33 import org.simantics.history.csv.ExportInterpolation;\r
34 import org.simantics.modeling.preferences.CSVPreferences;\r
35 \r
36 public class CSVPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {\r
37 \r
38     private ComboFieldEditor fDecimalSeparatorEditor = null;\r
39     private ComboFieldEditor fColumnSeparatorEditor = null;\r
40     private StringFieldEditor fExtensionEditor = null;\r
41     private BooleanFieldEditor fResamplingEditor = null;\r
42     private ComboFieldEditor fSamplingModeEditor = null;\r
43     private StringFieldEditor fStartEditor = null;\r
44     private StringFieldEditor fStepEditor = null;\r
45     private IntegerFieldEditor fTimeDigitsEditor = null;\r
46     private IntegerFieldEditor fFloatDigitsEditor = null;\r
47     private IntegerFieldEditor fDoubleDigitsEditor = null;\r
48 \r
49     private String decimalSeparatorValue;\r
50     private String columnSeparatorValue;\r
51 \r
52     public CSVPreferencePage() {\r
53         super(GRID);\r
54         setDescription("CSV import and export preferences");\r
55         IPreferenceStore pf = new ScopedPreferenceStore(InstanceScope.INSTANCE, CSVPreferences.P_NODE);\r
56         setPreferenceStore( pf );       \r
57     }\r
58 \r
59     @Override\r
60     public void createControl(Composite parent) {\r
61         super.createControl(parent);\r
62     }\r
63 \r
64     @Override\r
65         public void init(IWorkbench workbench) {\r
66         }\r
67 \r
68     @Override\r
69     protected void initialize() {\r
70         super.initialize();\r
71 \r
72         if (decimalSeparatorValue.equals(columnSeparatorValue)) {\r
73             setErrorMessage("Column and decimal separators must not be the same.");\r
74             checkState();\r
75             updateApplyButton();\r
76         }\r
77     }\r
78 \r
79         @Override\r
80         protected void createFieldEditors() {\r
81             decimalSeparatorValue = getPreferenceStore().getString(CSVPreferences.P_CSV_DECIMAL_SEPARATOR);\r
82             columnSeparatorValue = getPreferenceStore().getString(CSVPreferences.P_CSV_COLUMN_SEPARATOR);\r
83 \r
84             String[][] decimalSeparators = new String[DecimalSeparator.values().length][];\r
85             int i = 0;\r
86             for (DecimalSeparator ds : DecimalSeparator.values())\r
87                 decimalSeparators[i++] = new String[] { ds.label, ds.preference };\r
88             String[][] columnSeparators = new String[ColumnSeparator.values().length][];\r
89             i = 0;\r
90             for (ColumnSeparator cs : ColumnSeparator.values())\r
91                 columnSeparators[i++] = new String[] { cs.label, cs.preference };\r
92 \r
93             fDecimalSeparatorEditor = new ComboFieldEditor(CSVPreferences.P_CSV_DECIMAL_SEPARATOR, "Decimal separator", decimalSeparators, getFieldEditorParent()) {\r
94                 @Override\r
95                 public boolean isValid() {\r
96                     return !decimalSeparatorValue.equals(columnSeparatorValue);\r
97                 }\r
98             };\r
99             addField(fDecimalSeparatorEditor);\r
100 \r
101             fColumnSeparatorEditor = new ComboFieldEditor(CSVPreferences.P_CSV_COLUMN_SEPARATOR, "Column separator", columnSeparators, getFieldEditorParent()) {\r
102             @Override\r
103             public boolean isValid() {\r
104                 return !decimalSeparatorValue.equals(columnSeparatorValue);\r
105             }\r
106         };\r
107 \r
108             addField(fColumnSeparatorEditor);\r
109 \r
110             fExtensionEditor = new StringFieldEditor(CSVPreferences.P_CSV_FILE_EXTENSION, "File extension", getFieldEditorParent());\r
111             addField(fExtensionEditor);\r
112             \r
113             fResamplingEditor = new BooleanFieldEditor(CSVPreferences.P_CSV_RESAMPLE, "Resampling", getFieldEditorParent());\r
114             addField(fResamplingEditor);\r
115 \r
116             fSamplingModeEditor = new ComboFieldEditor(CSVPreferences.P_CSV_SAMPLING_MODE, "Sampling mode",\r
117                     new String[][] {\r
118                         {ExportInterpolation.LINEAR_INTERPOLATION.label, ExportInterpolation.LINEAR_INTERPOLATION.preference},\r
119                         {ExportInterpolation.PREVIOUS_SAMPLE.label, ExportInterpolation.PREVIOUS_SAMPLE.preference},\r
120                         }, getFieldEditorParent());\r
121             addField(fSamplingModeEditor);\r
122 \r
123             fStartEditor = new StringFieldEditor(CSVPreferences.P_CSV_START_TIME, "Start time (s)", getFieldEditorParent()) {\r
124                 protected boolean doCheckState() {\r
125                     String text = getTextControl().getText();\r
126                     if (text == null || text.isEmpty()) {\r
127                         return true;\r
128                     }\r
129                     try {\r
130                         double number = Double.parseDouble(text);\r
131                         return number>0;\r
132                     } catch (NumberFormatException e1) {\r
133                         return false;\r
134                     }\r
135                 };\r
136             };\r
137             fStartEditor.setEmptyStringAllowed(true);\r
138             fStartEditor.setErrorMessage("Enter valid start time value (s)");\r
139 \r
140             fStepEditor = new StringFieldEditor(CSVPreferences.P_CSV_TIME_STEP, "Step size (s)", getFieldEditorParent()) {\r
141                 protected boolean doCheckState() {\r
142                     String text = getTextControl().getText();\r
143                     if (text == null || text.isEmpty()) {\r
144                         return true;\r
145                     }\r
146                     try {\r
147                         double number = Double.parseDouble(text);\r
148                         return number>0;\r
149                     } catch (NumberFormatException e1) {\r
150                         return false;\r
151                     }\r
152                 };\r
153             };\r
154             fStepEditor.setEmptyStringAllowed(true);\r
155             fStepEditor.setErrorMessage("Enter valid time step value (s)");\r
156             addField(fStepEditor);\r
157 \r
158             Group significantDigitsGroup = new Group(getFieldEditorParent(), SWT.NONE);\r
159             significantDigitsGroup.setText("Significant digits");\r
160             significantDigitsGroup.setToolTipText("Control the Amount of Significant Digits in Exported Numbers");\r
161             GridDataFactory.fillDefaults().indent(0, 5).span(2, 1).applyTo(significantDigitsGroup);\r
162 \r
163             fTimeDigitsEditor = new IntegerFieldEditor(CSVPreferences.P_CSV_TIME_DIGITS, "Time stamps", significantDigitsGroup, 2);\r
164             fTimeDigitsEditor.setValidRange(1, 15);\r
165             addField(fTimeDigitsEditor);\r
166             fFloatDigitsEditor = new IntegerFieldEditor(CSVPreferences.P_CSV_FLOAT_DIGITS, "Single precision floating point", significantDigitsGroup, 1);\r
167             fFloatDigitsEditor.setValidRange(1, 7);\r
168             addField(fFloatDigitsEditor);\r
169             fDoubleDigitsEditor = new IntegerFieldEditor(CSVPreferences.P_CSV_DOUBLE_DIGITS, "Double precision floating point", significantDigitsGroup, 2);\r
170             fDoubleDigitsEditor.setValidRange(1, 15);\r
171             addField(fDoubleDigitsEditor);\r
172 \r
173             GridLayoutFactory.swtDefaults().numColumns(2).applyTo(significantDigitsGroup);\r
174         }\r
175 \r
176     /**\r
177      * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)\r
178      */\r
179     @Override\r
180     public void propertyChange(PropertyChangeEvent event) {\r
181         if (event.getProperty().equals(FieldEditor.VALUE)) {\r
182             Object source = event.getSource();\r
183             boolean validate = false;\r
184             if (source == fDecimalSeparatorEditor) {\r
185                 decimalSeparatorValue = (String) event.getNewValue();\r
186                 validate = true;\r
187             } else if (source == fColumnSeparatorEditor) {\r
188                 columnSeparatorValue = (String) event.getNewValue();\r
189                 validate = true;\r
190             }\r
191             if (validate) {\r
192                 if (decimalSeparatorValue.equals(columnSeparatorValue)) {\r
193                     setErrorMessage("Column and decimal separators must not be the same.");\r
194                 } else {\r
195                     setErrorMessage(null);\r
196                 }\r
197                 checkState();\r
198                 updateApplyButton();\r
199             }\r
200         }\r
201         super.propertyChange(event);\r
202     }\r
203 \r
204 }\r