Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / numberTextField / NumberTextField.java @ 40561

History | View | Annotate | Download (6.61 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.gui.beans.numberTextField;
25

    
26
import java.awt.event.FocusEvent;
27
import java.awt.event.FocusListener;
28
import java.text.NumberFormat;
29
import java.text.ParseException;
30

    
31
import javax.swing.JFormattedTextField;
32
import javax.swing.JTextField;
33
import javax.swing.text.DefaultFormatterFactory;
34
import javax.swing.text.NumberFormatter;
35

    
36
/**
37
 * <p>Provides a TextField component suitable for numbers. No error is
38
 * produced when other characters are introduced, but just numbers are kept
39
 * in the field after pressing enter or after focus is lost.</p>
40
 * 
41
 * <p>The component parses numbers according to the default Locale. For example
42
 * when es_ES locale is active, a comma (",") is expected to separate the
43
 * fractional part from the integer part, and when en_US is the default locale,
44
 * then a dot (".") is expected to separate them.</p>
45
 * 
46
 * <p>The format of the accepted numbers can be modified by using the
47
 *  {@link #getFormat()} method. For example, to get a TextField that accepts
48
 *  just integer numbes we would use:</p>
49
 *  <pre>
50
 *    NumberTextField field = new NumberTextField();
51
 *    field.getFormat().setParseIntegerOnly(true);</pre>
52
 *  
53
 *  <p>In order to get a TextField that accepts double values with a minimum
54
 *  of two fractional digits and a maximum of five, we would use:</p>
55
 *  <pre>
56
 *    NumberTextField field = new NumberTextField();
57
 *    field.getFormat().setMinimumFractionDigits(0);
58
 *    field.getFormat().setMaximumFractionDigits(5);
59
 *  </pre>
60
 *  
61
 *  <p>NumberTextField commits the value to the Field when the focus is lost,
62
 *  while standard JFormattedTextField just commits the value to the Field
63
 *  when the user presses ENTER.</p>
64
 */
65
public class NumberTextField extends JFormattedTextField
66
        implements FocusListener{
67
        private static final long serialVersionUID = 1045806433744300963L;
68
        private NumberFormat format;
69

    
70
        public NumberTextField() {
71
        super();
72
        format = NumberFormat.getInstance();
73
        setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter(
74
                (NumberFormat)format)));
75
                initialize();
76
        }
77
        
78
        public NumberTextField(int columns) {
79
                this();
80
                setColumns(columns);
81
        }
82

    
83
        public NumberTextField(double value, int columns) {
84
                this();
85
                setColumns(columns);
86
                super.setValue(new Double(value));
87
        }
88

    
89
        public NumberTextField(int value, int columns) {
90
                this();
91
                setColumns(columns);
92
                super.setValue(new Integer(value));
93
        }
94

    
95
        /**
96
         * <p>Gets the NumberFormat accepted by this text field. It can be
97
         * used to change the format accepted by this text field.</p>
98
         * 
99
         * <p>Examples:</p>
100
         * 
101
         * <ul>
102
         * <li>To accept numbers with 0 to 5 fraction digits (as 2, 2.232, 6.23211):
103
         * <pre>
104
         *   getFormat().setMinimumFractionDigits(0);
105
         *   getFormat().setMaximumFractionDigits(5);</pre></li>
106
         * <li>To accept numbers with exactly two fraction digits (as 76.23, 12.00):
107
         * <pre>
108
         *   getFormat().setMinimumFractionDigits(2);
109
         *   getFormat().setMaximumFractionDigits(2);</pre></li>
110
         * <li>To accept just integer numbers (as 23, 5542, 0):
111
         * <pre>
112
         *   getFormat().setParseIntegerOnly(true);</pre></li>
113
         * <li>To enable digit grouping (enabled by default):
114
         * <pre>
115
         *   getFormat().setGroupingUsed(true);</pre></li></ul>
116
         * <p>Note: digit grouping enabled means to display 2534313.23 as
117
         * 2,534,313.23</p> 
118
         * 
119
         * @return
120
         */
121
        public NumberFormat getFormat() {
122
                return format;
123
        }
124
        
125
        private void initialize() {
126
                addFocusListener(this);
127
                setHorizontalAlignment(JTextField.RIGHT);
128
        }
129
        
130
        /**
131
         * <p>Sets the value of this TextField as a
132
         * <code>double</code> primitive type.</p>
133
         * 
134
         */
135
        public void setValue(double value) {
136
                setValue(new Double(value));
137
        }
138

    
139
        /**
140
         * <p>Sets the value of this TextField as an
141
         * <code>int</code> primitive type.</p>
142
         */
143
        public void setValue(int value) {
144
                setValue(new Integer(value));
145
        }
146

    
147
        /**
148
         * <p>Sets the value of this TextField as a
149
         * <code>long</code> primitive type.</p>
150
         * 
151
         */
152
        public void setValue(long value) {
153
                setValue(new Long(value));
154
        }
155

    
156
        /**
157
         * <p>Gets the value of this TextField as a
158
         * <code>double</code> primitive type.</p>
159
         * 
160
         * @return The value as a <code>double</code>
161
         */
162
        public double getDoubleValue() {
163
                Object value = getValue();
164
                if (value instanceof Double) {
165
                        return ((Double)value).doubleValue();
166
                }
167
                else if (value instanceof Long) {
168
                        return ((Long)value).doubleValue();
169
                }
170
                else if (value instanceof Integer) {
171
                        return ((Integer)value).doubleValue();
172
                }
173
                else
174
                        return 0;
175
        }
176

    
177
        /**
178
         * <p>Gets the value of this TextField as an
179
         * <code>int</code> primitive type.</p>
180
         * 
181
         * @return The value as an <code>int</code>
182
         */
183
        public int getIntValue() {
184
                Object value = getValue();
185
                if (value instanceof Long) {
186
                        return ((Long)value).intValue();
187
                }
188
                else if (value instanceof Double) {
189
                        return ((Double)value).intValue();
190
                }
191
                else if (value instanceof Integer) {
192
                        return ((Integer)value).intValue();
193
                }
194
                else
195
                        return 0;
196
        }
197

    
198
        /**
199
         * <p>Gets the value of this TextField as a
200
         * <code>long</code> primitive type.</p>
201
         * 
202
         * @return The value as a <code>long</code>
203
         */
204
        public long getLongValue() {
205
                Object value = getValue();
206
                if (value instanceof Long) {
207
                        return ((Long)value).longValue();
208
                }
209
                else if (value instanceof Double) {
210
                        return ((Double)value).longValue();
211
                }
212
                else if (value instanceof Integer) {
213
                        return ((Integer)value).longValue();
214
                }
215
                else
216
                        return 0;
217
        }
218

    
219
        public void focusGained(FocusEvent e) {}
220

    
221
        public void focusLost(FocusEvent e) {
222
                try {
223
                        commitEdit();
224
                } catch (ParseException e1) {}
225
        }
226
}