// Fig 8.18 : DayPlanner.java
// Program for GUI interface for the day planner application.

import java.awt.*;                  
import java.awt.event.*;            
import javax.swing.*;               
import javax.swing.event.*;

public class DayPlanner extends JFrame
   implements ActionListener {

   // GUI components
   private JTextArea display;
   private JComboBox year, month, day, time;
   private JButton query;
   private JPanel panel1, panel2;
   private DOMPlanner handler;

   public DayPlanner()
   {
      super( "Day planner using DOM" );

      // set the output font      
      Font font = new Font( "Monospaced",
                            java.awt.Font.BOLD, 16 );
      display = new JTextArea();
      display.setFont( font );
      display.setEditable( false );
      
      handler = new DOMPlanner( display );

      // initialize the user interface components    
      year = new JComboBox( handler.getYears() );

      String months[] = new String[ 13 ];
      months[ 0 ] = "ANY";

      for ( int i = 1; i < 13; i++ )
         months[ i ] = Integer.toString( i );

      month = new JComboBox( months );

      String days[] = new String[ 32 ];
      days[ 0 ] = "ANY";

      for ( int i = 1; i < 32; i++ )
         days[ i ] = Integer.toString( i );

      day = new JComboBox( days );

      String times[] = { "ANY", "Morning", "Afternoon",
                         "Evening", "Night" };
      time = new JComboBox( times );

      query = new JButton( "Get Schedules" );
      query.addActionListener( this );

      // panel containing components for querying
      panel1 = new JPanel();
      panel1.setLayout( new GridLayout( 4, 2 ) );
      panel1.add( new JLabel( "Year" ) ); 
      panel1.add( year );
      panel1.add( new JLabel( "Month" ) ); 
      panel1.add( month );
      panel1.add( new JLabel( "Day" ) ); 
      panel1.add( day );
      panel1.add( new JLabel("Time") ); 
      panel1.add( time );

      // panel containing text area for output
      // and panel2 containing other GUI components.
      panel2 = new JPanel();
      panel2.setLayout( new GridLayout( 1, 2 ) );
      panel2.add( panel1 );      
      panel2.add( query );      

      Container c = getContentPane();
      c.setLayout( new BorderLayout() ); 
      c.add( new JScrollPane( display ), BorderLayout.CENTER );
      c.add( panel2, BorderLayout.SOUTH );
      setSize( 600, 450 );
      show();
   }

   // method executed when query button is pressed
   public void actionPerformed( ActionEvent e )
   {
      if ( e.getSource() == query ) {
         int yearIndex, monthIndex, dayIndex, timeIndex;

         // get the integer values of all the query parameters
         yearIndex =
            getIntegerValue( ( String ) year.getSelectedItem() );
         monthIndex =
            getIntegerValue( ( String ) month.getSelectedItem() );
         dayIndex =
            getIntegerValue( ( String ) day.getSelectedItem() );
         timeIndex = time.getSelectedIndex() - 1;

         // get the result of query
         handler.getQueryResult( yearIndex, monthIndex,
                                 dayIndex, timeIndex );
      }
   }

   // method to convert the string value to integer
   public int getIntegerValue( String str )
   {
      // if the value 'ANY' is selected, return -1
      if ( str.equals( "ANY" ) )
         return -1;
      else
         return Integer.parseInt( str );
   }

   public static void main( String s[] )
   {
      DayPlanner d = new DayPlanner();

      d.addWindowListener(
         new WindowAdapter()
         {
            public void windowClosing( WindowEvent e )
            {
               System.exit( 0 );
            }
         }
      );
   }
}
/*
 **************************************************************************
 * (C) Copyright 2001 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.                     *
 **************************************************************************
*/