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