// file:$ISIP_IFC/util/demo/isip_comm_client_speech_recognition/SpeechRecognitionServer.java
// version: $Id: ProgressBar.java 10572 2006-06-26 21:54:34Z rei5 $

// import necessary java libraries
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; 


/**
 * this class displays the progress bar which is used in the
 * verification demo when the enrollment and verification is done.
 */
public class ProgressBar extends JFrame {
    
    //-------------------------------------------------------------------------
    //
    // public members
    //
    //-------------------------------------------------------------------------
    
    public final static int ONE_SECOND = 1000;
    public JButton cancelButton;

    //-------------------------------------------------------------------------
    //
    // private members
    //
    //-------------------------------------------------------------------------
    
    private JProgressBar progressBar;
    private Timer timer;
    private LongTask task;
    private JTextArea taskOutput;
    private String newline = "\n";

      
    /**
     *  the constructor of the class
     */
    public ProgressBar(String s) {
        
	super("ProgressBar");
        task = new LongTask();

	// Create the demo's UI.
        //
	cancelButton = new JButton("Cancel");
        cancelButton.setActionCommand("start");
       	JLabel titleLabel = new JLabel(s);
        progressBar = new JProgressBar();
        progressBar.setIndeterminate(true);
        taskOutput = new JTextArea(5, 20);
        taskOutput.setMargin(new Insets(5,5,5,5));
        taskOutput.setEditable(false);
	
        JPanel panel = new JPanel();
	panel.setLayout(new BorderLayout());
	JPanel myPanel = new JPanel();

        panel.add(titleLabel, "North");
        myPanel.add(cancelButton);
        panel.add(progressBar, "Center");
        panel.add(myPanel, "South");

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout());
        contentPane.add(panel, BorderLayout.CENTER);
	contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
	setContentPane(contentPane);

        // Create a timer.
        //
	timer = new Timer(ONE_SECOND, new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
		taskOutput.append(task.getMessage() + newline);
                taskOutput.setCaretPosition(taskOutput
					    .getDocument().getLength());
                if (task.done()) {
                    Toolkit.getDefaultToolkit().beep();
                    timer.stop();
                    cancelButton.setEnabled(true);
		}
            }
	    });
    }
    
    /**
     * The actionPerformed method in this class
     * is called when the user presses the start button.
     */
    class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent evt) {
	    task.stop();
            timer.stop();
	    System.exit(0);
        }
    }
}
