import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Date;

public class Test extends Applet implements WindowListener, ActionListener, Runnable {

    public static void main(String[] args) {
        Frame F=new Frame("Event handling with threads and multirun prevention");
        Test BD=new Test();
        F.setSize(420, 350);
        F.addWindowListener(BD);
        F.add(BD);
        BD.init();
        F.setVisible(true);
    }


    // variables
    Button    BS;
    Button    BC;
    Checkbox  CB;
    TextArea  TA;
    int       cnt;

    public void init() {
        cnt=0;
        setLayout(new BorderLayout());

        Label l=new Label("Event handling with threads and multirun prevention",Label.CENTER);
        l.setFont(new Font("Helvetica",Font.BOLD,16));
        add("North", l);

        TA=new TextArea("", 60, 10, TextArea.SCROLLBARS_VERTICAL_ONLY);
        TA.setEditable(false);
        TA.setFont(new Font("Courier",Font.BOLD,14));
        add("Center", TA);

        // text&button Subpanel
        BS=new Button("Process");
        BS.addActionListener(this);
        BC=new Button("Clear");
        BC.addActionListener(this);
        CB=new Checkbox("only once", false);

        Panel SU=new Panel();
        SU.setLayout(new FlowLayout());
        SU.add(BS);
        SU.add(BC);
        SU.add(CB);
        add("South", SU);
        
    }

    private void DoSomething() {
        cnt++;
        int saved=cnt;
        TA.append(saved+" GO...\n");
        // the sleep instruction below simulates a time consuming task
        try {
            Thread.currentThread().sleep(5000);
        } catch (Exception err) {
        }
        // question: why we needed a saved variable???
        TA.append(saved+" DONE!\n");
        // reenable button (if it was disabled to start with)
        BS.setEnabled(true);
    }

// INTERFACE Runable -------
    public void run() {
        DoSomething();
    }

// INTERFACE ActionListener -------
    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==BS) {
            if (CB.getState()) BS.setEnabled(false);
            (new Thread(this)).start();
            // reenabe button when the task is done
        } else if (e.getSource()==BC) {
            TA.setText("");
        }
    }

// INTERFACE WindowListener -------
    public void windowClosing(WindowEvent e)      { System.exit(0); }
    public void windowClosed(WindowEvent e)       {  }
    public void windowOpened(WindowEvent e)       {  }
    public void windowIconified(WindowEvent e)    {  }
    public void windowDeiconified(WindowEvent e)  {  }
    public void windowActivated(WindowEvent e)    {  }
    public void windowDeactivated(WindowEvent e)  {  }
}