KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nqadmin > swingSet > SSTextField


1 /* $Id: SSTextField.java,v 1.22 2005/02/21 16:31:33 prasanth Exp $
2  *
3  * Tab Spacing = 4
4  *
5  * Copyright (c) 2003-2005, The Pangburn Company and Prasanth R. Pasala
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice, this
12  * list of conditions and the following disclaimer. Redistributions in binary
13  * form must reproduce the above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or other materials
15  * provided with the distribution. The names of its contributors may not be
16  * used to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  */

32
33 package com.nqadmin.swingSet;
34
35 import javax.swing.*;
36 import java.awt.event.*;
37 import java.awt.*;
38 import java.io.ObjectInputStream JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.util.StringTokenizer JavaDoc;
41 import com.nqadmin.swingSet.datasources.SSRowSet;
42
43 /**
44  * SSTextField.java
45  *<p>
46  * SwingSet - Open Toolkit For Making Swing Controls Database-Aware
47  *<p><pre>
48  * SSTextField extends the JTextField. This class provides different masks
49  * like date mask, SSN mask etc.
50  *</pre><p>
51  * @author $Author: prasanth $
52  * @version $Revision: 1.22 $
53  */

54 public class SSTextField extends JTextField {
55
56     /**
57      * Use this mask if mm/dd/yyyy format is required.
58      */

59     public static final int MMDDYYYY = 1;
60
61     /**
62      * Use this mask if mm/dd/yyyy format is required.
63      */

64     public static final int DDMMYYYY = 2;
65
66     /**
67      * Use this if the text field contains SSN
68      */

69     public static final int SSN = 3;
70
71     /**
72      * Use this if the text field contains decimal number and want to limit
73      * number of decimal places.
74      */

75     public static final int DECIMAL = 4;
76
77     /**
78      * SSRowSet from which component will get/set values.
79      */

80     protected SSRowSet sSRowSet;
81
82     /**
83      * SSRowSet column to which the component will be bound.
84      */

85     protected String JavaDoc columnName = "";
86
87     /**
88      * Type of mask to be used for text field (default = none).
89      */

90     protected int mask = 0;
91
92     /**
93      * Default number of decimals to show for float, double, etc.
94      */

95     protected int numberOfDecimalPlaces = 2;
96
97     /**
98      * Constructs a new text field with the specified text & mask.
99      *
100      * @param _text the text to be displayed.
101      * @param _mask the mask required for this textfield.
102      */

103     public SSTextField(String JavaDoc _text, int _mask) {
104         super(_text);
105         mask = _mask;
106         init();
107     }
108
109     /**
110      * Constructs a new, empty text field with the specified mask.
111      *
112      * @param _mask the mask required for this textfield.
113      */

114     public SSTextField(int _mask) {
115         super();
116         mask = _mask;
117         init();
118     }
119
120     /**
121      * Constructs a new, empty text field.
122      */

123     public SSTextField() {
124         init();
125     }
126
127     /**
128      * Constructs a new, empty text field with the specified mask & number of
129      * decimal places. Use this constructor only if you are using a decimal mask.
130      *
131      * @param _mask the mask required for this textfield.
132      * @param _numberOfDecimalPlaces number of decimal places required
133      */

134      public SSTextField(int _mask, int _numberOfDecimalPlaces) {
135         mask = _mask;
136         numberOfDecimalPlaces = _numberOfDecimalPlaces;
137         init();
138      }
139
140     /**
141      * Constructs a new, empty text field with the specified mask, number of
142      * decimal places, & alignment. Use this constructor only if you are using
143      * a decimal mask.
144      *<pre>
145      * (Horizontal alignment).
146      * Valid aligmnets are:
147      * JTextField.LEFT
148      * JTextField.CENTER
149      * JTextField.RIGHT
150      * JTextField.LEADING
151      * JTextField.TRAILING
152      *
153      * Use this constructor only if you are using a decimal mask.
154      *</pre>
155      * @param _mask the mask required for this text field.
156      * @param _numberOfDecimalPlaces number of decimal places required
157      * @param _align alignment required
158      */

159      public SSTextField(int _mask, int _numberOfDecimalPlaces, int _align) {
160         mask = _mask;
161         numberOfDecimalPlaces = _numberOfDecimalPlaces;
162         setHorizontalAlignment(_align);
163         init();
164      }
165
166     /**
167      * Creates a SSTextField instance and binds it to the specified
168      * SSRowSet column.
169      *
170      * @param _sSRowSet datasource to be used.
171      * @param _columnName name of the column to which this label should be bound
172      */

173     public SSTextField(SSRowSet _sSRowSet, String JavaDoc _columnName) {
174         sSRowSet = _sSRowSet;
175         columnName = _columnName;
176         init();
177         bind();
178     }
179
180     /**
181      * Sets the SSRowSet column name to which the component is bound.
182      *
183      * @param _columnName column name in the SSRowSet to which the component
184      * is bound
185      */

186     public void setColumnName(String JavaDoc _columnName) {
187         String JavaDoc oldValue = columnName;
188         columnName = _columnName;
189         firePropertyChange("columnName", oldValue, columnName);
190         bind();
191     }
192
193     /**
194      * Returns the SSRowSet column name to which the component is bound.
195      *
196      * @return column name to which the component is bound
197      */

198     public String JavaDoc getColumnName() {
199         return columnName;
200     }
201
202     /**
203      * Sets the SSRowSet to which the component is bound.
204      *
205      * @param _sSRowSet SSRowSet to which the component is bound
206      */

207     public void setSSRowSet(SSRowSet _sSRowSet) {
208         SSRowSet oldValue = sSRowSet;
209         sSRowSet = _sSRowSet;
210         firePropertyChange("sSRowSet", oldValue, sSRowSet);
211         bind();
212     }
213
214     /**
215      * Returns the SSRowSet to which the component is bound.
216      *
217      * @return SSRowSet to which the component is bound
218      */

219     public SSRowSet getSSRowSet() {
220         return sSRowSet;
221     }
222
223     /**
224      * Sets the text field mask.
225      *
226      * @param _mask the mask required for this text field.
227      */

228     public void setMask(int _mask) {
229         int oldValue = mask;
230         mask = _mask;
231         firePropertyChange("mask", oldValue, mask);
232
233         //init();
234
}
235
236     /**
237      * Returns the text field mask.
238      *
239      * @return editing mask for text field
240      */

241     public int getMask() {
242         return mask;
243     }
244
245     /**
246      * Sets the number of decimal places required.
247      * This number is used only when mask is set to DECIMAL.
248      * Default value is 2.
249      *
250      * @param _numberOfDecimalPlaces desired # of decimal places
251      */

252     public void setNumberOfDecimalPlaces(int _numberOfDecimalPlaces) {
253         int oldValue = numberOfDecimalPlaces;
254         numberOfDecimalPlaces = _numberOfDecimalPlaces;
255         firePropertyChange("numberOfDecimalPlaces", oldValue, numberOfDecimalPlaces);
256     }
257
258     /**
259      * Returns the number of decimal places required.
260      * This number is used only when mask is set to DECIMAL.
261      * Default value is 2.
262      *
263      * @return desired # of decimal places
264      */

265     public int getNumberOfDecimalPlaces() {
266         return numberOfDecimalPlaces;
267     }
268
269     /**
270      * Sets the SSRowSet and column name to which the component is to be bound.
271      *
272      * @param _sSRowSet datasource to be used.
273      * @param _columnName Name of the column to which this check box should be bound
274      */

275      public void bind(SSRowSet _sSRowSet, String JavaDoc _columnName) {
276         SSRowSet oldValue = sSRowSet;
277         sSRowSet = _sSRowSet;
278         firePropertyChange("sSRowSet", oldValue, sSRowSet);
279
280         String JavaDoc oldValue2 = columnName;
281         columnName = _columnName;
282         firePropertyChange("columnName", oldValue2, columnName);
283
284         bind();
285      }
286
287     /**
288      * Initialization code.
289      */

290     protected void init() {
291
292         // SET PREFERRED DIMENSIONS
293
setPreferredSize(new Dimension(200,20));
294
295          // ADD FOCUS LISTENER TO THE TEXT FEILD SO THAT WHEN THE FOCUS IS GAINED
296
// COMPLETE TEXT SHOULD BE SELECTED
297
this.addFocusListener(new FocusAdapter(){
298                 public void focusGained(FocusEvent fe){
299                     SSTextField.this.selectAll();
300                 }
301             });
302
303          // ADD KEY LISTENER FOR THE TEXT FIELD
304
this.addKeyListener(new KeyListener() {
305
306                 public void keyReleased(KeyEvent ke) {
307                     if(mask == DECIMAL || mask == SSN){
308                         int position = SSTextField.this.getCaretPosition();
309                         mask(ke);
310                         SSTextField.this.setCaretPosition(position);
311                     }
312                 }
313
314                 public void keyTyped(KeyEvent ke) {
315                 }
316
317                 public synchronized void keyPressed(KeyEvent ke) {
318                 // TRANSFER FOCUS TO NEXT COMPONENT WHEN ENTER KEY IS PRESSED
319
if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
320                         ((Component)ke.getSource()).transferFocus();
321                     }
322
323                     if(mask == MMDDYYYY || mask == DDMMYYYY){
324                         mask(ke);
325                     }
326                 }
327
328             });
329
330     } // end protected void init() {
331

