//****************************************************************************
// CLASS NAME:	TextDialog.java
//
// AUTHOR:	Rick Brink
//		    rick@mail.csh.rit.edu
//		    http://www.csh.rit.edu/~rick
//
// VERSION:	1.0
//
// HISTORY:	4/16/96		Initial Version
//
// COPYRIGHT INFORMATION:
//
// This program and the Java source is in the public domain.
// Permission to use, copy, modify, and distribute this software
// and its documentation for NON-COMMERCIAL purposes and
// without fee is hereby granted.
//
//    Copyright 1996
//
//    Rick Brink
//    1266 Brighton-Henrietta Townline Rd.
//    Rochester, NY 14623
//
// DISCLAIMER:
//
// The author claims no responsibility for any damage, direct or indirect,
// to any harware or software as a result of using this program.
//****************************************************************************

import java.awt.Dialog;
import java.awt.TextField;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.Event;

/**
  * A class representing a Dialog which prompts the user for a
  * text string, and displays two buttons (OK and Cancel) to
  * either accept or reject the text and close the window.
  *
  * @see ControlPanel
  * @see PetriToolFrame
  * @see java.awt.Button
  * @see java.awt.Event
  * @see java.awt.Dialog
  * @see java.awt.BorderLayout
  * @see java.awt.FlowLayout
  * @see java.awt.Panel
  * @see java.awt.Frame
  * @see java.awt.TextField
  * @see java.awt.Graphics
  *
  * @version 1.0 July 3, 1996
  *
  * @author  Rick Brink
**/
class TextDialog extends Dialog {
    /** The parent that instantiates this class **/
    private protected PetriToolFrame frame_;

    /** The text string in the TextField **/
    private protected String text_;

    /** A TextField where the user inputs their string **/
    private protected TextField textField_ = null;

    /**
      * Construct a new TextDialog.
      *
      * @param frame The parent frame that calls this constructor
      * @param text The initial text to display in the TextField
      * @param modal Whether or not the Dialog should be modal
    **/
    TextDialog (Frame frame, String text, boolean modal) {
        super (frame, "Enter text", true);
        frame_ = (PetriToolFrame) frame;
        text_ = text;

        setLayout(new BorderLayout());
        add ("North", textField_ = new TextField(text_)) ;

        Panel control = new Panel () ;
        control.setLayout (new FlowLayout ()) ;
        control.add (new Button ("OK"));
        control.add (new Button ("Cancel"));
        add ("South", control) ;

        resize (400, 100);
        show();
        textField_.requestFocus();
    }

    /**
      * Paint the TextDialog
    **/
    public void paint(Graphics g) {
    }

    /**
      * Handle events for the TextDialog by calling super.handleEvent()
    **/
    public boolean handleEvent(Event ev) {
        if (ev.id == Event.WINDOW_DESTROY) {
            dispose();
            return (true);
        }
        return super.handleEvent(ev);
    }

    /**
      * Handle action events for this frame by getting the string
      * from the TextField and passing it up to the parent frame
      * for processing
    **/
    public boolean action(Event ev, Object arg) {
        if ((ev.target instanceof Button) ||
           (ev.target instanceof TextField)) {
            String label = (String)arg;
            if (label.equals("Cancel")) {
                 dispose();
                 return (true);

            } else {
                dispose();
                return frame_.action (ev, "TEXTDIALOG_OK_" +
                                      textField_.getText());
            }
        }
        return (false);
    }
}

