/*
 *  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 javax.swing.JDialog;
import javax.swing.JMenu;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JList;

import java.util.ArrayList;

import javax.swing.JTable;

import com.approximatrix.draco.application.ApplicationManager;
import com.approximatrix.draco.application.ChildFrame;
import com.approximatrix.draco.datamodel.IndepDepTableModel;
import com.approximatrix.draco.datamodel.StatTableModel;

/*
 * Created on Jan 25, 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 PlotColumnData extends ChildFrame {

	private static final String DIALOG_NAME = "Plot Data";
	private static final int WARN_PLOT_THRESHHOLD = 5000;
	
	/** The string for requesting topical help */
	private static final String HELP_KEY = "plotcolumn";
	
	/** The icon to be displayed for this child window */
	private static final String PATH_TO_ICON = "/com/approximatrix/draco/main/ui/images/plotdata.png";
	
	private javax.swing.JPanel jContentPane = null;

	private JPanel jPanel = null;
	private JButton cancel_plot_button = null;
	private JButton create_plot_button = null;
	private StatTableModel current_model;
	
	private IndepDepTableModel selection_model = null;
	
	private JTable selectplot_table = null;
	private JScrollPane jScrollPane = null;
	
	
	public PlotColumnData(StatTableModel information) {
		super(DIALOG_NAME, 
				true, //resizable
				true, //closable
				true, //maximizable
				true);
		
		current_model = information;
				
		initialize();
		//this.setLocationRelativeTo(main_window.getMainWindow());
		
	}	
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setTitle("Plot Column Data");
		this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		this.setSize(300,200);
		this.setContentPane(getJContentPane());
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		//this.addWindowListener(new java.awt.event.WindowAdapter() { 
		//	public void windowClosing(java.awt.event.WindowEvent e) {    
		//		close_window();
		//	}
		//});

		// Put information in the list
		String[] dep_items = new String[current_model.getColumnCount()];
		for(int i=0;i<current_model.getColumnCount();i++) {
			dep_items[i] = current_model.getColumnName(i);
		}

		selection_model = new IndepDepTableModel(dep_items,true);
		selectplot_table.setModel(selection_model);
		
		super.setIcon(PATH_TO_ICON);
	}
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJContentPane() {
		if(jContentPane == null) {
			jContentPane = new javax.swing.JPanel();
			jContentPane.setLayout(new java.awt.BorderLayout());
			jContentPane.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
		}
		return jContentPane;
	}
	/**
	 * 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(getCreate_plot_button(), null);
			//jPanel.add(getCancel_plot_button(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getCreate_plot_button() {
		if (create_plot_button == null) {
			create_plot_button = new JButton();
			create_plot_button.setText("Create Plot");
			create_plot_button.addMouseListener(new java.awt.event.MouseAdapter() { 
				public void mouseClicked(java.awt.event.MouseEvent e) {
					
					// Get the data to plot
					String indep = selection_model.getIndependentVariable();
					String[] dep = selection_model.getDependentVariables();
					
					boolean already_warned = false;
					
					ArrayList data_sets = new ArrayList();
					for(int i=0;i<dep.length;i++) {
						String[] columns = new String[2];
						columns[0] = indep; columns[1] = dep[i];
						int[] rows = current_model.getNonNullRows(columns);
						
						// Check if this is a lot of data
						if(rows.length > WARN_PLOT_THRESHHOLD && !already_warned) {
							String[] message = new String[3];
							message[0] = new String("This data set contains approximately "+Integer.toString(current_model.getRowCount()));
							message[1] = new String("indpendent points.  Plotting may take a");
							message[2] = new String("significant amount of time.  Continue?");
							
							int rc = JOptionPane.showConfirmDialog(PlotColumnData.this.getMainWindow(),
																   message,
									                               ApplicationManager.APP_NAME+" Warning",
									                               JOptionPane.WARNING_MESSAGE);
							if(rc == JOptionPane.CANCEL_OPTION) {
								dispose();
								return;
							}
							already_warned = true;
						}
						
						double[][] data = current_model.getDataDouble(rows,current_model.getColumnNumber(columns));
						data_sets.add(data);
					}
					
					// Set the title
					String title = null;
					if(dep.length < 3) {
						StringBuffer title_buffer = new StringBuffer(dep[0]);
						for(int i=1;i<dep.length;i++) {
							title_buffer.append(" and ");
							title_buffer.append(dep[i]);
						}
						title_buffer.append(" vs. ");
						title_buffer.append(indep);
						title = title_buffer.toString();
					} else {
						StringBuffer title_buffer = new StringBuffer("Multiple Variables vs. ");
						title_buffer.append(indep);
						title = title_buffer.toString();
					}
					
					DataPlot panel = new DataPlot(data_sets,indep,dep,title);
					PlotColumnData.this.getMainWindow().openWindow(new PlotDialog(title,panel));
					dispose();
										
				}
			});
		}
		return create_plot_button;
	}
	/**
	 * This method initializes jTable	
	 * 	
	 * @return javax.swing.JTable	
	 */    
	private JTable getSelectplot_table() {
		if (selectplot_table == null) {
			selectplot_table = new JTable();
		}
		return selectplot_table;
	}
	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */    
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getSelectplot_table());
		}
		return jScrollPane;
	}

	/** For now, the Data 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() {
		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] = getCreate_plot_button();
		}
		return toolbarButtons;
	}

	/** There is no reason to rename this window.  It is used only to setup
	 * a data plot
	 * 
	 * @return false
	 * @see com.approximatrix.draco.application.ChildFrame#isRenameable()
	 */
	public boolean isRenameable() {
		return false;
	}
	
	/** Returns the help key for the Plot Column 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;
	}
}
