root / trunk / libraries / libUIComponent / src / de / ios / framework / swing / NumberField.java @ 13136
History | View | Annotate | Download (9.45 KB)
1 |
|
---|---|
2 |
|
3 |
/*
|
4 |
* $Id: NumberField.java 13136 2007-08-20 08:38:34Z evercher $
|
5 |
*
|
6 |
* (c)1997 IoS Gesellschaft fr innovative Softwareentwicklung mbH
|
7 |
* http://www.IoS-Online.de mailto:info@IoS-Online.de
|
8 |
*
|
9 |
* This program is free software; you can redistribute it and/or
|
10 |
* modify it under the terms of the GNU General Public License as
|
11 |
* published by the Free Software Foundation; either version 2 of
|
12 |
* the License, or (at your option) any later version.
|
13 |
*
|
14 |
* This program is distributed in the hope that it will be useful,
|
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
17 |
* General Public License for more details.
|
18 |
*
|
19 |
* You should have received a copy of the GNU General Public License
|
20 |
* along with this program; if not, write to the Free Software
|
21 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
22 |
*
|
23 |
*/
|
24 |
|
25 |
|
26 |
package de.ios.framework.swing; |
27 |
|
28 |
import java.math.BigInteger; |
29 |
import java.awt.event.*; |
30 |
import java.text.*; |
31 |
|
32 |
import de.ios.framework.basic.*; |
33 |
|
34 |
|
35 |
/**
|
36 |
* Input-Field for Numbers, supported Objects: BigInteger, Long, Integer, (long)
|
37 |
* Implementation of several Listener-Interfaces by the Basic-Class only for internal use!
|
38 |
* For further description
|
39 |
* @see IoSTextField
|
40 |
* @version $Id: NumberField.java 13136 2007-08-20 08:38:34Z evercher $
|
41 |
*/
|
42 |
public class NumberField extends IoSTextField { |
43 |
|
44 |
|
45 |
/** Input-Field-Size in Columns. */
|
46 |
public final static int DEFAULT_LENGTH = 10; |
47 |
|
48 |
/** Limited Character-Set of this Field. */
|
49 |
private static final String NUMBER_CHARSET = "-0123456789TMtm"; |
50 |
|
51 |
/** The Decimal-Format. */
|
52 |
protected DecimalFormat decimalFormat = null; |
53 |
|
54 |
/** The Decimal-Format-Pattern. */
|
55 |
protected String dformat = "0"; |
56 |
|
57 |
|
58 |
/**
|
59 |
* Default Constructor
|
60 |
*/
|
61 |
public NumberField() {
|
62 |
this( DEFAULT_LENGTH );
|
63 |
} |
64 |
|
65 |
|
66 |
/**
|
67 |
* Constructor
|
68 |
* @param cols columns of textfield
|
69 |
*/
|
70 |
public NumberField( int cols ) { |
71 |
super( cols, NUMBER_CHARSET );
|
72 |
} |
73 |
|
74 |
|
75 |
/**
|
76 |
* Constructor
|
77 |
* @param _autoFormat if true use the special character set, ignore invalid characters
|
78 |
*/
|
79 |
public NumberField( boolean _autoFormat ) { |
80 |
this( DEFAULT_LENGTH, _autoFormat );
|
81 |
} |
82 |
|
83 |
|
84 |
/**
|
85 |
* Constructor
|
86 |
* @param cols columns of textfield
|
87 |
* @param _autoFormat
|
88 |
*/
|
89 |
public NumberField( int cols, boolean _autoFormat ) { |
90 |
this( cols );
|
91 |
setAutoFormat( _autoFormat ); |
92 |
} |
93 |
|
94 |
|
95 |
/**
|
96 |
* Constructor defining the auto-formating and illegal-value-focus-keeping.
|
97 |
*/
|
98 |
public NumberField( boolean _autoFormat, boolean _keepFocus ) { |
99 |
this( DEFAULT_LENGTH, _autoFormat, _keepFocus );
|
100 |
} |
101 |
|
102 |
|
103 |
/**
|
104 |
* Constructor defining the auto-formating and illegal-value-focus-keeping.
|
105 |
*/
|
106 |
public NumberField( int cols, boolean _autoFormat, boolean _keepFocus ) { |
107 |
super( cols, NUMBER_CHARSET, _autoFormat, _keepFocus );
|
108 |
} |
109 |
|
110 |
|
111 |
/**
|
112 |
* Set the value
|
113 |
* @param value The value to set.
|
114 |
*/
|
115 |
public void setValue( BigInteger value ) { |
116 |
defineDecimalFormat(); |
117 |
setText( (value == null) ?
|
118 |
null :
|
119 |
( (decimalFormat == null) ?
|
120 |
value.toString() : |
121 |
decimalFormat.format( value.longValue() ) ) ); |
122 |
} |
123 |
|
124 |
|
125 |
/**
|
126 |
* Set the value
|
127 |
* @param value The value to set.
|
128 |
*/
|
129 |
public void setValue( Integer value ) { |
130 |
defineDecimalFormat(); |
131 |
setText( (value == null) ?
|
132 |
null :
|
133 |
( (decimalFormat == null) ?
|
134 |
value.toString() : |
135 |
decimalFormat.format( value.longValue() ) ) ); |
136 |
} |
137 |
|
138 |
|
139 |
/**
|
140 |
* Set the value
|
141 |
* @param value The value to set.
|
142 |
*/
|
143 |
public void setValue( Long value ) { |
144 |
defineDecimalFormat(); |
145 |
setText( (value == null) ?
|
146 |
null :
|
147 |
( (decimalFormat == null) ?
|
148 |
value.toString() : |
149 |
decimalFormat.format( value.longValue() ) ) ); |
150 |
} |
151 |
|
152 |
|
153 |
/**
|
154 |
* Set the value
|
155 |
* @param value The value to set.
|
156 |
*/
|
157 |
public void setValue( long value ) { |
158 |
defineDecimalFormat(); |
159 |
setText( (decimalFormat == null) ?
|
160 |
String.valueOf( value ) :
|
161 |
decimalFormat.format( value ) ); |
162 |
} |
163 |
|
164 |
|
165 |
/**
|
166 |
* Set the format for display (WARNING: Can't be used with BigInteger!).
|
167 |
* @param formater New formater or null.
|
168 |
* @see java.text.DecimalFormat
|
169 |
*/
|
170 |
public void setDecimalFormat( DecimalFormat formater ) |
171 |
throws IllegalArgumentException { |
172 |
decimalFormat = (formater == null) ? null : (DecimalFormat)formater.clone(); |
173 |
dformat = null;
|
174 |
} |
175 |
|
176 |
|
177 |
/**
|
178 |
* Set the format-pattern for display (WARNING: Can't be used with BigInteger!).
|
179 |
* @param pattern New pattern or null.
|
180 |
* @see java.text.DecimalFormat
|
181 |
*/
|
182 |
public NumberField showTPoints( boolean b ) { |
183 |
dformat = (b ? ",##0" : "0"); |
184 |
return this; |
185 |
} |
186 |
|
187 |
|
188 |
/**
|
189 |
* Internal Method for setting the DecimalFormat.
|
190 |
*/
|
191 |
protected void defineDecimalFormat() { |
192 |
DecimalFormatSymbols decFormSym;
|
193 |
|
194 |
if (dformat != null) { |
195 |
try {
|
196 |
decimalFormat = new DecimalFormat( dformat ); |
197 |
decFormSym = decimalFormat.getDecimalFormatSymbols(); |
198 |
setCharSet( getCharSet()+ |
199 |
decFormSym.getGroupingSeparator() ); |
200 |
} catch (IllegalArgumentException e) { |
201 |
e.printStackTrace(); |
202 |
} |
203 |
dformat = null;
|
204 |
} |
205 |
} |
206 |
|
207 |
|
208 |
/**
|
209 |
* Get the current value as BigInteger.
|
210 |
* @return The current value.
|
211 |
*/
|
212 |
public BigInteger getValue() { |
213 |
String bi = getFormatedText();
|
214 |
try {
|
215 |
defineDecimalFormat(); |
216 |
return ( (bi == null) ? |
217 |
null :
|
218 |
( (decimalFormat == null) ?
|
219 |
new BigInteger(bi) : |
220 |
BigInteger.valueOf( decimalFormat.parse( bi ).longValue() ) ) );
|
221 |
} catch ( ParseException p ) { // If this happens, the formatValue()-Method is buggy! |
222 |
p.printStackTrace(); |
223 |
return null; |
224 |
} catch ( NumberFormatException e ) { // If this happens, the formatValue()-Method is buggy! |
225 |
e.printStackTrace(); |
226 |
return null; |
227 |
} |
228 |
} |
229 |
|
230 |
|
231 |
/**
|
232 |
* Get the current value as Long.
|
233 |
* @return The current value.
|
234 |
*/
|
235 |
public Long getLongValue() { |
236 |
BigInteger bi = getValue();
|
237 |
return (bi == null) ? null : new Long( bi.longValue() ); |
238 |
} |
239 |
|
240 |
|
241 |
/**
|
242 |
* Get the current value as Integer.
|
243 |
* @return The current value.
|
244 |
*/
|
245 |
public Integer getIntegerValue() { |
246 |
BigInteger bi = getValue();
|
247 |
return (bi == null) ? null : new Integer( bi.intValue() ); |
248 |
} |
249 |
|
250 |
|
251 |
/**
|
252 |
* Format the Value (automatically called on lostFocus, manually called on getValue/Date()).
|
253 |
* @return false, if formating fails due to an illegal Value
|
254 |
* (results in keeping the Focus, if requested by setKeepFocusOnIllegalValue).
|
255 |
*/
|
256 |
public boolean formatValue() { |
257 |
String bi = getNullText();
|
258 |
try {
|
259 |
defineDecimalFormat(); |
260 |
if (bi != null) |
261 |
if ( bi.trim().length() == 0 ) |
262 |
setText( null );
|
263 |
else {
|
264 |
bi = bi.toUpperCase(); |
265 |
if (bi.endsWith("T")) |
266 |
bi = bi.substring(0, bi.length()-1)+"000"; |
267 |
else if (bi.endsWith("M")) |
268 |
bi = bi.substring(0, bi.length()-1)+"000000"; |
269 |
setValue( (decimalFormat == null) ?
|
270 |
new BigInteger(bi) : |
271 |
BigInteger.valueOf( decimalFormat.parse( bi ).longValue() ) );
|
272 |
} |
273 |
return true; |
274 |
} catch ( ParseException p ) { // If this happens, the formatValue()-Method is buggy! |
275 |
return false; |
276 |
} catch ( NumberFormatException e ) { // If this happens, the formatValue()-Method is buggy! |
277 |
return false; |
278 |
} |
279 |
} |
280 |
|
281 |
|
282 |
/**
|
283 |
* Implements KeyListener.
|
284 |
* Invoked when a key has been typed. Replaces manualy handled Characters.
|
285 |
*/
|
286 |
public synchronized void keyTyped(KeyEvent evt ) { |
287 |
defineDecimalFormat(); |
288 |
super.keyTyped( evt );
|
289 |
} |
290 |
|
291 |
} |
292 |
|
293 |
/*
|
294 |
* $Log$
|
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 |
|