]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.ui.workbench.preferences;\r
13 \r
14 import org.eclipse.jface.preference.StringFieldEditor;\r
15 import org.eclipse.swt.widgets.Composite;\r
16 import org.eclipse.swt.widgets.Text;\r
17 import org.simantics.ui.workbench.WorkbenchResources;\r
18 \r
19 public class NumberFieldEditor extends StringFieldEditor {\r
20 \r
21     private double minValidValue = 0;\r
22 \r
23     private double maxValidValue = Double.MAX_VALUE;\r
24 \r
25     private static final int DEFAULT_TEXT_LIMIT = 10;\r
26 \r
27     /**\r
28      * Creates a new number field editor \r
29      */\r
30     protected NumberFieldEditor() {\r
31     }\r
32 \r
33     /**\r
34      * Creates an number field editor.\r
35      * \r
36      * @param name the name of the preference this field editor works on\r
37      * @param labelText the label text of the field editor\r
38      * @param parent the parent of the field editor's control\r
39      */\r
40     public NumberFieldEditor(String name, String labelText, Composite parent) {\r
41         this(name, labelText, parent, DEFAULT_TEXT_LIMIT);\r
42     }\r
43 \r
44     /**\r
45      * Creates an number field editor.\r
46      * \r
47      * @param name the name of the preference this field editor works on\r
48      * @param labelText the label text of the field editor\r
49      * @param parent the parent of the field editor's control\r
50      * @param textLimit the maximum number of characters in the text.\r
51      */\r
52     public NumberFieldEditor(String name, String labelText, Composite parent,\r
53             int textLimit) {\r
54         init(name, labelText);\r
55         setTextLimit(textLimit);\r
56         setEmptyStringAllowed(false);\r
57         setErrorMessage(WorkbenchResources.getString("NumberFieldEditor.errorMessage"));//$NON-NLS-1$\r
58         createControl(parent);\r
59     }\r
60 \r
61     /**\r
62      * Sets the range of valid values for this field.\r
63      * \r
64      * @param min the minimum allowed value (inclusive)\r
65      * @param max the maximum allowed value (inclusive)\r
66      */\r
67     public void setValidRange(double min, double max) {\r
68         minValidValue = min;\r
69         maxValidValue = max;\r
70         setErrorMessage(WorkbenchResources.format(\r
71                 "NumberFieldEditor.errorMessageRange", //$NON-NLS-1$\r
72                 new Object[] { new Double(min), new Double(max) }));\r
73     }\r
74 \r
75     /* (non-Javadoc)\r
76      * Method declared on StringFieldEditor.\r
77      * Checks whether the entered String is a valid number or not.\r
78      */\r
79     protected boolean checkState() {\r
80 \r
81         Text text = getTextControl();\r
82 \r
83         if (text == null) {\r
84             return false;\r
85         }\r
86 \r
87         String numberString = text.getText();\r
88         try {\r
89             double number = Double.parseDouble(numberString);\r
90             if (number >= minValidValue && number <= maxValidValue) {\r
91                 clearErrorMessage();\r
92                 return true;\r
93             }\r
94 \r
95             showErrorMessage();\r
96             return false;\r
97 \r
98         } catch (NumberFormatException e1) {\r
99             showErrorMessage();\r
100         }\r
101 \r
102         return false;\r
103     }\r
104 \r
105     /* (non-Javadoc)\r
106      * Method declared on FieldEditor.\r
107      */\r
108     protected void doLoad() {\r
109         Text text = getTextControl();\r
110         if (text != null) {\r
111             double value = getPreferenceStore().getDouble(getPreferenceName());\r
112             text.setText("" + value);//$NON-NLS-1$\r
113             oldValue = "" + value; //$NON-NLS-1$\r
114         }\r
115 \r
116     }\r
117 \r
118     /* (non-Javadoc)\r
119      * Method declared on FieldEditor.\r
120      */\r
121     protected void doLoadDefault() {\r
122         Text text = getTextControl();\r
123         if (text != null) {\r
124             double value = getPreferenceStore().getDefaultDouble(getPreferenceName());\r
125             text.setText("" + value);//$NON-NLS-1$\r
126         }\r
127         valueChanged();\r
128     }\r
129 \r
130     /* (non-Javadoc)\r
131      * Method declared on FieldEditor.\r
132      */\r
133     protected void doStore() {\r
134         Text text = getTextControl();\r
135         if (text != null) {\r
136             getPreferenceStore().setValue(getPreferenceName(), Double.parseDouble(text.getText()));\r
137         }\r
138     }\r
139 \r
140     /**\r
141      * Returns this field editor's current value as a double.\r
142      *\r
143      * @return the value\r
144      * @exception NumberFormatException if the <code>String</code> does not\r
145      *   contain a valid number\r
146      */\r
147     public double getDoubleValue() throws NumberFormatException {\r
148         return Double.parseDouble(getStringValue());\r
149     }\r
150 }