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

import javax.swing.JMenu;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import com.approximatrix.draco.application.ChildFrame;
import com.approximatrix.draco.datamodel.StatTableModel;
import com.approximatrix.draco.general.ui.EquationPanel;


/*
 * Created on Feb 1, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author armstrongj
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ColumnEquationsDialog extends ChildFrame {

	private javax.swing.JPanel jContentPane = null;
	private static final String DIALOG_NAME = "Set Column Values"; 
	
	/** The string for requesting topical help */
	private static final String HELP_KEY = "equations";
	
	/** The icon to be displayed for this child window */
	private static final String PATH_TO_ICON = "/com/approximatrix/draco/main/ui/images/icons/eqn.png";
	
	private JPanel bottomPanel = null;
	
	private JPanel jPanel = null;
	private JButton cancel_set_values_button = null;
	private JButton set_values_button = null;
	
	private EquationPanel eqn_panel = null;
	
	// Computational crap
	private StatTableModel current_model = null;
	private String column_to_set = null;

	/**
	 * This is the default constructor
	 */
	public ColumnEquationsDialog(StatTableModel information, String set_this) {
		super(DIALOG_NAME, 
				true, //resizable
				false, //closable
				true, //maximizable
				true);
		
		current_model = information;
		column_to_set = set_this;
		
		initialize();
		
		//fill_window_components(information,set_this);
		
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setTitle(DIALOG_NAME);
		this.setPreferredSize(new java.awt.Dimension(200,250));
		this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		this.setSize(500, 250);
		this.setContentPane(getJContentPane());
		
		super.setIcon(PATH_TO_ICON);
		
		//this.setVisible(true);
		//this.addWindowListener(new java.awt.event.WindowAdapter() { 
		//	public void windowClosing(java.awt.event.WindowEvent e) {    
		//		closeDialog();
		//	}
		//});
	}
	
	/**
	 * This method initializes eqn_panel
	 * 
	 * @return javax.swing.JPanel
	 */
	private EquationPanel getEqn_panel() {
		if(eqn_panel == null) {
			eqn_panel = new EquationPanel(current_model,column_to_set);
		}
		return eqn_panel;
	}
	
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if(jContentPane == null) {
			BorderLayout borderLayout7 = new BorderLayout();
			jContentPane = new javax.swing.JPanel();
			jContentPane.setLayout(borderLayout7);
			borderLayout7.setHgap(4);
			
			
			
			jContentPane.add(getBottomPanel(), java.awt.BorderLayout.SOUTH);
			jContentPane.add(getEqn_panel(), java.awt.BorderLayout.CENTER);
		}
		return jContentPane;
	}
	
	private JPanel getBottomPanel() {
		if(bottomPanel == null) {
			bottomPanel = new JPanel();
			bottomPanel.setLayout(new BorderLayout());
			bottomPanel.add(getJPanel(),BorderLayout.EAST);
		}
		return bottomPanel;
	}
	
	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */    
	private JPanel getJPanel() {
		if (jPanel == null) {
			java.awt.FlowLayout flowLayout1 = new FlowLayout();
			jPanel = new JPanel();
			jPanel.setLayout(flowLayout1);
			flowLayout1.setAlignment(java.awt.FlowLayout.RIGHT);
			jPanel.add(getSet_values_button(), null);
			jPanel.add(getCancel_set_values_button(), null);
		}
		return jPanel;
	}
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getCancel_set_values_button() {
		if (cancel_set_values_button == null) {
			cancel_set_values_button = new JButton();
			cancel_set_values_button.setText("Cancel");
			cancel_set_values_button.addActionListener(new ActionListener() { 
				public void actionPerformed(ActionEvent e) {
					dispose();
				}
			});
		}
		return cancel_set_values_button;
	}
	
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getSet_values_button() {
		if (set_values_button == null) {
			set_values_button = new JButton();
			set_values_button.setText("Set Values");
			set_values_button.addActionListener(new ActionListener() { 
				public void actionPerformed(ActionEvent e) {
					performComputations();
				}
			});
		}
		return set_values_button;
	}
	

	@Override
	public JMenu[] getMenuItems() {
		return null;
	}

	@Override
	public Component[] getToolbarButtons() {
		return null;
	}

	@Override
	public boolean isRenameable() {
		return false;
	}	

	/** Returns the help key for the Set Column Values window
	 * 
	 * @return the proper help key, defined by HELP_KEY
	 * @see #HELP_KEY
	 * @see com.approximatrix.draco.application.ChildFrame#getHelpKey()
	 */
	public String getHelpKey() {
		return HELP_KEY;
	}
	
	public void performComputations() {
		JProgressBar bar = new JProgressBar();
		bar.setIndeterminate(true);
		bar.setString("Computing Values");
		bar.setStringPainted(true);
		
		JPanel barpanel = new JPanel();
		barpanel.add(bar);
		
		getBottomPanel().add(barpanel,BorderLayout.CENTER);
		getBottomPanel().doLayout();
		
		getSet_values_button().setEnabled(false);
		getCancel_set_values_button().setText("Close");
		
		getJPanel().doLayout();
		getJPanel().invalidate();
		
		Thread dowork = new Thread() {
			public void run() {
				int column = current_model.getColumnNumber(column_to_set);
				
				if(column != -1) {
					current_model.setColumnValues(column,getEqn_panel().getEquation());
				}
				Runnable finisher = new Runnable() {
					public void run() { dispose(); }
				};
				SwingUtilities.invokeLater(finisher);
			}
		};
		
		dowork.start();
	}
	
 }  //  @jve:decl-index=0:visual-constraint="16,53"
