/*
 * 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 {

    private static final long serialVersionUID = 1L;

    /**
     * The value by which the tillage direction is adjusted.
     * It is assigned a default value of 1.
     */
    public int delta = 1;
    private final 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(String.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);
    }

    @Override
    public void JBCancel_actionPerformed(java.awt.event.ActionEvent evt) {
        this.setVisible(false);
        //	setModal(false);
        delta = 0;
    }

    @Override
    public void JBOk_actionPerformed(java.awt.event.ActionEvent evt) {
        this.setVisible(false);
        //	setModal(false);
        if (JRB_decrement.isSelected()) {
            delta = -delta;
        }
        //System.out.println("TillageChange:JB_OK:" + delta);
    }

    @Override
    public void JRBDecrement_itemStateChanged(java.awt.event.ItemEvent evt) {
        if (evt.getStateChange() == java.awt.event.ItemEvent.SELECTED) {
            setTitle();
        }
    }

    @Override
    public void JRBIncrement_itemStateChanged(java.awt.event.ItemEvent evt) {
        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);
        }
    }

    @Override
    public void JTFValue_actionPerformed(java.awt.event.ActionEvent evt) {
        validateInt(JTF_value);
    }

    @Override
    public void JTFValue_focusLost(java.awt.event.FocusEvent evt) {
        if (evt.isTemporary()) {
            return;
        }
        validateInt(JTF_value);
    }

}