332     /**
333      * Method for handling binding of component to a SSRowSet column.
334      */

335     protected void bind() {
336
337         // CHECK FOR NULL COLUMN/ROWSET
338
if (columnName==null || columnName.trim().equals("") || sSRowSet==null) {
339                 return;
340             }
341
342         // REMOVE LISTENERS TO PREVENT DUPLICATION
343
// removeListeners();
344

345         // BIND THE TEXT AREA TO THE SPECIFIED COLUMN
346
setDocument(new SSTextDocument(sSRowSet, columnName));
347
348         // ADD BACK LISTENERS
349
// addListeners();;
350

351     }
352
353     /**
354      * Function to manage keystrokes for masks.
355      *
356      * @param _ke the KeyEvent that occured
357      */

358     protected void mask(KeyEvent _ke) {
359          // DECLARATIONS
360
String JavaDoc str = getText();
361             char ch = _ke.getKeyChar();
362
363          // IF THE KEY PRESSED IS ANY OF THE FOLLOWING DO NOTHING
364
if (_ke.getKeyCode() == KeyEvent.VK_BACK_SPACE ||
365                     _ke.getKeyCode() == KeyEvent.VK_DELETE ||
366                     _ke.getKeyCode() == KeyEvent.VK_END ||
367                     _ke.getKeyCode() == KeyEvent.VK_ENTER ||
368                     _ke.getKeyCode() == KeyEvent.VK_ESCAPE) {
369
370                 return;
371             } else if ( (_ke.getModifiersEx() & KeyEvent.CTRL_DOWN_MASK) == KeyEvent.CTRL_DOWN_MASK ||
372                      (_ke.getModifiersEx() & KeyEvent.ALT_DOWN_MASK) == KeyEvent.ALT_DOWN_MASK ) {
373
374                 return;
375             } else if(!Character.isDefined(ch)) {
376                 return;
377             }
378
379             if (getSelectionStart() != getSelectionEnd()) {
380                 str = str.substring(0,getSelectionStart())
381                     + str.substring(getSelectionEnd(), str.length());
382             }
383
384          // BASED ON TYPE OF MASK REQUESTED MODIFY THE TEXT
385
// ACCORDINGLY
386
switch(mask) {
387                 case MMDDYYYY:
388                 case DDMMYYYY:
389                     if (getCaretPosition() < str.length()) {
390                         return;
391                     }
392                     setText(dateMask(str, _ke));
393                     break;
394                 case SSN:
395                     setText(ssnMask(str, _ke));
396                     break;
397                 case DECIMAL:
398                     setText(decimalMask(str, numberOfDecimalPlaces));
399                     break;
400             } // end switch
401

402      } // end protected void mask(KeyEvent _ke) {
403

