001         package com.croftsoft.core.gui;
002    
003         import java.awt.*;
004         import java.awt.event.*;
005    
006         import com.croftsoft.core.lang.NullArgumentException;
007    
008         /*********************************************************************
009         * Used to reset a text field upon key press after user input error.
010         *
011         * <p>
012         * Example:
013         * <pre>
014         * String  text = textField.getText ( );
015         *
016         * if ( "".equals ( text.trim ( ) ) )
017         * {
018         *   textField.setBackground ( Color.red );
019         *
020         *   urlTextField.setText ( "data required" );
021         *
022         *   textField.addKeyListener (
023         *     new ResetTextFieldKeyListener ( textField, Color.white ) );
024         * }
025         * </pre>
026         * </p>
027         *
028         * <p>
029         * Upon key press detection, ResetTextFieldKeyListener will
030         * <ol>
031         * <li> remove itself as a KeyListener from the TextField,
032         * <li> set the TextField background to backgroundColor, and
033         * <li> set the TextField to defaultText.
034         * </ol>
035         * </p>
036         *
037         * @version
038         *   2001-03-23
039         * @since
040         *   2001-03-23
041         * @author
042         *   <A HREF="http://www.alumni.caltech.edu/~croft">David W. Croft</A>
043         *********************************************************************/
044    
045         public final class  ResetTextFieldKeyListener
046           extends KeyAdapter
047         //////////////////////////////////////////////////////////////////////
048         //////////////////////////////////////////////////////////////////////
049         {
050    
051         private TextField  textField;
052    
053         private Color      backgroundColor;
054    
055         private String     defaultText;
056    
057         //////////////////////////////////////////////////////////////////////
058         //////////////////////////////////////////////////////////////////////
059    
060         public  ResetTextFieldKeyListener (
061           TextField  textField,
062           Color      backgroundColor,
063           String     defaultText )
064         //////////////////////////////////////////////////////////////////////
065         {
066           NullArgumentException.check ( this.textField = textField );
067    
068           NullArgumentException.check (
069             this.backgroundColor = backgroundColor );
070    
071           NullArgumentException.check ( this.defaultText = defaultText );
072         }
073    
074         /*********************************************************************
075         * this ( textField, backgroundColor, "" );
076         *********************************************************************/
077         public  ResetTextFieldKeyListener (
078           TextField  textField,
079           Color      backgroundColor )
080         //////////////////////////////////////////////////////////////////////
081         {
082           this ( textField, backgroundColor, "" );
083         }
084    
085         //////////////////////////////////////////////////////////////////////
086         //////////////////////////////////////////////////////////////////////
087    
088         public void  keyPressed ( KeyEvent  keyEvent )
089         //////////////////////////////////////////////////////////////////////
090         {
091           textField.removeKeyListener ( this );
092    
093           textField.setBackground ( backgroundColor );
094    
095           textField.setText ( defaultText );
096         }
097    
098         //////////////////////////////////////////////////////////////////////
099         //////////////////////////////////////////////////////////////////////
100         }