root / trunk / libraries / libUIComponent / src / de / ios / framework / swing / NumberField.java @ 13655
History | View | Annotate | Download (9.73 KB)
1 |
/*
|
---|---|
2 |
* (c)1997 IoS Gesellschaft fr innovative Softwareentwicklung mbH
|
3 |
* http://www.IoS-Online.de mailto:info@IoS-Online.de
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License as
|
7 |
* published by the Free Software Foundation; either version 2 of
|
8 |
* the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13 |
* General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
18 |
*/
|
19 |
|
20 |
package de.ios.framework.swing; |
21 |
|
22 |
import java.awt.event.KeyEvent; |
23 |
import java.math.BigInteger; |
24 |
import java.text.DecimalFormat; |
25 |
import java.text.DecimalFormatSymbols; |
26 |
import java.text.ParseException; |
27 |
|
28 |
|
29 |
/**
|
30 |
* Input-Field for Numbers, supported Objects: BigInteger, Long, Integer, (long)
|
31 |
* Implementation of several Listener-Interfaces by the Basic-Class only for internal use!
|
32 |
* For further description
|
33 |
* @see IoSTextField
|
34 |
* @version $Id: NumberField.java 13655 2007-09-12 16:28:55Z bsanchez $
|
35 |
*/
|
36 |
public class NumberField extends IoSTextField { |
37 |
private static final long serialVersionUID = -2597892768297671667L; |
38 |
|
39 |
/** Input-Field-Size in Columns. */
|
40 |
public final static int DEFAULT_LENGTH = 10; |
41 |
|
42 |
/** Limited Character-Set of this Field. */
|
43 |
private static final String NUMBER_CHARSET = "-0123456789TMtm"; |
44 |
|
45 |
/** The Decimal-Format. */
|
46 |
protected DecimalFormat decimalFormat = null; |
47 |
|
48 |
/** The Decimal-Format-Pattern. */
|
49 |
protected String dformat = "0"; |
50 |
|
51 |
|
52 |
/**
|
53 |
* Default Constructor
|
54 |
*/
|
55 |
public NumberField() {
|
56 |
this( DEFAULT_LENGTH );
|
57 |
} |
58 |
|
59 |
|
60 |
/**
|
61 |
* Constructor
|
62 |
* @param cols columns of textfield
|
63 |
*/
|
64 |
public NumberField( int cols ) { |
65 |
super( cols, NUMBER_CHARSET );
|
66 |
} |
67 |
|
68 |
|
69 |
/**
|
70 |
* Constructor
|
71 |
* @param _autoFormat if true use the special character set, ignore invalid characters
|
72 |
*/
|
73 |
public NumberField( boolean _autoFormat ) { |
74 |
this( DEFAULT_LENGTH, _autoFormat );
|
75 |
} |
76 |
|
77 |
|
78 |
/**
|
79 |
* Constructor
|
80 |
* @param cols columns of textfield
|
81 |
* @param _autoFormat
|
82 |
*/
|
83 |
public NumberField( int cols, boolean _autoFormat ) { |
84 |
this( cols );
|
85 |
setAutoFormat( _autoFormat ); |
86 |
} |
87 |
|
88 |
|
89 |
/**
|
90 |
* Constructor defining the auto-formating and illegal-value-focus-keeping.
|
91 |
*/
|
92 |
public NumberField( boolean _autoFormat, boolean _keepFocus ) { |
93 |
this( DEFAULT_LENGTH, _autoFormat, _keepFocus );
|
94 |
} |
95 |
|
96 |
|
97 |
/**
|
98 |
* Constructor defining the auto-formating and illegal-value-focus-keeping.
|
99 |
*/
|
100 |
public NumberField( int cols, boolean _autoFormat, boolean _keepFocus ) { |
101 |
super( cols, NUMBER_CHARSET, _autoFormat, _keepFocus );
|
102 |
} |
103 |
|
104 |
|
105 |
/**
|
106 |
* Set the value
|
107 |
* @param value The value to set.
|
108 |
*/
|
109 |
public void setValue( BigInteger value ) { |
110 |
defineDecimalFormat(); |
111 |
setText( (value == null) ?
|
112 |
null :
|
113 |
( (decimalFormat == null) ?
|
114 |
value.toString() : |
115 |
decimalFormat.format( value.longValue() ) ) ); |
116 |
} |
117 |
|
118 |
|
119 |
/**
|
120 |
* Set the value
|
121 |
* @param value The value to set.
|
122 |
*/
|
123 |
public void setValue( Integer value ) { |
124 |
defineDecimalFormat(); |
125 |
setText( (value == null) ?
|
126 |
null :
|
127 |
( (decimalFormat == null) ?
|
128 |
value.toString() : |
129 |
decimalFormat.format( value.longValue() ) ) ); |
130 |
} |
131 |
|
132 |
|
133 |
/**
|
134 |
* Set the value
|
135 |
* @param value The value to set.
|
136 |
*/
|
137 |
public void setValue( Long value ) { |
138 |
defineDecimalFormat(); |
139 |
setText( (value == null) ?
|
140 |
null :
|
141 |
( (decimalFormat == null) ?
|
142 |
value.toString() : |
143 |
decimalFormat.format( value.longValue() ) ) ); |
144 |
} |
145 |
|
146 |
|
147 |
/**
|
148 |
* Set the value
|
149 |
* @param value The value to set.
|
150 |
*/
|
151 |
public void setValue( long value ) { |
152 |
defineDecimalFormat(); |
153 |
setText( (decimalFormat == null) ?
|
154 |
String.valueOf( value ) :
|
155 |
decimalFormat.format( value ) ); |
156 |
} |
157 |
|
158 |
|
159 |
/**
|
160 |
* Set the format for display (WARNING: Can't be used with BigInteger!).
|
161 |
* @param formater New formater or null.
|
162 |
* @see java.text.DecimalFormat
|
163 |
*/
|
164 |
public void setDecimalFormat( DecimalFormat formater ) |
165 |
throws IllegalArgumentException { |
166 |
decimalFormat = (formater == null) ? null : (DecimalFormat)formater.clone(); |
167 |
dformat = null;
|
168 |
} |
169 |
|
170 |
|
171 |
/**
|
172 |
* Set the format-pattern for display (WARNING: Can't be used with BigInteger!).
|
173 |
* @param pattern New pattern or null.
|
174 |
* @see java.text.DecimalFormat
|
175 |
*/
|
176 |
public NumberField showTPoints( boolean b ) { |
177 |
dformat = (b ? ",##0" : "0"); |
178 |
return this; |
179 |
} |
180 |
|
181 |
|
182 |
/**
|
183 |
* Internal Method for setting the DecimalFormat.
|
184 |
*/
|
185 |
protected void defineDecimalFormat() { |
186 |
DecimalFormatSymbols decFormSym;
|
187 |
|
188 |
if (dformat != null) { |
189 |
try {
|
190 |
decimalFormat = new DecimalFormat( dformat ); |
191 |
decFormSym = decimalFormat.getDecimalFormatSymbols(); |
192 |
setCharSet( getCharSet()+ |
193 |
decFormSym.getGroupingSeparator() ); |
194 |
} catch (IllegalArgumentException e) { |
195 |
e.printStackTrace(); |
196 |
} |
197 |
dformat = null;
|
198 |
} |
199 |
} |
200 |
|
201 |
|
202 |
/**
|
203 |
* Get the current value as BigInteger.
|
204 |
* @return The current value.
|
205 |
*/
|
206 |
public BigInteger getValue() { |
207 |
String bi = getFormatedText();
|
208 |
try {
|
209 |
defineDecimalFormat(); |
210 |
return ( (bi == null) ? |
211 |
null :
|
212 |
( (decimalFormat == null) ?
|
213 |
new BigInteger(bi) : |
214 |
BigInteger.valueOf( decimalFormat.parse( bi ).longValue() ) ) );
|
215 |
} catch ( ParseException p ) { // If this happens, the formatValue()-Method is buggy! |
216 |
p.printStackTrace(); |
217 |
return null; |
218 |
} catch ( NumberFormatException e ) { // If this happens, the formatValue()-Method is buggy! |
219 |
e.printStackTrace(); |
220 |
return null; |
221 |
} |
222 |
} |
223 |
|
224 |
|
225 |
/**
|
226 |
* Get the current value as Long.
|
227 |
* @return The current value.
|
228 |
*/
|
229 |
public Long getLongValue() { |
230 |
BigInteger bi = getValue();
|
231 |
return (bi == null) ? null : new Long( bi.longValue() ); |
232 |
} |
233 |
|
234 |
|
235 |
/**
|
236 |
* Get the current value as Integer.
|
237 |
* @return The current value.
|
238 |
*/
|
239 |
public Integer getIntegerValue() { |
240 |
BigInteger bi = getValue();
|
241 |
return (bi == null) ? null : new Integer( bi.intValue() ); |
242 |
} |
243 |
|
244 |
|
245 |
/**
|
246 |
* Format the Value (automatically called on lostFocus, manually called on getValue/Date()).
|
247 |
* @return false, if formating fails due to an illegal Value
|
248 |
* (results in keeping the Focus, if requested by setKeepFocusOnIllegalValue).
|
249 |
*/
|
250 |
public boolean formatValue() { |
251 |
String bi = getNullText();
|
252 |
try {
|
253 |
defineDecimalFormat(); |
254 |
if (bi != null) |
255 |
if ( bi.trim().length() == 0 ) |
256 |
setText( null );
|
257 |
else {
|
258 |
bi = bi.toUpperCase(); |
259 |
if (bi.endsWith("T")) |
260 |
bi = bi.substring(0, bi.length()-1)+"000"; |
261 |
else if (bi.endsWith("M")) |
262 |
bi = bi.substring(0, bi.length()-1)+"000000"; |
263 |
setValue( (decimalFormat == null) ?
|
264 |
new BigInteger(bi) : |
265 |
BigInteger.valueOf( decimalFormat.parse( bi ).longValue() ) );
|
266 |
} |
267 |
return true; |
268 |
} catch ( ParseException p ) { // If this happens, the formatValue()-Method is buggy! |
269 |
return false; |
270 |
} catch ( NumberFormatException e ) { // If this happens, the formatValue()-Method is buggy! |
271 |
return false; |
272 |
} |
273 |
} |
274 |
|
275 |
|
276 |
/**
|
277 |
* Implements KeyListener.
|
278 |
* Invoked when a key has been typed. Replaces manualy handled Characters.
|
279 |
*/
|
280 |
public synchronized void keyTyped(KeyEvent evt ) { |
281 |
defineDecimalFormat(); |
282 |
super.keyTyped( evt );
|
283 |
} |
284 |
|
285 |
} |
286 |
|
287 |
/*
|
288 |
* $Log$
|
289 |
* Revision 1.3 2007-09-12 16:28:23 bsanchez
|
290 |
* *** empty log message ***
|
291 |
*
|
292 |
* Revision 1.2 2007/08/21 08:37:09 bsanchez
|
293 |
* - Quitados warnings en imports innecesarios
|
294 |
*
|
295 |
* Revision 1.1 2007/08/20 08:34:46 evercher
|
296 |
* He fusionado LibUI con LibUIComponents
|
297 |
*
|
298 |
* Revision 1.2 2007/01/23 13:11:06 caballero
|
299 |
* valor no num?rico
|
300 |
*
|
301 |
* Revision 1.1.2.1 2007/01/19 13:42:54 caballero
|
302 |
* NumericField
|
303 |
*
|
304 |
* Revision 1.1.1.1 2001/02/07 15:23:12 rtfm
|
305 |
* initial
|
306 |
*
|
307 |
* Revision 1.17 1999/12/15 09:08:45 fw
|
308 |
* allowed also small 't/m'
|
309 |
*
|
310 |
* Revision 1.16 1999/12/03 10:00:52 js
|
311 |
* comment fix
|
312 |
*
|
313 |
* Revision 1.15 1999/04/22 11:47:33 fw
|
314 |
* added xxxT & xxxM Support
|
315 |
*
|
316 |
* Revision 1.14 1999/04/15 15:50:09 fw
|
317 |
* using Formaters
|
318 |
*
|
319 |
* Revision 1.13 1999/04/09 15:36:27 fw
|
320 |
* extended Formater-Support
|
321 |
*
|
322 |
* Revision 1.12 1998/12/08 18:26:10 fw
|
323 |
* *Field: cleanup; autoFormat-, keepFocus-, limited-Charset-Support for all Fields (enabled by default)
|
324 |
* ViewLayout: Litle change due to changes at the *Fields
|
325 |
* DBListRow: toString()-Method for BigDecimals exchanging the '.' by ','
|
326 |
*
|
327 |
* Revision 1.11 1998/12/02 11:27:43 js
|
328 |
* Modifications for changes in IoSText (special character sets).
|
329 |
*
|
330 |
* Revision 1.10 1998/11/30 12:01:26 bw
|
331 |
* Support for DecimalFormat added (automatic formating by java.text.DecimalFormat).
|
332 |
* Not tested!!!!
|
333 |
*
|
334 |
* Revision 1.9 1998/06/30 12:18:30 bw
|
335 |
* Bugfix (keyTyped now set Caret-Position and can handle empty fields).
|
336 |
*
|
337 |
* Revision 1.8 1998/05/13 11:55:38 fw
|
338 |
* Added new basic TextField-Component IoSTextField (ExtendedTextField
|
339 |
* with JDK 1.1 Event-Handling), replaced the Base-Class ExtendedTextField
|
340 |
* of all other 'Fields' by IoSTextField
|
341 |
* ExtendedTextField uses now the JDK 1.0 Event-Handling again (due to massive
|
342 |
* Problems with blocked 1.0-Event-Handling at older Projects) - from now
|
343 |
* on the complete Class is DEPRECATED.
|
344 |
*
|
345 |
* Revision 1.7 1998/04/28 13:21:41 js
|
346 |
* bugfix in constructor
|
347 |
*
|
348 |
* Revision 1.6 1998/04/28 12:56:26 bw
|
349 |
* Automativ supression of non-digits added.
|
350 |
*
|
351 |
* Revision 1.5 1998/04/08 11:58:49 fw
|
352 |
* bugfix / extension
|
353 |
*
|
354 |
* Revision 1.4 1998/04/08 09:01:22 fw
|
355 |
* bugfix: Standard-Default-Value is null
|
356 |
*
|
357 |
* Revision 1.3 1998/04/07 11:44:48 fw
|
358 |
* *** empty log message ***
|
359 |
*
|
360 |
* Revision 1.2 1998/03/18 01:15:24 fw
|
361 |
* added some Methods
|
362 |
*
|
363 |
* Revision 1.1 1998/03/15 15:40:09 mh
|
364 |
* added new fields DateField, DecimalField and NumberField
|
365 |
*
|
366 |
*/
|
367 |
|