// Fig. 11.21: ValidateFrame.java
// Validate user information using regular expressions.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ValidateFrame extends JFrame {
   private JTextField phoneTextField, zipTextField, stateTextField,
      cityTextField, addressTextField, firstTextField, lastTextField;

   public ValidateFrame()
   {
      super( "Validate" );
        
      // create the GUI components
      JLabel phoneLabel = new JLabel( "Phone" );
      JLabel zipLabel = new JLabel( "Zip" );
      JLabel stateLabel = new JLabel( "State" );
      JLabel cityLabel = new JLabel( "City" );
      JLabel addressLabel = new JLabel( "Address" );
      JLabel firstLabel = new JLabel( "First Name" );
      JLabel lastLabel = new JLabel( "Last Name" );
         
      JButton okButton = new JButton( "OK" );
      okButton.addActionListener( 
            
         new ActionListener() { // inner class

            public void actionPerformed( ActionEvent event ) {
               validateDate();
            }
               
         } // end inner class
         
      ); // end call to addActionListener
         
      phoneTextField = new JTextField( 15 );
      zipTextField = new JTextField( 5 );
      stateTextField = new JTextField( 2 );
      cityTextField = new JTextField( 12 );
      addressTextField = new JTextField( 20 );
      firstTextField = new JTextField( 20 );
      lastTextField = new JTextField( 20 );
      
      JPanel firstName = new JPanel();
      firstName.add( firstLabel );
      firstName.add( firstTextField );
      
      JPanel lastName = new JPanel();
      lastName.add( lastLabel );
      lastName.add( lastTextField );
      
      JPanel address1 = new JPanel();
      address1.add( addressLabel );
      address1.add( addressTextField );
      
      JPanel address2 = new JPanel();
      address2.add( cityLabel );
      address2.add( cityTextField );
      address2.add( stateLabel );
      address2.add( stateTextField );
      address2.add( zipLabel );
      address2.add( zipTextField );
      
      JPanel phone = new JPanel();
      phone.add( phoneLabel );
      phone.add( phoneTextField );
      
      JPanel ok = new JPanel();
      ok.add( okButton );
      
      // add the components to the application
      Container container = getContentPane();
      container.setLayout( new GridLayout( 6, 1 ) );

      container.add( firstName );
      container.add( lastName );
      container.add( address1 );
      container.add( address2 );
      container.add( phone );
      container.add( ok );
         
      setSize( 325, 225 );
      setVisible( true );

   } // end ValidateFrame constructor

   public static void main( String args[] ) 
   {
      ValidateFrame application = new ValidateFrame();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   }

   // handles okButton action event
   private void validateDate()
   {
      // ensure that no textboxes are empty
      if ( lastTextField.getText().equals( "" ) || 
           firstTextField.getText().equals( "" ) ||
           addressTextField.getText().equals( "" ) || 
           cityTextField.getText().equals( "" ) ||
           stateTextField.getText().equals( "" ) || 
           zipTextField.getText().equals( "" ) ||
           phoneTextField.getText().equals( "" ) ) // end condition
            
         JOptionPane.showMessageDialog( this, "Please fill all fields" );

      // if first name format invalid show message
      else if ( !firstTextField.getText().matches( "[A-Z][a-zA-Z]*" ) )
         JOptionPane.showMessageDialog( this, "Invalid first name" ); 

      // if last name format invalid show message
      else if ( !lastTextField.getText().matches( "[A-Z][a-zA-Z]*" ) )
         JOptionPane.showMessageDialog( this, "Invalid last name" ); 

      // if address format invalid show message
      else if ( !addressTextField.getText().matches( 
                "\\d+\\s+([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)" ) )
         JOptionPane.showMessageDialog( this, "Invalid address" );

      // if city format invalid show message
      else if ( !cityTextField.getText().matches( 
                "([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)" ) )
         JOptionPane.showMessageDialog( this, "Invalid city" ); 

      // if state format invalid show message
      else if ( !stateTextField.getText().matches( 
                "([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)" ) )
         JOptionPane.showMessageDialog( this, "Invalid state" ); 

      // if zip code format invalid show message
      else if ( !zipTextField.getText().matches( "\\d{5}" ) )
         JOptionPane.showMessageDialog( this, "Invalid zip code" ); 

      // if phone number format invalid show message
      else if ( !phoneTextField.getText().matches( 
                 "[1-9]\\d{2}-[1-9]\\d{2}-\\d{4}" ) )
         JOptionPane.showMessageDialog( this, "Invalid phone number" ); 
      
      else // information is valid, signal user
         JOptionPane.showMessageDialog( this, "Thank you" ); 

   } // end method validateDate

} // end class ValidateFrame

/*
 **************************************************************************
 * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and               *
 * Prentice Hall. All Rights Reserved.                                    *
 *                                                                        *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 **************************************************************************
*/

