Statistics
| Revision:

root / branches / v10 / libraries / libUI / src / de / ios / framework / swing / JNumberField.java @ 11087

History | View | Annotate | Download (9.43 KB)

1

    
2

    
3
/*
4
 * $Id$
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$
41
 * @deprecated
42
 */
43
public class JNumberField extends IoSTextField {
44

    
45

    
46
  /** Input-Field-Size in Columns. */
47
  public final static int DEFAULT_LENGTH = 10;
48

    
49
  /** Limited Character-Set of this Field. */
50
  private static final String NUMBER_CHARSET = "-0123456789TMtm";
51

    
52
  /** The Decimal-Format. */
53
  protected DecimalFormat decimalFormat = null;
54

    
55
  /** The Decimal-Format-Pattern. */
56
  protected String dformat = "0";
57

    
58

    
59
  /**
60
   * Default Constructor
61
   */
62
  public JNumberField() {
63
    this( DEFAULT_LENGTH );
64
  }
65

    
66

    
67
  /**
68
   * Constructor
69
   * @param cols columns of textfield
70
   */
71
  public JNumberField( int cols ) {
72
    super( cols, NUMBER_CHARSET );
73
  }
74

    
75

    
76
  /**
77
   * Constructor
78
   * @param _autoFormat if true use the special character set, ignore invalid characters
79
   */
80
  public JNumberField( boolean _autoFormat ) {
81
    this( DEFAULT_LENGTH, _autoFormat );
82
  }
83

    
84

    
85
  /**
86
   * Constructor
87
   * @param cols columns of textfield
88
   * @param _autoFormat
89
   */
90
  public JNumberField( int cols, boolean _autoFormat ) {
91
    this( cols );
92
    setAutoFormat( _autoFormat );
93
  }
94

    
95

    
96
  /**
97
   * Constructor defining the auto-formating and illegal-value-focus-keeping.
98
   */
99
  public JNumberField( boolean _autoFormat, boolean _keepFocus ) {
100
    this( DEFAULT_LENGTH, _autoFormat, _keepFocus );
101
  }
102

    
103

    
104
  /**
105
   * Constructor defining the auto-formating and illegal-value-focus-keeping.
106
   */
107
  public JNumberField( int cols, boolean _autoFormat, boolean _keepFocus ) {
108
    super( cols, NUMBER_CHARSET, _autoFormat, _keepFocus );
109
  }
110

    
111

    
112
  /**
113
   * Set the value
114
   * @param value The value to set.
115
   */
116
  public void setValue( BigInteger value ) {
117
    defineDecimalFormat();
118
    setText( (value == null) ?
119
             null :
120
             ( (decimalFormat == null) ?
121
               value.toString() :
122
               decimalFormat.format( value.longValue() ) ) );
123
  }
124

    
125

    
126
  /**
127
   * Set the value
128
   * @param value The value to set.
129
   */
130
  public void setValue( Integer value ) {
131
    defineDecimalFormat();
132
    setText( (value == null) ?
133
             null :
134
             ( (decimalFormat == null) ?
135
               value.toString() :
136
               decimalFormat.format( value.longValue() ) ) );
137
  }
138

    
139

    
140
  /**
141
   * Set the value
142
   * @param value The value to set.
143
   */
144
  public void setValue( Long value ) {
145
    defineDecimalFormat();
146
    setText( (value == null) ?
147
             null :
148
             ( (decimalFormat == null) ?
149
               value.toString() :
150
               decimalFormat.format( value.longValue() ) ) );
151
  }
152

    
153

    
154
  /**
155
   * Set the value
156
   * @param value The value to set.
157
   */
158
  public void setValue( long value ) {
159
    defineDecimalFormat();
160
    setText( (decimalFormat == null) ?
161
             String.valueOf( value ) :
162
             decimalFormat.format( value ) );
163
  }
164

    
165

    
166
  /**
167
   * Set the format for display (WARNING: Can't be used with BigInteger!).
168
   * @param formater New formater or null.
169
   * @see java.text.DecimalFormat
170
   */
171
  public void setDecimalFormat( DecimalFormat formater )
172
    throws IllegalArgumentException {
173
      decimalFormat = (formater == null) ? null : (DecimalFormat)formater.clone();
174
      dformat = null;
175
  }
176

    
177

    
178
  /**
179
   * Set the format-pattern for display (WARNING: Can't be used with BigInteger!).
180
   * @param pattern New pattern or null.
181
   * @see java.text.DecimalFormat
182
   */
183
  public JNumberField showTPoints( boolean b ) {
184
    dformat = (b ? ",##0" : "0");
185
    return this;
186
  }
187

    
188

    
189
  /**
190
   * Internal Method for setting the DecimalFormat.
191
   */
192
  protected void defineDecimalFormat() {
193
    DecimalFormatSymbols decFormSym;
194

    
195
    if (dformat != null) {
196
      try {
197
        decimalFormat = new DecimalFormat( dformat );
198
        decFormSym    = decimalFormat.getDecimalFormatSymbols();
199
        setCharSet( getCharSet()+
200
                    decFormSym.getGroupingSeparator() );
201
      } catch (IllegalArgumentException e) {
202
              e.printStackTrace();
203
      }
204
      dformat = null;
205
    }
206
  }
207

    
208

    
209
  /**
210
   * Get the current value as BigInteger.
211
   * @return The current value.
212
   */
213
  public BigInteger getValue() {
214
    String bi = getFormatedText();
215
    try {
216
      defineDecimalFormat();
217
      return ( (bi == null) ?
218
               null :
219
               ( (decimalFormat == null) ?
220
                 new BigInteger(bi) :
221
                 BigInteger.valueOf( decimalFormat.parse( bi ).longValue() ) ) );
222
    } catch ( ParseException p ) {  // If this happens, the formatValue()-Method is buggy!
223
             p.printStackTrace();
224
      return null;
225
    } catch ( NumberFormatException e ) {  // If this happens, the formatValue()-Method is buggy!
226
             e.printStackTrace();
227
      return null;
228
    }
229
  }
230

    
231

    
232
  /**
233
   * Get the current value as Long.
234
   * @return The current value.
235
   */
236
  public Long getLongValue() {
237
    BigInteger bi = getValue();
238
    return (bi == null) ? null : new Long( bi.longValue() );
239
  }
240

    
241

    
242
  /**
243
   * Get the current value as Integer.
244
   * @return The current value.
245
   */
246
  public Integer getIntegerValue() {
247
    BigInteger bi = getValue();
248
    return (bi == null) ? null : new Integer( bi.intValue() );
249
  }
250

    
251

    
252
  /**
253
   * Format the Value (automatically called on lostFocus, manually called on getValue/Date()).
254
   * @return false, if formating fails due to an illegal Value
255
   * (results in keeping the Focus, if requested by setKeepFocusOnIllegalValue).
256
   */
257
  public boolean formatValue() {
258
    String bi = getNullText();
259
    try {
260
      defineDecimalFormat();
261
      if (bi != null)
262
        if ( bi.trim().length() == 0 )
263
          setText( null );
264
        else {
265
          bi = bi.toUpperCase();
266
          if (bi.endsWith("T"))
267
            bi = bi.substring(0, bi.length()-1)+"000";
268
          else  if (bi.endsWith("M"))
269
            bi = bi.substring(0, bi.length()-1)+"000000";
270
          setValue( (decimalFormat == null) ?
271
                    new BigInteger(bi) :
272
                    BigInteger.valueOf( decimalFormat.parse( bi ).longValue() ) );
273
        }
274
      return true;
275
    } catch ( ParseException p ) {  // If this happens, the formatValue()-Method is buggy!
276
      return false;
277
    } catch ( NumberFormatException e ) {  // If this happens, the formatValue()-Method is buggy!
278
      return false;
279
    }
280
  }
281

    
282

    
283
  /**
284
   * Implements KeyListener.
285
   * Invoked when a key has been typed. Replaces manualy handled Characters.
286
   */
287
  public synchronized void keyTyped(KeyEvent evt ) {
288
    defineDecimalFormat();
289
    super.keyTyped( evt );
290
  }
291

    
292
}
293

    
294
/*
295
 * $Log$
296
 * Revision 1.2.2.4  2007-04-10 08:15:10  jmvivo
297
 * Por compatibilidad con la v1_0_1 se vuelve a usar JNumberField
298
 *
299
 * Revision 1.1.2.1  2007/01/19 13:42:54  caballero
300
 * NumericField
301
 *
302
 * Revision 1.1.1.1  2001/02/07 15:23:12  rtfm
303
 * initial
304
 *
305
 * Revision 1.17  1999/12/15 09:08:45  fw
306
 * allowed also small 't/m'
307
 *
308
 * Revision 1.16  1999/12/03 10:00:52  js
309
 * comment fix
310
 *
311
 * Revision 1.15  1999/04/22 11:47:33  fw
312
 * added xxxT & xxxM Support
313
 *
314
 * Revision 1.14  1999/04/15 15:50:09  fw
315
 * using Formaters
316
 *
317
 * Revision 1.13  1999/04/09 15:36:27  fw
318
 * extended Formater-Support
319
 *
320
 * Revision 1.12  1998/12/08 18:26:10  fw
321
 * *Field: cleanup; autoFormat-, keepFocus-, limited-Charset-Support for all Fields (enabled by default)
322
 * ViewLayout: Litle change due to changes at the *Fields
323
 * DBListRow: toString()-Method for BigDecimals exchanging the '.' by ','
324
 *
325
 * Revision 1.11  1998/12/02 11:27:43  js
326
 * Modifications for changes in IoSText (special character sets).
327
 *
328
 * Revision 1.10  1998/11/30 12:01:26  bw
329
 * Support for DecimalFormat added (automatic formating by java.text.DecimalFormat).
330
 * Not tested!!!!
331
 *
332
 * Revision 1.9  1998/06/30 12:18:30  bw
333
 * Bugfix (keyTyped now set Caret-Position and can handle empty fields).
334
 *
335
 * Revision 1.8  1998/05/13 11:55:38  fw
336
 * Added new basic TextField-Component IoSTextField (ExtendedTextField
337
 * with JDK 1.1 Event-Handling), replaced the Base-Class ExtendedTextField
338
 * of all other 'Fields' by IoSTextField
339
 * ExtendedTextField uses now the JDK 1.0 Event-Handling again (due to massive
340
 * Problems with blocked 1.0-Event-Handling at older Projects) - from now
341
 * on the complete Class is DEPRECATED.
342
 *
343
 * Revision 1.7  1998/04/28 13:21:41  js
344
 * bugfix in constructor
345
 *
346
 * Revision 1.6  1998/04/28 12:56:26  bw
347
 * Automativ supression of non-digits added.
348
 *
349
 * Revision 1.5  1998/04/08 11:58:49  fw
350
 * bugfix / extension
351
 *
352
 * Revision 1.4  1998/04/08 09:01:22  fw
353
 * bugfix: Standard-Default-Value is null
354
 *
355
 * Revision 1.3  1998/04/07 11:44:48  fw
356
 * *** empty log message ***
357
 *
358
 * Revision 1.2  1998/03/18 01:15:24  fw
359
 * added some Methods
360
 *
361
 * Revision 1.1  1998/03/15 15:40:09  mh
362
 * added new fields DateField, DecimalField and NumberField
363
 *
364
 */
365