1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.modeling.ui.preferences;
\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
36 public class CSVPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
\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
49 private String decimalSeparatorValue;
\r
50 private String columnSeparatorValue;
\r
52 public CSVPreferencePage() {
\r
54 setDescription("CSV import and export preferences");
\r
55 IPreferenceStore pf = new ScopedPreferenceStore(InstanceScope.INSTANCE, CSVPreferences.P_NODE);
\r
56 setPreferenceStore( pf );
\r
60 public void createControl(Composite parent) {
\r
61 super.createControl(parent);
\r
65 public void init(IWorkbench workbench) {
\r
69 protected void initialize() {
\r
72 if (decimalSeparatorValue.equals(columnSeparatorValue)) {
\r
73 setErrorMessage("Column and decimal separators must not be the same.");
\r
75 updateApplyButton();
\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
84 String[][] decimalSeparators = new String[DecimalSeparator.values().length][];
\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
90 for (ColumnSeparator cs : ColumnSeparator.values())
\r
91 columnSeparators[i++] = new String[] { cs.label, cs.preference };
\r
93 fDecimalSeparatorEditor = new ComboFieldEditor(CSVPreferences.P_CSV_DECIMAL_SEPARATOR, "Decimal separator", decimalSeparators, getFieldEditorParent()) {
\r
95 public boolean isValid() {
\r
96 return !decimalSeparatorValue.equals(columnSeparatorValue);
\r
99 addField(fDecimalSeparatorEditor);
\r
101 fColumnSeparatorEditor = new ComboFieldEditor(CSVPreferences.P_CSV_COLUMN_SEPARATOR, "Column separator", columnSeparators, getFieldEditorParent()) {
\r
103 public boolean isValid() {
\r
104 return !decimalSeparatorValue.equals(columnSeparatorValue);
\r
108 addField(fColumnSeparatorEditor);
\r
110 fExtensionEditor = new StringFieldEditor(CSVPreferences.P_CSV_FILE_EXTENSION, "File extension", getFieldEditorParent());
\r
111 addField(fExtensionEditor);
\r
113 fResamplingEditor = new BooleanFieldEditor(CSVPreferences.P_CSV_RESAMPLE, "Resampling", getFieldEditorParent());
\r
114 addField(fResamplingEditor);
\r
116 fSamplingModeEditor = new ComboFieldEditor(CSVPreferences.P_CSV_SAMPLING_MODE, "Sampling mode",
\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
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
130 double number = Double.parseDouble(text);
\r
132 } catch (NumberFormatException e1) {
\r
137 fStartEditor.setEmptyStringAllowed(true);
\r
138 fStartEditor.setErrorMessage("Enter valid start time value (s)");
\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
147 double number = Double.parseDouble(text);
\r
149 } catch (NumberFormatException e1) {
\r
154 fStepEditor.setEmptyStringAllowed(true);
\r
155 fStepEditor.setErrorMessage("Enter valid time step value (s)");
\r
156 addField(fStepEditor);
\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
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
173 GridLayoutFactory.swtDefaults().numColumns(2).applyTo(significantDigitsGroup);
\r
177 * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
\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
187 } else if (source == fColumnSeparatorEditor) {
\r
188 columnSeparatorValue = (String) event.getNewValue();
\r
192 if (decimalSeparatorValue.equals(columnSeparatorValue)) {
\r
193 setErrorMessage("Column and decimal separators must not be the same.");
\r
195 setErrorMessage(null);
\r
198 updateApplyButton();
\r
201 super.propertyChange(event);
\r