/**
 * ExPrimeApp
 * 
 * This program is an applet version of ExPrime
 * (jdk 1.1)
 */

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class ExPrimeApp extends Applet implements Runnable, ActionListener {
  // output console
  TextArea textarea;

  // start button
  Button startbutton;

  // clear button
  Button clearbutton;

  // message field
  TextField message;

  // simulation program
  ExPrime ex_prime;

  // output stream for the program
  ExAppStream ex_out;

  /*
   * Sets up the applet view
   */
  public void init() {
    textarea = new TextArea(20, 40);
    startbutton = new Button("Start");
    clearbutton = new Button("Clear");
    message = new TextField(20);

    startbutton.addActionListener(this);
    add(startbutton);
    clearbutton.addActionListener(this);
    add(clearbutton);
    add(message);
    add("South", textarea);
  }

  /*
   * Starts the applet
   */
  public void start() {
    ex_prime = new ExPrime();
    ex_out = new ExAppStream(this, textarea);
    message.setText("Click on start");
  }

  /*
   * Starts the simulation
   */
  public void run() {
    message.setText("Running...");
    ex_prime.work(ex_out);
    message.setText("Finished");
  }

  /*
   * Handles the button events.
   */
  public void actionPerformed(ActionEvent event) {
    Object src = event.getSource();
    if (src == startbutton) {
      message.setText("Starting...");
      textarea.setText("");
      new Thread(this).start();
    }
    else if (src == clearbutton) {
      textarea.setText("");
    }
  }
}
