--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2012 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.ui.preferences;\r
+\r
+import org.eclipse.core.runtime.preferences.InstanceScope;\r
+import org.eclipse.jface.layout.GridDataFactory;\r
+import org.eclipse.jface.layout.GridLayoutFactory;\r
+import org.eclipse.jface.preference.BooleanFieldEditor;\r
+import org.eclipse.jface.preference.ComboFieldEditor;\r
+import org.eclipse.jface.preference.FieldEditor;\r
+import org.eclipse.jface.preference.FieldEditorPreferencePage;\r
+import org.eclipse.jface.preference.IPreferenceStore;\r
+import org.eclipse.jface.preference.IntegerFieldEditor;\r
+import org.eclipse.jface.preference.StringFieldEditor;\r
+import org.eclipse.jface.util.PropertyChangeEvent;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Group;\r
+import org.eclipse.ui.IWorkbench;\r
+import org.eclipse.ui.IWorkbenchPreferencePage;\r
+import org.eclipse.ui.preferences.ScopedPreferenceStore;\r
+import org.simantics.history.csv.ColumnSeparator;\r
+import org.simantics.history.csv.DecimalSeparator;\r
+import org.simantics.history.csv.ExportInterpolation;\r
+import org.simantics.modeling.preferences.CSVPreferences;\r
+\r
+public class CSVPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {\r
+\r
+ private ComboFieldEditor fDecimalSeparatorEditor = null;\r
+ private ComboFieldEditor fColumnSeparatorEditor = null;\r
+ private StringFieldEditor fExtensionEditor = null;\r
+ private BooleanFieldEditor fResamplingEditor = null;\r
+ private ComboFieldEditor fSamplingModeEditor = null;\r
+ private StringFieldEditor fStartEditor = null;\r
+ private StringFieldEditor fStepEditor = null;\r
+ private IntegerFieldEditor fTimeDigitsEditor = null;\r
+ private IntegerFieldEditor fFloatDigitsEditor = null;\r
+ private IntegerFieldEditor fDoubleDigitsEditor = null;\r
+\r
+ private String decimalSeparatorValue;\r
+ private String columnSeparatorValue;\r
+\r
+ public CSVPreferencePage() {\r
+ super(GRID);\r
+ setDescription("CSV import and export preferences");\r
+ IPreferenceStore pf = new ScopedPreferenceStore(InstanceScope.INSTANCE, CSVPreferences.P_NODE);\r
+ setPreferenceStore( pf ); \r
+ }\r
+\r
+ @Override\r
+ public void createControl(Composite parent) {\r
+ super.createControl(parent);\r
+ }\r
+\r
+ @Override\r
+ public void init(IWorkbench workbench) {\r
+ }\r
+\r
+ @Override\r
+ protected void initialize() {\r
+ super.initialize();\r
+\r
+ if (decimalSeparatorValue.equals(columnSeparatorValue)) {\r
+ setErrorMessage("Column and decimal separators must not be the same.");\r
+ checkState();\r
+ updateApplyButton();\r
+ }\r
+ }\r
+\r
+ @Override\r
+ protected void createFieldEditors() {\r
+ decimalSeparatorValue = getPreferenceStore().getString(CSVPreferences.P_CSV_DECIMAL_SEPARATOR);\r
+ columnSeparatorValue = getPreferenceStore().getString(CSVPreferences.P_CSV_COLUMN_SEPARATOR);\r
+\r
+ String[][] decimalSeparators = new String[DecimalSeparator.values().length][];\r
+ int i = 0;\r
+ for (DecimalSeparator ds : DecimalSeparator.values())\r
+ decimalSeparators[i++] = new String[] { ds.label, ds.preference };\r
+ String[][] columnSeparators = new String[ColumnSeparator.values().length][];\r
+ i = 0;\r
+ for (ColumnSeparator cs : ColumnSeparator.values())\r
+ columnSeparators[i++] = new String[] { cs.label, cs.preference };\r
+\r
+ fDecimalSeparatorEditor = new ComboFieldEditor(CSVPreferences.P_CSV_DECIMAL_SEPARATOR, "Decimal separator", decimalSeparators, getFieldEditorParent()) {\r
+ @Override\r
+ public boolean isValid() {\r
+ return !decimalSeparatorValue.equals(columnSeparatorValue);\r
+ }\r
+ };\r
+ addField(fDecimalSeparatorEditor);\r
+\r
+ fColumnSeparatorEditor = new ComboFieldEditor(CSVPreferences.P_CSV_COLUMN_SEPARATOR, "Column separator", columnSeparators, getFieldEditorParent()) {\r
+ @Override\r
+ public boolean isValid() {\r
+ return !decimalSeparatorValue.equals(columnSeparatorValue);\r
+ }\r
+ };\r
+\r
+ addField(fColumnSeparatorEditor);\r
+\r
+ fExtensionEditor = new StringFieldEditor(CSVPreferences.P_CSV_FILE_EXTENSION, "File extension", getFieldEditorParent());\r
+ addField(fExtensionEditor);\r
+ \r
+ fResamplingEditor = new BooleanFieldEditor(CSVPreferences.P_CSV_RESAMPLE, "Resampling", getFieldEditorParent());\r
+ addField(fResamplingEditor);\r
+\r
+ fSamplingModeEditor = new ComboFieldEditor(CSVPreferences.P_CSV_SAMPLING_MODE, "Sampling mode",\r
+ new String[][] {\r
+ {ExportInterpolation.LINEAR_INTERPOLATION.label, ExportInterpolation.LINEAR_INTERPOLATION.preference},\r
+ {ExportInterpolation.PREVIOUS_SAMPLE.label, ExportInterpolation.PREVIOUS_SAMPLE.preference},\r
+ }, getFieldEditorParent());\r
+ addField(fSamplingModeEditor);\r
+\r
+ fStartEditor = new StringFieldEditor(CSVPreferences.P_CSV_START_TIME, "Start time (s)", getFieldEditorParent()) {\r
+ protected boolean doCheckState() {\r
+ String text = getTextControl().getText();\r
+ if (text == null || text.isEmpty()) {\r
+ return true;\r
+ }\r
+ try {\r
+ double number = Double.parseDouble(text);\r
+ return number>0;\r
+ } catch (NumberFormatException e1) {\r
+ return false;\r
+ }\r
+ };\r
+ };\r
+ fStartEditor.setEmptyStringAllowed(true);\r
+ fStartEditor.setErrorMessage("Enter valid start time value (s)");\r
+\r
+ fStepEditor = new StringFieldEditor(CSVPreferences.P_CSV_TIME_STEP, "Step size (s)", getFieldEditorParent()) {\r
+ protected boolean doCheckState() {\r
+ String text = getTextControl().getText();\r
+ if (text == null || text.isEmpty()) {\r
+ return true;\r
+ }\r
+ try {\r
+ double number = Double.parseDouble(text);\r
+ return number>0;\r
+ } catch (NumberFormatException e1) {\r
+ return false;\r
+ }\r
+ };\r
+ };\r
+ fStepEditor.setEmptyStringAllowed(true);\r
+ fStepEditor.setErrorMessage("Enter valid time step value (s)");\r
+ addField(fStepEditor);\r
+\r
+ Group significantDigitsGroup = new Group(getFieldEditorParent(), SWT.NONE);\r
+ significantDigitsGroup.setText("Significant digits");\r
+ significantDigitsGroup.setToolTipText("Control the Amount of Significant Digits in Exported Numbers");\r
+ GridDataFactory.fillDefaults().indent(0, 5).span(2, 1).applyTo(significantDigitsGroup);\r
+\r
+ fTimeDigitsEditor = new IntegerFieldEditor(CSVPreferences.P_CSV_TIME_DIGITS, "Time stamps", significantDigitsGroup, 2);\r
+ fTimeDigitsEditor.setValidRange(1, 15);\r
+ addField(fTimeDigitsEditor);\r
+ fFloatDigitsEditor = new IntegerFieldEditor(CSVPreferences.P_CSV_FLOAT_DIGITS, "Single precision floating point", significantDigitsGroup, 1);\r
+ fFloatDigitsEditor.setValidRange(1, 7);\r
+ addField(fFloatDigitsEditor);\r
+ fDoubleDigitsEditor = new IntegerFieldEditor(CSVPreferences.P_CSV_DOUBLE_DIGITS, "Double precision floating point", significantDigitsGroup, 2);\r
+ fDoubleDigitsEditor.setValidRange(1, 15);\r
+ addField(fDoubleDigitsEditor);\r
+\r
+ GridLayoutFactory.swtDefaults().numColumns(2).applyTo(significantDigitsGroup);\r
+ }\r
+\r
+ /**\r
+ * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)\r
+ */\r
+ @Override\r
+ public void propertyChange(PropertyChangeEvent event) {\r
+ if (event.getProperty().equals(FieldEditor.VALUE)) {\r
+ Object source = event.getSource();\r
+ boolean validate = false;\r
+ if (source == fDecimalSeparatorEditor) {\r
+ decimalSeparatorValue = (String) event.getNewValue();\r
+ validate = true;\r
+ } else if (source == fColumnSeparatorEditor) {\r
+ columnSeparatorValue = (String) event.getNewValue();\r
+ validate = true;\r
+ }\r
+ if (validate) {\r
+ if (decimalSeparatorValue.equals(columnSeparatorValue)) {\r
+ setErrorMessage("Column and decimal separators must not be the same.");\r
+ } else {\r
+ setErrorMessage(null);\r
+ }\r
+ checkState();\r
+ updateApplyButton();\r
+ }\r
+ }\r
+ super.propertyChange(event);\r
+ }\r
+\r
+}\r