package usda.weru.erosion;


import de.schlichtherle.io.File;
import java.beans.*; 
import java.util.*;
import usda.weru.util.*;
import java.text.NumberFormat;

class Biomass extends usda.weru.erosion.gui.Biomass_n implements PropertyChangeListener {

	private String measurementUnits = Util.SIUnits;
	private int curSubr = 0;
	
	private DataStore ds;

    public static void main(String[]args) {
        Biomass bio = new Biomass();
		bio.ds = new DataStore();
		bio.addPropertyChangeListener(bio.ds);
		bio.ds.addPropertyChangeListener(bio);
        bio.setSize(640,480);
		bio.setTitle("Daily Wind Erosion Model - Biomass Test");
		bio.setVisible(true);
//		bio.ds.readTextFile(new File("c:/weps.wrk/test/barrier_test.in"));
		bio.ds.readTextFile(new File("c:/weps.wrk/test/barrier_downwind.in"));
    }

	Hashtable HT_displayFields = new Hashtable();
	
	Biomass() {
		super();
		
		HT_displayFields.put(DataStore.BiomassHgt, JTF_height);
		HT_displayFields.put(DataStore.BiomassRSAI, JTF_stemAreaIndex);
		HT_displayFields.put(DataStore.BiomassRLAI, JTF_leafAreaIndex);
		HT_displayFields.put(DataStore.BiomassFlat, JTF_flatCover);
//		HT_displayFields.put(DataStore.BiomassStanding, JTF_standingCover);
//		HT_displayFields.put(DataStore.BiomassTotal, JTF_totalCover);
		HT_displayFields.put(DataStore.CropHgt, JTF_cropHgt);
		HT_displayFields.put(DataStore.CropGSAI, JTF_cropSAI);
		HT_displayFields.put(DataStore.CropGLAI, JTF_cropLAI);
		HT_displayFields.put(DataStore.CropRowSpacing, JTF_rowSpacing);
		HT_displayFields.put(DataStore.CropRowPlacemnt, JRB_seedRidge);

	}

/** Field and button handlers **/	

    double dummyDouble;

    protected void JP_residuePicsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JP_residuePicsActionPerformed
        PictureChooser pc = new PictureChooser(new File("pictures/residue/residue.xml"));
        pc.setDialogTitle("Residue flat cover  (m^2/m^2)");
        int result = pc.showDialog(this);        
        if (result == PictureChooser.APPROVE_OPTION){
            String valStr = pc.getValue(DataStore.BiomassFlat);
            if (valStr != null){
                changes.firePropertyChange(DataStore.BiomassFlat+"+"+curSubr, null, valStr);
            }
        } 
        pc.dispose();
        pc = null;
    }//GEN-LAST:event_JP_residuePicsActionPerformed
    


	protected void JTF_cropLAIFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_JTF_cropLAIFocusLost
//		if (evt.isTemporary()) return;
		String valStr = (String) HT_prop.get(evt.getSource());
		try {
			dummyDouble = Double.parseDouble(valStr);
		} catch (NumberFormatException k) {
		}
		Util.checkNumericJTF(JP_biomass, JTF_cropLAI, dummyDouble,
							 DataStore.CropGLAI+"+"+curSubr,
							 changes, measurementUnits);
	}//GEN-LAST:event_JTF_cropLAIFocusLost

	protected void JTF_cropLAIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JTF_cropLAIActionPerformed
		String valStr = (String) HT_prop.get(evt.getSource());
		try {
			dummyDouble = Double.parseDouble(valStr);
		} catch (NumberFormatException k) {
		}
		Util.checkNumericJTF(JP_biomass, JTF_cropLAI, dummyDouble,
							 DataStore.CropGLAI+"+"+curSubr,
							 changes, measurementUnits);
	}//GEN-LAST:event_JTF_cropLAIActionPerformed

	protected void JTF_cropSAIFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_JTF_cropSAIFocusLost
