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