404     /**
405      * Function to manage formatting date _strings with slashes as the user types
406      * to format date _string.
407      *
408      * @param _str the present string in the text field.
409      * @param _ke the KeyEvent that occured
410      *
411      * @return returns the formated string.
412      */

413     protected String JavaDoc dateMask(String JavaDoc _str, KeyEvent _ke) {
414         switch(_str.length()) {
415             case 1:
416                 if (_ke.getKeyChar() == '/') {
417                     _str = "0" + _str ;
418                 }
419                 break;
420             case 2:
421                 if ( _ke.getKeyChar() == '/' ) {
422                     // do nothing
423
} else {
424                     _str = _str + "/";
425                 }
426                 break;
427             case 4:
428                 if ( _ke.getKeyChar() == '/' ){
429                     String JavaDoc newStr = _str.substring(0,3);
430                     newStr = newStr + "0" + _str.substring(3,4);
431                     _str = newStr;
432                 }
433                 break;
434             case 5:
435                 if ( _ke.getKeyChar() != '/' ) {
436                     _str = _str + "/";
437                 }
438                 break;
439         } // end switch
440

441         return _str;
442
443     } // end protected String dateMask(String _str, KeyEvent _ke) {
444

445     /**
446      * Function to format SSN
447      *
448      * @param _str the present string in the text field.
449      * @param _ke the KeyEvent that occured
450      *
451      * @return returns the formated string.
452      */

453     protected String JavaDoc ssnMask(String JavaDoc _str, KeyEvent _ke) {
454         switch(_str.length()) {
455             case 3:
456             case 6:
457                 _str = _str + "-";
458                 break;
459             case 5:
460             case 8:
461                 if (_ke.getKeyChar() == '-') {
462                     _str = _str.substring(0,_str.length()-1);
463                 }
464                 break;
465         }
466
467         return _str;
468
469     }
470
471     /**
472      * Function to modify the text for a decimal number as needed.
473      *
474      * @param _str the present string in the text field.
475      * @param numberOfDecimalPlaces number of decimal places allowed
476      *
477      * @return returns the formatted string.
478      */

