/*
 * AirDensityEstimateDialog.java
 *
 * Created on February 9, 2007, 12:29 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package usda.weru.erosion;

import java.awt.Window;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import usda.weru.erosion.gui.StemAreaIndexEstimateDialog_n;
import usda.weru.util.FormValidator;

/**
 *
 * @author Joseph Levin
 */
public class StemAreaIndexEstimateDialog extends StemAreaIndexEstimateDialog_n{
    private static final long serialVersionUID = 1L;
    
    /**
     * Return value if cancel is chosen.
     */
    public static final int CANCEL_OPTION = 1;
    
    /**
     * Return value if approve (yes, ok) is chosen.
     */
    public static final int APPROVE_OPTION = 0;

	/**
	 *
	 */
	protected int c_result = CANCEL_OPTION;

	/**
	 *
	 */
	protected double c_diameter;

	/**
	 *
	 */
	protected double c_height;

	/**
	 *
	 */
	protected double c_population;
    /** Creates a new instance of AirDensityEstimateDialog
	 * @param parent
	 * @param modal
	 * @param fv */
    public StemAreaIndexEstimateDialog(java.awt.Frame parent, FormValidator fv, boolean modal) {
        super(parent, modal);
        
        setFormValidator(fv);
    }

	/**
	 *
	 * @return
	 */
	public double calculateStemAreaIndex(){   
        //diamter is mm
        //height is cm                   
        return (c_diameter *  0.001) * (c_height * 0.01) * c_population;        
    }
    
    FormValidator fv = null;

	/**
	 *
	 * @param fv
	 */
	public void setFormValidator(FormValidator fv) {
        this.fv = fv;
        if (fv != null) fv.validateForm(this);
    }

	/**
	 *
	 * @return
	 */
	public int showDialog(){
        setVisible(true);       
        return c_result;
    }

	/**
	 *
	 * @param evt
	 */
	@Override
    protected void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {        
        c_result = CANCEL_OPTION;
        setVisible(false);
    }

	/**
	 *
	 * @param evt
	 */
	@Override
    protected void okayButtonActionPerformed(java.awt.event.ActionEvent evt) {        
        if(!fv.validateForm(this)){
            JOptionPane.showMessageDialog(this, "Estimation parameters are out of range.", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        c_result = APPROVE_OPTION;
         setVisible(false);
         Window win = SwingUtilities.getWindowAncestor(this);
      if (win != null) {
         win.dispose();
      }      
    }

	/**
	 *
	 * @param evt
	 */
	@Override
    protected void diameterFieldFocusLost(java.awt.event.FocusEvent evt) {
        if (!evt.isTemporary()){
            diameterFieldActionPerformed(null);
        }
    }

	/**
	 *
	 */
	protected String c_oldDiameter = "0.0";

	/**
	 *
	 * @param evt
	 */
	@Override
    protected void diameterFieldActionPerformed(java.awt.event.ActionEvent evt) {
        String valStr = g_diameterField.getText();
        try {
            double value = Double.parseDouble(valStr);            
            c_diameter = value;          
            c_oldDiameter = valStr;
        } catch (NumberFormatException k) {                
            c_diameter = Double.NaN;
            g_diameterField.setText(c_oldDiameter); 
        }
        fv.validateForm(g_diameterField);
    }

	/**
	 *
	 * @param evt
	 */
	@Override
    protected void heightFieldFocusLost(java.awt.event.FocusEvent evt) {
        if (!evt.isTemporary()){
            heightFieldActionPerformed(null);
        }
    }

	/**
	 *
	 */
	protected String c_oldHeight = "0.0";

	/**
	 *
	 * @param evt
	 */
	@Override
    protected void heightFieldActionPerformed(java.awt.event.ActionEvent evt) {
        String valStr = g_heightField.getText();
        try {
            double value = Double.parseDouble(valStr);            
            c_height = value;          
            c_oldHeight = valStr;
        } catch (NumberFormatException k) {                
            c_height = Double.NaN;
            g_heightField.setText(c_oldHeight); 
        }
        fv.validateForm(g_heightField);
    }

	/**
	 *
	 * @param evt
	 */
	@Override
    protected void populationFieldFocusLost(java.awt.event.FocusEvent evt) {        
        if (!evt.isTemporary()){
            populationFieldActionPerformed(null);
        }
    }

	/**
	 *
	 */
	protected String c_oldPopulation = "0.0";

	/**
	 *
	 * @param evt
	 */
	@Override
    protected void populationFieldActionPerformed(java.awt.event.ActionEvent evt) {
        String valStr = g_populationField.getText();
        try {
            double value = Double.parseDouble(valStr);            
            c_population = value;          
            c_oldPopulation = valStr;
        } catch (NumberFormatException k) {                
            c_population = Double.NaN;
            g_populationField.setText(c_oldPopulation); 
        }
        fv.validateForm(g_populationField);
    }
    
}
