import java.awt.*;
import java.awt.event.*;

/**
 * Alert provides a simple text window (or Label for single line alerts)
 * and an OK button.
 * <p>
 * This widget is not modal, and will not demand the user pay attention
 * to it.  This is considered a bug, but not a serious one.
 * <p>
 * The display window places itself down and to the right of the upper
 * left hand corner of the primary window.
 *
 * @author Clif Flynt
 * @author Samuel A. Rebelsky
 * @version 1.1
 */

public class Alert
    implements ActionListener 
{

    // +--------+-----------------------------------------
    // | Fields |
    // +--------+

    /** This widget creates its own frame for display.  */ 
    protected Frame alertFrame;

    /** 
     * The OK button will be displayed for all
     * instantiations of this object.
     */ 
    protected Button ok;

    /**
     * The text1 label will be displayed if the
     * Constructor is invoked with a single String of output,
     */
    protected Label text1;

    /**
     * The text2 TextArea widget will be displayed if the
     * Constructor is invoked with an array of Strings
     */
    protected TextArea text2;
    
    // +----------------+---------------------------------
    // | Helper Methods |
    // +----------------+

    /**
     * Do the standard initialization that is required
     * whether the Alert object is created with a single 
     * String or an array of Strings.
     */
    private void basicSetup (String winName, Point loc) {
	alertFrame = new Frame(winName);
	ok = new Button("OK");
	text2 = new TextArea ();
	text1 = new Label();
	
        // The BorderLayout class makes it possible to have some
        // control over the placement of buttons.
	alertFrame.setLayout(new BorderLayout());

        // We want to position the alert box appropriately.
	alertFrame.setLocation(loc.x+20, loc.y+15);
	
	alertFrame.add(ok, "South");
	ok.addActionListener(this);
    } // basicSetup(String, Point)
    
    /**
     * Finishes the creation of the alert.  Packs, shows, and 
     * does whatever else may be required for the object after 
     * everything else is taken care of.
     */
    private void doneSetup () {
	alertFrame.pack();
	alertFrame.show();
    } // doneSetup()
   
 
    // +--------------+-----------------------------------
    // | Constructors |
    // +--------------+

    /**
     * Create a new alert with a title and a single displayed string,
     * near a particular point on the screen.
     * <P>
     * The initialization that's common to the two constructors
     * is done in private methods.
     */
    public Alert(String winName, String txt, Point loc) {
	basicSetup(winName, loc);
	text1.setText(txt);
	alertFrame.add(text1, "North");
	doneSetup ();
	return;
    } // Alert(String,String,Point)
    
    /**
     * Create a new alert with a title and a number of strings to
     * display, near a particular point on the screen.
     * Each string appears on its own line.
     * <P>
     * The initialization that's common to the two constructors
     * is done in private methods.
     */

    public Alert(String winName, String[] txt, Point loc) {
	int i;
	
	basicSetup(winName, loc);
	
	for(i=0; i< txt.length; i++) {
	    text2.append(txt[i]);
	    text2.append("\n");
	} // for(i)
	alertFrame.add(text2, "North");
	doneSetup ();
	return;
    } // Alert(String, String[], Point)
    
    /**
     * The only action that can be performed by the Alert widget
     * is to turn off this window when somone clicks the [OK] button.
     */
    public void actionPerformed(ActionEvent e) {
	alertFrame.setVisible(false);
	alertFrame.dispose();
    } // actionPerformed(ActionEvent)
} // class Alert

