/*******************************************************************************
 * GenRGenS v.1.0, Generation of Random Genomic Sequences                      *
 * Copyright (C) 2001  Alain Denise/Yann Ponty                                 *
 *                                                                             *
 * This program is free software; you can redistribute it and/or               *
 * modify it under the terms of the GNU General Public License                 *
 * as published by the Free Software Foundation; either version 2              *
 * of the License, or (at your option) any later version.                      *
 *                                                                             *
 * This program is distributed in the hope that it will be useful,             *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of              *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
 * GNU General Public License for more details.                                *
 *                                                                             *
 * You should have received a copy of the GNU General Public License           *
 * along with this program; if not, write to the Free Software                 *
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. *
 *******************************************************************************/

/**
 * Title:        <p>
 * Description:  <p>
 * Copyright:    Copyright (c) <p>
 * Company:      <p>
 * @author 
 * @version 1.0
 */
package GenRGenS.IU;

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import GenRGenS.*;
public class ProgressDialog extends JDialog
{
  JPanel panel1 = new JPanel();
  BorderLayout borderLayout1 = new BorderLayout();
  JPanel jPanel1 = new JPanel();
  JButton jButton1 = new JButton();
  JPanel jPanel2 = new JPanel();
  JProgressBar jProgressBar1 = new JProgressBar();
  FlowLayout flowLayout1 = new FlowLayout();
  JPanel jPanel3 = new JPanel();
  JLabel jLabel1 = new JLabel();

  public ProgressDialog(Frame frame, String title, boolean modal)
  {
    super(frame, title, modal);
    try
    {
      jbInit();
      pack();
    }
    catch(Exception ex)
    {
      ex.printStackTrace();
    }
  }

  public ProgressDialog()
  {
    this(null, "", false);
  }

  void jbInit() throws Exception
  {
    panel1.setLayout(borderLayout1);
    jButton1.setText("Abort");
    jButton1.addActionListener(new java.awt.event.ActionListener()
    {

      public void actionPerformed(ActionEvent e)
      {
        jButton1_actionPerformed(e);
      }
    });
    jPanel2.setLayout(flowLayout1);
    flowLayout1.setVgap(15);
    jLabel1.setFont(new java.awt.Font("DialogInput", 1, 12));
    jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
    jLabel1.setText("Generating ...");
    jProgressBar1.setStringPainted(true);
    this.addWindowListener(new java.awt.event.WindowAdapter()
    {

      public void windowClosed(WindowEvent e)
      {
        this_windowClosed(e);
      }

      public void windowClosing(WindowEvent e)
      {
        this_windowClosing(e);
      }
    });
    getContentPane().add(panel1);
    panel1.add(jPanel1, BorderLayout.SOUTH);
    jPanel1.add(jButton1, null);
    panel1.add(jPanel2, BorderLayout.CENTER);
    jPanel2.add(jProgressBar1, null);
    panel1.add(jPanel3, BorderLayout.NORTH);
    this.getContentPane().add(jLabel1, BorderLayout.NORTH);
  }
  
  private Thread task = null;
  
  public void setThread(Thread t)
  {
    task = t;
  }
  
  public void show()
  {
    jButton1.setText("Abort");
    task.start();
    super.show();
  }
  
  public void setProgress(int n)
  {
    jProgressBar1.setValue(n);
    if (n == jProgressBar1.getMaximum())
    {
      jButton1.setText("Ok");
    }
  }
  
  public void setMax(int n)
  {
    jProgressBar1.setMaximum(n);
  }

  void jButton1_actionPerformed(ActionEvent e)
  {
    if (task.isAlive())
    {
      task.stop();
      GenRGenS.err.println("Thread interrupted !");
    }
    hide();
  }

  void this_windowClosed(WindowEvent e)
  {
  }

  void this_windowClosing(WindowEvent e)
  {
    if (task.isAlive())
    {
      task.stop();
    }

  }
}