/*
 * 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 javax.swing.JOptionPane;
import usda.weru.erosion.gui.StemAreaIndexEstimateDialog_n;

/**
 *
 * @author Joseph Levin
 */
public class StemAreaIndexEstimateDialog extends StemAreaIndexEstimateDialog_n{
    /**
     * 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 */
    public StemAreaIndexEstimateDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
    }
    
    public double calculateStemAreaIndex(){        
        return c_diameter * c_height * c_population;        
    }
    
    public int showDialog(){
        setVisible(true);       
        return c_result;
    }
    
    protected void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {        
        c_result = CANCEL_OPTION;
        setVisible(false);
    }
    
    protected void okayButtonActionPerformed(java.awt.event.ActionEvent evt) {
        c_result = APPROVE_OPTION;
        setVisible(false);
    }
          
    protected void diameterFieldFocusLost(java.awt.event.FocusEvent evt) {
        if (!evt.isTemporary()){
            diameterFieldActionPerformed(null);
        }
    }
    
    protected String c_oldDiameter = "0.0";
    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); 
        }
    }

    protected void heightFieldFocusLost(java.awt.event.FocusEvent evt) {
        if (!evt.isTemporary()){
            heightFieldActionPerformed(null);
        }
    }
    
    protected String c_oldHeight = "0.0";
    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); 
        }
    }

    protected void populationFieldFocusLost(java.awt.event.FocusEvent evt) {
        if (!evt.isTemporary()){
            populationFieldActionPerformed(null);
        }
    }

    protected String c_oldPopulation = "0.0";
    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); 
        }
    }
    
}
