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.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;
36 public class CSVPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
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;
49 private String decimalSeparatorValue;
50 private String columnSeparatorValue;
52 public CSVPreferencePage() {
54 setDescription("CSV import and export preferences");
55 IPreferenceStore pf = new ScopedPreferenceStore(InstanceScope.INSTANCE, CSVPreferences.P_NODE);
56 setPreferenceStore( pf );
60 public void createControl(Composite parent) {
61 super.createControl(parent);
65 public void init(IWorkbench workbench) {
69 protected void initialize() {
72 if (decimalSeparatorValue.equals(columnSeparatorValue)) {
73 setErrorMessage("Column and decimal separators must not be the same.");
80 protected void createFieldEditors() {
81 decimalSeparatorValue = getPreferenceStore().getString(CSVPreferences.P_CSV_DECIMAL_SEPARATOR);
82 columnSeparatorValue = getPreferenceStore().getString(CSVPreferences.P_CSV_COLUMN_SEPARATOR);
84 String[][] decimalSeparators = new String[DecimalSeparator.values().length][];
86 for (DecimalSeparator ds : DecimalSeparator.values())
87 decimalSeparators[i++] = new String[] { ds.label, ds.preference };
88 String[][] columnSeparators = new String[ColumnSeparator.values().length][];
90 for (ColumnSeparator cs : ColumnSeparator.values())
91 columnSeparators[i++] = new String[] { cs.label, cs.preference };
93 fDecimalSeparatorEditor = new ComboFieldEditor(CSVPreferences.P_CSV_DECIMAL_SEPARATOR, "Decimal separator", decimalSeparators, getFieldEditorParent()) {
95 public boolean isValid() {
96 return !decimalSeparatorValue.equals(columnSeparatorValue);
99 addField(fDecimalSeparatorEditor);
101 fColumnSeparatorEditor = new ComboFieldEditor(CSVPreferences.P_CSV_COLUMN_SEPARATOR, "Column separator", columnSeparators, getFieldEditorParent()) {
103 public boolean isValid() {
104 return !decimalSeparatorValue.equals(columnSeparatorValue);
108 addField(fColumnSeparatorEditor);
110 fExtensionEditor = new StringFieldEditor(CSVPreferences.P_CSV_FILE_EXTENSION, "File extension", getFieldEditorParent());
111 addField(fExtensionEditor);
113 fResamplingEditor = new BooleanFieldEditor(CSVPreferences.P_CSV_RESAMPLE, "Resampling", getFieldEditorParent());
114 addField(fResamplingEditor);
116 fSamplingModeEditor = new ComboFieldEditor(CSVPreferences.P_CSV_SAMPLING_MODE, "Sampling mode",
118 {ExportInterpolation.LINEAR_INTERPOLATION.label, ExportInterpolation.LINEAR_INTERPOLATION.preference},
119 {ExportInterpolation.PREVIOUS_SAMPLE.label, ExportInterpolation.PREVIOUS_SAMPLE.preference},
120 }, getFieldEditorParent());
121 addField(fSamplingModeEditor);
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()) {
130 double number = Double.parseDouble(text);
132 } catch (NumberFormatException e1) {
137 fStartEditor.setEmptyStringAllowed(true);
138 fStartEditor.setErrorMessage("Enter valid start time value (s)");
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()) {
147 double number = Double.parseDouble(text);
149 } catch (NumberFormatException e1) {
154 fStepEditor.setEmptyStringAllowed(true);
155 fStepEditor.setErrorMessage("Enter valid time step value (s)");
156 addField(fStepEditor);
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);
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);
173 GridLayoutFactory.swtDefaults().numColumns(2).applyTo(significantDigitsGroup);
177 * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
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();
187 } else if (source == fColumnSeparatorEditor) {
188 columnSeparatorValue = (String) event.getNewValue();
192 if (decimalSeparatorValue.equals(columnSeparatorValue)) {
193 setErrorMessage("Column and decimal separators must not be the same.");
195 setErrorMessage(null);
201 super.propertyChange(event);