/*
 * 1.1 code.
 */

import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class WindowEventDemo extends Applet
		             implements WindowListener,
				        ActionListener {

    TextArea display;
    Frame window;
    Button b1, b2;
    static final String SHOW = "show";
    static final String CLEAR = "clear";
    String newline;

    public void init() {
	b1 = new Button("Click to bring up a window.");
	b1.setActionCommand(SHOW);
	b1.addActionListener(this);

	b2 = new Button("Click to clear the display.");
	b2.setActionCommand(CLEAR);
	b2.addActionListener(this);

	display = new TextArea(5, 20);
	display.setEditable(false);

	setLayout(new BorderLayout());
	add("North", b1);
	add("Center", display);
	add("South", b2);

	//Create but don't show window.
	window = new Frame("Window Event Window");
	window.addWindowListener(this);
	window.add("Center",
		   new Label("The applet listens to this window for window events."));
	window.pack();

	newline = System.getProperty("line.separator");
    }

    public void stop() {
	window.setVisible(false);
    }

    public void windowClosing(WindowEvent e) {
    	window.setVisible(false);
	displayMessage("Window closing", e);
    }

    public void windowClosed(WindowEvent e) {
	displayMessage("Window closed", e);
    }

    public void windowOpened(WindowEvent e) {
	displayMessage("Window opened", e);
    }

    public void windowIconified(WindowEvent e) {
	displayMessage("Window iconified", e);
    }

    public void windowDeiconified(WindowEvent e) {
	displayMessage("Window deiconified", e);
    }

    public void windowActivated(WindowEvent e) {
	displayMessage("Window activated", e);
    }

    public void windowDeactivated(WindowEvent e) {
	displayMessage("Window deactivated", e);
    }

    void displayMessage(String prefix, WindowEvent e) {
	display.append(prefix
		       + ": "
		       + e.getWindow()
		       + newline); 
    }

    public void actionPerformed(ActionEvent e) {
	if (e.getActionCommand() == SHOW) {
	    window.pack();
	    window.setVisible(true);
	} else {
	    display.setText("");
	}
    }
}
