/*
 *  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.fit.ui;

import com.sun.java.SwingWorker;

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import com.approximatrix.draco.fit.LSFit;

public class SimpleProgress extends ProgressDialog {
	
	private JPanel jContentPane = null;
	private JPanel jPanel = null;
	private JProgressBar jIterationProgressBar = null;
	private JPanel jPanel1 = null;
	private JPanel jPanel2 = null;
	private JButton jStopButton = null;
	private JLabel jLabel1 = null;
	
	private String title = null;
	
	/**
	 * This is the default constructor
	 */
	public SimpleProgress(String title, JFrame master, LSFit analysis) {
		super(master);
		this.title = title;
		initialize();
		this.analysis = analysis;
		buildWorker();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 180);
		this.setPreferredSize(new java.awt.Dimension(300, 180));
		this.setLocationRelativeTo(super.getParent());
		this.setResizable(false);
		this.setTitle(this.title);
		this.setContentPane(getJContentPane());
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				fireWorkerEvent();
				dispose();
			}
		});
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanel(), java.awt.BorderLayout.NORTH);
			jContentPane.add(getJPanel1(), java.awt.BorderLayout.CENTER);
			jContentPane.add(getJPanel2(), java.awt.BorderLayout.SOUTH
					);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.LOWERED), "Iteration Progress", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
			jPanel.add(getJIterationProgressBar(), null);
		}
		return jPanel;
	}

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

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

	/**
	 * This method initializes jPanel2	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			jPanel2 = new JPanel();
			jPanel2.add(getJStopButton(), null);
		}
		return jPanel2;
	}

	/**
	 * This method initializes jStopButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJStopButton() {
		if (jStopButton == null) {
			jStopButton = new JButton();
			jStopButton.setText("Stop!");
			jStopButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					// Something...
				}
			});
		}
		return jStopButton;
	}
	
	/** This routine represents the meat of the SwingWorker thread that performs
	 * all iteration control
	 * 
	 * @return for now, a String
	 */
	private Object doIterations() {
		this.analysis.perform();
        //System.out.println("done");
        return analysis;         // or this
	}
	
	/** Constructs the worker thread and starts it on its way
	 * 
	 */
	private void buildWorker() {
		/* 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().
		 */
		worker = new SwingWorker() {
			public Object construct() {
				return doIterations();
			}
			public void finished() {
				System.out.println("Done");
				SwingUtilities.invokeLater(new Runnable() {
		            public void run() {
		            	setVisible(false);
		            	fireWorkerEvent();
		            }
		        });
			}
		};
		//worker.start();
	}
	
}  //  @jve:decl-index=0:visual-constraint="10,10"
