KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > preference > IntegerFieldEditor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.preference;
12
13 import org.eclipse.jface.resource.JFaceResources;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.swt.widgets.Text;
16
17 /**
18  * A field editor for an integer type preference.
19  */

20 public class IntegerFieldEditor extends StringFieldEditor {
21     private int minValidValue = 0;
22
23     private int maxValidValue = Integer.MAX_VALUE;
24
25     private static final int DEFAULT_TEXT_LIMIT = 10;
26
27     /**
28      * Creates a new integer field editor
29      */

30     protected IntegerFieldEditor() {
31     }
32
33     /**
34      * Creates an integer 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 IntegerFieldEditor(String JavaDoc name, String JavaDoc labelText, Composite parent) {
41         this(name, labelText, parent, DEFAULT_TEXT_LIMIT);
42     }
43
44     /**
45      * Creates an integer 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 IntegerFieldEditor(String JavaDoc name, String JavaDoc labelText, Composite parent,
53             int textLimit) {
54         init(name, labelText);
55         setTextLimit(textLimit);
56         setEmptyStringAllowed(false);
57         setErrorMessage(JFaceResources
58                 .getString("IntegerFieldEditor.errorMessage"));//$NON-NLS-1$
59
createControl(parent);
60     }
61
62     /**
63      * Sets the range of valid values for this field.
64      *
65      * @param min the minimum allowed value (inclusive)
66      * @param max the maximum allowed value (inclusive)
67      */

68     public void setValidRange(int min, int max) {
69         minValidValue = min;
70         maxValidValue = max;
71         setErrorMessage(JFaceResources.format(
72                 "IntegerFieldEditor.errorMessageRange", //$NON-NLS-1$
73
new Object JavaDoc[] { new Integer JavaDoc(min), new Integer JavaDoc(max) }));
74     }
75
76     /* (non-Javadoc)
77      * Method declared on StringFieldEditor.
78      * Checks whether the entered String is a valid integer or not.
79      */

80     protected boolean checkState() {
81
82         Text text = getTextControl();
83
84         if (text == null) {
85             return false;
86         }
87
88         String JavaDoc numberString = text.getText();
89         try {
90             int number = Integer.valueOf(numberString).intValue();
91             if (number >= minValidValue && number <= maxValidValue) {
92                 clearErrorMessage();
93                 return true;
94             }
95             
96             showErrorMessage();
97             return false;
98             
99         } catch (NumberFormatException JavaDoc e1) {
100             showErrorMessage();
101         }
102
103         return false;
104     }
105
106     /* (non-Javadoc)
107      * Method declared on FieldEditor.
108      */

109     protected void doLoad() {
110         Text text = getTextControl();
111         if (text != null) {
112             int value = getPreferenceStore().getInt(getPreferenceName());
113             text.setText("" + 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             int value = getPreferenceStore().getDefaultInt(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             Integer JavaDoc i = new Integer JavaDoc(text.getText());
137             getPreferenceStore().setValue(getPreferenceName(), i.intValue());
138         }
139     }
140
141     /**
142      * Returns this field editor's current value as an integer.
143      *
144      * @return the value
145      * @exception NumberFormatException if the <code>String</code> does not
146      * contain a parsable integer
147      */

148     public int getIntValue() throws NumberFormatException JavaDoc {
149         return new Integer JavaDoc(getStringValue()).intValue();
150     }
151 }
152
Popular Tags