// Fig. 10.22: TimeTestWindow.java
// Inner class declarations used to create event handlers.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TimeTestWindow extends JFrame {
   private Time time;
   private JLabel hourLabel, minuteLabel, secondLabel;
   private JTextField hourField, minuteField, secondField, displayField;
   private JButton exitButton;

   // set up GUI
   public TimeTestWindow()
   {
      // call JFrame constructor to set title bar string
      super( "Inner Class Demonstration" );  

      time = new Time();  // create Time object

      // use inherited method getContentPane to get window's content pane
      Container container = getContentPane();  
      container.setLayout( new FlowLayout() );  // change layout

      // set up hourLabel and hourField
      hourLabel = new JLabel( "Set Hour" );
      hourField = new JTextField( 10 );
      container.add( hourLabel );
      container.add( hourField );

      // set up minuteLabel and minuteField
      minuteLabel = new JLabel( "Set Minute" );
      minuteField = new JTextField( 10 );
      container.add( minuteLabel );
      container.add( minuteField );

      // set up secondLabel and secondField
      secondLabel = new JLabel( "Set Second" );
      secondField = new JTextField( 10 );
      container.add( secondLabel );
      container.add( secondField );

      // set up displayField
      displayField = new JTextField( 30 );
      displayField.setEditable( false );
      container.add( displayField );

      // set up exitButton
      exitButton = new JButton( "Exit" );
      container.add( exitButton );

      // create an instance of inner class ActionEventHandler
      ActionEventHandler handler = new ActionEventHandler();

      // register event handlers; the object referenced by handler 
      // is the ActionListener, which contains method actionPerformed 
      // that will be called to handle action events generated by 
      // hourField, minuteField, secondField and exitButton
      hourField.addActionListener( handler ); 
      minuteField.addActionListener( handler );
      secondField.addActionListener( handler );
      exitButton.addActionListener( handler );

   } // end constructor

   // display time in displayField
   public void displayTime()
   {
      displayField.setText( "The time is: " + time );
   }

   // launch application: create, size and display TimeTestWindow; 
   // when main terminates, program continues execution because a 
   // window is displayed by the statements in main
   public static void main( String args[] )
   {
      TimeTestWindow window = new TimeTestWindow();

      window.setSize( 400, 140 );
      window.setVisible( true );

   } // end main

   // inner class declaration for handling JTextField and JButton events
   private class ActionEventHandler implements ActionListener {

      // method to handle action events 
      public void actionPerformed( ActionEvent event )
      {
         // user pressed exitButton
         if ( event.getSource() == exitButton )
            System.exit( 0 );   // terminate the application

         // user pressed Enter key in hourField
         else if ( event.getSource() == hourField ) {
            time.setHour( Integer.parseInt( 
               event.getActionCommand() ) );
            hourField.setText( "" );
         }

         // user pressed Enter key in minuteField
         else if ( event.getSource() == minuteField ) {
            time.setMinute( Integer.parseInt( 
               event.getActionCommand() ) );
            minuteField.setText( "" );
         }

         // user pressed Enter key in secondField
         else if ( event.getSource() == secondField ) {
            time.setSecond( Integer.parseInt( 
               event.getActionCommand() ) );
            secondField.setText( "" );
         }

         displayTime();  // call outer class's method

      } // end method actionPerformed

   } // end inner class ActionEventHandler

} // end class TimeTestWindow


/**************************************************************************
 * (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.                     *
 *************************************************************************/
