/*
 A basic implementation of the JFrame class.
 */
package usda.weru.weps;

import de.schlichtherle.truezip.file.TFile;
import javax.swing.*;
import java.beans.*;
import javax.help.*;

import usda.weru.util.*;

/**
 * This panel contains information regarding the customer for a WEPS simulation run. There are three methods 
 * available for specifying the length of a WEPS simulation run (NRCS, Dates, Cycle). The user can select the 
 * specified "WEPS run length" method to use from the 'Run' tab of the 'Configuration' panel, which is 
 * accessible via the 'Configuration' menu. These methods require different user inputs. The pertinent input
 * and display fields are visible at the bottom of the Simulation Run Information panel, depending upon the 
 * method specified in the configuration settings.
 */
public class ClientPanel extends usda.weru.weps.gui.ClientPanel_n implements PropertyChangeListener {

    private static final long serialVersionUID = 1L;

    /**
     * Default constructor for Simulation panel that that makes all the components related to that panel like
     * farm, track, field number with the client name visible along with the initialized state showing 
     * start & end dates of the Date mode of the simulation RUN.
     */
    public ClientPanel() {
        super();
        addHelp();
    }

    /**
     * Single argument constructor for Simulation panel that that makes all the components related to that panel like
     * farm, track, field number with the client name visible along with the initialized state showing 
     * start & end dates of the Date mode of the simulation RUN.
     * @param sTitle The string that is used as the title for the window in which the GUI appears.
     */
    public ClientPanel(String sTitle) {
        this();
        setTitle(sTitle);
    }

    private void addHelp() {
        // Context Sensitive Help tags for Simulation Run Information Panel

        CSH.setHelpIDString(JP_main, "clientPanel_html");
        //CSH.setHelpIDString(JL_Title, "WEPS client");
        CSH.setHelpIDString(JLabel1, "clientPanel_html");
        CSH.setHelpIDString(JTF_ClientName, "clientPanel_html");
        CSH.setHelpIDString(JLabel2, "clientPanel_html");
        CSH.setHelpIDString(JTF_FieldID, "clientPanel_html");
        CSH.setHelpIDString(JLabel3, "clientPanel_html");
        CSH.setHelpIDString(JTF_TractID, "clientPanel_html");
        CSH.setHelpIDString(JLabel4, "clientPanel_html");
        CSH.setHelpIDString(JTF_FarmID, "clientPanel_html");

    }

    /*======== End of constructors / Start of standalone test code */
    /**
     * Entry point for testing standalone. Used if this dialog is rum as an independent application. 
     * If executed, makes the GUI for Simulation Region panel visible.
     * @param args These are the command line arguments passed to the main method.
     */
    static public void main(String args[]) {

        String projectsDir = "";

        WepsFileChooser wfc = new WepsFileChooser(WepsFileChooser.Filetype.PROJECT, projectsDir,
                WepsFileChooser.SELECT);
        wfc.setCurrentDirectory(new de.schlichtherle.truezip.file.TFile("c:/weps/weps.install/projects"));
        RunFileData rfd = new RunFileData();
        if (wfc.showDialog(null) == JFileChooser.APPROVE_OPTION) {
            TFile sf = new TFile(wfc.getSelectedFile());
            try {
                projectsDir = sf.getCanonicalPath();
            } catch (java.io.IOException e) {//System.err.println(
//				"Error getting canoncial path of projectsDir");
            }
            rfd.readRunData(projectsDir);
            rfd.showRunFileData("this is a test");
        } else {
            //System.err.println("No project directory selected");
            System.exit(1);
        }

        ConfigData cd = ConfigData.getDefault();

        ClientPanel sp = new ClientPanel();
        sp.setVisible(true);

        ConfigPanel cp = new ConfigPanel();

        cp.addPropertyChangeListener(cd);
        cd.addPropertyChangeListener(cp);
        cd.addPropertyChangeListener(sp);
        rfd.addPropertyChangeListener(sp);
        sp.addPropertyChangeListener(rfd);

        cd.fireAll(cp);
        rfd.fireAll(cp);

        cp.setVisible(true);
    }

    /*======== End of standalone test code / Start of component handlers */

    /*
     * ActionPerformed and FocusLost are considered identical for text
     * fields.  Therefore, ActionPerformed is simply going to call
     * FocusLost to do validation.
     */
    @Override
    public void JTFClientName_focusLost(java.awt.event.FocusEvent event) {
        changes.firePropertyChange(RunFileData.UserName, null, JTF_ClientName.getText());
    }

    @Override
    public void JTFFieldID_focusLost(java.awt.event.FocusEvent event) {
        changes.firePropertyChange(RunFileData.FieldId, null, JTF_FieldID.getText());
    }

    @Override
    public void JTFTractID_focusLost(java.awt.event.FocusEvent event) {
        changes.firePropertyChange(RunFileData.TractId, null, JTF_TractID.getText());
    }

    @Override
    public void JTFFarmID_focusLost(java.awt.event.FocusEvent event) {
        changes.firePropertyChange(RunFileData.FarmId, null, JTF_FarmID.getText());
    }

    @Override
    public void JTFClientName_actionPerformed(java.awt.event.ActionEvent event) {
        JTFClientName_focusLost(new java.awt.event.FocusEvent(JTF_ClientName, 0));
    }

    @Override
    public void JTFFieldID_actionPerformed(java.awt.event.ActionEvent event) {
        JTFFieldID_focusLost(new java.awt.event.FocusEvent(JTF_FieldID, 0));
    }

    @Override
    public void JTFTractID_actionPerformed(java.awt.event.ActionEvent event) {
        JTFTractID_focusLost(new java.awt.event.FocusEvent(JTF_TractID, 0));
    }

    @Override
    public void JTFFarmID_actionPerformed(java.awt.event.ActionEvent event) {
        JTFFarmID_focusLost(new java.awt.event.FocusEvent(JTF_FarmID, 0));
    }

    /**
     *
     * @param event
     */
    public void JBWrite_actionPerformed(java.awt.event.ActionEvent event) {
        changes.firePropertyChange(RunFileData.ShowRunData, null, "SimPanel");
    }

    /**
     * Recognizes and takes appropriate actions on registered properties from different screens to update and
     * synchronize data and GUI screens as needed. SimPanel throws a property change each time setData is
     * called. Wrapper classes listen to determine if the the event is relevant to them.
     * @param e The event itself that is responsible for triggering the change required for registered 
     * properties/components.
     */
    @Override
    public void propertyChange(PropertyChangeEvent e) {
        switch (e.getPropertyName()) {
            case RunFileData.UserName:
                JTF_ClientName.setText((String) e.getNewValue());
                break;
            case RunFileData.FarmId:
                JTF_FarmID.setText((String) e.getNewValue());
                break;
            case RunFileData.TractId:
                JTF_TractID.setText((String) e.getNewValue());
                break;
            case RunFileData.FieldId:
                JTF_FieldID.setText((String) e.getNewValue());
                break;
        }
    }

    /*======== End of property change handlers / Start of property change support */
    private final 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.
     */
    @Override
    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.
     */
    @Override
    public void removePropertyChangeListener(PropertyChangeListener l) {
        changes.removePropertyChangeListener(l);
    }
}
