/*---------------------------------------------------------------------------
File:         			TextDialog.java
Package:                        JFLAP Version 3.0
Author:			        Eric Gramond 
                                
Description of Contents:	Contains class TextDialog.
				
--------------------------------------------------------------------------*/

/* 
 * Susan H. Rodger, Magda Procopiuc, Octavian Procopiuc, Eric Gramond
 * Computer Science Department
 * Duke University
 * June 1998
 * Supported by National Science Foundation DUE-9596002 and DUE-9555084.
 *
 * Copyright (c) 1998
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice and this paragraph are
 * duplicated in all such forms and that any documentation,
 * advertising materials, and other materials related to such
 * distribution and use acknowledge that the software was developed
 * by the author.  The name of the author may not be used to
 * endorse or promote products derived from this software without
 * specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */
package flap;

import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.io.*;
import java.net.URL;
import java.util.*;



/**
 * A little dialog window with text in it and a Button
 */

public class TextDialog extends Frame implements ActionListener {


    protected DialogCanvas dc;
    protected String result;

    /**
     * default constructor
     */

    public TextDialog() {
	setBackground(Params._e_backcolor);
    }


/**
 * full constructor. use this one. please.
 */
    public TextDialog(String text,  String[] buttons, String title) {		
	setBackground(Params._e_backcolor);	
	setTitle(title);

	int i;
	Panel p = new Panel(new FlowLayout());
	dc = new DialogCanvas(text);
	p.add(dc);
	add(p, "North");

	p = new Panel(new FlowLayout());
	Separator sep = new Separator();
	sep.setSize(250, 4);
	p.add(sep);
	add(p, "Center");

	Button b; 		
	Panel buttonPanel = new Panel(new FlowLayout());
	add(buttonPanel, "South");
	for(i=0; i < buttons.length; i++) {
	    b = new Button(buttons[i]);
	    buttonPanel.add(b);
	    b.addActionListener(this);
	}

	validate();	
	pack();
	dc.calcSize(250,80);
	pack();
	setLocation(
	    (Toolkit.getDefaultToolkit().getScreenSize().width - getSize().width) / 2,
	    (Toolkit.getDefaultToolkit().getScreenSize().height - getSize().height) / 2);
	setResizable(false);
	setVisible(true);
    }

  
    /**
     * set the size of the box.
     */
    public void setDim( int width, int height) {
	dc.setSize(width, height);
	setResizable(true);
	validate();
	pack();
	setResizable(false);
    }
    
    public void actionPerformed(ActionEvent e) {
	result = e.getActionCommand();
	setVisible(false);
    }
    

    /** 
     * basically pause until a button is pressed and return the result.
     * run this in a separate thread or be frozen.
     */
    public String findResult() {
	Thread th = Thread.currentThread();

	while(result == null) {
	    try {
		th.sleep(5);
	    }
	    catch(InterruptedException e) {}
	}
	setVisible(false);
	return result;
    }

}
