import java.awt.*;

/*
 * PQTest - An applet/application for testing PriorityQueue.
 */

public class PQTest extends java.applet.Applet {
  
  private PQJob pq = new PQJob();         // Our PriorityQueue.
  private TextArea display;               // Displays PriorityQueue.
  private boolean isApplication = false;  // True iff running as application.
  
  // Applet initialization
  public void init() {
    setBackground(Color.green);     // Background color.
    setLayout(new BorderLayout());  // Placement scheme.

    // Panel for inserting.
    Panel ip = new Panel();
    ip.setBackground(Color.gray);
    ip.add(new Button("Bed")); 
    ip.add(new Button("Box")); 
    ip.add(new Button("Chair")); 
    ip.add(new Button("Chest")); 
    ip.add(new Button("Desk"));
    ip.add(new Button("Door"));
    ip.add(new Button("Shelf"));
    ip.add(new Button("Table"));
    ip.add(new Button("Toy"));
    ip.add(new Button("Trunk"));
    add("North", ip);
  
    // Panel for deleting.
    Panel dp = new Panel();
    dp.setBackground(Color.gray);
    dp.add(new Button("Get"));
    dp.add(new Button("Clear"));
    add("South", dp);

    // Display area.
    display = new TextArea(pq.toString());
    display.setEditable(false);
    add("Center", display);
  }

  /*
   * The main() method acts as the applet's entry point when it is run
   * as a standalone application. It is ignored if the applet is run
   * from within an HTML page.
   */

  public static void main(String args[]) {
    Frame frame = new Frame();       // create a window
    frame.setSize(1000, 200);          // choose a size
    frame.setTitle("Queue Window");  // give it a title
    PQTest applet = new PQTest();    // create the applet
    applet.isApplication = true;     // indicate application
    frame.add("Center", applet);     // put the applet in the window
    applet.init();                   // standard applet initializaion
    applet.start();                  // standard applet start
    frame.show();                    // make it visible
  }


  // Standard method for reporting information about an applet.
  public String getAppletInfo() {
    return "Name: QTest\r\n" +
           "Author: Nick Danger\r\n" +
           "Created with Microsoft Visual J++ Version 6.0";
  }

  /*
   * Standard method for handling control events.  This is where we
   * can set things up to handle events generated by buttons or other
   * standard controls.
   *
   * e = the Event, arg = the Event's argument; for a Button this is
   * the Button's name
   *
   * return true iff the Event has been handled
   */

  public boolean action (Event e, Object arg) {

    // If it's not a button, forget about it
    if (!(e.target instanceof Button)) return false;
    if (arg.equals("Clear")) {
      pq.clear();
    } else if (arg.equals("Get")) {
      if (!pq.isEmpty()) {
        Object oo = pq.get();
        System.out.println(oo.toString());
      }
    } else {
      // Put the appropriate record in the queue

      // There should be a way to define a constant array of Person
      // objects so instead of hard-coding the people in this
      // procedure it can be done in the class and then references
      // made.
      //
      // The advantage, however, to re-allocating and
      // re-initializing here is that we don't need to make the
      // pq.insert() procedure copy its data, in order to avoid
      // possible aliasing problems.

      Job j = new Job();
      if (arg.equals("Bed")) {
        j.name = "Bed";
        j.processTime = 10;
        j.dueDate = 15;
        j.priority = 10;
      } else if (arg.equals("Box")) {
        j.name = "Box";
        j.processTime = 4;
        j.dueDate = 18;
        j.priority = 13;
      } else if (arg.equals("Chair")) {
        j.name = "Chair";
        j.processTime = 5;
        j.dueDate = 12;
        j.priority = 15;
      } else if (arg.equals("Chest")) {
        j.name = "Chest";
        j.processTime = 5;
        j.dueDate = 19;
        j.priority = 12;
      } else if (arg.equals("Desk")) {
        j.name = "Desk";
        j.processTime = 10;
        j.dueDate = 25;
        j.priority = 11;
      } else if (arg.equals("Door")) {
        j.name = "Door";
        j.processTime = 2;
        j.dueDate = 4;
        j.priority = 20;
      } else if (arg.equals("Shelf")) {
        j.name = "Shelf";
        j.processTime = 1;
        j.dueDate = 30;
        j.priority = 5;
      } else if (arg.equals("Table")) {
        j.name = "Table";
        j.processTime = 6;
        j.dueDate = 10;
        j.priority = 12;
      } else if (arg.equals("Toy")) {
        j.name = "Toy";
        j.processTime = 2;
        j.dueDate = 50;
        j.priority = 2;
      } else if (arg.equals("Trunk")) {
        j.name = "Trunk";
        j.processTime = 3;
        j.dueDate = 45;
        j.priority = 8;
      }
      pq.insert(j);
    }
    
    // Display the queue's contents
    String contents = pq.toString();
    display.setText(contents);
    if (isApplication) System.out.println(contents);
    return true;  // Indicates event has been handled
  }
}
