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

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.Frame;

public class ImportODS extends JDialog {

	private static final String BASE_TITLE = "Import ODS - ";
	
	private JPanel jContentPane = null;
	private JPanel jPanel = null;
	private JLabel jLabel = null;
	private JComboBox jWorksheetComboBox = null;
	private JPanel jPanel1 = null;
	private JLabel jLabel1 = null;
	private JTextField jHeaderRowTextField = null;
	private JLabel jLabel2 = null;
	private JTextField jRowStartTextField = null;
	private JLabel jLabel3 = null;
	private JTextField jRowEndTextField = null;
	private JLabel jLabel4 = null;
	private JTextField jColumnsTextField = null;
	private JPanel jPanel2 = null;
	private JButton jCancelButton = null;
	private JButton jImportButton = null;
	
	private String[] available_sheets = {"None"};
	
	private String selected_sheet = null;
	
	/**
	 * This is the default constructor
	 */
	public ImportODS(Frame parent, String filename, String[] worksheets) {
		super(parent,true);
		
		available_sheets = worksheets;
		
		System.out.print("Worksheet Count: ");
		System.out.println(worksheets.length);
		for(int i=0;i<worksheets.length;i++) {
			System.out.println(worksheets[i]);
		}
		
		initialize();
		if(filename != null)
			this.setTitle(BASE_TITLE+filename);
		this.setLocationRelativeTo(parent);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(364, 212);
		this.setContentPane(getJContentPane());
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				selected_sheet = null;
				ImportODS.this.setVisible(false);
			}
		});
	}

	/**
	 * 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) {
			jLabel = new JLabel();
			jLabel.setText("Worksheet to Import: ");
			jPanel = new JPanel();
			jPanel.add(jLabel, null);
			jPanel.add(getJWorksheetComboBox(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jWorksheetComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJWorksheetComboBox() {
		if (jWorksheetComboBox == null) {
			jWorksheetComboBox = new JComboBox(available_sheets);
			jWorksheetComboBox.setPreferredSize(new java.awt.Dimension(150,25));
		}
		return jWorksheetComboBox;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints4.gridy = 4;
			gridBagConstraints4.weightx = 1.0;
			gridBagConstraints4.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints4.gridx = 1;
			GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
			gridBagConstraints31.gridx = 0;
			gridBagConstraints31.anchor = java.awt.GridBagConstraints.EAST;
			gridBagConstraints31.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints31.gridy = 4;
			jLabel4 = new JLabel();
			jLabel4.setText("Columns to Import (Blank for all):");
			GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
			gridBagConstraints21.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints21.gridy = 3;
			gridBagConstraints21.weightx = 1.0;
			gridBagConstraints21.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints21.gridx = 1;
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 0;
			gridBagConstraints11.anchor = java.awt.GridBagConstraints.EAST;
			gridBagConstraints11.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints11.gridy = 3;
			jLabel3 = new JLabel();
			jLabel3.setText("Data End Row (Blank for all):");
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints3.gridy = 2;
			gridBagConstraints3.weightx = 1.0;
			gridBagConstraints3.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints3.gridx = 1;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.anchor = java.awt.GridBagConstraints.EAST;
			gridBagConstraints2.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints2.gridy = 2;
			jLabel2 = new JLabel();
			jLabel2.setText("Data Start Row:");
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints1.gridy = 0;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints1.gridx = 1;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
			gridBagConstraints.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints.gridy = 0;
			jLabel1 = new JLabel();
			jLabel1.setText("Row Containing Variable Names (0 for none):");
			jPanel1 = new JPanel();
			jPanel1.setLayout(new GridBagLayout());
			jPanel1.add(jLabel1, gridBagConstraints);
			jPanel1.add(getJHeaderRowTextField(), gridBagConstraints1);
			jPanel1.add(jLabel2, gridBagConstraints2);
			jPanel1.add(getJRowStartTextField(), gridBagConstraints3);
			jPanel1.add(jLabel3, gridBagConstraints11);
			jPanel1.add(getJRowEndTextField(), gridBagConstraints21);
			jPanel1.add(jLabel4, gridBagConstraints31);
			jPanel1.add(getJColumnsTextField(), gridBagConstraints4);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jHeaderRowTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJHeaderRowTextField() {
		if (jHeaderRowTextField == null) {
			jHeaderRowTextField = new JTextField("1");
		}
		return jHeaderRowTextField;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJRowStartTextField() {
		if (jRowStartTextField == null) {
			jRowStartTextField = new JTextField("2");
		}
		return jRowStartTextField;
	}

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

	/**
	 * This method initializes jColumnsTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJColumnsTextField() {
		if (jColumnsTextField == null) {
			jColumnsTextField = new JTextField();
			jColumnsTextField.setEnabled(false);
		}
		return jColumnsTextField;
	}

	/**
	 * This method initializes jPanel2	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setAlignment(java.awt.FlowLayout.RIGHT);
			jPanel2 = new JPanel();
			jPanel2.setLayout(flowLayout);
			jPanel2.add(getJImportButton(), null);
			jPanel2.add(getJCancelButton(), null);
		}
		return jPanel2;
	}

	/**
	 * This method initializes jCancelButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJCancelButton() {
		if (jCancelButton == null) {
			jCancelButton = new JButton();
			jCancelButton.setText("Cancel");
			jCancelButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					selected_sheet = null;
					ImportODS.this.setVisible(false);
				}
			});
		}
		return jCancelButton;
	}

	/**
	 * This method initializes jImportButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJImportButton() {
		if (jImportButton == null) {
			jImportButton = new JButton();
			jImportButton.setText("Import");
			jImportButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					selected_sheet = (String)getJWorksheetComboBox().getSelectedItem();
					ImportODS.this.setVisible(false);
				}
			});
		}
		return jImportButton;
	}
	
	public String getSelectedSheet() {
		return selected_sheet;
	}
	
	public int getHeaderRow() {
		try {
			return new Integer(getJHeaderRowTextField().getText()).intValue();
		} catch(NumberFormatException nfe) {
			return 0;
		}
	}
	
	public int getStartRow() {
		try {
			return new Integer(getJRowStartTextField().getText()).intValue();
		} catch(NumberFormatException nfe) {
			return 0;
		}
	}
	
	public int getEndRow() {
		try {
			return new Integer(getJRowEndTextField().getText()).intValue();
		} catch(NumberFormatException nfe) {
			return 0;
		}
	}

	public String getColumnString() {
		return getJColumnsTextField().getText();
	}
	
}  //  @jve:decl-index=0:visual-constraint="10,10"
