import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * A frame that illustrates the use of checkboxes
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class CheckBoxHandler implements ActionListener, ItemListener
{
    private JTextArea displayArea;
    

    /**
     * Explicit Value Constructor.
     */
    public CheckBoxHandler(JTextArea displayArea)
    {
        this.displayArea = displayArea;
    }


    /**
     * Handle actionPerformed events (required by ActionListener)
     *
     * @param e   The event
     */
    public void actionPerformed(ActionEvent e)
    {
       // You could handle events generated by the
       // JCheckBox objects here
    }


    /**
     * Handle itemStateChanged events (required by ItemListener)
     *
     * @param e   The event
     */
    public void itemStateChanged(ItemEvent e)
    {
       int             state;
       JToggleButton   tb;


       tb = (JToggleButton)(e.getItem());
       state = e.getStateChange();

       if (tb.getActionCommand().equals("planesCB")) 
       {
          if (state == ItemEvent.SELECTED) 
          {
             displayArea.append("If you were meant to fly "+
                             "you'd have wings\n\n");
          }
          else if (state == ItemEvent.DESELECTED) 
          {
             displayArea.append("Afraid to fly???\n\n");
          }
       } 
       else if (tb.getActionCommand().equals("trainsCB")) 
       {
          if (state == ItemEvent.SELECTED) 
          {
             displayArea.append("Choo, Choo...\n\n");
          } 
          else if (state == ItemEvent.DESELECTED) 
          {
             displayArea.append("The little engine that couldn't!\n\n");
          }
       } 
       else if (tb.getActionCommand().equals("automobilesCB")) 
       {
          if (state == ItemEvent.SELECTED) 
          {
             displayArea.append("I drove a Geo Prizm in 1995!\n\n");
          } 
          else if (state == ItemEvent.DESELECTED) 
          {
             displayArea.append("I drive the same one today!\n\n");
          }
       }
    }
}
