]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/preferences/CSVPreferencePage.java
Improved Copy Visible Data usability in time series chart editor
[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.graphics.Color;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Group;
29 import org.eclipse.swt.widgets.Label;
30 import org.eclipse.ui.IWorkbench;
31 import org.eclipse.ui.IWorkbenchPreferencePage;
32 import org.eclipse.ui.preferences.ScopedPreferenceStore;
33 import org.simantics.history.csv.ColumnSeparator;
34 import org.simantics.history.csv.DecimalSeparator;
35 import org.simantics.history.csv.ExportInterpolation;
36 import org.simantics.modeling.preferences.CSVPreferences;
37
38 public class CSVPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
39
40     private ComboFieldEditor fDecimalSeparatorEditor = null;
41     private ComboFieldEditor fColumnSeparatorEditor = null;
42     private StringFieldEditor fExtensionEditor = null;
43     private BooleanFieldEditor fResamplingEditor = null;
44     private ComboFieldEditor fSamplingModeEditor = null;
45     private StringFieldEditor fStartEditor = null;
46     private StringFieldEditor fStepEditor = null;
47     private IntegerFieldEditor fTimeDigitsEditor = null;
48     private IntegerFieldEditor fFloatDigitsEditor = null;
49     private IntegerFieldEditor fDoubleDigitsEditor = null;
50
51     private String decimalSeparatorValue;
52     private String columnSeparatorValue;
53
54     public CSVPreferencePage() {
55         super(GRID);
56         setDescription("CSV import and export preferences");
57         IPreferenceStore pf = new ScopedPreferenceStore(InstanceScope.INSTANCE, CSVPreferences.P_NODE);
58         setPreferenceStore( pf );       
59     }
60
61     @Override
62     public void createControl(Composite parent) {
63         super.createControl(parent);
64     }
65
66     @Override
67         public void init(IWorkbench workbench) {
68         }
69
70     @Override
71     protected void initialize() {
72         super.initialize();
73
74         if (decimalSeparatorValue.equals(columnSeparatorValue)) {
75             setErrorMessage("Column and decimal separators must not be the same.");
76             checkState();
77             updateApplyButton();
78         }
79     }
80
81         @Override
82         protected void createFieldEditors() {
83             decimalSeparatorValue = getPreferenceStore().getString(CSVPreferences.P_CSV_DECIMAL_SEPARATOR);
84             columnSeparatorValue = getPreferenceStore().getString(CSVPreferences.P_CSV_COLUMN_SEPARATOR);
85
86             String[][] decimalSeparators = new String[DecimalSeparator.values().length][];
87             int i = 0;
88             for (DecimalSeparator ds : DecimalSeparator.values())
89                 decimalSeparators[i++] = new String[] { ds.label, ds.preference };
90             String[][] columnSeparators = new String[ColumnSeparator.values().length][];
91             i = 0;
92             for (ColumnSeparator cs : ColumnSeparator.values())
93                 columnSeparators[i++] = new String[] { cs.label, cs.preference };
94
95             fDecimalSeparatorEditor = new ComboFieldEditor(CSVPreferences.P_CSV_DECIMAL_SEPARATOR, "Decimal separator", decimalSeparators, getFieldEditorParent()) {
96                 @Override
97                 public boolean isValid() {
98                     return !decimalSeparatorValue.equals(columnSeparatorValue);
99                 }
100             };
101             addField(fDecimalSeparatorEditor);
102
103             fColumnSeparatorEditor = new ComboFieldEditor(CSVPreferences.P_CSV_COLUMN_SEPARATOR, "Column separator", columnSeparators, getFieldEditorParent()) {
104             @Override
105             public boolean isValid() {
106                 return !decimalSeparatorValue.equals(columnSeparatorValue);
107             }
108         };
109
110             addField(fColumnSeparatorEditor);
111
112             fExtensionEditor = new StringFieldEditor(CSVPreferences.P_CSV_FILE_EXTENSION, "File extension", getFieldEditorParent());
113             addField(fExtensionEditor);
114             
115             fResamplingEditor = new BooleanFieldEditor(CSVPreferences.P_CSV_RESAMPLE, "&Resample", getFieldEditorParent());
116             fResamplingEditor.getDescriptionControl(getFieldEditorParent()).setToolTipText("Resample exported data from raw data");
117             addField(fResamplingEditor);
118
119             fSamplingModeEditor = new ComboFieldEditor(CSVPreferences.P_CSV_SAMPLING_MODE, "Sampling mode",
120                     new String[][] {
121                         {ExportInterpolation.LINEAR_INTERPOLATION.label, ExportInterpolation.LINEAR_INTERPOLATION.preference},
122                         {ExportInterpolation.PREVIOUS_SAMPLE.label, ExportInterpolation.PREVIOUS_SAMPLE.preference},
123                         }, getFieldEditorParent());
124             addField(fSamplingModeEditor);
125
126             fStartEditor = new StringFieldEditor(CSVPreferences.P_CSV_START_TIME, "Start time (s)", getFieldEditorParent()) {
127                 protected boolean doCheckState() {
128                     String text = getTextControl().getText();
129                     if (text == null || text.isEmpty()) {
130                         return true;
131                     }
132                     try {
133                         @SuppressWarnings("unused")
134                         double number = Double.parseDouble(text);
135                         return true;
136                     } catch (NumberFormatException e1) {
137                         return false;
138                     }
139                 };
140             };
141             fStartEditor.setEmptyStringAllowed(true);
142             fStartEditor.setErrorMessage("Enter valid start time value (s)");
143             fStartEditor.getTextControl(getFieldEditorParent()).setToolTipText("Time to start resampling from");
144             addField(fStartEditor);
145
146             fStepEditor = new StringFieldEditor(CSVPreferences.P_CSV_TIME_STEP, "Step size (s)", getFieldEditorParent()) {
147                 protected boolean doCheckState() {
148                     String text = getTextControl().getText();
149                     if (text == null || text.isEmpty()) {
150                         return true;
151                     }
152                     try {
153                         double number = Double.parseDouble(text);
154                         return number>0;
155                     } catch (NumberFormatException e1) {
156                         return false;
157                     }
158                 };
159             };
160             fStepEditor.setEmptyStringAllowed(true);
161             fStepEditor.setErrorMessage("Enter valid time step value (s)");
162             fStepEditor.getTextControl(getFieldEditorParent()).setToolTipText("Time step to use when resampling");
163             addField(fStepEditor);
164
165             Group significantDigitsGroup = new Group(getFieldEditorParent(), SWT.NONE);
166             significantDigitsGroup.setText("Significant digits");
167             significantDigitsGroup.setToolTipText("Control the Amount of Significant Digits in Exported Numbers");
168             GridDataFactory.fillDefaults().indent(0, 5).span(2, 1).applyTo(significantDigitsGroup);
169
170             fTimeDigitsEditor = new IntegerFieldEditor(CSVPreferences.P_CSV_TIME_DIGITS, "Time stamps", significantDigitsGroup, 2);
171             fTimeDigitsEditor.setValidRange(1, 15);
172             addField(fTimeDigitsEditor);
173             fFloatDigitsEditor = new IntegerFieldEditor(CSVPreferences.P_CSV_FLOAT_DIGITS, "Single precision floating point", significantDigitsGroup, 1);
174             fFloatDigitsEditor.setValidRange(1, 7);
175             addField(fFloatDigitsEditor);
176             fDoubleDigitsEditor = new IntegerFieldEditor(CSVPreferences.P_CSV_DOUBLE_DIGITS, "Double precision floating point", significantDigitsGroup, 2);
177             fDoubleDigitsEditor.setValidRange(1, 15);
178             addField(fDoubleDigitsEditor);
179
180             GridLayoutFactory.swtDefaults().numColumns(2).applyTo(significantDigitsGroup);
181
182             updateSampling(getPreferenceStore().getBoolean(CSVPreferences.P_CSV_RESAMPLE));
183         }
184
185     /**
186      * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
187      */
188     @Override
189     public void propertyChange(PropertyChangeEvent event) {
190         if (event.getProperty().equals(FieldEditor.VALUE)) {
191             Object source = event.getSource();
192             boolean validate = false;
193             if (source == fDecimalSeparatorEditor) {
194                 decimalSeparatorValue = (String) event.getNewValue();
195                 validate = true;
196             } else if (source == fColumnSeparatorEditor) {
197                 columnSeparatorValue = (String) event.getNewValue();
198                 validate = true;
199             } else if (source == fResamplingEditor) {
200                 updateSampling();
201             }
202             if (validate) {
203                 if (decimalSeparatorValue.equals(columnSeparatorValue)) {
204                     setErrorMessage("Column and decimal separators must not be the same.");
205                 } else {
206                     setErrorMessage(null);
207                 }
208                 checkState();
209                 updateApplyButton();
210             }
211         }
212         super.propertyChange(event);
213     }
214
215     private void updateSampling() {
216         updateSampling(fResamplingEditor.getBooleanValue());
217     }
218
219     private void updateSampling(boolean resample) {
220         Label fStartLabel = fStartEditor.getLabelControl(getFieldEditorParent());
221         Label fStepLabel = fStepEditor.getLabelControl(getFieldEditorParent());
222         Color gray = fStartLabel.getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY);
223         fStartLabel.setForeground(resample ? null : gray);
224         fStepLabel.setForeground(resample ? null : gray);
225     }
226
227 }