svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / numberTextField / PositiveNumberField.java @ 40561
History | View | Annotate | Download (3.29 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.ParseException; |
29 |
|
30 |
/**
|
31 |
* <p>Provides a TextField component suitable for numbers. No error is
|
32 |
* produced when other characters are introduced, but just numbers are kept
|
33 |
* in the field after pressing enter or after focus is lost.</p>
|
34 |
*
|
35 |
* <p>The component parses numbers according to the default Locale. For example
|
36 |
* when es_ES locale is active, a comma (",") is expected to separate the
|
37 |
* fractional part from the integer part, and when en_US is the default locale,
|
38 |
* then a dot (".") is expected to separate them.</p>
|
39 |
*
|
40 |
* <p>The format of the accepted numbers can be modified by using the
|
41 |
* {@link #getFormat()} method. For example, to get a TextField that accepts
|
42 |
* just integer numbes we would use:</p>
|
43 |
* <pre>
|
44 |
* NumberTextField field = new NumberTextField();
|
45 |
* field.getFormat().setParseIntegerOnly(true);</pre>
|
46 |
*
|
47 |
* <p>In order to get a TextField that accepts double values with a minimum
|
48 |
* of two fractional digits and a maximum of five, we would use:</p>
|
49 |
* <pre>
|
50 |
* NumberTextField field = new NumberTextField();
|
51 |
* field.getFormat().setMinimumFractionDigits(0);
|
52 |
* field.getFormat().setMaximumFractionDigits(5);
|
53 |
* </pre>
|
54 |
*
|
55 |
* <p>NumberTextField commits the value to the Field when the focus is lost,
|
56 |
* while standard JFormattedTextField just commits the value to the Field
|
57 |
* when the user presses ENTER.</p>
|
58 |
*/
|
59 |
public class PositiveNumberField extends NumberTextField |
60 |
implements FocusListener{ |
61 |
|
62 |
private static final long serialVersionUID = 1289959692215502516L; |
63 |
|
64 |
public PositiveNumberField() {
|
65 |
super();
|
66 |
initialize(); |
67 |
} |
68 |
|
69 |
public PositiveNumberField(int columns) { |
70 |
super(columns);
|
71 |
initialize(); |
72 |
} |
73 |
|
74 |
public PositiveNumberField(double value, int columns) { |
75 |
super(value, columns);
|
76 |
initialize(); |
77 |
} |
78 |
|
79 |
public PositiveNumberField(int value, int columns) { |
80 |
super(value, columns);
|
81 |
initialize(); |
82 |
} |
83 |
|
84 |
private void initialize() { |
85 |
addFocusListener(this);
|
86 |
getFormat().setParseIntegerOnly(true);
|
87 |
getFormat().setGroupingUsed(false);
|
88 |
} |
89 |
|
90 |
public void focusLost(FocusEvent e) { |
91 |
try {
|
92 |
commitEdit(); |
93 |
} catch (ParseException e1) {} |
94 |
if (getIntValue()<0) { |
95 |
setValue(-getIntValue()); |
96 |
} |
97 |
} |
98 |
} |