/*
 *  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.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
/*
 * Created on Feb 10, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
import java.awt.Dimension;

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

	private javax.swing.JPanel jContentPane = null;

	private JPanel jPanel = null;
	private JButton set_width_cancel_button = null;
	private JButton set_width_ok_button = null;
	private JPanel jPanel1 = null;
	private JLabel jLabel = null;
	private JTextField column_width_textfield = null;
	private JCheckBox auto_size_checkbox = null;
	
	private JTable main_table = null;
	private int selected_column = -1;
	/**
	 * This is the default constructor
	 */
	
	public ColumnWidthDialog(JFrame parent, JTable passed_table, int this_column) {
		super(parent,true);
		initialize();
		this.setLocationRelativeTo(parent);
		
		main_table = passed_table;
		selected_column = this_column;
		
		auto_size_checkbox.setSelected(false);
		auto_size_checkbox.setEnabled(false);
		
		column_width_textfield.setText(new Integer(main_table.getColumnModel().getColumn(this_column).getWidth()).toString());		
	}
	
	public ColumnWidthDialog(JFrame parent, JTable passed_table) {
		super(parent,true);
		initialize();
		this.setLocationRelativeTo(parent);
		
		main_table = passed_table;
		
		if(main_table.getAutoResizeMode() == javax.swing.JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS) {
			auto_size_checkbox.setSelected(true);
			column_width_textfield.setEnabled(false);
		} else {
			auto_size_checkbox.setSelected(false);
			column_width_textfield.setEnabled(true);
		}
		
		column_width_textfield.setText(new Integer(main_table.getColumnModel().getColumn(0).getWidth()).toString());		
	}
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setTitle("Column Width");
		this.setSize(new Dimension(235, 133));
		this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		//this.setSize(300, 122);
		this.setContentPane(getJContentPane());
	}
	/**
	 * 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(getJPanel(), java.awt.BorderLayout.SOUTH);
			jContentPane.add(getJPanel1(), java.awt.BorderLayout.NORTH);
			jContentPane.add(getAuto_size_checkbox(), 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(getSet_width_ok_button(), null);
			jPanel.add(getSet_width_cancel_button(), null);
		}
		return jPanel;
	}
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getSet_width_cancel_button() {
		if (set_width_cancel_button == null) {
			set_width_cancel_button = new JButton();
			set_width_cancel_button.setText("Cancel");
			set_width_cancel_button.addMouseListener(new java.awt.event.MouseAdapter() { 
				public void mouseClicked(java.awt.event.MouseEvent e) {    
					dispose();
				}
			});
		}
		return set_width_cancel_button;
	}
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getSet_width_ok_button() {
		if (set_width_ok_button == null) {
			set_width_ok_button = new JButton();
			set_width_ok_button.setText("Ok");
			set_width_ok_button.addMouseListener(new java.awt.event.MouseAdapter() { 
				public void mouseClicked(java.awt.event.MouseEvent e) {    
					
					if(auto_size_checkbox.isEnabled()) { // All columns case
						if(auto_size_checkbox.isSelected()) {
							main_table.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
						} else {
							main_table.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
							for(int i=0;i<main_table.getColumnCount();i++) {
								main_table.getColumnModel().getColumn(i).setPreferredWidth(new Integer(column_width_textfield.getText()).intValue());
								main_table.getColumnModel().getColumn(i).setMinWidth(5);
								main_table.getColumnModel().getColumn(i).setWidth(new Integer(column_width_textfield.getText()).intValue());
							}
						}
					} else {
						int[] widths = new int[main_table.getColumnCount()];
						for(int i=0;i<main_table.getColumnCount();i++) {
							widths[i] = main_table.getColumnModel().getColumn(i).getWidth();
						}
						widths[selected_column] = new Integer(column_width_textfield.getText()).intValue();
						main_table.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
						for(int i=0;i<main_table.getColumnCount();i++) {
							main_table.getColumnModel().getColumn(i).setPreferredWidth(widths[i]);
							main_table.getColumnModel().getColumn(i).setMinWidth(5);
							main_table.getColumnModel().getColumn(i).setWidth(widths[i]);
						}
					}
					
					dispose();
				}
			});
		}
		return set_width_ok_button;
	}
	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */    
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jLabel = new JLabel();
			jPanel1 = new JPanel();
			jLabel.setText("Column Width: ");
			jPanel1.setLayout(new FlowLayout());
			jPanel1.add(jLabel, null);
			jPanel1.add(getColumn_width_textfield(), null);
		}
		return jPanel1;
	}
	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getColumn_width_textfield() {
		if (column_width_textfield == null) {
			column_width_textfield = new JTextField();
			column_width_textfield.setPreferredSize(new Dimension(100, 20));
		}
		return column_width_textfield;
	}
	/**
	 * This method initializes jCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */    
	private JCheckBox getAuto_size_checkbox() {
		if (auto_size_checkbox == null) {
			auto_size_checkbox = new JCheckBox();
			auto_size_checkbox.setText("Automatic Sizing");
			auto_size_checkbox.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {    
					if(auto_size_checkbox.isSelected())  {
						column_width_textfield.disable();
					} else {
						column_width_textfield.enable();
					}
					column_width_textfield.repaint();
				}
			});
			
			
			
		}
		return auto_size_checkbox;
	}
      }  //  @jve:decl-index=0:visual-constraint="11,12"