//		if (evt.isTemporary()) return;
		String valStr = (String) HT_prop.get(evt.getSource());
		try {
			dummyDouble = Double.parseDouble(valStr);
		} catch (NumberFormatException k) {
		}
		Util.checkNumericJTF(JP_biomass, JTF_cropSAI, dummyDouble,
							 DataStore.CropGSAI+"+"+curSubr,
							 changes, measurementUnits);
	}//GEN-LAST:event_JTF_cropSAIFocusLost

	protected void JTF_cropSAIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JTF_cropSAIActionPerformed
		String valStr = (String) HT_prop.get(evt.getSource());
		try {
			dummyDouble = Double.parseDouble(valStr);
		} catch (NumberFormatException k) {
		}
		Util.checkNumericJTF(JP_biomass, JTF_cropSAI, dummyDouble,
							 DataStore.CropGSAI+"+"+curSubr,
							 changes, measurementUnits);
	}//GEN-LAST:event_JTF_cropSAIActionPerformed
    
    protected void JB_estimateCropStemAreaIndex_actionPerformed(java.awt.event.ActionEvent evt) {
        StemAreaIndexEstimateDialog said = new StemAreaIndexEstimateDialog(this, true);        
        int result = said.showDialog();
        if (result == StemAreaIndexEstimateDialog.APPROVE_OPTION){
            JTF_cropSAI.setText(Double.toString(said.calculateStemAreaIndex()));
            Util.checkNumericJTF(JP_biomass, JTF_cropSAI, said.calculateStemAreaIndex(), DataStore.CropGSAI+"+"+curSubr,changes, measurementUnits);
        }
        said.dispose();
    }
    
	protected void JTF_cropHgtFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_JTF_cropHgtFocusLost
//		if (evt.isTemporary()) return;
		String valStr = (String) HT_prop.get(evt.getSource());
		try {
			dummyDouble = Double.parseDouble(valStr);
		} catch (NumberFormatException k) {
		}
		Util.checkNumericJTF(JP_biomass, JTF_cropHgt, dummyDouble,
							 DataStore.CropHgt+"+"+curSubr,
							 changes, measurementUnits);
	}//GEN-LAST:event_JTF_cropHgtFocusLost

	protected void JTF_cropHgtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JTF_cropHgtActionPerformed
		String valStr = (String) HT_prop.get(evt.getSource());
		try {
			dummyDouble = Double.parseDouble(valStr);
		} catch (NumberFormatException k) {
		}
		Util.checkNumericJTF(JP_biomass, JTF_cropHgt, dummyDouble,
							 DataStore.CropHgt+"+"+curSubr,
							 changes, measurementUnits);
	}//GEN-LAST:event_JTF_cropHgtActionPerformed

/*    protected void JTF_totalCoverFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_JTF_totalCoverFocusLost
//		if (evt.isTemporary()) return;
		String valStr = (String) HT_prop.get(evt.getSource());
		try {
			dummyDouble = Double.parseDouble(valStr);
		} catch (NumberFormatException k) {
		}
		Util.checkNumericJTF(JP_biomass, JTF_totalCover, dummyDouble,
							 DataStore.BiomassTotal+"+"+curSubr,
							 changes, measurementUnits);
    }//GEN-LAST:event_JTF_totalCoverFocusLost

    protected void JTF_totalCoverActionPerformed(java.awt.event.ActionEvent evt) {                                               
		String valStr = (String) HT_prop.get(evt.getSource());
		try {
			dummyDouble = Double.parseDouble(valStr);
		} catch (NumberFormatException k) {
		}
		Util.checkNumericJTF(JP_biomass, JTF_totalCover, dummyDouble,
							 DataStore.BiomassTotal+"+"+curSubr,
							 changes, measurementUnits);
    }                                                

/*    protected void JTF_standingCoverFocusLost(java.awt.event.FocusEvent evt) {                                            
//		if (evt.isTemporary()) return;
		String valStr = (String) HT_prop.get(evt.getSource());
		try {
			dummyDouble = Double.parseDouble(valStr);
		} catch (NumberFormatException k) {
		}
		Util.checkNumericJTF(JP_biomass, JTF_standingCover, dummyDouble,
							 DataStore.BiomassStanding+"+"+curSubr,
							 changes, measurementUnits);
    }                                             

/*    protected void JTF_standingCoverActionPerformed(java.awt.event.ActionEvent evt) {                                                  
		String valStr = (String) HT_prop.get(evt.getSource());
		try {
			dummyDouble = Double.parseDouble(valStr);
		} catch (NumberFormatException k) {
		}
		Util.checkNumericJTF(JP_biomass, JTF_standingCover, dummyDouble,
							 DataStore.BiomassStanding+"+"+curSubr,
							 changes, measurementUnits);
    }                                                   
*/
    protected void JTF_flatCoverFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_JTF_flatCoverFocusLost
