/*
 * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and without
 * fee is hereby granted provided that this copyright notice
 * appears in all copies. Please refer to the file "copyright.html"
 * for further important copyright and licensing information.
 *
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */
import java.awt.*;

public class DialogWindow extends Frame {
    private boolean inAnApplet = true;
    private SimpleDialog dialog;
    private TextArea textArea;

    public DialogWindow() {
	textArea = new TextArea(5, 40);
	textArea.setEditable(false);
	add("Center", textArea);
	Button button = new Button("Click to bring up dialog");
	Panel panel = new Panel();
	panel.add(button);
	add("South", panel);
    }

    public boolean handleEvent(Event event) {
        if (event.id == Event.WINDOW_DESTROY) {
            if (inAnApplet) {
                dispose();
            } else {
                System.exit(0);
            }
        }   
        return super.handleEvent(event);
    }

    public boolean action(Event event, Object arg) {
	if (dialog == null) {
	    dialog = new SimpleDialog(this, "Title Goes Here");
	}
	dialog.show();
	return false;
    }

    public void setText(String text) {
	textArea.appendText(text + "\n");
    }

    public static void main(String args[]) {
	DialogWindow window = new DialogWindow();
        window.inAnApplet = false;

	window.setTitle("DialogWindow Application");
	window.resize(250, 150);
        window.show();
    }
}

//[HOW DO I MAKE THIS GET THE FOCUS?]
class SimpleDialog extends Dialog {
    private TextField field;
    private DialogWindow parent;
    private Button setButton;

    SimpleDialog(Frame dw, String title) {
	super(dw, title, false);
	parent = (DialogWindow)dw;

	GridBagLayout gridBag = new GridBagLayout();
	setLayout(gridBag);
	GridBagConstraints c = new GridBagConstraints();

	//Create top row.
	Label label = new Label("Enter random text here:");
	c.anchor = GridBagConstraints.EAST;
	gridBag.setConstraints(label, c);
	add(label);

	c.fill = GridBagConstraints.HORIZONTAL;
	c.gridwidth = GridBagConstraints.REMAINDER;
	c.weightx = 1.0;
	c.weighty = 1.0;
	field = new TextField(20);
	gridBag.setConstraints(field, c);
	add(field);

	//Create bottom row.
	Panel panel = new Panel();
	panel.setLayout(new FlowLayout(FlowLayout.RIGHT));
	Button b = new Button("Cancel");
	setButton = new Button("Set");
	panel.add(b);
	panel.add(setButton);
	c.weightx = 0.0;
	c.weighty = 0.0;
	gridBag.setConstraints(panel, c);
	add(panel);

	resize(350, 125);
    }

    public boolean action(Event event, Object arg) {
	if ( (event.target == setButton)
	   | (event.target instanceof TextField)) {
	    parent.setText(field.getText());
	}
	field.selectAll();
	hide();
	return true;
    }
}
