/*
 *  Draco Econometrics
 *  Copyright (C) 2006-2007 Approximatrix, LLC
 *  http://www.approximatrix.com
 *  
 *  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 3 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, see <http://www.gnu.org/licenses/>.
 */

package com.approximatrix.draco.general.ui;

import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;
import javax.swing.JDialog;
import java.awt.Dimension;
import javax.swing.BoxLayout;
import java.awt.GridBagLayout;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;


import com.approximatrix.draco.io.ui.IOFileWorkerDialog;
import com.sun.java.SwingWorker;

public class GeneralWorker extends JDialog {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JPanel jPanel = null;

	private JPanel jPanel1 = null;

	private JLabel jMessageLabel = null;

	private JProgressBar jBouncyProgressBar = null;
	
	private SwingWorker workerthread = null;  //  @jve:decl-index=0:
	
	private WorkRoutine worker = null;

	/**
	 * @param owner
	 */
	public GeneralWorker(Frame parent, String name, String message, WorkRoutine todo) {
		super(parent,true);
		initialize();
		this.setLocationRelativeTo(parent);
		jMessageLabel.setText(message);
		this.setTitle(name);
		worker = todo;
		build_worker();
		this.setVisible(true);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 100);
		this.setContentPane(getJContentPane());
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				if(workerthread!=null)
					workerthread.interrupt();
			}
		});
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BoxLayout(getJContentPane(), BoxLayout.Y_AXIS));
			jContentPane.add(getJPanel(), null);
			jContentPane.add(getJPanel1(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(new FlowLayout());
			jPanel.add(getJBouncyProgressBar(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jMessageLabel = new JLabel();
			jMessageLabel.setText("JLabel");
			jPanel1 = new JPanel();
			jPanel1.setLayout(new FlowLayout());
			jPanel1.add(jMessageLabel, null);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jBouncyProgressBar	
	 * 	
	 * @return javax.swing.JProgressBar	
	 */
	private JProgressBar getJBouncyProgressBar() {
		if (jBouncyProgressBar == null) {
			jBouncyProgressBar = new JProgressBar();
			jBouncyProgressBar.setPreferredSize(new Dimension(275, 14));
			jBouncyProgressBar.setIndeterminate(true);
		}
		return jBouncyProgressBar;
	}

	private Object startWork() {
		//System.out.println("Working...");
		worker.doWork();
		//System.out.println("Done!");
		return "All Done"; 
	}
	
	/** Constructs the worker thread and starts it on its way
	 * 
	 */
	private void build_worker() {
		/* Invoking start() on the SwingWorker causes a new Thread
		 * to be created that will call construct(), and then
		 * finished().  Note that finished() is called even if
		 * the worker is interrupted because we catch the
		 * InterruptedException in doWork().
		 */
		workerthread = new SwingWorker() {
			public Object construct() {
				return startWork();
			}
			public void finished() {
				Runnable doSetProgress = new Runnable() {
		            public void run() {
		            	GeneralWorker.this.setVisible(false);
		            	GeneralWorker.this.dispose();
		            }
		        };
		        SwingUtilities.invokeLater(doSetProgress);
			}
		};
		workerthread.start();
	}
	
	public interface WorkRoutine {
		public void doWork();
	}
	
}  //  @jve:decl-index=0:visual-constraint="10,10"