//		if (evt.isTemporary()) return;
		String valStr = (String) HT_prop.get(evt.getSource());
		try {
			dummyDouble = Double.parseDouble(valStr);
		} catch (NumberFormatException k) {
		}
		Util.checkNumericJTF(JP_biomass, JTF_flatCover, dummyDouble,
							 DataStore.BiomassFlat+"+"+curSubr,
							 changes, measurementUnits);
    }//GEN-LAST:event_JTF_flatCoverFocusLost

	protected void JTF_flatCoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JTF_flatCoverActionPerformed
		String valStr = (String) HT_prop.get(evt.getSource());
		try {
			dummyDouble = Double.parseDouble(valStr);
		} catch (NumberFormatException k) {
		}
		Util.checkNumericJTF(JP_biomass, JTF_flatCover, dummyDouble,
							 DataStore.BiomassFlat+"+"+curSubr,
							 changes, measurementUnits);
	}//GEN-LAST:event_JTF_flatCoverActionPerformed

    protected void JRB_seedFurrowItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_JRB_seedFurrowItemStateChanged
		if (evt.getStateChange() != evt.SELECTED) return;
		changes.firePropertyChange(DataStore.CropRowPlacemnt+"+"+curSubr, null, "0");
    }//GEN-LAST:event_JRB_seedFurrowItemStateChanged

    protected void JRB_seedRidgeItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_JRB_seedRidgeItemStateChanged
		if (evt.getStateChange() != evt.SELECTED) return;
		changes.firePropertyChange(DataStore.CropRowPlacemnt+"+"+curSubr, null, "1");
    }//GEN-LAST:event_JRB_seedRidgeItemStateChanged

    protected void JTF_rowSpacingFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_JTF_rowSpacingFocusLost
//		if (evt.isTemporary()) return;
		String valStr = (String) HT_prop.get(evt.getSource());
		try {
			dummyDouble = Double.parseDouble(valStr);
		} catch (NumberFormatException k) {
		}
		Util.checkNumericJTF(JP_biomass, JTF_rowSpacing, dummyDouble,
							 DataStore.CropRowSpacing+"+"+curSubr,
							 changes, measurementUnits);
    }//GEN-LAST:event_JTF_rowSpacingFocusLost

    protected void JTF_rowSpacingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JTF_rowSpacingActionPerformed
		String valStr = (String) HT_prop.get(evt.getSource());
		try {
			dummyDouble = Double.parseDouble(valStr);
		} catch (NumberFormatException k) {
		}
		Util.checkNumericJTF(JP_biomass, JTF_rowSpacing, dummyDouble,
							 DataStore.CropRowSpacing+"+"+curSubr,
							 changes, measurementUnits);
    }//GEN-LAST:event_JTF_rowSpacingActionPerformed

    protected void JTF_leafAreaIndexFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_JTF_leafAreaIndexFocusLost
//		if (evt.isTemporary()) return;
		String valStr = (String) HT_prop.get(evt.getSource());
		try {
			dummyDouble = Double.parseDouble(valStr);
		} catch (NumberFormatException k) {
		}
		Util.checkNumericJTF(JP_biomass, JTF_leafAreaIndex, dummyDouble,
							 DataStore.BiomassRLAI+"+"+curSubr,
							 changes, measurementUnits);
    }//GEN-LAST:event_JTF_leafAreaIndexFocusLost

    protected void JTF_leafAreaIndexActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JTF_leafAreaIndexActionPerformed
		String valStr = (String) HT_prop.get(evt.getSource());
		try {
			dummyDouble = Double.parseDouble(valStr);
		} catch (NumberFormatException k) {
		}
		Util.checkNumericJTF(JP_biomass, JTF_leafAreaIndex, dummyDouble,
							 DataStore.BiomassRLAI+"+"+curSubr,
							 changes, measurementUnits);
    }//GEN-LAST:event_JTF_leafAreaIndexActionPerformed


    
    protected void JTF_stemAreaIndexFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_JTF_stemAreaIndexFocusLost
