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

import de.schlichtherle.io.File;
import de.schlichtherle.io.FileReader;
import java.awt.*;
import javax.swing.*;
import java.beans.*;
import java.util.*;
import java.awt.event.*;
import java.io.BufferedReader;
import javax.help.*;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

import org.apache.log4j.Logger;
import usda.weru.util.*;

/**
 * This GUI allows the user to select the different types of barries that could be
 * put along the different directions like north, east, etc.
 * @author  manmohan
 */
public class BarPanel extends usda.weru.weps.gui.BarPanel_n implements PropertyChangeListener {
    private static final Logger LOGGER = Logger.getLogger(BarPanel.class);
    private boolean init;
    Hashtable ht = new Hashtable();
    Vector barVec = new Vector();
    String measurementUnits = "";
    private boolean JRBN_Flag = false;
    private boolean JRBE_Flag = false;
    private boolean JRBW_Flag = false;
    private boolean JRBS_Flag = false;
    /**
     * Tells if the radio button for none items is selected or not.
     */
    protected boolean JRBNone_Flag = false;

    /** 
     * Default constructor that creates the new form BarPanel_n 
     */
    public BarPanel() {
        super();
        loadData();
        JP_main.setVisible(true);
        addHelp();
        JRB_None.setVisible(false);

//		ht.put(RunFileData.BarrierN, new Barrier().makeBarrier());
//		ht.put(RunFileData.BarrierS, new Barrier().makeBarrier());
//		ht.put(RunFileData.BarrierW, new Barrier().makeBarrier());
//		ht.put(RunFileData.BarrierE, new Barrier().makeBarrier());

        JCB_E.addPopupMenuListener(new PopupMenuListener() {

            public void popupMenuCanceled(PopupMenuEvent e) {
            }

            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
            }

            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                ItemEvent tmp = new ItemEvent(JCB_E, ItemEvent.ITEM_LAST - 1, JCB_E.getSelectedItem(), ItemEvent.SELECTED);
                JRB_E.setSelected(true);
                selectBarrierType(tmp);
            }
        });
        JCB_W.addPopupMenuListener(new PopupMenuListener() {

            public void popupMenuCanceled(PopupMenuEvent e) {
            }

            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
            }

            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                ItemEvent tmp = new ItemEvent(JCB_W, ItemEvent.ITEM_LAST - 2, JCB_W.getSelectedItem(), ItemEvent.SELECTED);
                JRB_W.setSelected(true);
                selectBarrierType(tmp);
            }
        });
        JCB_S.addPopupMenuListener(new PopupMenuListener() {

            public void popupMenuCanceled(PopupMenuEvent e) {
            }

            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
            }

            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                ItemEvent tmp = new ItemEvent(JCB_S, ItemEvent.ITEM_LAST - 3, JCB_S.getSelectedItem(), ItemEvent.SELECTED);
                JRB_S.setSelected(true);
                selectBarrierType(tmp);
            }
        });
        JCB_N.addPopupMenuListener(new PopupMenuListener() {

            public void popupMenuCanceled(PopupMenuEvent e) {
            }

            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
            }

            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                ItemEvent tmp = new ItemEvent(JCB_N, ItemEvent.ITEM_LAST - 4, JCB_N.getSelectedItem(), ItemEvent.SELECTED);
                JRB_N.setSelected(true);
                selectBarrierType(tmp);
            }
        });
    }

    /**
     * Single argument constructor for the barrier panel
     * @param sTitle This is the title string of the GUI frame.
     */
    public BarPanel(String sTitle) {
        this();
        setTitle(sTitle);
    }


    /**
     * This method populates the drop down list on this panel with the required 
     * iformation to be selected by the user when creating a barrier along each 
     * direction.
     * @param inputFile The file that has the data that is used as barriers in
     * different drop down list or combo boxes
     */
    private Vector readBarrierFile(File inputFile) {

        Vector rtnVec = new Vector();
        Barrier inpBar = new Barrier();
        rtnVec.add(inpBar);
        try {
            BufferedReader in = new BufferedReader(new FileReader(inputFile));
            for (String line = in.readLine(); line != null; line = in.readLine()) {
                StringTokenizer st = new StringTokenizer(line, "|");
                if (st.countTokens() == 5) {
                    inpBar = new Barrier(line);
                    if (inpBar.wid > 0.0) {
                        rtnVec.add(inpBar);
                    }
                } else {
                }
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rtnVec;
    }

    /**
     * Initializes the combo boxes with the data populared in the vector from
     * the input file that was used as data source for these components.
     * @param barVec The vector that is used as data source to populate all 
     * the drop down list for different types of barriers used.
     */
    private void initTypeBoxes(Vector barVec) {
        init = true;
        JCB_N.removeAllItems();
        JCB_S.removeAllItems();
        JCB_E.removeAllItems();
        JCB_W.removeAllItems();
        for (Iterator vi = barVec.iterator(); vi.hasNext();) {
            Object obj = vi.next();
            JCB_N.addItem(obj);
            JCB_S.addItem(obj);
            JCB_E.addItem(obj);
            JCB_W.addItem(obj);
        }

        // make sure that no button is on
        //System.out.println("BP : initTypeBoxes() : ----START---- Setting all the RADIO-BUTTON OFF");
        JRB_N.setSelected(false);
        JRB_E.setSelected(false);
        JRB_S.setSelected(false);
        JRB_W.setSelected(false);
        //System.out.println("BP : initTypeBoxes() : ----END---- Set all the RADIO-BUTTON OFF");
        init = false;
    }

    /**
     * Links the "Help" jar file and the "HelpSet" files from the jar and help
     * directories so that they could be used in the application for setting up 
     * the generic as well as context sensitive help system for visible GUI 
     * components. The html pages are used for making it web
     */
    private void addHelp() {
        CSH.setHelpIDString(JP_main, "barPanel_html");
        //  CSH.setHelpIDString(JL_Title, "WEPS Field Barriers");
        CSH.setHelpIDString(JRB_N, "barLocType_html");
        CSH.setHelpIDString(JCB_N, "barLocType_html");
        CSH.setHelpIDString(JRB_S, "barLocType_html");
        CSH.setHelpIDString(JCB_S, "barLocType_html");
        CSH.setHelpIDString(JRB_E, "barLocType_html");
        CSH.setHelpIDString(JCB_E, "barLocType_html");
        CSH.setHelpIDString(JRB_W, "barLocType_html");
        CSH.setHelpIDString(JCB_W, "barLocType_html");
        CSH.setHelpIDString(JB_detail, "barEditPanel_html");
    }

    /**
     * This method is used if this dialog is rum as an independent application.
     * @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.PROJECT, projectsDir,
                WepsFileChooser.OPEN);
        wfc.setCurrentDirectory(new de.schlichtherle.io.File("c:/weps/weps.install/projects"));
        RunFileData rfd = new RunFileData();
        if (wfc.showDialog(null) == JFileChooser.APPROVE_OPTION) {
            File sf = new File(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");
            rfd = new RunFileData();
        }

        BarPanel bp = new BarPanel();
        bp.setVisible(true);
        rfd.addPropertyChangeListener(bp);
        bp.addPropertyChangeListener(rfd);

        DrawField df = new DrawField();
        df.setBackground(Color.white);

        JFrame frame = new JFrame("Draw Field Test");
        frame.setBackground(Color.white);
        frame.setSize(240, 240);
        frame.setLocation(200, 200);
        frame.setContentPane(df);
        frame.setVisible(true);

        bp.addPropertyChangeListener(df);
//		df.addPropertyChangeListener(bp);
        bp.changes.firePropertyChange(RunFileData.XLength, null, "100");
        bp.changes.firePropertyChange(RunFileData.YLength, null, "150");
        rfd.fireAll(bp);
//		bp.dumpHT();
    }
    /*****************************************************************************/
    private String XLen = "";
    private String YLen = "";

    @SuppressWarnings("empty-statement")
    public void JB_detail_actionPerformed(java.awt.event.ActionEvent event) {
        Barrier bi = null;
        String title = null;
        String barName = null;
        String length = "";

        if (JRB_N.isSelected()) {
            title = "North Barrier";
            bi = (Barrier) ht.get(RunFileData.BarrierN);
            barName = RunFileData.BarrierN;
            length = XLen;
        } else if (JRB_S.isSelected()) {
            title = "South Barrier";
            bi = (Barrier) ht.get(RunFileData.BarrierS);
            barName = RunFileData.BarrierS;
            length = XLen;
        } else if (JRB_E.isSelected()) {
            title = "East Barrier";
            bi = (Barrier) ht.get(RunFileData.BarrierE);
            barName = RunFileData.BarrierE;
            length = YLen;
        } else if (JRB_W.isSelected()) {
            title = "West Barrier";
            bi = (Barrier) ht.get(RunFileData.BarrierW);
            barName = RunFileData.BarrierW;
            length = YLen;
        }

        if(barName == null){
            Logger.getLogger(BarPanel.class).debug("No barrier selected for editing.");
            return;
        }

        Component j = this;
        for (; j.getParent() != null; j = j.getParent());
        
        

        if(bi != null){            
            BarDialog bd = new BarDialog(Weps.getInstance(), title, measurementUnits, bi, changes,barName, length);
            bd.setModal(true);
            bd.setVisible(true);
        }
        else{
            JOptionPane.showMessageDialog(this, "Selected barrier is NULL.  Unable to edit or view", "Barrier Editor", JOptionPane.WARNING_MESSAGE);
        }
    }

 /*****************************************************************************/
    private void updateBarrierBox(PropertyChangeEvent pce, JComboBox jcb) {
        if (pce == null) {
            return;
        }
        Barrier inpBar = new Barrier((String) pce.getNewValue());
////System.out.println("BP_uBB: " + inpBar);
        ht.put(pce.getPropertyName(), inpBar);
        Barrier selBar = (Barrier) jcb.getSelectedItem();
        if (!inpBar.equals(selBar) && !inpBar.name.equals("none")) {
//		if (!inpBar.equals(selBar)) {
            jcb.addItem(inpBar);			// comment out for debugging
            jcb.setSelectedItem(inpBar);	// comment out for debugging
        }
    }

    /*****************************************************************************/
    private void clearData(PropertyChangeEvent e) {
        if (e.getNewValue() != null) {
            initTypeBoxes(barVec);
        }
    }

    /*****************************************************************************/
    private void loadData() {
        String barrierPath = ConfigData.getDefault().getDataParsed(ConfigData.BarriersFile);
        File barrierFile = new File(barrierPath).getCanOrAbsFile();
        if(!barrierFile.canRead()){
            LOGGER.warn("Unable to read barrier file: " + barrierFile.getCanOrAbsPath());
            barVec = new Vector();
            initTypeBoxes(barVec);
            return;
        }
        barVec = readBarrierFile(barrierFile);

        initTypeBoxes(barVec);

        Enumeration e = inpHt.keys();
        while (e.hasMoreElements()) {
            Object key = e.nextElement();
            Object value = inpHt.get(key);
//			//System.out.println("LP_lD: " + key + " >> " + ((PropertyChangeEvent) value).getNewValue());
        }
//		setLatLong((PropertyChangeEvent) ht.get(RunFileData.LatLong));

        updateBarrierBox((PropertyChangeEvent) inpHt.get(RunFileData.BarrierN), JCB_N);
        updateBarrierBox((PropertyChangeEvent) inpHt.get(RunFileData.BarrierS), JCB_S);
        updateBarrierBox((PropertyChangeEvent) inpHt.get(RunFileData.BarrierW), JCB_W);
        updateBarrierBox((PropertyChangeEvent) inpHt.get(RunFileData.BarrierE), JCB_E);
        setXLength((PropertyChangeEvent) inpHt.get(RunFileData.XLength));
        setYLength((PropertyChangeEvent) inpHt.get(RunFileData.YLength));

    //JRB_None.setSelected(true);
    }
    /*****************************************************************************/
    private Hashtable inpHt = new Hashtable();
    private boolean initFlg = false;

    /**
     * This method triggers specific actions based upon who generates the 
     * events that occur on accessing the components sitting in their parent
     * container on the Barrier Panel.
     * @param e The generated event by the component's access.
     */
    public void propertyChange(PropertyChangeEvent e) {

        // init properties
        if (e.getPropertyName().equals(RunFileData.LoadData)) {
            initFlg = e.getNewValue().equals("1");
            if (!initFlg) {
                loadData();
            }
        } else if (initFlg) {
            inpHt.put(e.getPropertyName(), e);

        // runfiledata properties


        } else if (e.getPropertyName().equals(RunFileData.BarrierN)) {
            updateBarrierBox(e, JCB_N);
        } else if (e.getPropertyName().equals(RunFileData.BarrierS)) {
            updateBarrierBox(e, JCB_S);
        } else if (e.getPropertyName().equals(RunFileData.BarrierW)) {
            updateBarrierBox(e, JCB_W);
        } else if (e.getPropertyName().equals(RunFileData.BarrierE)) {
            updateBarrierBox(e, JCB_E);
        } else if (e.getPropertyName().equals(RunFileData.BarrierD)) {
        } else if (e.getPropertyName().equals(ConfigData.Units)) {
            setUnits(e);
        } else if (e.getPropertyName().equals(RunFileData.XLength)) {
            setXLength(e);
        } else if (e.getPropertyName().equals(RunFileData.YLength)) {
            setYLength(e);
        }
        else if(ConfigData.BarriersReadonly.equals(e.getPropertyName())){
            if("1".equals(e.getNewValue())){
                JB_detail.setText("View Selected Barrier");
            }
            else{
                JB_detail.setText("Edit Selected Barrier");
            }
        }
        else if(ConfigData.BarriersFile.equals(e.getPropertyName())){
            loadData();
        }
    }

    private void setUnits(PropertyChangeEvent e) {
        if(e == null){
            return;
        }
        measurementUnits = (String) e.getNewValue();
    }

    private void setXLength(PropertyChangeEvent e) {
        if(e == null){
            return;
        }
        XLen = (String) e.getNewValue();
    }

    private void setYLength(PropertyChangeEvent e) {
        if(e == null){
            return;
        }
        YLen = (String) e.getNewValue();
    }
    private PropertyChangeSupport changes = new PropertyChangeSupport(this);

    /**
     * Add listeners to the components to recognize the changes made on this panel.
     * @param l The listener object for the components on the panel to be removed.
     */
    public void addPropertyChangeListener(PropertyChangeListener l) {
        changes.addPropertyChangeListener(l);
    }

    /**
     * Removes listeners for the components to recognize the changes made on this panel.
     * @param l The listener object for the components on the panel to be removed.
     */
    public void removePropertyChangeListener(PropertyChangeListener l) {
        changes.removePropertyChangeListener(l);
    }

    void getBarrier(Barrier b) {
        /*		b.width = JTF_Width.getText();
        b.porosity = JTF_Porosity.getText();
        b.height = JTF_Height.getText();
        //		b.name = (String) JCB_Type.getSelectedItem();
        try {
        b.hgt = Double.parseDouble(b.height);
        b.por = Double.parseDouble(b.porosity);
        b.wid = Double.parseDouble(b.width);
        } catch (NumberFormatException e) {
        //System.err.println("BP-gB: " + e);
        }*/
    }

    void setBarrier(Barrier b) {
        /*		barCurrent = b;
        JTF_Width.setText(b.width);
        JTF_Porosity.setText(b.porosity);
        JTF_Height.setText(b.height);
        try {
        //			JCB_Type.setSelectedItem(b.name);
        } catch (java.util.NoSuchElementException e) {
        //System.err.println("sB: barrier type not found");
        //			JCB_Type.setSelectedIndex(0);
        }*/
    }

    private void setSelectionOff(JRadioButton me) {
        if (me != JRB_N) {
            JRB_N.setSelected(false);
        }
        if (me != JRB_E) {
            JRB_E.setSelected(false);
        }
        if (me != JRB_S) {
            JRB_S.setSelected(false);
        }
        if (me != JRB_W) {
            JRB_W.setSelected(false);
        }
        if (me != JRB_None) {
            JRB_None.setSelected(false);
        }
    }

    void changeToNoneButton(java.awt.event.MouseEvent evt) {

        //System.out.println("changeToNoneButton: Some JRB thats already selected called me.");

        JRadioButton me = (JRadioButton) evt.getSource();
        if (JRBNone_Flag && me == JRB_N) {
            //System.out.println("changeToNoneButton : De-Selecting NORTH radio Button when it was already selected Manmohan");
            JRB_None.setSelected(true);
            JRBNone_Flag = false;
            setSelectionOff(me);
        }

        if (JRBNone_Flag && me == JRB_E) {
            //System.out.println("changeToNoneButton : De-Selecting EAST radio Button when it was already selected Manmohan");
            JRB_None.setSelected(true);
            JRBNone_Flag = false;
            setSelectionOff(me);
        }

        if (JRBNone_Flag && me == JRB_W) {
            //System.out.println("changeToNoneButton : De-Selecting WEST radio Button when it was already selected Manmohan");
            JRB_None.setSelected(true);
            JRBNone_Flag = false;
            setSelectionOff(me);
        }

        if (JRBNone_Flag && me == JRB_S) {
            //System.out.println("changeToNoneButton : De-Selecting SOUTH radio Button when it was already selected Manmohan");
            JRB_None.setSelected(true);
            JRBNone_Flag = false;
            setSelectionOff(me);
        }
    }

    private void changeSelectedButton(java.awt.event.MouseEvent event) {

        //JRadioButton jrb = null;
        JRadioButton me = (JRadioButton) event.getSource();

        if (JRB_N.isSelected() || JRB_S.isSelected() || JRB_E.isSelected() || JRB_W.isSelected()) {
            if (me == JRB_N) {
                //System.out.println("BP_changeSelectedButton_Selected : Manmohan_N");
                JB_detail.setEnabled((JCB_N.getSelectedIndex() != 0));
                changes.firePropertyChange(DrawField.SelectN, null, " ");

                // else{
                setSelectionOff(me);
            //JRBNone_Flag = false;
            // }

            } else if (me == JRB_S) {
                //System.out.println("BP_changeSelectedButton_Selected : Manmohan_S");
                JB_detail.setEnabled((JCB_S.getSelectedIndex() != 0));
                changes.firePropertyChange(DrawField.SelectS, null, " ");

                // else{
                setSelectionOff(me);
            //JRBNone_Flag = false;
            //}

            } else if (me == JRB_E) {
                //System.out.println("BP_changeSelectedButton_Selected : Manmohan_E");
                JB_detail.setEnabled((JCB_E.getSelectedIndex() != 0));
                changes.firePropertyChange(DrawField.SelectE, null, " ");

                //else{
                setSelectionOff(me);
            //JRBNone_Flag = false;
            // }     

            } else if (me == JRB_W) {
                //System.out.println("BP_changeSelectedButton_Selected : Manmohan_W");
                JB_detail.setEnabled((JCB_W.getSelectedIndex() != 0));
                changes.firePropertyChange(DrawField.SelectW, null, " ");

                // else{
                setSelectionOff(me);
            //JRBNone_Flag = false;
            //}

            }

        } else {
            if (me == JRB_N) {
                //System.out.println("BP_changeSelectedButton_NON_Selected : Manmohan");
                changes.firePropertyChange(DrawField.DeselectN, null, " ");
            } else if (me == JRB_S) {
                changes.firePropertyChange(DrawField.DeselectS, null, " ");
            } else if (me == JRB_E) {
                changes.firePropertyChange(DrawField.DeselectE, null, " ");
            } else if (me == JRB_W) {
                changes.firePropertyChange(DrawField.DeselectW, null, " ");
            } else if (me == JRB_None) {
                changes.firePropertyChange(DrawField.SelectNone, null, " ");
            }
        }

        if (!JRB_N.isSelected() && !JRB_S.isSelected() &&
                !JRB_E.isSelected() && !JRB_W.isSelected()) {
            JB_detail.setEnabled(false);
        }
        if (JRB_None.isSelected()) {
            changes.firePropertyChange(DrawField.SelectNone, null, " ");
            setSelectionOff(me);

        }


    }

    private void changeSelectedButton(JRadioButton jrb_passed) {


        //if (JRB_N.isSelected() || JRB_S.isSelected() || JRB_E.isSelected() || JRB_W.isSelected() ){
        if (JRB_N == jrb_passed) {
            //System.out.println("BP_changeSelectedButton_Selected : JRB_N");
            JB_detail.setEnabled((JCB_N.getSelectedIndex() != 0));
            changes.firePropertyChange(DrawField.SelectN, null, " ");
            setSelectionOff(JRB_N);

        // else{
        // setSelectionOff(me);
        //JRBNone_Flag = false;
        // }

        } else if (JRB_S == jrb_passed) {
            //System.out.println("BP_changeSelectedButton_Selected : JRB_S");
            JB_detail.setEnabled((JCB_S.getSelectedIndex() != 0));
            changes.firePropertyChange(DrawField.SelectS, null, " ");
            setSelectionOff(JRB_S);

        // else{
        //setSelectionOff(me);
        //JRBNone_Flag = false;
        //}

        } else if (JRB_E == jrb_passed) {
            //System.out.println("BP_changeSelectedButton_Selected : JRB_E");
            JB_detail.setEnabled((JCB_E.getSelectedIndex() != 0));
            changes.firePropertyChange(DrawField.SelectE, null, " ");
            setSelectionOff(JRB_E);

        //else{
        // setSelectionOff(me);
        //JRBNone_Flag = false;
        // }     

        } else if (JRB_W == jrb_passed) {
            //System.out.println("BP_changeSelectedButton_Selected : JRB_W");
            JB_detail.setEnabled((JCB_W.getSelectedIndex() != 0));
            changes.firePropertyChange(DrawField.SelectW, null, " ");
            setSelectionOff(JRB_W);

        // else{
        //setSelectionOff(me);
        //JRBNone_Flag = false;
        //}

        }
    //}
    }

    /**
     * The "North" direction radio button that recognizes the mouse event for
     * selecting/de-selecting the options for the barriers on the panel.
     * @param evt The mouse click event for the radio button to change the state.
     */
    public void JRBN_mouseClicked(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:  if(JRB_W.isSelected() == true ){
        if (JRB_N.isSelected() == true) {
            JRB_W.setSelected(false);
            JRB_E.setSelected(false);
            JRB_S.setSelected(false);
            JRB_None.setSelected(false);
            changeSelectedButton(evt);
        } else {
            changes.firePropertyChange(DrawField.DeselectN, null, " ");
        }
        if (JRB_N.isSelected() == false && JRB_E.isSelected() == false && JRB_W.isSelected() == false && JRB_S.isSelected() == false) {
            JRB_None.setSelected(true);
        }


    }

    /**
     * The "South" direction radio button that recognizes the mouse event for
     * selecting/de-selecting the options for the barriers on the panel.
     * @param evt The mouse click event for the radio button to change the state.
     */
    public void JRBS_mouseClicked(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:  if(JRB_W.isSelected() == true ){
        if (JRB_S.isSelected() == true) {
            JRB_N.setSelected(false);
            JRB_E.setSelected(false);
            JRB_W.setSelected(false);
            JRB_None.setSelected(false);
            changeSelectedButton(evt);
        } else {
            changes.firePropertyChange(DrawField.DeselectS, null, " ");
        }
        if (JRB_N.isSelected() == false && JRB_E.isSelected() == false && JRB_W.isSelected() == false && JRB_S.isSelected() == false) {
            JRB_None.setSelected(true);
        }

    }

    /**
     * The "East" direction radio button that recognizes the mouse event for
     * selecting/de-selecting the options for the barriers on the panel.
     * @param evt The mouse click event for the radio button to change the state.
     */
    public void JRBE_mouseClicked(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:  if(JRB_W.isSelected() == true ){
        if (JRB_E.isSelected() == true) {
            JRB_N.setSelected(false);
            JRB_W.setSelected(false);
            JRB_S.setSelected(false);
            JRB_None.setSelected(false);
            changeSelectedButton(evt);
        } else {
            changes.firePropertyChange(DrawField.DeselectE, null, " ");
        }

        if (JRB_N.isSelected() == false && JRB_E.isSelected() == false && JRB_W.isSelected() == false && JRB_S.isSelected() == false) {
            JRB_None.setSelected(true);
        }

    }

    /**
     * The "West" direction radio button that recognizes the mouse event for
     * selecting/de-selecting the options for the barriers on the panel.
     * @param evt The mouse click event for the radio button to change the state.
     */
    public void JRBW_mouseClicked(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
        if (JRB_W.isSelected() == true) {
            JRB_N.setSelected(false);
            JRB_E.setSelected(false);
            JRB_S.setSelected(false);
            JRB_None.setSelected(false);
            changeSelectedButton(evt);
        } else {
            changes.firePropertyChange(DrawField.DeselectW, null, " ");
        }
        if (JRB_N.isSelected() == false && JRB_E.isSelected() == false && JRB_W.isSelected() == false && JRB_S.isSelected() == false) {
            JRB_None.setSelected(true);
        }
    }

    /**
     * The "NONE" direction radio button that recognizes the mouse event for
     * selecting/de-selecting the options for the barriers on the panel. 
     * Basically this button when selected de-selects all the other radio button
     * and the dropdown list boxes.
     * @param evt The mouse click event for the radio button to change the state.
     */
    public void JRBNone_mouseClicked(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
        JRB_None.setSelected(true);
    }

    private void selectBarrierType(java.awt.event.ItemEvent event) {
        if (event.getStateChange() != java.awt.event.ItemEvent.SELECTED) {
            return;
        }
        JRadioButton jrb = null;
        String barName = null;
        String showBar = null;
        String hideBar = null;

        if (event.getSource() == JCB_N) {
            //jrb = null;
            jrb = JRB_N;
            //setSelectionOff(jrb);
            changeSelectedButton(jrb);
            barName = RunFileData.BarrierN;
            showBar = DrawField.ShowN;
            hideBar = DrawField.HideN;
        //changeSelectedButton(JRB_N);

        } else if (event.getSource() == JCB_S) {
            //jrb = null;
            jrb = JRB_S;
            //setSelectionOff(jrb);
            changeSelectedButton(jrb);
            barName = RunFileData.BarrierS;
            showBar = DrawField.ShowS;
            hideBar = DrawField.HideS;
        //changeSelectedButton(JRB_S);
        } else if (event.getSource() == JCB_W) {
            //jrb = null;
            jrb = JRB_W;
            //setSelectionOff(jrb);
            barName = RunFileData.BarrierW;
            showBar = DrawField.ShowW;
            hideBar = DrawField.HideW;
            changeSelectedButton(JRB_W);
        } else if (event.getSource() == JCB_E) {
            //jrb = null;
            jrb = JRB_E;
            //setSelectionOff(jrb);
            barName = RunFileData.BarrierE;
            showBar = DrawField.ShowE;
            hideBar = DrawField.HideE;
            changeSelectedButton(JRB_E);
        }

        //changeSelectedButton(jrb);

        if (barName == null || !ht.containsKey(barName)) {
            return;
        }
        Barrier bi = (Barrier) event.getItem();
        String newBar = bi.makeBarrier();
        ht.put(barName, bi);

        changes.firePropertyChange(barName, /*oldBar,*/ "", newBar);

        if (!bi.name.equals(Barrier.NoBarrier)) {
            changes.firePropertyChange(showBar, null, " ");
            jrb.setSelected(true);
        } else {
            changes.firePropertyChange(hideBar, null, " ");

            //jrb.setSelected(false);

            if ((JCB_N.getSelectedIndex() == 0) && (JCB_E.getSelectedIndex() == 0) && (JCB_W.getSelectedIndex() == 0) && (JCB_S.getSelectedIndex() == 0)) {

                JRB_None.setSelected(true);
                changes.firePropertyChange(DrawField.SelectNone, null, " ");

            }

        }


    }

    /**
     * The "NORTH" direction combo box when selected, allows the user to 
     * select/de-select the various barrier options provided in the list of 
     * north combo box in the panel.
     * Basically this drop down list allows the user to select/de-select the 
     * options provided there and incorporate those changes in the model's data.
     * @param evt The mouse click event for the listbox to allow the change of 
     * state due to a different selection of the items.
     */
    public void JCBN_itemStateChanged(java.awt.event.ItemEvent event) {

        if (init) {
            return;
        }
        // to do: code goes here.
        selectBarrierType(event);
        if (event.getStateChange() == java.awt.event.ItemEvent.SELECTED) {
            JB_detail.setEnabled(JCB_N.getSelectedIndex() != 0);
        }
        if ((JCB_N.getSelectedIndex() == 0) && (JCB_S.getSelectedIndex() != 0 || JCB_E.getSelectedIndex() != 0 || JCB_W.getSelectedIndex() != 0)) {

            JRB_N.setSelected(true);
        }
    }

    /**
     * The "SOUTH" direction combo box when selected, allows the user to 
     * select/de-select the various barrier options provided in the list of 
     * south combo box in the panel.
     * Basically this drop down list allows the user to select/de-select the 
     * options provided there and incorporate those changes in the model's data.
     * @param evt The mouse click event for the listbox to allow the change of 
     * state due to a different selection of the items.
     */
    public void JCBS_itemStateChanged(java.awt.event.ItemEvent event) {
        if (init) {
            return;
        }
        selectBarrierType(event);
        if (event.getStateChange() == java.awt.event.ItemEvent.SELECTED) {
            JB_detail.setEnabled(JCB_S.getSelectedIndex() != 0);
        }
        if ((JCB_S.getSelectedIndex() == 0) && (JCB_W.getSelectedIndex() != 0 || JCB_E.getSelectedIndex() != 0 || JCB_N.getSelectedIndex() != 0)) {

            JRB_S.setSelected(true);
        }
    }

    /**
     * The "EAST" direction combo box when selected, allows the user to 
     * select/de-select the various barrier options provided in the list of 
     * east combo box in the panel.
     * Basically this drop down list allows the user to select/de-select the 
     * options provided there and incorporate those changes in the model's data.
     * @param evt The mouse click event for the listbox to allow the change of 
     * state due to a different selection of the items.
     */
    public void JCBE_itemStateChanged(java.awt.event.ItemEvent event) {
        if (init) {
            return;
        }
        // to do: code goes here.
        selectBarrierType(event);
        if (event.getStateChange() == java.awt.event.ItemEvent.SELECTED) {
            JB_detail.setEnabled(JCB_E.getSelectedIndex() != 0);
        }
        if ((JCB_E.getSelectedIndex() == 0) && (JCB_S.getSelectedIndex() != 0 || JCB_W.getSelectedIndex() != 0 || JCB_N.getSelectedIndex() != 0)) {

            JRB_E.setSelected(true);
        }
    }

    /**
     * The "WEST" direction combo box when selected, allows the user to 
     * select/de-select the various barrier options provided in the list of 
     * west combo box in the panel.
     * Basically this drop down list allows the user to select/de-select the 
     * options provided there and incorporate those changes in the model's data.
     * @param evt The mouse click event for the listbox to allow the change of 
     * state due to a different selection of the items.
     */
    public void JCBW_itemStateChanged(java.awt.event.ItemEvent event) {
        if (init) {
            return;
        }
        selectBarrierType(event);
        if (event.getStateChange() == java.awt.event.ItemEvent.SELECTED) {
            JB_detail.setEnabled(JCB_W.getSelectedIndex() != 0);
        }
        if ((JCB_W.getSelectedIndex() == 0) && (JCB_S.getSelectedIndex() != 0 || JCB_E.getSelectedIndex() != 0 || JCB_N.getSelectedIndex() != 0)) {

            JRB_W.setSelected(true);
        }
    }

    /**
     * When the "NORTH" direction combo box gains focus, it indicates that
     * the items from the list are ready to be selected as barriers along that
     * direction.
     * @param evt The mouse click / keyboard event for the listbox to allow 
     * the change of state for selection of different items.
     */
    void JCBN_focusGained(java.awt.event.FocusEvent event) {
        // to do: code goes here.
        JB_detail.setEnabled((JCB_N.getSelectedIndex() != 0));
        changes.firePropertyChange(DrawField.SelectN, null, " ");
        setSelectionOff(JRB_N);

    }

    /**
     * When the "SOUTH" direction combo box gains focus, it indicates that
     * the items from the list are ready to be selected as barriers along that
     * direction.
     * @param evt The mouse click / keyboard event for the listbox to allow 
     * the change of state for selection of different items.
     */
    void JCBS_focusGained(java.awt.event.FocusEvent event) {
        // to do: code goes here.
        JB_detail.setEnabled((JCB_S.getSelectedIndex() != 0));
        changes.firePropertyChange(DrawField.SelectS, null, " ");
        setSelectionOff(JRB_S);

    }

    /**
     * When the "EAST" direction combo box gains focus, it indicates that
     * the items from the list are ready to be selected as barriers along that
     * direction.
     * @param evt The mouse click / keyboard event for the listbox to allow 
     * the change of state for selection of different items.
     */
    void JCBE_focusGained(java.awt.event.FocusEvent event) {
        // to do: code goes here.
        JB_detail.setEnabled((JCB_E.getSelectedIndex() != 0));
        changes.firePropertyChange(DrawField.SelectE, null, " ");
        setSelectionOff(JRB_E);

    }

    /**
     * When the "WEST" direction combo box gains focus, it indicates that
     * the items from the list are ready to be selected as barriers along that
     * direction.
     * @param evt The mouse click / keyboard event for the listbox to allow 
     * the change of state for selection of different items.
     */
    void JCBW_focusGained(java.awt.event.FocusEvent event) {
        // to do: code goes here.
        JB_detail.setEnabled((JCB_W.getSelectedIndex() != 0));
        changes.firePropertyChange(DrawField.SelectW, null, " ");
        setSelectionOff(JRB_W);

    }
}
