/*
		A basic implementation of the JDialog class.
*/

package usda.weru.mcrew;

import java.awt.*;
import javax.swing.*;

/**
 * This class basically is responsible for the changes that happen to the date cell in
 * main table of MCREW screen.
 */
public class DateChange extends usda.weru.mcrew.gui.DateChange_n
{

    /**
     * Constructor that sets the default date in date column cells of the MCREW table when
     * MCREW is instantiated from the WEPS main screen.
     * @param parent The frame in which the table sits i:e the container.
     */    
	public DateChange(Frame parent)
	{
		super(parent);
                setTitle("Adjust Date");
		updateFields();
		JTF_year.setText("1");
		JTF_month.setText("1");
		JTF_day.setText("7");
		delta = 1;
		//}}
	}

    /**
     * The basic constructor that makes calls to the one argument constructor passing the
     * frame object that is needed for holding the table.
     */    
	public DateChange()
	{
		this((Frame)null);
	}

    /**
     * The constructor for date change
     * @param sTitle The title of the frame that hold the table,
     */    
	public DateChange(String sTitle)
	{
		this();
		setTitle(sTitle);
	}

    /**
     * Main method that instantiates the DateChange object.
     * @param args
     */    
	static public void main(String args[])
	{
		(new DateChange()).setVisible(true);
	}

	private void updateFields() {
		String dir = "";
		String unit = "";

		dir = (JRB_increment.isSelected()) ? "Increment" : "Decrement";

		JTF_year.setVisible(false);
		JTF_month.setVisible(false);
		JTF_day.setVisible(false);

		if (JRB_year.isSelected()) {
			unit = "years";
			JTF_year.setVisible(true);
			delta = Integer.parseInt(JTF_year.getText().trim()); // added by Sada on July, 02, 03
			/* Consider the case, where in  the user just selects a different radio button thna the default 
			 * and then clicks OK. Now delta contains 1 as intialized in the cconstructor. But the default values for other 
			 * fields are different
			 */
		} else if(JRB_month.isSelected()) {
			unit = "months";
			JTF_month.setVisible(true);
			delta = Integer.parseInt(JTF_month.getText().trim());
		} else {
			unit = "days";
			JTF_day.setVisible(true);
			delta = Integer.parseInt(JTF_day.getText().trim());
		}
	}		
	
	public void JRBYear_itemStateChanged(java.awt.event.ItemEvent event)
	{
		// to do: code goes here.
		if (event.getStateChange() == java.awt.event.ItemEvent.SELECTED) {
			updateFields();
		}
	}

	public void JRBMonth_itemStateChanged(java.awt.event.ItemEvent event)
	{
		// to do: code goes here.
		if (event.getStateChange() == java.awt.event.ItemEvent.SELECTED) {
			updateFields();
		}
	}

	public void JRBDay_itemStateChanged(java.awt.event.ItemEvent event)
	{
		// to do: code goes here.
		if (event.getStateChange() == java.awt.event.ItemEvent.SELECTED) {
			updateFields();
		}
	}

	public void JRBIncrement_itemStateChanged(java.awt.event.ItemEvent event)
	{
		// to do: code goes here.
		if (event.getStateChange() == java.awt.event.ItemEvent.SELECTED) {
			updateFields();
		}
	}

	public void JRBDecrement_itemStateChanged(java.awt.event.ItemEvent event)
	{
		// to do: code goes here.
		if (event.getStateChange() == java.awt.event.ItemEvent.SELECTED) {
			updateFields();
		}
			 
	}

    
    /**
     * Holds the integer value for the date field.
     */    
	public int delta = 0;
	
	private void validateInt(JTextField jtf) {
		String valStr = jtf.getText().trim();
		try {
			delta = (valStr.length() == 0) ? 0 : Integer.parseInt(valStr);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this,
										  "Invalid number " + valStr,
										  "Error converting number",
										  JOptionPane.ERROR_MESSAGE);
			usda.weru.util.Util.setFocus(jtf);
		}
	}

	public void JTFYear_actionPerformed(java.awt.event.ActionEvent event)
	{
		// to do: code goes here.
		validateInt(JTF_year);
	}

	public void JTFMonth_actionPerformed(java.awt.event.ActionEvent event)
	{
		// to do: code goes here.
		validateInt(JTF_month);
	}

	public void JTFDay_actionPerformed(java.awt.event.ActionEvent event)
	{
		// to do: code goes here.
		validateInt(JTF_day);
	}

	public void JTFYear_focusLost(java.awt.event.FocusEvent event)
	{
		// to do: code goes here.
		if (event.isTemporary()) return;
		validateInt(JTF_year);
	}

	public void JTFMonth_focusLost(java.awt.event.FocusEvent event)
	{
		// to do: code goes here.
		if (event.isTemporary()) return;
		validateInt(JTF_month);
	}

	public void JTFDay_focusLost(java.awt.event.FocusEvent event)
	{
		// to do: code goes here.
		if (event.isTemporary()) return;
		validateInt(JTF_day);
	}

    /**
     * Stores the integer value for year/month/day converted from the text value.
     */    
	public int type = 0;;
	
	public void JBOK_actionPerformed(java.awt.event.ActionEvent event)
	{
		// to do: code goes here.
		this.setVisible(false);
		setModal(false);
		if (JRB_year.isSelected()) {
			type = JulianCalendar.YEAR;
		} else if (JRB_month.isSelected()) {
			//System.out.println("Month selected");
			type = JulianCalendar.MONTH;
		} else {
			type = JulianCalendar.DAY_OF_MONTH;
		}
		if (JRB_decrement.isSelected()) {
			delta = - delta;
		}
		//System.out.println("DataChange:JB_OK" + delta);
	}

	public void JBCancel_actionPerformed(java.awt.event.ActionEvent event)
	{
		// to do: code goes here.
		this.setVisible(false);
		setModal(false);
		type = 0;
	}
}
