// Written by Dr. Deborah Hwang for CS 350
// Pizza Parlor Order Form Java application

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

public class PizzaApp1 extends JPanel // application is a JPanel
{  

   // The components that are named for entire application
   JFrame frame;   // outer frame
   JTextField nameField;
   JTextArea addressField;
   JTextArea debugArea;
   JRadioButton[] sizeButtons;
   JCheckBox[] toppingsBoxes;
   final int numButtons = 4;
   final int numBoxes = 6;

   public PizzaApp1 (JFrame frame) // constructor
   {
      // initialize layout manager using superclass constructor
      // super class construction must always be first
      super (new BorderLayout());

      this.frame = frame;

      // Create the components

      // Create title label with HTML decorated text
      JLabel title = new JLabel ("<html><font size=+2 color=\"purple\">" +
                                  "Purple Pizza Parlor</font></html>", 
                                  JLabel.CENTER);
      // Pad the title on top and bottom
      title.setBorder(BorderFactory.createEmptyBorder(10,0,10,0));

      // Create other panels
      JPanel namePanel = createNamePanel();
      JPanel addressPanel = createAddressPanel();
      JPanel sizePanel = createSizePanel();
      JPanel toppingsPanel = createToppingsPanel();
      JPanel buttonPanel = createButtonPanel();

      // Create options panel with sizes and toppings
      JPanel optionsPanel = new JPanel();
      optionsPanel.add (sizePanel);
      optionsPanel.add (toppingsPanel);

      // Create center panel in a horizontal column
      JPanel centerPanel = new JPanel();
      centerPanel.setLayout (new BoxLayout (centerPanel, BoxLayout.PAGE_AXIS));
      centerPanel.add (namePanel);
      centerPanel.add (addressPanel);
      centerPanel.add (optionsPanel);
      centerPanel.add (buttonPanel);


      // Create the debug area at bottom with vertical scrollbar
      debugArea = new JTextArea ("The status of your order is:\n", 10, 40);
      JScrollPane debugScrollPane = new JScrollPane (
         debugArea, 
         JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
         JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
      debugArea.setEditable (false);

      // Lay out the main panel
      add (title, BorderLayout.PAGE_START);
      add (centerPanel, BorderLayout.CENTER);
      add (debugScrollPane, BorderLayout.PAGE_END);
   }  // end constructor

   private JPanel createNamePanel ()
   {
      JPanel panel = new JPanel ();
      nameField = new JTextField(20);

      panel.add (new JLabel ("Name: "));
      panel.add (nameField);
      return panel;
   }  // end createNamePanel

   private JPanel createAddressPanel ()
   {
      JPanel panel = new JPanel ();
      addressField = new JTextArea(3, 20);

      panel.add (new JLabel ("Address: "));
      panel.add (addressField);
      return panel;
   }  // end createAddressPanel

   private JPanel createSizePanel ()
   {
      JPanel panel = new JPanel(new GridLayout(0,1));

      final String[] sizes = {"Small", "Medium", "Large", "Extra Large"};
      ButtonGroup sizeButtonGroup = new ButtonGroup();  // create button group

      sizeButtons = new JRadioButton[numButtons];

      // Create an action listener
      ActionListener radioListener = 
         new ActionListener()  // anonymous inner class
         {
             public void actionPerformed (ActionEvent e)  // handler
             {
                String size = e.getActionCommand();
                debugArea.append (size + " has been chosen.\n");
                debugArea.setCaretPosition(
                   debugArea.getDocument().getLength());
             }  // end actionPerformed
         };  // end ActionListener

      for (int i = 0; i < numButtons; i++)
      {
         // Create radio button and add it to button group and panel
         sizeButtons[i] = new JRadioButton (sizes[i]);
         sizeButtonGroup.add (sizeButtons[i]);
         panel.add(sizeButtons[i]);

         // Set action command (for clicks)
         // and add the listener to each button
         sizeButtons[i].setActionCommand(sizes[i]);
         sizeButtons[i].addActionListener(radioListener);
      }  // end for

      // Make Large the default choice
      sizeButtons[2].setSelected(true);

      // Add compound border around panel
      panel.setBorder(BorderFactory.createCompoundBorder(
                         BorderFactory.createTitledBorder("Choose one:"),
                         BorderFactory.createEmptyBorder(5,5,5,5)));
      
      return panel;
   }  // end createSizePanel

   
   private JPanel createToppingsPanel()
   {
      JPanel panel = new JPanel (new GridLayout(0,1));

      final String[] toppings = {"Italian Sausage", "Hamburger", "Pepperoni",
                                 "Onions", "Green Peppers", "Mushrooms"};

      toppingsBoxes = new JCheckBox[numBoxes];

      // Create an item listener
      ItemListener boxListener = 
         new ItemListener ()  // anonymous inner class
         {
               public void itemStateChanged (ItemEvent e)  // handler
               {
                  for (int i = 0; i < numBoxes; i++)
                  {
                     Object source = e.getItemSelectable();
                     if (source == toppingsBoxes[i])
                     {
                        String text = toppings[i] + " was ";
                        if (e.getStateChange() == ItemEvent.DESELECTED)
                           text = text + "unchecked.\n";
                        else
                           text = text + "checked.\n";
                        debugArea.append (text);
                        debugArea.setCaretPosition(
                           debugArea.getDocument().getLength());
                        break;
                     }  // end if
                  }  // end for
               }  // end itemStateChanged
         }; // end ItemListener

      for (int i = 0; i < numBoxes; i++)
      {
         // Create checkbox and add it to panel
         toppingsBoxes[i] = new JCheckBox (toppings[i]);
         panel.add (toppingsBoxes[i]);
         
         // Add the listener to each box
         toppingsBoxes[i].addItemListener (boxListener);
      }  // end for

      // Add compound border around panel
      panel.setBorder(BorderFactory.createCompoundBorder(
                         BorderFactory.createTitledBorder("Choose from:"),
                         BorderFactory.createEmptyBorder(5,5,5,5)));
      
      return panel;
   }  // end createSizePanel

   private JPanel createButtonPanel()
   {
      JPanel panel = new JPanel();  // default layout is FlowLayout
      JButton submit = new JButton ("Submit");
      JButton reset = new JButton ("Reset");
      JButton quit = new JButton ("Quit");

      // Create an action listener
      ActionListener buttonListener =
         new ActionListener () // anonymous inner class
         {
               public void actionPerformed (ActionEvent e) // handler
               {
                  String command = e.getActionCommand();
                  if (command == "Submit")
                  {
                     System.out.println ("Submit button has been pressed");
                     debugArea.append ("We'll pretend that something " +
                                        "interesting has happened...\n");
                     debugArea.setCaretPosition(
                        debugArea.getDocument().getLength());
                  }  // end Submit
                  else if (command == "Reset")
                  {
                     System.out.println ("Reset button has been pressed");
                     nameField.setText ("");
                     addressField.setText ("");
                     for (int i = 0; i < numButtons; i++)
                        sizeButtons[i].setSelected(false);
                     sizeButtons[2].setSelected(true);
                     for (int i = 0; i < numBoxes; i++)
                        toppingsBoxes[i].setSelected(false);
                     debugArea.setText ("The status of your order is:\n");
                  }  // end Reset
                  else if (command == "Quit")
                  {
                     System.out.println ("Quit button has been pressed");
                     System.exit(0);
                  }  // end Quit
                  else
                     System.out.println ("Unknown command");
               }  // end actionPerformed
         };  // end ActionListener

      // Set action command and add listener to each button
      submit.setActionCommand("Submit");
      submit.addActionListener (buttonListener);

      reset.setActionCommand("Reset");
      reset.addActionListener (buttonListener);

      quit.setActionCommand("Quit");
      quit.addActionListener (buttonListener);

      // Set default button (when Enter is pressed)
      frame.getRootPane().setDefaultButton(submit);
      
      // Lay out panel
      panel.add (submit);
      panel.add (reset);
      panel.add (quit);

      return panel;
   }  // end createButtonPanel

    // Every Swing application will have these two functions

    // Create the GUI and show it.  For thread safety, this method should 
    // be invoked from the event-dispatching thread.
    private static void createAndShowGUI() 
    {
        // Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(true);
       
        // Create and set up the main window.
        JFrame frame = new JFrame("Purple Pizza Parlor Order Form");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

        // Set up the main frame panel, 
        // only the application itself is created and added
        frame.setLayout(new GridLayout(1,1));
        frame.add(new PizzaApp1 (frame));

        // Display the window.
        frame.pack();
        frame.setVisible(true);
    }  // createAndShowGUI

    public static void main(String[] args) 
    {
        // Schedule a job for the event-dispatching thread:
        // creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(
           new Runnable() // anonymous inner class
           {
                 public void run() // from Runnable interface
                 {
                    createAndShowGUI();
                 }  // end run
           });  // end new Runnable
    }  // end main
}  // end PizzaApp1
