1 /*******************************************************************************
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.preferences;
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;
38 public class CSVPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
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;
51 private String decimalSeparatorValue;
52 private String columnSeparatorValue;
54 public CSVPreferencePage() {
56 setDescription("CSV import and export preferences");
57 IPreferenceStore pf = new ScopedPreferenceStore(InstanceScope.INSTANCE, CSVPreferences.P_NODE);
58 setPreferenceStore( pf );
62 public void createControl(Composite parent) {
63 super.createControl(parent);
67 public void init(IWorkbench workbench) {
71 protected void initialize() {
74 if (decimalSeparatorValue.equals(columnSeparatorValue)) {
75 setErrorMessage("Column and decimal separators must not be the same.");
82 protected void createFieldEditors() {
83 decimalSeparatorValue = getPreferenceStore().getString(CSVPreferences.P_CSV_DECIMAL_SEPARATOR);
84 columnSeparatorValue = getPreferenceStore().getString(CSVPreferences.P_CSV_COLUMN_SEPARATOR);
86 String[][] decimalSeparators = new String[DecimalSeparator.values().length][];
88 for (DecimalSeparator ds : DecimalSeparator.values())
89 decimalSeparators[i++] = new String[] { ds.label, ds.preference };
90 String[][] columnSeparators = new String[ColumnSeparator.values().length][];
92 for (ColumnSeparator cs : ColumnSeparator.values())
93 columnSeparators[i++] = new String[] { cs.label, cs.preference };
95 fDecimalSeparatorEditor = new ComboFieldEditor(CSVPreferences.P_CSV_DECIMAL_SEPARATOR, "Decimal separator", decimalSeparators, getFieldEditorParent()) {
97 public boolean isValid() {
98 return !decimalSeparatorValue.equals(columnSeparatorValue);
101 addField(fDecimalSeparatorEditor);
103 fColumnSeparatorEditor = new ComboFieldEditor(CSVPreferences.P_CSV_COLUMN_SEPARATOR, "Column separator", columnSeparators, getFieldEditorParent()) {
105 public boolean isValid() {
106 return !decimalSeparatorValue.equals(columnSeparatorValue);
110 addField(fColumnSeparatorEditor);
112 fExtensionEditor = new StringFieldEditor(CSVPreferences.P_CSV_FILE_EXTENSION, "File extension", getFieldEditorParent());
113 addField(fExtensionEditor);
115 fResamplingEditor = new BooleanFieldEditor(CSVPreferences.P_CSV_RESAMPLE, "&Resample", getFieldEditorParent());
116 fResamplingEditor.getDescriptionControl(getFieldEditorParent()).setToolTipText("Resample exported data from raw data");
117 addField(fResamplingEditor);
119 fSamplingModeEditor = new ComboFieldEditor(CSVPreferences.P_CSV_SAMPLING_MODE, "Sampling mode",
121 {ExportInterpolation.LINEAR_INTERPOLATION.label, ExportInterpolation.LINEAR_INTERPOLATION.preference},
122 {ExportInterpolation.PREVIOUS_SAMPLE.label, ExportInterpolation.PREVIOUS_SAMPLE.preference},
123 }, getFieldEditorParent());
124 addField(fSamplingModeEditor);
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()) {
133 @SuppressWarnings("unused")
134 double number = Double.parseDouble(text);
136 } catch (NumberFormatException e1) {
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);
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()) {
153 double number = Double.parseDouble(text);
155 } catch (NumberFormatException e1) {
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);
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);
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);
180 GridLayoutFactory.swtDefaults().numColumns(2).applyTo(significantDigitsGroup);
182 updateSampling(getPreferenceStore().getBoolean(CSVPreferences.P_CSV_RESAMPLE));
186 * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
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();
196 } else if (source == fColumnSeparatorEditor) {
197 columnSeparatorValue = (String) event.getNewValue();
199 } else if (source == fResamplingEditor) {
203 if (decimalSeparatorValue.equals(columnSeparatorValue)) {
204 setErrorMessage("Column and decimal separators must not be the same.");
206 setErrorMessage(null);
212 super.propertyChange(event);
215 private void updateSampling() {
216 updateSampling(fResamplingEditor.getBooleanValue());
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);