]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.ui/src/org/simantics/ui/workbench/preferences/NumberFieldEditor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / preferences / NumberFieldEditor.java
diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/workbench/preferences/NumberFieldEditor.java b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/preferences/NumberFieldEditor.java
new file mode 100644 (file)
index 0000000..97ad65e
--- /dev/null
@@ -0,0 +1,150 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in 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.ui.workbench.preferences;\r
+\r
+import org.eclipse.jface.preference.StringFieldEditor;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Text;\r
+import org.simantics.ui.workbench.WorkbenchResources;\r
+\r
+public class NumberFieldEditor extends StringFieldEditor {\r
+\r
+    private double minValidValue = 0;\r
+\r
+    private double maxValidValue = Double.MAX_VALUE;\r
+\r
+    private static final int DEFAULT_TEXT_LIMIT = 10;\r
+\r
+    /**\r
+     * Creates a new number field editor \r
+     */\r
+    protected NumberFieldEditor() {\r
+    }\r
+\r
+    /**\r
+     * Creates an number field editor.\r
+     * \r
+     * @param name the name of the preference this field editor works on\r
+     * @param labelText the label text of the field editor\r
+     * @param parent the parent of the field editor's control\r
+     */\r
+    public NumberFieldEditor(String name, String labelText, Composite parent) {\r
+        this(name, labelText, parent, DEFAULT_TEXT_LIMIT);\r
+    }\r
+\r
+    /**\r
+     * Creates an number field editor.\r
+     * \r
+     * @param name the name of the preference this field editor works on\r
+     * @param labelText the label text of the field editor\r
+     * @param parent the parent of the field editor's control\r
+     * @param textLimit the maximum number of characters in the text.\r
+     */\r
+    public NumberFieldEditor(String name, String labelText, Composite parent,\r
+            int textLimit) {\r
+        init(name, labelText);\r
+        setTextLimit(textLimit);\r
+        setEmptyStringAllowed(false);\r
+        setErrorMessage(WorkbenchResources.getString("NumberFieldEditor.errorMessage"));//$NON-NLS-1$\r
+        createControl(parent);\r
+    }\r
+\r
+    /**\r
+     * Sets the range of valid values for this field.\r
+     * \r
+     * @param min the minimum allowed value (inclusive)\r
+     * @param max the maximum allowed value (inclusive)\r
+     */\r
+    public void setValidRange(double min, double max) {\r
+        minValidValue = min;\r
+        maxValidValue = max;\r
+        setErrorMessage(WorkbenchResources.format(\r
+                "NumberFieldEditor.errorMessageRange", //$NON-NLS-1$\r
+                new Object[] { new Double(min), new Double(max) }));\r
+    }\r
+\r
+    /* (non-Javadoc)\r
+     * Method declared on StringFieldEditor.\r
+     * Checks whether the entered String is a valid number or not.\r
+     */\r
+    protected boolean checkState() {\r
+\r
+        Text text = getTextControl();\r
+\r
+        if (text == null) {\r
+            return false;\r
+        }\r
+\r
+        String numberString = text.getText();\r
+        try {\r
+            double number = Double.parseDouble(numberString);\r
+            if (number >= minValidValue && number <= maxValidValue) {\r
+                clearErrorMessage();\r
+                return true;\r
+            }\r
+\r
+            showErrorMessage();\r
+            return false;\r
+\r
+        } catch (NumberFormatException e1) {\r
+            showErrorMessage();\r
+        }\r
+\r
+        return false;\r
+    }\r
+\r
+    /* (non-Javadoc)\r
+     * Method declared on FieldEditor.\r
+     */\r
+    protected void doLoad() {\r
+        Text text = getTextControl();\r
+        if (text != null) {\r
+            double value = getPreferenceStore().getDouble(getPreferenceName());\r
+            text.setText("" + value);//$NON-NLS-1$\r
+            oldValue = "" + value; //$NON-NLS-1$\r
+        }\r
+\r
+    }\r
+\r
+    /* (non-Javadoc)\r
+     * Method declared on FieldEditor.\r
+     */\r
+    protected void doLoadDefault() {\r
+        Text text = getTextControl();\r
+        if (text != null) {\r
+            double value = getPreferenceStore().getDefaultDouble(getPreferenceName());\r
+            text.setText("" + value);//$NON-NLS-1$\r
+        }\r
+        valueChanged();\r
+    }\r
+\r
+    /* (non-Javadoc)\r
+     * Method declared on FieldEditor.\r
+     */\r
+    protected void doStore() {\r
+        Text text = getTextControl();\r
+        if (text != null) {\r
+            getPreferenceStore().setValue(getPreferenceName(), Double.parseDouble(text.getText()));\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Returns this field editor's current value as a double.\r
+     *\r
+     * @return the value\r
+     * @exception NumberFormatException if the <code>String</code> does not\r
+     *   contain a valid number\r
+     */\r
+    public double getDoubleValue() throws NumberFormatException {\r
+        return Double.parseDouble(getStringValue());\r
+    }\r
+}
\ No newline at end of file