//		if (evt.isTemporary()) return;
		String valStr = (String) HT_prop.get(evt.getSource());
		try {
			dummyDouble = Double.parseDouble(valStr);
		} catch (NumberFormatException k) {
		}
		Util.checkNumericJTF(JP_biomass, JTF_stemAreaIndex, dummyDouble,
							 DataStore.BiomassRSAI+"+"+curSubr,
							 changes, measurementUnits);
    }//GEN-LAST:event_JTF_stemAreaIndexFocusLost

    protected void JTF_stemAreaIndexActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JTF_stemAreaIndexActionPerformed
		String valStr = (String) HT_prop.get(evt.getSource());
		try {
			dummyDouble = Double.parseDouble(valStr);
		} catch (NumberFormatException k) {
		}
		Util.checkNumericJTF(JP_biomass, JTF_stemAreaIndex, dummyDouble,
							 DataStore.BiomassRSAI+"+"+curSubr,
							 changes, measurementUnits);
    }//GEN-LAST:event_JTF_stemAreaIndexActionPerformed

    protected void JB_estimateResidueStemAreaIndex_actionPerformed(java.awt.event.ActionEvent evt) {
        StemAreaIndexEstimateDialog said = new StemAreaIndexEstimateDialog(this, true);        
        int result = said.showDialog();
        if (result == StemAreaIndexEstimateDialog.APPROVE_OPTION){
            JTF_stemAreaIndex.setText(Double.toString(said.calculateStemAreaIndex()));
            Util.checkNumericJTF(JP_biomass, JTF_stemAreaIndex, said.calculateStemAreaIndex(), DataStore.BiomassRSAI+"+"+curSubr,changes, measurementUnits);
        }
        said.dispose();
    }
    protected void JTF_heightFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_JTF_heightFocusLost
//		if (evt.isTemporary()) return;
		String valStr = (String) HT_prop.get(evt.getSource());
		try {
			dummyDouble = Double.parseDouble(valStr);
		} catch (NumberFormatException k) {
		}
		Util.checkNumericJTF(JP_biomass, JTF_height, dummyDouble,
							 DataStore.BiomassHgt+"+"+curSubr,
							 changes, measurementUnits);
    }//GEN-LAST:event_JTF_heightFocusLost

    protected void JTF_heightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JTF_heightActionPerformed
		String valStr = (String) HT_prop.get(evt.getSource());
		try {
			dummyDouble = Double.parseDouble(valStr);
		} catch (NumberFormatException k) {
		}
		Util.checkNumericJTF(JP_biomass, JTF_height, dummyDouble,
							 DataStore.BiomassHgt+"+"+curSubr,
							 changes, measurementUnits);
    }//GEN-LAST:event_JTF_heightActionPerformed

//	double dummyDouble;

	/****** Property change support methods *****/

	/**
     * RunFileData throws a property change each time setData is called.
     * Wrapper classes listen to determine if the the event is relevant to
     * them.
     */

	private PropertyChangeSupport changes = new PropertyChangeSupport(this);

	/**
     * Allows the container to add or register some other components to recognize the changes that occur
     * on this component.
     * @param l The listener that listens and reacts towards the the changes to be reflected.
     */
	public void addPropertyChangeListener(PropertyChangeListener l) {
		changes.addPropertyChangeListener(l);
	}

	/**
     * Allows the container to remove or de-register some other components and stop recognizing the
     * changes that occur on this component.
     * @param l The listener that does not listen and react towards the the changes to be reflected.
     */
	public void removePropertyChangeListener(PropertyChangeListener l) {
		changes.removePropertyChangeListener(l);
	}

	/**
     * Recognizes and takes appropriate actions on registered properties from different
     * screens to update and synchronize data and GUI screens as needed.
     * @param e The event itself that is responsible for triggering the change required for registered
     * properties/components.
     */

	Hashtable HT_prop = new Hashtable();

	public void propertyChange(PropertyChangeEvent e) {

		String property = e.getPropertyName();
        String value = "";
        try {
            value = (String) e.getNewValue();
        } catch (ClassCastException cce) {
            // not our kind of thing
            return;
        }
		if (property.indexOf("+") > 0) property = property.substring(0, property.indexOf("+"));
		Object obj = HT_displayFields.get(property);
//if (property.startsWith("Bio") || property.startsWith("Crop")) //System.out.println("B_pC: 1 " + property + " " + value);			
		if (obj != null) {
////System.out.println("B_pC: " + obj);
////System.out.println("B_pC: 2 " + property + " " + value);			
			if (obj instanceof javax.swing.JTextField) {
				javax.swing.JTextField jtf = (javax.swing.JTextField) obj;
				double val = 0.0;
				HT_prop.put(jtf, value);
				try {
					val = new Double(value).doubleValue();
					NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
					nf.setMaximumFractionDigits(3);
					nf.setMinimumFractionDigits(0);
					nf.setGroupingUsed(false);
					value = nf.format(val);
				} catch (NumberFormatException f) {
				}
				jtf.setText(value.trim());
			} else if (obj instanceof javax.swing.JRadioButton) {
				javax.swing.JRadioButton jcr = (javax.swing.JRadioButton) obj;
//				jcr.setSelected(!value.trim().equals("0"));
				if (value.trim().equals("1")) jcr.doClick();
				JRB_seedFurrow.setSelected(value.trim().equals("0"));
			}
		}
	}

	/***** specific property change handles ********/


}
	
	