/*
 * TillageChange.java
 *
 * Created on May 2, 2005, 5:10 PM
 */

package usda.weru.mcrew;

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


/**
 * @author neha
 */
public class TillageChange extends usda.weru.mcrew.gui.TillageChange_n
{
    
    /**
     * The value by which the tillage direction is adjusted.
     * It is assigned a default value of 1.
     */    
    public int delta = 1;
    private String adjValue = "";
        
    /**
     * Creates a new instance of TillageChange
     * @param parent The owner frame from which the dialog is displayed
     * @param modal true for a modal dialog, false for one that allows
     * other windows to be active at the same time.
     */
    public TillageChange(Frame parent, boolean modal)
	{
		super(parent,modal);
		setTitle();
		JTF_value.setText(adjValue.valueOf(delta));
	}

    /**
     * An empty Constructor that does not take any arguments
     */    
        public TillageChange()
        {
            
        }
        /**
         * A constructor that sets the title of the dialog
         * @param sTitle The String to display in the dialog's title bar
         */        
        public TillageChange(String sTitle)
	{
		setTitle(sTitle);
	}
    
        
     public void JBCancel_actionPerformed(java.awt.event.ActionEvent evt) {
     // to do: code goes here.
		this.setVisible(false);
	//	setModal(false);
                delta = 0;
    }
    
    public void JBOk_actionPerformed(java.awt.event.ActionEvent evt) {
        // to do: code goes here.
		this.setVisible(false);
	//	setModal(false);
		if (JRB_decrement.isSelected()) {
			delta = - delta;
		}
		//System.out.println("TillageChange:JB_OK:" + delta);
    }
    
    public void JRBDecrement_itemStateChanged(java.awt.event.ItemEvent evt) {
        // to do: code goes here.
	if (evt.getStateChange() == java.awt.event.ItemEvent.SELECTED) {
		setTitle();
	}
    }
    
    public void JRBIncrement_itemStateChanged(java.awt.event.ItemEvent evt) {
        // to do: code goes here.
	if (evt.getStateChange() == java.awt.event.ItemEvent.SELECTED) {
		setTitle();
	}
    }
    
    

    /**
     * Show the dialog
     * @param args No need of any command line arguments to run main
     */    
	static public void main(String args[])
	{
		(new TillageChange()).setVisible(true);
	}

	private void setTitle() {
		String dir = "";
		dir = (JRB_increment.isSelected()) ? "Increment" : "Decrement";
		setTitle(dir + " Tillage Direction");
	}		
	
	
	
	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 JTFValue_actionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
            	validateInt(JTF_value);
        }
        
        public void JTFValue_focusLost(java.awt.event.FocusEvent evt) {
            // TODO add your handling code here:
            if (evt.isTemporary()) return;
		validateInt(JTF_value);
        }
    
}