479     protected String JavaDoc decimalMask(String JavaDoc _str, int numberOfDecimalPlaces) {
480         StringTokenizer JavaDoc strtok = new StringTokenizer JavaDoc(_str,".",false);
481         String JavaDoc intPart = "";
482         String JavaDoc decimalPart = "";
483         String JavaDoc returnStr = _str;
484         // BREAK THE STRING IN TO INTERGER AND DECIMAL PARTS
485
if (strtok.hasMoreTokens()) {
486             intPart = strtok.nextToken();
487         }
488         if (strtok.hasMoreTokens()) {
489             decimalPart = strtok.nextToken();
490         }
491         // IF THE DECIMAL PART IS MORE THAN SPECIFIED
492
// TRUNCATE THE EXTRA DECIMAL PLACES
493
if ( decimalPart.length() > numberOfDecimalPlaces ) {
494             returnStr = intPart +"."+ decimalPart.substring(0,numberOfDecimalPlaces);
495         }
496
497         return returnStr;
498     }
499
500 } // end public class SSTextField extends JTextField {
501

502
503
504 /*
505  * $Log: SSTextField.java,v $
506  * Revision 1.22 2005/02/21 16:31:33 prasanth
507  * In bind checking for empty columnName before binding the component.
508  *
509  * Revision 1.21 2005/02/13 15:40:15 yoda2
510  * Removed redundant PropertyChangeListener and VetoableChangeListener class variables and methods from components with JComponent as an ancestor. Also removed call to init() from setMask() method.
511  *
512  * Revision 1.20 2005/02/12 03:29:26 yoda2
513  * Added bound properties (for beans).
514  *
515  * Revision 1.19 2005/02/11 22:59:46 yoda2
516  * Imported PropertyVetoException and added some bound properties.
517  *
518  * Revision 1.18 2005/02/11 20:16:06 yoda2
519  * Added infrastructure to support property & vetoable change listeners (for beans).
520  *
521  * Revision 1.17 2005/02/10 20:13:04 yoda2
522  * Setter/getter cleanup & method reordering for consistency.
523  *
524  * Revision 1.16 2005/02/09 19:46:32 yoda2
525  * JavaDoc cleanup.
526  *
527  * Revision 1.15 2005/02/05 05:16:33 yoda2
528  * API cleanup.
529  *
530  * Revision 1.14 2005/02/04 23:05:10 yoda2
531  * no message
532  *
533  * Revision 1.13 2005/02/04 22:48:54 yoda2
534  * API cleanup & updated Copyright info.
535  *
536  * Revision 1.12 2004/11/11 14:45:48 yoda2
537  * Using TextPad, converted all tabs to "soft" tabs comprised of four actual spaces.
538  *
539  * Revision 1.11 2004/10/25 22:13:43 yoda2
540  * Updated JavaDoc for new datasource abstraction layer in 0.9.0 release.
541  *
542  * Revision 1.10 2004/10/25 19:51:03 prasanth
543  * Modified to use the new SSRowSet instead of RowSet.
544  *
545  * Revision 1.9 2004/10/19 21:17:03 prasanth
546  * Transfering focus on enter key. It was doing so only if a mask was applied
547  * to the text field.
548  *
549  * Revision 1.8 2004/10/07 14:35:27 prasanth
550  * Updated the way the masks work.
551  *
552  * Revision 1.7 2004/09/13 15:42:15 prasanth
553  * Changed the default mask to non.
554  * It used to be MMDDYYYY.
555  *
556  * Revision 1.6 2004/08/10 22:06:59 yoda2
557  * Added/edited JavaDoc, made code layout more uniform across classes, made various small coding improvements suggested by PMD.
558  *
559  * Revision 1.5 2004/08/09 15:34:40 prasanth
560  * 1. Added bind function.
561  * 2. In the key listener transferring focus on enter key.
562  *
563  * Revision 1.4 2004/08/02 15:48:09 prasanth
564  * 1. Added the readObject method.
565  *
566  * Revision 1.3 2004/03/08 16:43:37 prasanth
567  * Updated copy right year.
568  *
569  * Revision 1.2 2004/02/23 16:45:51 prasanth
570  * Added new constructor
571  * public SSTextField(int _mask, int _numberOfDecimalPlaces, int _align)
572  *
573  * Revision 1.1 2003/12/16 18:02:47 prasanth
574  * Initial version.
575  *
576  */
Popular Tags