/*---------------------------------------------------------------------------
File:         			ToGrammarDialog.java
Package:                        JFLAP Version 3.1
Author:			        Ted Hung
                                
Description of Contents:	Contains class ToGrammarDialog.
				
--------------------------------------------------------------------------*/

/* 
 * Susan H. Rodger, Magda Procopiuc, Octavian Procopiuc, Eric Gramond, Ted Hung
 * Computer Science Department
 * Duke University
 * August 1999
 * Supported by National Science Foundation DUE-9596002, DUE-9555084,
 *  and DUE-9752583
 *
 * Copyright (c) 1999
 * 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.event.*;
import java.util.*;



/**
 * Displays the Steps of the Conversion to a Grammar from an FSA or
 * a PDA
 */

public class ToGrammarDialog extends Frame implements ActionListener
{
    protected ToGrammarCanvas dc;
    protected String result;
    private Label lbl;

    public static final String NEXT_STEP = "Next";
    public static final String SHOW = "Show";
    public static final String TRANSITION = "highlighted transition.";
    public static final String STATE = "highlighted state.";

    public ToGrammarDialog()
    {		
	setBackground(Params._e_backcolor);	
	setTitle("Conversion to Grammar");

	setLayout(new BorderLayout());
	add(dc = new ToGrammarCanvas(), "Center");

	Panel p = new Panel(new GridLayout(2,1));
	p.add(new Label("Generating Grammar Rules for", Label.CENTER));
	p.add(lbl = new Label(TRANSITION, Label.CENTER)); 
	add(p, "North");

	Button b;
	Panel buttonPanel = new Panel(new FlowLayout());
	buttonPanel.add(b = new Button(NEXT_STEP));
	add(buttonPanel, "South");
	b.addActionListener(this);
	
	buttonPanel.add(b = new Button(SHOW));
	add(buttonPanel, "South");
	b.addActionListener(this);

	validate();
	pack();
	setResizable(false);
	setVisible(false);
    }

    /**
     * Sets the rules that are displayed in the dialog
     */
    public void setRules(Vector rules)
    {
       setResizable(true);
       result = null;
       dc.setRules(rules);
       pack();

       setResizable(false);
    }
   
    /**
     * Sets the dialog's message: "Generating Grammar Rules for " + str
     */
    public void setMessage(String str)
    { 
       setResizable(true);
       lbl.setText(str);
       pack();
    }

    public void actionPerformed(ActionEvent e)
    {
       result = e.getActionCommand();
    }

    /** 
     * basically pause until a button is pressed and return the result.
     * Modified so that it doesn't hide itself after finding the result
     * calling function must call setVisible(false);
     */
    public String findResult()
    {
	Thread th = Thread.currentThread();
	setVisible(true);
	while(result == null)
	{
	    try
	    {
		th.sleep(5);
	    }
	    catch(InterruptedException e) {}
	}
	//setVisible(false);
	return result;
    }
}
