import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.net.*;
import java.io.*;

//import com.sun.java.swing.UIManager;
public class OrderStatusApplet extends JApplet {

  boolean isStandalone = false;
  JPanel jStatusPanel = new JPanel();
  JPanel jActionPanel = new JPanel();
  GridLayout gridLayout1 = new GridLayout(1, 2);
  JButton jGetStatusButton = new JButton();
  JTextField jOrderTextField = new JTextField();
  JLabel jLabel1 = new JLabel();
  JTextArea jStatusResultTextArea = new JTextArea();

  //Get a parameter value
  public String getParameter(String key, String def) {

    return isStandalone ? System.getProperty(key, def) :
      (getParameter(key) != null ? getParameter(key) : def);
  }

  // Default Construct
  public OrderStatusApplet() {

  }

  //Initialize the applet
  public void init() {

    try {

      jbInit();
    }
    catch (Exception e) {

      e.printStackTrace();
    }
  }

  //Component initialization
  private void jbInit() throws Exception {

    this.setSize(400,150);
    this.getContentPane().setLayout(gridLayout1);
    jGetStatusButton.setText("Get Status");
    jGetStatusButton.addActionListener(
      new java.awt.event.ActionListener() {

        public void actionPerformed(ActionEvent e) {

          jGetStatusButton_actionPerformed(e);
      }
    });
    jLabel1.setText("Order #");
    jOrderTextField.setPreferredSize(new Dimension(50, 19));
    jStatusResultTextArea.setPreferredSize(
      new Dimension(175, 135));
    this.getContentPane().add(jActionPanel, null);
    jActionPanel.add(jLabel1, null);
    jActionPanel.add(jOrderTextField, null);
    jActionPanel.add(jGetStatusButton, null);
    this.getContentPane().add(jStatusPanel, null);
    jStatusPanel.add(jStatusResultTextArea, null);
  }

  //Get Applet information
  public String getAppletInfo() {

    return "Applet Information";
  }

  //Get parameter info
  public String[][] getParameterInfo() {

    return null;
  }

  // Write the StudentList to the Connection
  public void writeOrder(URLConnection connection,
    Order value) {

    try {

      // Set this to false in order to ignore caching
      connection.setUseCaches(false);

      // Set the content-type of the request
      //  application/octet-stream is used when writing
      // application specific byte size data
      connection.setRequestProperty("CONTENT_TYPE",
        "application/octet-stream");

      // Set these vales to true to use the same connection
      // for both input and output
      connection.setDoInput(true);
      connection.setDoOutput(true);

      // Create the ObjectOutputStream passing it the
      // ByteArrayOutputStream object.
      ObjectOutputStream os =
        new ObjectOutputStream(connection.getOutputStream());

      // Write the StudentList to the ObjectOutputStream
      System.err.println("Writing Order Object.");
      os.writeObject(value);
//      os.flush();
//      os.close();
    }
    catch (IOException e) {

      System.err.println(e.getMessage());
    }
  }

  public Order readOrder(URLConnection connection)
  {

    Order order = null;

    try {

      // Create the ObjectInputStream passing it the
      // InputStream object from the URLConnection
      ObjectInputStream is = new ObjectInputStream(
        connection.getInputStream());

      System.err.println("Waiting for response.");

      // Read the stored object and downcast it back to
      // a Order
      order = (Order)is.readObject();
      is.close();
    }
    catch (IOException e) {
      System.err.println(e.getMessage());
      System.err.println(e);
    }
    catch (ClassNotFoundException ce) {

      System.err.println(ce.getMessage());
    }
    return order;
  }

  void jGetStatusButton_actionPerformed(ActionEvent event) {

    try {

      // This is where the OrderStatus Transaction begins
      Order order = new Order();

      order.setOrder(jOrderTextField.getText());

      // create our URL
      URL url = new URL("http://localhost:8080" +
        "/servlet/OrderStatusServlet");

      // Open our URLConnection
      System.err.println("Opening Connection.");
      URLConnection con = url.openConnection();

      // Write the Order
      writeOrder(con, order);

      // Get the Order from the response,
      // after the status has been checked, and print it out.
      Order response_order = readOrder(con);
      if ( response_order != null ) {

        // Put the status String returned from the
        // OrderStatusServlet into the jTextArea Object
        jStatusResultTextArea.setText(
          response_order.getStatus());
      }
      else {

        System.err.println("readObject failed.");
      }
    }
    catch (MalformedURLException mue) {

      System.err.println(mue.getMessage());
    }
    catch (Exception e) {

      System.err.println(e.getMessage());
    }
  }
}

