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

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.JPanel;
import javax.swing.JDialog;


import com.approximatrix.draco.application.ChildFrame;
import com.approximatrix.draco.datamodel.StatTableModel;
import com.approximatrix.draco.math.CalculateHistogram;

import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JComboBox;
import javax.swing.JMenu;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JOptionPane;

public class SetupPlotHisto extends ChildFrame {

	private static final String DIALOG_NAME = "Plot Histogram";
	
	/** The string for requesting topical help */
	private static final String HELP_KEY = "plothistogram";
	
	/** The icon to be displayed for this child window */
	private static final String PATH_TO_ICON = "/com/approximatrix/draco/main/ui/images/plothisto.png";
	
	
	private JPanel jContentPane = null;


	private StatTableModel current_model = null;

	private JPanel jPanel = null;

	private JLabel jLabel = null;

	private JComboBox variable_combobox = null;

	private JLabel jLabel1 = null;

	private JTextField start_textfield = null;

	private JLabel jLabel2 = null;

	private JTextField end_textfield = null;

	private JLabel jLabel3 = null;

	private JTextField bin_textfield = null;

	private JButton plot_button = null;

	/**
	 * This is the default constructor
	 */
	public SetupPlotHisto(StatTableModel model) {
		super(DIALOG_NAME, 
				true, //resizable
				true, //closable
				true, //maximizable
				true);
		
		current_model = model;
		initialize();
	}
	
	/**
	 * This is the default constructor
	 */
	public SetupPlotHisto(StatTableModel model, String selected_column) {
		this(model);
		getVariable_combobox().setSelectedItem(selected_column);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(211, 198);
		this.setTitle(DIALOG_NAME);
		this.setContentPane(getJContentPane());
		super.setIcon(PATH_TO_ICON);
	}

	/**
	 * 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.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints7.gridy = 3;
			gridBagConstraints7.weightx = 1.0;
			gridBagConstraints7.insets = new java.awt.Insets(5,5,5,5);
			gridBagConstraints7.gridx = 1;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 0;
			gridBagConstraints6.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints6.insets = new java.awt.Insets(5,5,5,5);
			gridBagConstraints6.gridy = 3;
			jLabel3 = new JLabel();
			jLabel3.setText("Bin Size: ");
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints5.gridy = 2;
			gridBagConstraints5.weightx = 1.0;
			gridBagConstraints5.insets = new java.awt.Insets(5,5,5,5);
			gridBagConstraints5.gridx = 1;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 0;
			gridBagConstraints4.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints4.insets = new java.awt.Insets(5,5,5,5);
			gridBagConstraints4.gridy = 2;
			jLabel2 = new JLabel();
			jLabel2.setText("End:");
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints3.gridy = 1;
			gridBagConstraints3.weightx = 1.0;
			gridBagConstraints3.insets = new java.awt.Insets(5,5,5,5);
			gridBagConstraints3.gridx = 1;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints2.insets = new java.awt.Insets(5,5,5,5);
			gridBagConstraints2.gridy = 1;
			jLabel1 = new JLabel();
			jLabel1.setText("Start:");
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints1.gridy = 0;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.insets = new java.awt.Insets(5,5,5,5);
			gridBagConstraints1.gridx = 1;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints.insets = new java.awt.Insets(5,5,5,5);
			gridBagConstraints.gridy = 0;
			jLabel = new JLabel();
			jLabel.setText("Variable:");
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.add(jLabel, gridBagConstraints);
			jPanel.add(getVariable_combobox(), gridBagConstraints1);
			jPanel.add(jLabel1, gridBagConstraints2);
			jPanel.add(getStart_textfield(), gridBagConstraints3);
			jPanel.add(jLabel2, gridBagConstraints4);
			jPanel.add(getEnd_textfield(), gridBagConstraints5);
			jPanel.add(jLabel3, gridBagConstraints6);
			jPanel.add(getBin_textfield(), gridBagConstraints7);
		}
		return jPanel;
	}

	/**
	 * This method initializes variable_combobox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getVariable_combobox() {
		if (variable_combobox == null) {
			variable_combobox = new JComboBox(current_model.getColumnNames());
			variable_combobox.setSelectedItem(current_model.getColumnName(0));
		}
		return variable_combobox;
	}

	/**
	 * This method initializes start_textfield	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getStart_textfield() {
		if (start_textfield == null) {
			start_textfield = new JTextField();
		}
		return start_textfield;
	}

	/**
	 * This method initializes end_textfield	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getEnd_textfield() {
		if (end_textfield == null) {
			end_textfield = new JTextField();
		}
		return end_textfield;
	}

	/**
	 * This method initializes bin_textfield	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getBin_textfield() {
		if (bin_textfield == null) {
			bin_textfield = new JTextField();
		}
		return bin_textfield;
	}

	/**
	 * This method initializes plot_button	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getPlot_button() {
		if (plot_button == null) {
			plot_button = new JButton();
			plot_button.setText("Create Histogram");
			plot_button.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					
					String variable = (String)variable_combobox.getSelectedItem();
					if(variable == null) return;
					
					double first=0; double last=0; double bin_size=0;
					try {
						first = new Double(getStart_textfield().getText()).doubleValue();
						last = new Double(getEnd_textfield().getText()).doubleValue();
						bin_size = new Double(getBin_textfield().getText()).doubleValue();
					} catch(NumberFormatException nfe) {
						JOptionPane.showMessageDialog(SetupPlotHisto.this,
								                      "Please check the input values for the histogram",
								                      "Error Generating Histogram",
								                      JOptionPane.ERROR_MESSAGE);
						return;
					}
					
					int[] rows = current_model.getNonNullRows(variable);
					double[] values = current_model.getDataDouble(rows, current_model.getColumnNumber(variable));
					
					CalculateHistogram ch = new CalculateHistogram(values,first,last,bin_size);
					
					// ch now contains all data for the histogram plot
					HistogramPlot plotter = new HistogramPlot(variable,ch.getBins(),ch.getFrequency());
					SetupPlotHisto.this.getMainWindow().openWindow(new PlotDialog("Histogram: "+variable, plotter));
					dispose();
				}
			});
		}
		return plot_button;
	}

	/** For now, the Histogram plot setup window does not have any menu items
	 * 
	 * @return null, as there are no menu items
	 * @see com.approximatrix.draco.application.ChildFrame#getMenuItems()
	 */
	public JMenu[] getMenuItems() {
		// TODO Auto-generated method stub
		return null;
	}

	/** Returns the toolbar buttons for creating the histogram plot.
	 * 
	 * @see com.approximatrix.draco.application.ChildFrame#getToolbarButtons()
	 */
	public Component[] getToolbarButtons() {
		if(toolbarButtons == null) {
			toolbarButtons = new Component[1];
			toolbarButtons[0] = getPlot_button();
		}
		return toolbarButtons;
	}

	/** There is no reason to rename this window.  It is used only to setup
	 * a histogram plot
	 * 
	 * @return false
	 * @see com.approximatrix.draco.application.ChildFrame#isRenameable()
	 */
	public boolean isRenameable() {
		return false;
	}

	/** Returns the help key for the Data 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;
	}
	
}  //  @jve:decl-index=0:visual-constraint="10,10"
