<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package usda.weru.mcrew;

import de.schlichtherle.truezip.file.TFile;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.awt.EventQueue;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.help.CSH;
import javax.help.HelpBroker;
import javax.help.HelpSet;
import javax.swing.Icon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JToggleButton;
import javax.swing.MenuElement;

import org.apache.log4j.Logger;

import usda.weru.mcrew.gui.TablePanel_n;
import usda.weru.remoteDataAccess.csip.CsipLmodUtil;
import usda.weru.remoteDataAccess.remoteFiles.inetFile.CrLmodFile;
import usda.weru.resources.Resources;
import usda.weru.util.ConfigData;
import usda.weru.util.Util;
import usda.weru.util.wepsFileChooser2.WepsFileChooser2;
import usda.weru.util.wepsFileChooser2.WepsFileTypes2;
import usda.weru.weps.Weps;

/**
 * This is the main class for the MCREW (Management/Crop Rotation Editor for WEPS)
 * and will serve as the entry point for it. It also sets up
 * the management and has all the functions associated with it.
 *
 * The visual components are derived from Mcrew_b.java class author : Sudhir Kaul, Sada, Manmohan
 * date : Dec 2004
 */
public class TablePanel extends TablePanel_n implements ActionListener, ItemListener {

    private static final long serialVersionUID = 1L;
    private static final Logger LOGGER = Logger.getLogger(TablePanel.class);
    private String refreshMcrewScreen = "false";
    
    /**
     * Use this flag when running MCREW in standalone mode.
     */
    private boolean standaloneFlg = false;

    /**
     * This flag tells if the label was set for the MCREW screen.
     */
    private boolean c_labelState = false;
    public static final Icon rotMode = Resources.getIcon("MCREWRot.png");
    public static final Icon intMode = Resources.getIcon("MCREWInt.png");
    private MCREWWindow coordinator;
    public static final String tablePanelNewFilePropChange = "tablePanelNewFilePropChange";
    
  
    /**
     * Tells whether the modified data from crop, operation or main MCREW table was saved
     * successfully in the underlying data structure.
     */
    public boolean saveSuccess = false;
    
    /**
     * This string tells us the whether the file to be saved has a title or not.
     */
    public static final String sUntitledFileName = "Untitled";
    
    /**
     * This is the version of this software &amp; not of the man files
     */
    public static final String sVersionLine = "Version: 1.1";
    
    /**
     * Sets the default extension for the files that are saved in.
     */
    public static final String sDefaultFileExtension = XMLConstants.sWepsFileExtension;
    
    /**
     * Directory path where the files are saved.
     */
    public  String savePath = "";
    
    /**
     * Absolute Template directory path
     */
    public String saveTemplatePath;
    
    /**
     * Name of the present open Management file in Mcrew
     */
    public String fileName = null;
    
    /**
     * Name of the absolute path of the Present SAVED Management file in Mcrew so that this can be
     * sent to AuxPanel
     */
    public String absFileName = null;
    
    /**
     * Name of the absolute path of the present Management file OPENED in MCREW
     */
    public String filePathName = null;
    
    /**
     * Table object associated with the main MCREW screen and the subsequent Crop &amp; operation dialog
     * screens that hold the data in various rows.
     */
    Table table;
    
    /**
     * Notes related to Weps management file go here in the text area held by this dialog box which
     * can be pulled any time and modifications made here are saved for future retrieval.
     */
    public NotesMan notes;
    
    /**
     * Minimum Width of the main MCREW screen. Cannot reduce the screen width below this width even
     * if done manually.
     */
    public static final int MIN_WIDTH = 630;
    
    /**
     * Minimum Height of the main MCREW screen. Cannot reduce the screen height below this height
     * even if done manually.
     */
    public static final int MIN_HEIGHT = 380;

    /**
     * Side offsets used when the MCREW frame is resized and other components sitting in the
     * container are to be automatically resized to make the application look evenly spaced in with
     * reference the edges.
     */
    public static final int sideOffSet = 10;

    /**
     * Component offset is used when the MCREW frame is resized and other components sitting in the
     * container are to be automatically resized and spaced for uniformity with respect to the
     * adjacent components in the container.
     */
    public static final int componentOffSet = 5;

    /**
     * Popup is the default popup menu which is shown on single /multiple row selections.
     */
    private JPopupMenu popup;

    public JPopupMenu getPopup() {
        return popup;
    }

    /**
     * datePopup is the default popup menu which is shown on single /multiple row selections in the
     * date column of the main MCREW table.
     */
    private JPopupMenu datePopup;

    public JPopupMenu getDatePopup() {
        return datePopup;
    }

    /**
     * Popup for operation cell to get to the operation dialog box or accessing the functions
     * provided for the operation cell in main MCREW screen. This popup is the default popup menu
     * which is shown on single /multiple row selections in the operation column of the main MCREW
     * table.
     */
    private JPopupMenu operationCellPopup;

    public JPopupMenu getOperationPopup() {
        return operationCellPopup;
    }

    /**
     * Popup for crop cell to get to the crop dialog box or accessing the functions provided for the
     * crop cell in main MCREW screen. This popup is the default popup menu which is shown on single
     * /multiple row selections in the crop column of the main MCREW table.
     */
    private JPopupMenu cropCellPopup;

    public JPopupMenu getCropPopup() {
        return cropCellPopup;
    }

    /**
     * Popup for tillage cell of a crop to access the update functions for the tillage operation in
     * main MCREW screen. This popup is the default popup menu which is shown on single row
     * selections in the tillage column of the main MCREW table.
     */
    private JPopupMenu tillageCellPopup;

    public JPopupMenu getTillagePopup() {
        return tillageCellPopup;
    }

    /**
     * These vectors are created when the requirement to show combined menus arise. i.e for eg When
     * a complete row(s) is(are) selected and the user right clicks in the operation column, then it
     * should display a combination of operationCellPopup and operationPopup. The combination of
     * these two menu have to be created every time, or after the first time, the individual popup or
     * operationCellPopup will show nothing Also, showing one menu directly below the other will not
     * work either.
     */
    private List&lt;MenuElement&gt; popupVec;

    public List&lt;MenuElement&gt; getPopupElements() {
        return popupVec;
    }

    /**
     * Holds all the pop-up options for the right click event on the cells in date column
     */
    private List&lt;MenuElement&gt; datePopupVec;

    public List&lt;MenuElement&gt; getDatePopupElements() {
        return datePopupVec;
    }

    /**
     * Holds all the pop-up options for the right click event on the cells in operation column
     */
    private List&lt;MenuElement&gt; operationCellPopupVec;

    public List&lt;MenuElement&gt; getOperationPopupElements() {
        return operationCellPopupVec;
    }

    /**
     * Holds all the pop-up options for the right click event on the cells in crop column
     */
    private List&lt;MenuElement&gt; cropCellPopupVec;

    public List&lt;MenuElement&gt; getCropPopupElements() {
        return cropCellPopupVec;
    }

    /**
     * Holds all the pop-up options for the right click event on the cells in tillage column
     */
    private List&lt;MenuElement&gt; tillageCellPopupVec;

    public List&lt;MenuElement&gt; getTillagePopupElements() {
        return tillageCellPopupVec;
    }
 
    private MouseListener popupListener;
    private boolean calibIcon = false;

    /**
     * The HelpBroker is the default presentation of a HelpSet. A HelpBroker is an abstraction of
     * the presentation for a HelpSet; a straight-forward implementation is a JHelp() on the
     * HelpSet. A HelpBroker can be asked to show a given Navigational View, and can display a given
     * ID (help topic).
     */
    public HelpBroker hb_mcrew;

    /**
     * Default constructor that initializes the object that holds the data, adds pop-up menu, loads
     * tooltips file, adds help to the application, etc.
     */
    public TablePanel() {
        super(); //call to super mcrew_b  
        ManageData data = new ManageData();

        table = new Table(this, data);
        JTF_rotYears.setText(Integer.toString(data.getRotationYears()));
        double version = table.getData().getVersion();
        if(version &lt; 1.7) {
            jOldFormatButton.setSelected(true);
        } else {
            jNewFormatButton.setSelected(true);
        }
        VersionField.setText("Version: " + Double.toString(version));
        standaloneFlg = true;
        addPopupMenu();
        /*
         mcrew.configFrame = new ConfigFrame(mcrew.columnConfig);
         */
        Util.loadToolTips((Container) this, new TFile("mcrew_cfg", "mcrewtooltips.cfg"));
    }


    /**
     * Single argument constructor that initializes the object that holds the data, adds pop-up
     * menu, loads tooltips file, adds help to the application, etc. Also puts the title for the
     * application.
     *
     * @param fileName The .MAN or .XML file from which the the data needs to be pulled in the
     * application.
     * @param cd
     */
    public TablePanel(String fileName, usda.weru.util.ConfigData cd) {
        super();

        changeRotFileName(fileName);

        TFile filedir = new TFile(fileName);
        absFileName = filedir.getAbsolutePath();

        MCREWConfig.initialize(cd);
        //If the mcrew_cfg directory is not found, then return to weps.
        if (MCREWConfig.getReturnFlag() == true) {
            LOGGER.warn("Mcrew Data Config File Not Found.");
            JOptionPane.showMessageDialog(null, "Mcrew Data Config File Not Found." + "\nPlease load the mcrew_cfg Directory",
                    "Warning", JOptionPane.WARNING_MESSAGE);
            c_labelState = true;
            return;
        }
        ManageData data = new ManageData();
        data.readDataFile(absFileName);
        // here we create the table
        table = new Table(this, data);
        table.setVisible(false);
        table.setLocation(0, 0);
        
        JTF_rotYears.setText(Integer.toString(data.getRotationYears()));
        double version = data.getVersion();
        if(version &lt; 1.7) {
            jOldFormatButton.setSelected(true);
        } else {
            jNewFormatButton.setSelected(true);
        }
        VersionField.setText("Version: " + Double.toString(version));
        if(data.getUpdateStatus()) {
            jNewFormatButton.setSelected(true);
            VersionField.setText("Version: 1.7");
        }
        standaloneFlg = true;
        addPopupMenu();

        Util.loadToolTips((Container) this, new TFile("mcrew_cfg", "mcrewtooltips.cfg"));

        Dimension mdem = getSize();
        table.setSize(mdem.width - 20, mdem.height - 70);
        addToMain(table);
        this.setMinimumSize(new Dimension(table.getWidth(), table.getHeight()));

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                setVisible(true);
            }

        });

        c_cmis = new HashMap&lt;String, JCheckBoxMenuItem&gt;();
        c_tbs = new HashMap&lt;String, JToggleButton&gt;();

        //Setup the views defined for the table.
        
        //Init to the default
        table.toggleTableView("default", true);
        table.setVisible(true);

        calibIcon = MCREWConfig.getInitCalibColumnState();
        //here we create the instance of notes.
        notes = new NotesMan("Management File Notes", table.getData(), this);

        if ((fileName == null) || (fileName.length() == 0)) {
            return;
        }
        de.schlichtherle.truezip.file.TFile selectedFile = new TFile(fileName);
        if (selectedFile.exists()) {
            try {
                String fileDir = selectedFile.getCanonicalPath();
                //System.out.println("Mcrew:Mcrew() " + " project Dir = " + coordinator.getProjectPath());
                openFile(fileDir); // call open file method here
                table.checkData();
                //dataChanged = true;
            } catch (IOException e) {
                System.err.println("Error in opening file " + fileName);
            }
        }
        if ((coordinator.getProjectPath() == null) || (coordinator.getProjectPath().length() == 0)) {
            /* Disable Open copy of Template and Save as Template menu options	 */
            coordinator.disableOpenButtons();
            //SaveAsTJMI.setEnabled(false);
        }
        if(!table.getData().intView()) changeTableLabel(rotMode);
        else changeTableLabel(intMode);
    }
    
    public void addToMain(Component table)
    {
        JP_main.add(table);
        javax.swing.GroupLayout timePanelLayout = new javax.swing.GroupLayout(JP_main);
        JP_main.setLayout(timePanelLayout);
        timePanelLayout.setHorizontalGroup(
            timePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(table).addGap(10, 10, Short.MAX_VALUE)
        );
        timePanelLayout.setVerticalGroup(
            timePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(table).addGap(10, 10, Short.MAX_VALUE)
        );
    }
    
    public TablePanel(ManageData data, ConfigData cd, MCREWWindow mcrew)
    {
        // create an instance of mcrew
        super();
        coordinator = mcrew;
        fileName = data.manFile != null ? data.manFile.getName() : TablePanel.sUntitledFileName;
        ManageData dataTemp = new ManageData();

        table = new Table(this, dataTemp);
        JTF_rotYears.setText(Integer.toString(dataTemp.getRotationYears()));
        double version = data.getVersion();
        if(version &lt; 1.7) {
            jOldFormatButton.setSelected(true);
        } else {
            jNewFormatButton.setSelected(true);
        }
        VersionField.setText("Version: " + Double.toString(version));
        if(data.getUpdateStatus()) {
            jNewFormatButton.setSelected(true);
            VersionField.setText("Version: 1.7");
        }
        /*
         mcrew.configFrame = new ConfigFrame(mcrew.columnConfig);
         */
        Util.loadToolTips((Container) this, new TFile("mcrew_cfg", "mcrewtooltips.cfg"));

        changeRotFileName(fileName);

        TFile filedir = new TFile(fileName);
        absFileName = filedir.getAbsolutePath();
        //If the mcrew_cfg directory is not found, then return to weps.
        if (MCREWConfig.getReturnFlag() == true) {
            LOGGER.warn("Mcrew Data Config File Not Found.");
            JOptionPane.showMessageDialog(null, "Mcrew Data Config File Not Found." + "\nPlease load the mcrew_cfg Directory",
                    "Warning", JOptionPane.WARNING_MESSAGE);
            c_labelState = true;
            return;
        }
        // here we create the table
        table = new Table(this, data);
        table.setVisible(false);
        table.setLocation(0, 0);
        
        JTF_rotYears.setText(Integer.toString(data.getRotationYears()));
        if(version &lt; 1.7) {
            jOldFormatButton.setSelected(true);
        } else {
            jNewFormatButton.setSelected(true);
        }
        VersionField.setText("Version: " + Double.toString(version));
        if(data.getUpdateStatus()) {
            jNewFormatButton.setSelected(true);
            VersionField.setText("Version: 1.7");
        }
        standaloneFlg = true;
        addPopupMenu();
        /*
         mcrew.configFrame = new ConfigFrame(mcrew.columnConfig);
         */
        Util.loadToolTips((Container) this, new TFile("mcrew_cfg", "mcrewtooltips.cfg"));

        Dimension mdem = getSize();
        table.setSize(mdem.width - 20, mdem.height - 70);
        addToMain(table);
        this.setMinimumSize(new Dimension(table.getWidth(), table.getHeight()));

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                setVisible(true);
            }

        });

        c_cmis = new HashMap&lt;String, JCheckBoxMenuItem&gt;();
        c_tbs = new HashMap&lt;String, JToggleButton&gt;();

        //Setup the views defined for the table.
        
        //Init to the default
        table.toggleTableView("default", true);
        table.setVisible(true);
        table.dataChanged(null);
//        saveCurrent();
        
        calibIcon = MCREWConfig.getInitCalibColumnState();
        //here we create the instance of notes.
        notes = new NotesMan("Management File Notes", table.getData(), this);

        if ((fileName == null) || (fileName.length() == 0)) {
            return;
        }
        de.schlichtherle.truezip.file.TFile selectedFile = new TFile(fileName);
        if (selectedFile.exists()) {
            try {
                String fileDir = selectedFile.getCanonicalPath();
                //System.out.println("Mcrew:Mcrew() " + " project Dir = " + coordinator.getProjectPath());
                openFile(fileDir); // call open file method here
                table.checkData();
                //dataChanged = true;
            } catch (IOException e) {
                System.err.println("Error in opening file " + fileName);
            }
        }
        if ((coordinator.getProjectPath() == null) || (coordinator.getProjectPath().length() == 0)) {
            /* Disable Open copy of Template and Save as Template menu options	 */
            coordinator.disableOpenButtons();
            //SaveAsTJMI.setEnabled(false);
        }
    }
    
    /**
     * Adds the specified view JCheckBoxMenuItem.
     */
    public void addCmis(String name, JCheckBoxMenuItem item) { c_cmis.put(name, item); }
    
    /**
     * Returns the checkbox associated with the name.
     */
    public JCheckBoxMenuItem getCmis(String name) { return c_cmis.get(name); }
    
    /**
     * Adds the specified view Togglebutton.
     */
    public void addTbs(String name, JToggleButton item) { c_tbs.put(name, item); }
    
    /**
     * Returns the specified view Togglebutton associated with the name.
     */
    public JToggleButton getTbs(String name) { return c_tbs.get(name); }
    
    /**
     * Adds the specified togglebutton to the toolbar.
     */
    public void addtoToolbar(JToggleButton tb) { JTB_view.add(tb); }
    
    private Map&lt;String, JCheckBoxMenuItem&gt; c_cmis;
    private Map&lt;String, JToggleButton&gt; c_tbs;

    public void handleViewToggle(String name, boolean state) {
        JCheckBoxMenuItem cmi = c_cmis.get(name);
        JToggleButton tb = c_tbs.get(name);

        table.toggleTableView(name, state);

        if (cmi != null) {
            cmi.setSelected(state);
        }

        if (tb != null) {
            tb.setSelected(state);
        }
        this.setPreferredSize(new Dimension(table.getWidth(), table.getHeight()));
    }
    
    @Override
    protected void rotIntToggle(ActionEvent evt)
    {
        if(table.getData().intView()) coordinator.JMI_ItR_ActionPerformed(evt);
        else coordinator.JMI_RtI_ActionPerformed(evt);
    }

    public boolean getLabelState() {
        return c_labelState;
    }
    
    public MCREWWindow getCoordinator() { return coordinator; }
    
    public void commitEdit() { if(table != null) table.commitEdit(); } 
    
    public void setRotText(String input)
    {
        try { Integer.parseInt(input); }
        catch(NumberFormatException nfe) { return; } 
        JTF_rotYears.setText(input);
        table.checkWrap();
    }

    /**
     * In case running a standalone version of the application, this becomes the starting point of
     * the application.
     *
     * @param args A command line array of arguments such as fille name, screen title, etc.
     */
    public static void main(String[] args) {
        System.setProperty("line.separator", "\n"); // make file line separator unix compatible
        // create an instance of mcrew
        JFrame fram = new JFrame();
        TablePanel mp = new TablePanel();

        mp.standaloneFlg = true;

        //ConfigData holds all static data such as data from
        //mcrewconfig.xmland all Meta data
        MCREWConfig.initialize(ConfigData.getDefault());
    }
    
    private javax.swing.JMenuItem popCutJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem popCopyJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem popPasteJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem popDeleteJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem popUndoDeleteJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem popInsertJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem popInsertOperationJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem popInsertFileJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem popInsertFileDateAdjustedJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem popInsertFileWEPPmanFile = new javax.swing.JMenuItem(); // for right click sub menu for inserting WEPP management files into table
    private javax.swing.JMenuItem popInsertFileCRLMODJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem popAddRotationDateAdjustedJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem popSetPivotJMI = new javax.swing.JMenuItem();

    private javax.swing.JMenuItem popDrillDownScreenJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem popAdjustDateJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem popSetDateJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem setDateJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem calendarDateJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem dateChangeDateJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem dateIncrementYearJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem dateDecrementYearJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem dateIncrementMonthJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem dateDecrementMonthJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem dateIncrementWeekJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem dateDecrementWeekJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem dateIncrementDayJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem dateDecrementDayJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem operationScreenJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem operationNotesJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem addChangeOperationJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem cropScreenJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem addChangeCropJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem cropNotesJMI = new javax.swing.JMenuItem();
    // Menu items for popup for "Tillage direction" cells
    private javax.swing.JMenuItem setDirectionJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem adjustDirectionJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem setTozeroJMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem setTo45JMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem setTo90JMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem incrementBy15JMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem decrementBy15JMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem incrementBy45JMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem decrementBy45JMI = new javax.swing.JMenuItem();
    private javax.swing.JMenuItem changeBy90JMI = new javax.swing.JMenuItem();

    @Override
    public void setCursor(Cursor cursor) {
        super.setCursor(cursor);
        Table temp = table;
        if (temp != null) {
            temp.setCursor(cursor);
        }
    }

    public void setWaiting(boolean wait) {
        if (wait) {
            setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        } else {
            setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        }
    }

    @Override
    protected void jFormatButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
        setDataChanged(true);
    }
    
    private void addPopupMenu() {
        MenuElement[] menuElements;

        popup = new JPopupMenu();
        datePopup = new JPopupMenu();
        operationCellPopup = new JPopupMenu();
        cropCellPopup = new JPopupMenu();
        tillageCellPopup = new JPopupMenu();

        popupVec = new ArrayList&lt;MenuElement&gt;();
        datePopupVec = new ArrayList&lt;MenuElement&gt;();
        operationCellPopupVec = new ArrayList&lt;MenuElement&gt;();
        cropCellPopupVec = new ArrayList&lt;MenuElement&gt;();
        tillageCellPopupVec = new ArrayList&lt;MenuElement&gt;();

        SymAction lSymAction = new SymAction();

        popSetDateJMI.setText("Set Date");
        popSetDateJMI.setActionCommand("Set Date");
        popSetDateJMI.addActionListener(lSymAction);
        popup.add(popSetDateJMI);

        popAdjustDateJMI.setText("Adjust Date");
        popAdjustDateJMI.setActionCommand("Adjust Date");
        popAdjustDateJMI.addActionListener(lSymAction);
        popup.add(popAdjustDateJMI);

        popInsertJMI.setText("Insert Blank Row");
        popInsertJMI.setActionCommand("Insert Blank Row");
        popInsertJMI.addActionListener(lSymAction);
        popup.add(popInsertJMI);

        popInsertOperationJMI.setText("Insert Operation");
        popInsertOperationJMI.setActionCommand("Insert Operation");
        popInsertOperationJMI.addActionListener(lSymAction);
        popup.add(popInsertOperationJMI);

        popAddRotationDateAdjustedJMI.setText("Insert Rotation");
        popAddRotationDateAdjustedJMI.setActionCommand("Add Rotation");
        popAddRotationDateAdjustedJMI.addActionListener(lSymAction);
        popup.add(popAddRotationDateAdjustedJMI);

        popInsertFileDateAdjustedJMI.setText("Insert Management");
        popInsertFileDateAdjustedJMI.setActionCommand("Insert File");
        popInsertFileDateAdjustedJMI.addActionListener(lSymAction);
        popup.add(popInsertFileDateAdjustedJMI);
        
        popInsertFileWEPPmanFile.setText("Insert WEPP Management");
        popInsertFileWEPPmanFile.setActionCommand("Insert File");
        popInsertFileWEPPmanFile.addActionListener(lSymAction);
        popup.add(popInsertFileWEPPmanFile);

        //only show if not in Interval view
        if (!usda.weru.util.ConfigData.getDefault().isCompatibilityNRCS()) {
            popInsertFileJMI.setText("Insert Management (Unadjusted)");
            popInsertFileJMI.setActionCommand("Insert File (Unadjusted)");
            popInsertFileJMI.addActionListener(lSymAction);

            popup.add(popInsertFileJMI);
        }

        //only show if not in Interval view
        if (ConfigData.checkParmValue(ConfigData.crlmodEnabled, "1")) {
            popInsertFileCRLMODJMI.setText("Insert Management (CRLMOD)");
            popInsertFileCRLMODJMI.setActionCommand("Insert File (CRLMOD)");
            popInsertFileCRLMODJMI.addActionListener(lSymAction);

            popup.add(popInsertFileCRLMODJMI);
        }
        
        // Now follws multiple row operations
        popCutJMI.setText("Cut Row(s)");
        popCutJMI.setActionCommand("Cut ");
        popCutJMI.addActionListener(lSymAction);
        popup.add(popCutJMI);

        popCopyJMI.setText("Copy Row(s)");
        popCopyJMI.setActionCommand("Copy");
        popCopyJMI.addActionListener(lSymAction);
        popup.add(popCopyJMI);

        popPasteJMI.setText("Paste Row(s)");
        popPasteJMI.setActionCommand("Paste");
        popPasteJMI.addActionListener(lSymAction);
        popup.add(popPasteJMI);

        popDeleteJMI.setText("Delete Row(s)");
        popDeleteJMI.setActionCommand("Delete");
        popDeleteJMI.addActionListener(lSymAction);
        popup.add(popDeleteJMI);

        popUndoDeleteJMI.setText("Undo Delete Row(s)");
        popUndoDeleteJMI.setActionCommand("Undo Ddelete");
        popUndoDeleteJMI.addActionListener(lSymAction);
        popup.add(popUndoDeleteJMI);
        setUndoDeleteStatus(false);
        
        popSetPivotJMI.setText("Set Pivot Point");
        popSetPivotJMI.setActionCommand("Pivot Point");
        popSetPivotJMI.addActionListener(lSymAction);
        popup.add(popSetPivotJMI);

        menuElements = popup.getSubElements();
        popupVec.addAll(Arrays.asList(menuElements));

        /* date popup menu optipns */
        calendarDateJMI.setText("Calendar Date");
        calendarDateJMI.setActionCommand("Calender Date");
        calendarDateJMI.addActionListener(lSymAction);
        datePopup.add(calendarDateJMI);

        setDateJMI.setText("Set Date");
        setDateJMI.setActionCommand("Set Date");
        setDateJMI.addActionListener(lSymAction);
        datePopup.add(setDateJMI);

        dateChangeDateJMI.setText("Adjust Date");
        dateChangeDateJMI.setActionCommand("Change Date");
        dateChangeDateJMI.addActionListener(lSymAction);
        datePopup.add(dateChangeDateJMI);

        dateIncrementYearJMI.setText("Increment Year");
        dateIncrementYearJMI.setActionCommand("Increment Year");
        dateIncrementYearJMI.addActionListener(lSymAction);
        datePopup.add(dateIncrementYearJMI);

        dateDecrementYearJMI.setText("Decrement Year");
        dateDecrementYearJMI.setActionCommand("Decrement Year");
        dateDecrementYearJMI.addActionListener(lSymAction);
        datePopup.add(dateDecrementYearJMI);

        dateIncrementMonthJMI.setText("Increment Month");
        dateIncrementMonthJMI.setActionCommand("Increment Month");
        dateIncrementMonthJMI.addActionListener(lSymAction);
        datePopup.add(dateIncrementMonthJMI);

        dateDecrementMonthJMI.setText("Decrement Month");
        dateDecrementMonthJMI.setActionCommand("Decrement Month");
        dateDecrementMonthJMI.addActionListener(lSymAction);
        datePopup.add(dateDecrementMonthJMI);

        dateIncrementWeekJMI.setText("Increment by week");
        dateIncrementWeekJMI.setActionCommand("Increment by week");
        dateIncrementWeekJMI.addActionListener(lSymAction);
        datePopup.add(dateIncrementWeekJMI);

        dateDecrementWeekJMI.setText("Decrement by week");
        dateDecrementWeekJMI.setActionCommand("Decrement by week");
        dateDecrementWeekJMI.addActionListener(lSymAction);
        datePopup.add(dateDecrementWeekJMI);

        dateIncrementDayJMI.setText("Increment Day");
        dateIncrementDayJMI.setActionCommand("Increment Day");
        dateIncrementDayJMI.addActionListener(lSymAction);
        datePopup.add(dateIncrementDayJMI);

        dateDecrementDayJMI.setText("Decrement Day");
        dateDecrementDayJMI.setActionCommand("Decrement Day");
        dateDecrementDayJMI.addActionListener(lSymAction);
        datePopup.add(dateDecrementDayJMI);

        menuElements = datePopup.getSubElements();
        datePopupVec.addAll(Arrays.asList(menuElements));

        /* operation and crop cell popup menu items */
        addChangeOperationJMI.setText("Change/Select Operation");
        addChangeOperationJMI.setActionCommand("Add/Change Operation");
        addChangeOperationJMI.addActionListener(lSymAction);
        operationCellPopup.add(addChangeOperationJMI);

        operationScreenJMI.setText("Operation Drill-down Screen");
        operationScreenJMI.setActionCommand("Operation Drill-down Screen");
        operationScreenJMI.addActionListener(lSymAction);
        operationCellPopup.add(operationScreenJMI);
        
        operationNotesJMI.setText("View Operation Notes");
        operationNotesJMI.setActionCommand("View Operation Notes");
        operationNotesJMI.addActionListener(lSymAction);
        operationCellPopup.add(operationNotesJMI);

        menuElements = operationCellPopup.getSubElements();
        operationCellPopupVec.addAll(Arrays.asList(menuElements));

        addChangeCropJMI.setText("Change/Select Crop");
        addChangeCropJMI.setActionCommand("Add/Change Crop");
        addChangeCropJMI.addActionListener(lSymAction);
        cropCellPopup.add(addChangeCropJMI);

        cropScreenJMI.setText("Crop Drill-down Screen");
        cropScreenJMI.setActionCommand("Crop Drill-down Screen");
        cropScreenJMI.addActionListener(lSymAction);
        cropCellPopup.add(cropScreenJMI);
        
        cropNotesJMI.setText("View Crop/Residue Notes");
        cropNotesJMI.setActionCommand("View Crop/Residue Notes");
        cropNotesJMI.addActionListener(lSymAction);
        cropCellPopup.add(cropNotesJMI);

        menuElements = cropCellPopup.getSubElements();
        cropCellPopupVec.addAll(Arrays.asList(menuElements));

        /* Tillage Direction popup menu items - neha */
        setDirectionJMI.setText("Set Direction");
        setDirectionJMI.setActionCommand("Set Direction");
        setDirectionJMI.addActionListener(lSymAction);
        tillageCellPopup.add(setDirectionJMI);

        adjustDirectionJMI.setText("Adjust Direction");
        adjustDirectionJMI.setActionCommand("Adjust Direction");
        adjustDirectionJMI.addActionListener(lSymAction);
        tillageCellPopup.add(adjustDirectionJMI);

        setTozeroJMI.setText("Set to 0 deg");
        setTozeroJMI.setActionCommand("Set to 0 deg");
        setTozeroJMI.addActionListener(lSymAction);
        tillageCellPopup.add(setTozeroJMI);

        setTo45JMI.setText("Set to 45 deg");
        setTo45JMI.setActionCommand("Set to 45 deg");
        setTo45JMI.addActionListener(lSymAction);
        tillageCellPopup.add(setTo45JMI);

        setTo90JMI.setText("Set to 90 deg");
        setTo90JMI.setActionCommand("Set to 90 deg");
        setTo90JMI.addActionListener(lSymAction);
        tillageCellPopup.add(setTo90JMI);

        incrementBy15JMI.setText("Increment 15 deg");
        incrementBy15JMI.setActionCommand("Increment 15 deg");
        incrementBy15JMI.addActionListener(lSymAction);
        tillageCellPopup.add(incrementBy15JMI);

        decrementBy15JMI.setText("Decrement 15 deg");
        decrementBy15JMI.setActionCommand("Decrement 15 deg");
        decrementBy15JMI.addActionListener(lSymAction);
        tillageCellPopup.add(decrementBy15JMI);

        incrementBy45JMI.setText("Increment 45 deg");
        incrementBy45JMI.setActionCommand("Increment 45 deg");
        incrementBy45JMI.addActionListener(lSymAction);
        tillageCellPopup.add(incrementBy45JMI);

        decrementBy45JMI.setText("Decrement 45 deg");
        decrementBy45JMI.setActionCommand("Decrement 45 deg");
        decrementBy45JMI.addActionListener(lSymAction);
        tillageCellPopup.add(decrementBy45JMI);

        changeBy90JMI.setText("Change by 90 deg");
        changeBy90JMI.setActionCommand("Change by 90 deg");
        changeBy90JMI.addActionListener(lSymAction);
        tillageCellPopup.add(changeBy90JMI);

        menuElements = tillageCellPopup.getSubElements();
        tillageCellPopupVec.addAll(Arrays.asList(menuElements));

        popupListener = new PopupListener();
        this.addMouseListener(popupListener);
    }

    /**
     * This method sets the rotaion year in the frame from the management data Changes the Rotation
     * File Name displayed and adjusts the scroll bar accordingly
     *
     * @param fileName The new filename to be used.
     */
    public void changeRotFileName(String fileName) {
        filePathName = fileName;
        updateRotFileName();
        changes.firePropertyChange(tablePanelNewFilePropChange, null, new TFile(fileName).getPath());
    }

    public void updateRotFileName() {
        String text = "";
        if (table.getDataChanged()) {
            text = filePathName + "  (Modified)";
            JL_rotation2.setForeground(Color.BLUE);
        } else {
            text = filePathName;
            JL_rotation2.setForeground(Color.BLACK);
        }
        JL_rotation2.setText(text);
    }

    public void setDataChanged(boolean changed) {
        if(table.getDataChanged() ^ changed) coordinator.dataChange(changed);
        table.setDataChanged(changed);

        //Cheating, but we only allow one mcrew open at a time.
        if (MCREWWindow.openMcrewWindows.isEmpty()) {
            return;
        }
        updateRotFileName();
    }

    /**
     * This method sends the new rotation year to the data
     *
     * @param yrs The new year to be set.
     */
    public void changeRotYears(String yrs) {
        //local cache to try and work around rare threading issues
        ManageData temp = table.getData();
        if (temp == null) {
            return;
        }
        try {
            int old = temp.getRotationYears();
            int years = Integer.parseInt(yrs);
            if (years != old) {
                temp.setRotationYears(years);
                setDataChanged(true);
            }
        } catch (NumberFormatException e) {
            LOGGER.error("Mcrew:changeRotYears()-&gt;" + "Please enter a number for RotationYears");
            JTF_rotYears.setText(Integer.toString(temp.getRotationYears()));
            table.checkWrap();
        } catch (NullPointerException npe) {
            LOGGER.debug("Null Data Object", npe);
        }

    }

    @Override
    public void RotYearsJT_focusLost(java.awt.event.FocusEvent event) {
        changeRotYears(JTF_rotYears.getText());
    }

    @Override
    public void RotYearsJT_actionPerformed(java.awt.event.ActionEvent event) {
        changeRotYears(JTF_rotYears.getText());
    }

    /**
     * This function opens an existing management file and sets the rotation text box
     *
     * @param fullPathName The path where this file resides in the system or on the network.
     */
    public void openFile(String fullPathName) throws IllegalStateException {
        boolean resort = table.getData().intView();
        if(resort) 
        {
            try { this.table.getData().intToRotSort(); }
            catch(Exception e) 
            {
                table.getData().setRotationYears(table.getData().kRotationYears + 100);
                try { this.table.getData().intToRotSort(); }
                catch(Exception ex) {}
            }
            
        }
        setWaiting(true);
        try {
            if (fullPathName.equals("")) {
                System.err.println("File not selected error");
                return;
            }

            //Issue: 543, there is a chance of threading race condition.  So we wait to make sure the data is not null.
            while (table.getData() == null) {
                Thread.yield();
            }
            String oldFileName = table.getData().manFile.getAbsolutePath();
            int dataReadValue = table.getData().readDataFile(fullPathName);
            //System.out.println("Mcrew:openfile" + "  datareadvalue = " +dataReadValue);
            switch (dataReadValue) {
                case ManageData.kCorruptedFile:
                    System.err.println("Error in TablePanel");
                    JOptionPane.showMessageDialog(this, "Unable to read management file " + fullPathName
                            + "\nThe file appears to be corrupted.", "Corrupted File", JOptionPane.ERROR_MESSAGE);
                    table.getData().readDataFile(oldFileName);
                    return;
                case ManageData.kWrong_Version:
                    String[] errorMsg = new String[2];
                    errorMsg[0] = "Management file " + fullPathName + "\nis of version " + table.getData().version
                            + ", whereas the current version is " + ManageData.VERSION_MINIMUM + ".";

                    errorMsg[1] = "Do you want to continue reading ?";

                    int reply = JOptionPane.showConfirmDialog(this, errorMsg, "Version", JOptionPane.YES_NO_OPTION,
                            JOptionPane.QUESTION_MESSAGE);
                    if (reply == JOptionPane.NO_OPTION) {
                        table.getData().readDataFile(oldFileName);
                        return;
                    }
                    break;
                case ManageData.kFile_NotFound:
                    System.err.println("Mcrew:openFile() " + "Cannot find the file. Strange!!");
                    table.getData().readDataFile(oldFileName);
                    return;
                case ManageData.kUnknown_Error:
                    System.err.println("Mcrew:openFile() " + "Unknown error reading data file");
                    table.getData().readDataFile(oldFileName);
                    return;
                case ManageData.kFailureOld:
                    return;
                case ManageData.kSuccessOld:
                    System.out.println("Data read succesfully (kSuccessOld)");
                    jOldFormatButton.setSelected(true);
                    break;
                case ManageData.kSuccess:
                    System.out.println("Data read succesfully (kSuccess)");
                    break;
                case ManageData.kSuccessUpdate:
                    fullPathName = table.getData().manFile.getAbsolutePath();
                    
                    // re-read the updated version of the man file.
                    // readDataFile() caused the file to be updated, but continued to read the old file.
                    // This behavior works in other places because of all the ConfigData.changes.fire() calls
                    // that are triggered.
                    // Here we just re-read directly.
                    table.getData().readDataFile(fullPathName);
                    break;
                    
                default:
                    break;
            }

            table.checkData();

            TFile file = new TFile(fullPathName);
            fileName = file.getName();
            savePath = file.getParent();

            //Set the Text Field to the absolute path of current opened file.
            try {
                filePathName = file.getCanonicalPath();
                changeRotFileName(filePathName);
                absFileName = filePathName;
            } catch (IOException e) {
                LOGGER.error("Unable to read file: " + file.getAbsolutePath(), e);
            }

            JTF_rotYears.setText(Integer.toString(table.getData().getRotationYears()));
            double version = table.getData().getVersion();
            if(version &lt; 1.7) {
                jOldFormatButton.setSelected(true);
            } else {
                jNewFormatButton.setSelected(true);
            }
            VersionField.setText("Version: " + Double.toString(version));
            if(table.getData().getUpdateStatus()) {
                jNewFormatButton.setSelected(true);
                VersionField.setText("Version: 1.7");
            }
            table.checkWrap();
        } finally {
            setWaiting(false);
        }

        if(resort) {
            rotToIntSort();
        }
        setDataChanged(false);
        table.checkAllConditions();
    }
 
    /**
     * This function resets the table and the current manage data to a new file.
     */
    public void newFile()
    {
        table.getData().setWepsManFileNotes("");
        fileName = sUntitledFileName;
        //RotationJT.setText(fileName);
        changeRotFileName(fileName);
        table.getData().clear();
        notes.JTA_Notes.setText("");
        JTF_rotYears.setText(Integer.toString(table.getData().getRotationYears()));
        double version = 1.7;
        jNewFormatButton.setSelected(true);
        VersionField.setText("Version: " + Double.toString(version));
        setDataChanged(false);

        invalidate();
        repaint();
    }

    /**
     * This method lets you open an updated file when a file data is modified and saved and the new
     * data needs to be displayed.
     *
     * @param fullPathName The system or network path where this file resides.
     */
    public void openRefreshedFile(String fullPathName) {
        setWaiting(true);
        try {
            table.getData().readDataFile(fullPathName);
            table.checkData();
            TFile file = new TFile(fullPathName);
            fileName = file.getName();
            savePath = file.getParent();

            //Set the Text Field to the absolute path of current opened file.
            try {
                filePathName = file.getCanonicalPath();
                changeRotFileName(filePathName);
            } catch (IOException e) {
                LOGGER.error("Error reading file: " + file.getAbsolutePath(), e);
            }

            JTF_rotYears.setText(Integer.toString(table.getData().getRotationYears()));
            double version = table.getData().getVersion();
            if(version &lt; 1.7) {
                jOldFormatButton.setSelected(true);
            } else {
                jNewFormatButton.setSelected(true);
            }
            VersionField.setText("Version: " + Double.toString(version));
            if(table.getData().getUpdateStatus()) {
                jNewFormatButton.setSelected(true);
                VersionField.setText("Version: 1.7");
            }   
        } finally {
            setWaiting(false);
        }
        setDataChanged(false);
        table.checkAllConditions();

    }

    public void cleanup() {
        try {
            MCREWConfig.cleanup();

            Table tempTable = table;
            if (tempTable != null) {
                tempTable.cleanup();
            }
        } catch (Exception e) {
            LOGGER.warn("Unable to clean up mcrew data structures.", e);
        }

    }

    public void setUndoDeleteStatus(boolean enabled) {
        enabled = table.getData() != null &amp;&amp; table.getData().deletedRows != null &amp;&amp; table.getData().deletedRows.size() &gt; 0;
        coordinator.setUndoDeleteStatus(enabled);
        popUndoDeleteJMI.setEnabled(enabled);
    }

    public void setDeleted() {
        setUndoDeleteStatus(true);
    }

    // here we map the tool bar buttons to the menu items
    @Override
    public void OpenJB_actionPerformed(java.awt.event.ActionEvent event) {
        setUndoDeleteStatus(false);
        coordinator.existingOverwrite();
    }

    @Override
    protected void NewJB_actionPerformed(java.awt.event.ActionEvent event) {
        setUndoDeleteStatus(false);
        coordinator.newOverwrite();
    }

    @Override
    protected void SaveJB_actionPerformed(java.awt.event.ActionEvent event) {
        setUndoDeleteStatus(false);
        saveCurrent();
    }
    
    @Override
    public void CycFwdJB_actionPerformed(java.awt.event.ActionEvent evt) {
        coordinator.CycleFwdJMI_actionPerformed(evt);
    }
    
    @Override
    public void CycBwdJB_actionPerformed(java.awt.event.ActionEvent evt) {
        coordinator.CycleBwdJMI_actionPerformed(evt);
    }

    @Override
    protected void CutJB_actionPerformed(java.awt.event.ActionEvent event) {
        setUndoDeleteStatus(false);
        CutJMI_actionPerformed(event);
    }

    @Override
    protected void CopyJB_actionPerformed(java.awt.event.ActionEvent event) {
        setUndoDeleteStatus(false);
        CopyJMI_actionPerformed(event);
    }

    @Override
    protected void PasteJB_actionPerformed(java.awt.event.ActionEvent event) {
        setUndoDeleteStatus(false);
        PasteJMI_actionPerformed(event);
    }

    @Override
    protected void SortJB_actionPerformed(java.awt.event.ActionEvent event) {
        setUndoDeleteStatus(false);
        SortJMI_actionPerformed(event);
    }

    @Override
    public void NotesJB_actionPerformed(java.awt.event.ActionEvent evt) {

        NotesJMI_actionPerformed(evt);
    }
    
    /**
     * This method knows what to do when state change occurs for a particular object or item.
     *
     * @param e The state change that occured for a particular item.
     */
    @Override
    public void itemStateChanged(ItemEvent e) {
    }

    /**
     * The action to be taken based on the event that happens.
     *
     * @param e The event that is to be recognised for action to be performed.
     */
    @Override
    public void actionPerformed(ActionEvent e) {
    }


    private boolean checked = false;

    /**
     * If refreshmcrewscreen is true, we try to open the saved data .MAN/.XML to load the most
     * recent data onto the main MCREW screen table.
     */
    public void refreshMcrewTableData() {

        if ((fileName == null) || (fileName.length() == 0) || (fileName.equals(sUntitledFileName))) {
            return;
        }
        if (!checked) {
            if (table.checkAllConditions() == false) {
                saveSuccess = false;
                return; // user pressed cancel on some option, Don't save
            }
        }

        //If the use doesnt opens a file or opens a tamplate of a file
        // and wothout making any changes saves the file, then the "absFileName"
        // should reflect this saved file.
        absFileName = filePathName;

        if (table != null &amp;&amp; table.getDataChanged() == false) {
            return;
        }
        if ((savePath == null) || (savePath.length() == 0)) {
            savePath = MCREWConfig.getDirectoryName(XMLConstants.smanagement_template);

            ////System.out.println("Data changed " + dataChanged);
        }
        String fullPathFileName = savePath + TFile.separator + fileName;
        TFile mcrewFile = new TFile(fullPathFileName);
        TFile parentFile = new TFile(mcrewFile.getParentFile());
        if (parentFile.canWrite() == false) {
            JOptionPane.showMessageDialog(this, "The File location Specified is not writable\n "
                    + fullPathFileName, "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        if(jOldFormatButton.isSelected()) {
            table.getData().setWriteVersionToOld(true);
            System.out.println("writing old version");
        } else {
            table.getData().setWriteVersionToOld(false);
            System.out.println("writing new version");
        }
        table.getData().writeDataFile(savePath + TFile.separator + fileName);
        //update the abs file path name to this path of the saved file
        absFileName = fullPathFileName;

        setDataChanged(false);
        saveSuccess = true;

        if (refreshMcrewScreen.equals("true")) {
            openRefreshedFile(absFileName);
            refreshMcrewScreen = "false";
        }
    }
    private PropertyChangeSupport changes = new PropertyChangeSupport(this);

    /**
     * Add a PropertyChangeListener to the listener list. The listener is registered for all
     * properties
     *
     * @param l PropertyChangeListener to be added
     */
    @Override
    public void addPropertyChangeListener(PropertyChangeListener l) {
        changes.addPropertyChangeListener(l);
    }

    /**
     * Remove a PropertyChangeListener from the listener list. This removes a PropertyChangeListener
     * that was registered for all properties.
     *
     * @param l The PropertyChangeListener to be removed
     */
    @Override
    public void removePropertyChangeListener(PropertyChangeListener l) {
        changes.removePropertyChangeListener(l);
    }

    /* This code is for the pop-up menu
     */
    class PopupListener extends MouseAdapter {

        @Override
        public void mousePressed(MouseEvent e) {
            maybeShowPopup(e);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            maybeShowPopup(e);
        }

        private void maybeShowPopup(MouseEvent e) {
            if (e.isPopupTrigger()) {
                e.consume();
                popup.show(e.getComponent(), e.getX(), e.getY());
            }
        }
    }

   public void addCSH(HelpSet hs_mcrew) {
        hb_mcrew = hs_mcrew.createHelpBroker();
        hb_mcrew.enableHelpKey(getRootPane(), "McrewIntro_html", hs_mcrew);
        ActionListener helper = new CSH.DisplayHelpFromSource(hb_mcrew);
        CSH.setHelpIDString(JTB_main, "mcrewButtonBar_html");
        CSH.setHelpIDString(JB_new, "mcrewNewButton_html");
        CSH.setHelpIDString(JB_open, "mcrewOpenButton_html");
        CSH.setHelpIDString(JB_save, "mcrewSaveButton_html");
        CSH.setHelpIDString(JB_cut, "mcrewCutButton_html");
        CSH.setHelpIDString(JB_copy, "mcrewCopyButton_html");
        CSH.setHelpIDString(JB_paste, "mcrewPasteButton_html");
        CSH.setHelpIDString(JB_sort, "mcrewSortButton_html");
        CSH.setHelpIDString(JB_cycleFwd, "mcrewRotateFowardButton_html");
        CSH.setHelpIDString(JB_cycleBwd, "mcrewRotateBackwardButton_html");
        CSH.setHelpIDString(JB_notes, "mcrewPencilButton_html");
        CSH.setHelpIDString(JL_yrofRot, "MCREWYrRot_html");
        CSH.setHelpIDString(JTF_rotYears, "MCREWYrRot_html");
        CSH.setHelpIDString(JP_rotationFile, "MCREWRotName_html");
        CSH.setHelpIDString(JP_main, "mcrewTableview_html");
        CSH.setHelpIDString(JTB_RtI, "mcrewplantbutton_html");
        if(c_tbs != null)
        {
            for(String item : c_tbs.keySet())
            {
                CSH.setHelpIDString(c_tbs.get(item), "mcrewYieldButton_html");
            }
        }
    } //end of addCSH

    void DrillDownScreenJMI_actionPerformed(java.awt.event.ActionEvent event) {
        table.showDrillDownScreen();
    }

    void SetDateJMI_actionPerformed(java.awt.event.ActionEvent event) {
        table.setDates();
    }

    void ChangeDateJMI_actionPerformed(java.awt.event.ActionEvent event) {
        table.changeDates();
    }

    void CalendarDateJMI_actionPerformed(java.awt.event.ActionEvent event) {
        table.calendarDates();
    }

    void SetDirectionJMI_actionPerformed(java.awt.event.ActionEvent event) {
        table.setDirection();
    }

    void AdjustDirectionJMI_actionPerformed(java.awt.event.ActionEvent event) {
        table.adjustDirection();
    }

    void SetToZeroJMI_actionPerformed(java.awt.event.ActionEvent event) {
        table.setAbsoluteDirection(0);
    }

    void SetTo45JMI_actionPerformed(java.awt.event.ActionEvent event) {
        table.setAbsoluteDirection(45);
    }

    void SetTo90JMI_actionPerformed(java.awt.event.ActionEvent event) {
        table.setAbsoluteDirection(90);
    }

    void IncrementBy15JMI_actionPerformed(java.awt.event.ActionEvent event) {
        table.incrDecrDir(15);
    }

    void DecrementBy15JMI_actionPerformed(java.awt.event.ActionEvent event) {
        table.incrDecrDir(-15);
    }

    void IncrementBy45JMI_actionPerformed(java.awt.event.ActionEvent event) {
        table.incrDecrDir(45);
    }

    void DecrementBy45JMI_actionPerformed(java.awt.event.ActionEvent event) {
        table.incrDecrDir(-45);
    }

    void ChangeBy90JMI_actionPerformed(java.awt.event.ActionEvent event) {
        table.changeBy90();
    }
    
    void CutJMI_actionPerformed(java.awt.event.ActionEvent event) {
        setWaiting(true);
        try {
            setUndoDeleteStatus(false);
            table.cutRows();
            table.checkData();
            setDataChanged(true);
        } finally {
            setWaiting(false);
        }
    }
    
    /**
     * This method is called when the copy option is selected
     *
     * @param event
     */
    void CopyJMI_actionPerformed(java.awt.event.ActionEvent event) {
        setUndoDeleteStatus(false);
        table.copyRows();
    }
    
    void PasteJMI_actionPerformed(java.awt.event.ActionEvent event) {
        setWaiting(true);
        try {
            setUndoDeleteStatus(false);
            table.pasteRows();
            setDataChanged(true);
        } finally {
            setWaiting(false);
        }
    }
    
    void DeleteJMI_actionPerformed(java.awt.event.ActionEvent event) {
        setWaiting(true);
        try {
            table.deleteRows();
            table.checkData();
            setUndoDeleteStatus(true);
            setDataChanged(true);
        } finally {
            setWaiting(false);
        }
    }
    
    void UndoDeleteJMI_ActionPerformed(java.awt.event.ActionEvent evt) {
        setWaiting(true);
        try {
            table.undoDeleteRows();
            table.checkData();
            setUndoDeleteStatus(false);
        } finally {
            setWaiting(false);
        }
    }
    
    void InsertOperationJMI_actionPerformed(java.awt.event.ActionEvent event) {
        setUndoDeleteStatus(false);
        table.insertRow();
        boolean added = table.addColumnData(1);
        //Make sure a operation was added.
        if (!added) {
            table.deleteRows();
        }
    }
    
    void CycleFwdJMI_actionPerformed(java.awt.event.ActionEvent event) {
        setWaiting(true);
        try {
            long time = System.currentTimeMillis();
            while (table.checkAndSetRotYears() == -1) {
                if (System.currentTimeMillis() - time &gt; 5000) {
                    return;
                }
            }
            int rotYears = Integer.parseInt(JTF_rotYears.getText());
            table.cycleForward(rotYears);
        } finally {
            setWaiting(false);
        }
    }
    
    void CycleBwdJMI_actionPerformed(java.awt.event.ActionEvent event) {
        setWaiting(true);
        try {
            long time = System.currentTimeMillis();
            while (table.checkAndSetRotYears() == -1) {
                if (System.currentTimeMillis() - time &gt; 5000) {
                    return;
                }
            }
            int rotYears = Integer.parseInt(JTF_rotYears.getText());
            table.cycleBackward(rotYears);
        } finally {
            setWaiting(false);
        }
    }
    
    void NotesJMI_actionPerformed(java.awt.event.ActionEvent evt) {
        if (notes.notesPresent == false) {
            notes.JTA_Notes.setText(table.getData().getWepsManFileNotes());
            notes.setVisible(true);
            //Position this frame relative to the position of Mcrew-by Neha
            notes.setLocationRelativeTo(this);
            notes.notesPresent = true;
        } else if (notes.isActive() == false) {
            notes.JTA_Notes.setText(table.getData().getWepsManFileNotes());
            notes.toFront();
            notes.setVisible(true);
            notes.requestFocus();
        }
    }
    
    void InsertJMI_actionPerformed(java.awt.event.ActionEvent event) {
        setWaiting(true);
        try {
            setUndoDeleteStatus(false);
            table.insertRow();
        } finally {
            setWaiting(false);
        }

    }

    public void SortJMI_actionPerformed(java.awt.event.ActionEvent event) {
        setWaiting(true);
        try {
            setUndoDeleteStatus(false);
            table.sortData();
            table.checkData();
        } finally {
            setWaiting(false);
        }
    }
    
    void InsertFileJMI_actionPerformed(java.awt.event.ActionEvent event) {
        setWaiting(true);
        try {
            ManageData dataToInsert = new ManageData();

            TFile dirXML = new TFile(MCREWConfig.getDirectoryName(XMLConstants.smanagement_template));
            WepsFileChooser2 mfc;

            mfc = WepsFileChooser2.create(WepsFileChooser2.Action.Select, dirXML, false, false, null, WepsFileTypes2.Management,
                    WepsFileTypes2.Rotation);
            mfc.setCurrentDirectory(dirXML); //new de.schlichtherle.truezip.file.File(ConfigData.INITIAL_DIR));

            mfc.setDialogTitle("Select a Management File");

            //set the tooltip of "home" button to "Home" instead of "Desktop"
            mfc.homeToolTip(mfc);
            //remove the NewFolder button
            mfc.disableNewFolder(mfc);
            mfc.enableFolderButtons();
            if (!(mfc.showDialog(null) == WepsFileChooser2.APPROVE_OPTION)) {
                return;
            }
            TFile selectedFile = new TFile(mfc.getSelectedFile());

            try {
                String filePath = selectedFile.getCanonicalPath();
                System.err.println("Mcrew:InsertFileJMI(): " + "Opening file " + filePath);
                //System.out.println("Performing the InsertFileJMI_actionPerformed.");
                dataToInsert.readDataFile(filePath);
                table.insertFile(dataToInsert, false, false);
                table.checkData();
                //savePath = delectedFile.getParent();
            } catch (IOException e) {
                System.err.println("Error in opening file " + e);
            }
            setDataChanged(true);
        } finally {
            setWaiting(false);
        }

    }
    
    /**
     * This code uses the table panels instance of MCREWWindow to call a method that 
     * takes a WEPP .json management file, translates it and inserts it into the table
     * after translation. This is available from the menu option in a right click that says 
     * 'Insert WEPP Management '
     * @param event 
     */
    private void popInsertFileWEPPmanFile_actionPreformed(java.awt.event.ActionEvent event) {
        if (this.getCoordinator() == null) {
            // This should ALWAYS be initialized in constructor, but better to check
            System.err.println("in \'popInsertFileWEPPmanFile_actionPreformed()\' no instance of MCREWWindow found");
        }
        // A little dirty, but code already existed to do this
        this.getCoordinator().weppOverwrite_butBetter();
    }
    
    void InsertFileDateAdjustedJMI_actionPerformed(java.awt.event.ActionEvent event) {
        setWaiting(true);
        try {
            if (table.getData().getRows().isEmpty()) {
                InsertFileJMI_actionPerformed(event);
                return;
            }
            ManageData dataToInsert = new ManageData();

            TFile dirXML = new TFile(MCREWConfig.getDirectoryName(XMLConstants.smanagement_template));
            WepsFileChooser2 mfc;

            mfc = WepsFileChooser2.create(WepsFileChooser2.Action.Select, dirXML, false, false, null, WepsFileTypes2.Management,
                    WepsFileTypes2.Rotation);
            mfc.setCurrentDirectory(dirXML); //new de.schlichtherle.truezip.file.File(ConfigData.INITIAL_DIR));
            mfc.setDialogTitle("Select a Management File");

            //set the tooltip of "home" button to "Home" instead of "Desktop"
            mfc.homeToolTip(mfc);
            //remove the NewFolder button
            mfc.disableNewFolder(mfc);
            mfc.enableFolderButtons();
            if (!(mfc.showDialog(null) == WepsFileChooser2.APPROVE_OPTION)) {
                return;
            }
            TFile selectedFile = new TFile(mfc.getSelectedFile());

            try {
                String filePath = selectedFile.getCanonicalPath();
                System.err.println("Mcrew:InsertFileDateAdjustedJMI(): " + "Opening file " + filePath);
                //******************************The Bad Line
                dataToInsert.readDataFile(filePath);
                //******************************The Bad Line
                table.insertFile(dataToInsert, true, false);
                table.checkData();
                //savePath = delectedFile.getParent();
            } catch (IOException e) {
                System.err.println("Error in opening file " + e);
            }

            setDataChanged(true);
        } finally {
            setWaiting(false);
        }
    }
    
    void InsertFileCRLMODJMI_actionPerformed(java.awt.event.ActionEvent event) {
        setWaiting(true);
        try {
            if (table.getData().getRows().isEmpty()) {
                InsertFileJMI_actionPerformed(event);
                return;
            }
            ManageData dataToInsert = new ManageData();
            CrLmodFile rootFile = new CrLmodFile("CrLmod Managements", CsipLmodUtil.lmodType.lmodManagement);
            rootFile.listFiles();
            WepsFileChooser2 mfc = new WepsFileChooser2(WepsFileTypes2.Management, rootFile,
                    WepsFileChooser2.Action.Select);
            mfc.setCurrentDirectory(rootFile);
            mfc.getPath().setDefaultDir(rootFile);
            TFile shaMan = new TFile(Util.parse(Weps.getInstance().cd.getData(ConfigData.SharedManDB)));
            TFile locMan = new TFile(Util.parse(Weps.getInstance().cd.getData(ConfigData.LocalManDB)));
            TFile sysMan = new TFile(Util.parse(MCREWConfig.getDirectoryName(ConfigData.ManTemp)));
            mfc.getPath().addItem(shaMan);
            mfc.getPath().addItem(locMan);
            mfc.getPath().addItem(sysMan);
            //set the tooltip of "home" button to "Home" instead of "Desktop"
            mfc.homeToolTip(mfc);
            //remove the NewFolder button
            mfc.disableNewFolder(mfc);
            mfc.enableFolderButtons();
            mfc.getPath().addAndSelectItem(rootFile);
            if (!(mfc.showDialog(null) == WepsFileChooser2.APPROVE_OPTION)) {
                return;
            }
            TFile selectedFile = new TFile(mfc.getSelectedFile());
            try {
                String filePath = selectedFile.getCanonicalPath();
                System.err.println("Mcrew:InsertFileCRLMODJMI(): " + "Opening file " + filePath);
                //******************************The Bad Line
                dataToInsert.readDataFile(filePath);
                //******************************The Bad Line
                table.insertFile(dataToInsert, true, false);
                table.checkData();
                //savePath = delectedFile.getParent();
            } catch (IOException e) {
                System.err.println("Error in opening file " + e);
            }

            setDataChanged(true);
        } finally {
            setWaiting(false);
        }
    }
    
    void AddRotationDateAdjustedJMI_actionPerformed(java.awt.event.ActionEvent event) {
        setWaiting(true);
        try {

            ManageData dataToInsert = new ManageData();

            TFile dirXML = new TFile(MCREWConfig.getDirectoryName(XMLConstants.smanagement_template));
            WepsFileChooser2 mfc;

            mfc = WepsFileChooser2.create(WepsFileChooser2.Action.Select, dirXML, WepsFileTypes2.Management);
            mfc.setCurrentDirectory(dirXML); //new de.schlichtherle.truezip.file.File(ConfigData.INITIAL_DIR));
            mfc.setDialogTitle("Select a Rotation File");

            //set the tooltip of "home" button to "Home" instead of "Desktop"
            mfc.homeToolTip(mfc);
            //remove the NewFolder button
            mfc.disableNewFolder(mfc);
            mfc.enableFolderButtons();
            if (!(mfc.showDialog(null) == WepsFileChooser2.APPROVE_OPTION)) {
                return;
            }
            TFile selectedFile = new TFile(mfc.getSelectedFile());

            try {
                String filePath = selectedFile.getCanonicalPath();
                //System.err.println("Mcrew:AddRotationDateAdjustedJMI(): " + "Opening file " + filePath);
                dataToInsert.readDataFile(filePath);
                dataToInsert.rotToIntSort();
                for(RowInfo info : dataToInsert.getRows()) info.enforceDisplayDate();
                table.insertFile(dataToInsert, true, true);
                table.checkData();
                //savePath = delectedFile.getParent();
            } catch (IOException e) {
                System.err.println("Error in opening file " + e);
            }

            setDataChanged(true);
        } finally {
            setWaiting(false);
        }
    }
    
    // SymAction is overridden here to allow the popup menu objects to
    // be included in the list
    class SymAction implements java.awt.event.ActionListener {

        @Override
        public void actionPerformed(java.awt.event.ActionEvent event) {

            Object object = event.getSource();

            if (object == popCutJMI) {
                CutJMI_actionPerformed(event);
            } else if (object == popCopyJMI) {
                CopyJMI_actionPerformed(event);
            } else if (object == popPasteJMI) {
                PasteJMI_actionPerformed(event);
            } else if (object == popDeleteJMI) {
                DeleteJMI_actionPerformed(event);
            } else if (object == popUndoDeleteJMI) {
                UndoDeleteJMI_ActionPerformed(event);
            } else if (object == popSetPivotJMI) {
                table.setPivot(event);
            } else if (object == popInsertJMI) {
                InsertJMI_actionPerformed(event);
            } else if (object == popInsertOperationJMI) {
               InsertOperationJMI_actionPerformed(event);
            } else if (object == popInsertFileJMI) {
                // added by Sada
               InsertFileJMI_actionPerformed(event);
            } else if (object == popInsertFileCRLMODJMI) {
                // added by DB
               InsertFileCRLMODJMI_actionPerformed(event);
            } else if (object == popInsertFileDateAdjustedJMI) {
                // added by Sada
                InsertFileDateAdjustedJMI_actionPerformed(event);
            } else if (object == popInsertFileWEPPmanFile) {
                // Added by CKM
                popInsertFileWEPPmanFile_actionPreformed(event);
            } else if (object == popAddRotationDateAdjustedJMI) {
                AddRotationDateAdjustedJMI_actionPerformed(event);
            } else if (object == popDrillDownScreenJMI) {
                // added by Sada
                DrillDownScreenJMI_actionPerformed(event);
            } else if (object == popSetDateJMI) {
                SetDateJMI_actionPerformed(event);
            } else if (object == popAdjustDateJMI) {
                ChangeDateJMI_actionPerformed(event);
            } else if (object == setDateJMI) {
                SetDateJMI_actionPerformed(event);
            } else if (object == calendarDateJMI) {
                CalendarDateJMI_actionPerformed(event);
            } else if (object == dateChangeDateJMI) {
                ChangeDateJMI_actionPerformed(event);
            } else if (object == dateIncrementYearJMI) {
                table.changeDates(Calendar.YEAR, 1);
                getCoordinator().dateChange(this);
            } else if (object == dateDecrementYearJMI) {
                table.changeDates(Calendar.YEAR, -1);
                getCoordinator().dateChange(this);
            } else if (object == dateIncrementMonthJMI) {
                table.changeDates(Calendar.MONTH, 1);
                getCoordinator().dateChange(this);
            } else if (object == dateDecrementMonthJMI) {
                table.changeDates(Calendar.MONTH, -1);
                getCoordinator().dateChange(this);
            } else if (object == dateIncrementWeekJMI) {
                table.changeDates(Calendar.DATE, 7);
                getCoordinator().dateChange(this);
            } else if (object == dateDecrementWeekJMI) {
                table.changeDates(Calendar.DATE, -7);
                getCoordinator().dateChange(this);
            } else if (object == dateIncrementDayJMI) {
                table.changeDates(Calendar.DATE, 1);
                getCoordinator().dateChange(this);
            } else if (object == dateDecrementDayJMI) {
                table.changeDates(Calendar.DATE, -1);
                getCoordinator().dateChange(this);
            } else if ((object == operationScreenJMI) || (object == cropScreenJMI)) {
                DrillDownScreenJMI_actionPerformed(event);
            } else if (object == addChangeOperationJMI){
                table.addColumnData(1);
            } else if(object == operationNotesJMI) {
                table.showNotes(1);
            } else if(object == addChangeCropJMI) {
                table.addColumnData(2);
            } else if(object == cropNotesJMI) {
                table.showNotes(2);
            } else if (object == setDirectionJMI) {
                SetDirectionJMI_actionPerformed(event);
            } else if (object == adjustDirectionJMI) {
                AdjustDirectionJMI_actionPerformed(event);
            } else if (object == setTozeroJMI) {
                SetToZeroJMI_actionPerformed(event);
            } else if (object == setTo45JMI) {
                SetTo45JMI_actionPerformed(event);
            } else if (object == setTo90JMI) {
                SetTo90JMI_actionPerformed(event);
            } else if (object == incrementBy15JMI) {
                IncrementBy15JMI_actionPerformed(event);
            } else if (object == decrementBy15JMI) {
                DecrementBy15JMI_actionPerformed(event);
            } else if (object == incrementBy45JMI) {
                IncrementBy45JMI_actionPerformed(event);
            } else if (object == decrementBy45JMI) {
                DecrementBy45JMI_actionPerformed(event);
            } else if (object == changeBy90JMI) {
                ChangeBy90JMI_actionPerformed(event);
                //Start here
            }
        }
    }
    
    
    /**
     * Saves the table to a .man file.
     */
    @SuppressWarnings("fallthrough")
    public void saveCurrent()
    {
        boolean resort = table.getData().intView();
        switch(table.getData().getSave())
        {
            case NOSAVE:
                JOptionPane.showMessageDialog(null, "Unable to save:\nSome data out of limits.",
                "Management File", JOptionPane.INFORMATION_MESSAGE);
                return;
            case WARNSAVE:
                JOptionPane.showMessageDialog(null, "Warning:\nSome data out of recommended limits.",
                "Management File", JOptionPane.INFORMATION_MESSAGE);
                /*FallThrough*/
            default:
        }
        if ((fileName == null) || (fileName.length() == 0) || (fileName.equals(sUntitledFileName))) 
        {
            coordinator.saveCurrentAs();
            return;
        }

        if (!usda.weru.util.ConfigData.getDefault().isFormatXMLAllowed() &amp;&amp; fileName.toLowerCase().endsWith(".xml")) {
            coordinator.saveCurrentAs();
            return;

        }
        if(resort) coordinator.JMI_ItR_ActionPerformed(new ActionEvent(this, 2, ""));

        if (table.checkAllConditions() == false) {
            saveSuccess = false;
            if(resort) coordinator.JMI_RtI_ActionPerformed(new ActionEvent(this, 2, ""));
            return; // user pressed cancel on some option, Don't save
        }

        //If the use doesnt opens a file or opens a template of a file
        // and without making any changes saves the file, then the "absFileName"
        // should reflect this saved file.
        absFileName = filePathName;

        if (table.getDataChanged() == false) {
            if(resort) coordinator.JMI_RtI_ActionPerformed(new ActionEvent(this, 2, ""));
            return;
        }
        if ((savePath == null) || (savePath.length() == 0)) {
            savePath = MCREWConfig.getDirectoryName(XMLConstants.smanagement_template);

            ////System.out.println("Data changed " + dataChanged);
        }
        String fullPathFileName = savePath + TFile.separator + fileName;
        TFile mcrewFile = new TFile(fullPathFileName);
        TFile parentFile = new TFile(mcrewFile.getParentFile());
        if (parentFile.canWrite() == false) {
            JOptionPane.showMessageDialog(this, "The File location Specified is not writable\n " + fullPathFileName, "Error",
                    JOptionPane.ERROR_MESSAGE);
            if(resort) coordinator.JMI_RtI_ActionPerformed(new ActionEvent(this, 2, ""));
            return;
        }
        if(jOldFormatButton.isSelected()) {
            table.getData().setWriteVersionToOld(true);
            System.out.println("writing old version");
        } else {
            table.getData().setWriteVersionToOld(false);
            System.out.println("writing new version");
        }
        table.getData().writeDataFile(savePath + TFile.separator + fileName);
        absFileName = fullPathFileName;

        setDataChanged(false);
        saveSuccess = true;

        if (refreshMcrewScreen.equals("true")) {
            openRefreshedFile(absFileName);
            refreshMcrewScreen = "false";
        }
        if(resort) coordinator.JMI_RtI_ActionPerformed(new ActionEvent(this, 2, ""));
    }
    
    /**
     * Allows the user to choose a name and location, and saves the table with
     * said name and location.
     */
    @SuppressWarnings("fallthrough")
    public void saveCurrentAs() {
        boolean resort = table.getData().intView();
        switch(table.getData().getSave())
        {
            case NOSAVE:
                JOptionPane.showMessageDialog(null, "Unable to save:\nSome data out of limits.",
                "Management File", JOptionPane.INFORMATION_MESSAGE);
                return;
            case WARNSAVE:
                JOptionPane.showMessageDialog(null, "Warning:\nSome data out of recommended limits.",
                "Management File", JOptionPane.INFORMATION_MESSAGE);
                /*FallThrough*/
            default:
        }
        RandomAccessFile in;
        long endPos;

        if(resort) coordinator.JMI_ItR_ActionPerformed(new ActionEvent(this, 2, ""));
        if (table.checkAllConditions() == false) {
            saveSuccess = false;
            if(resort) coordinator.JMI_RtI_ActionPerformed(new ActionEvent(this, 2, ""));
            return; // user pressed cancel on some option, Don't save
        }

        WepsFileChooser2 mfc;
        TFile tempFile;
        if ((savePath == null) || (savePath.length() == 0)) {
            fileName = "untitled.man";
            //The file chooser dialog will open in the current WEPS directory.
            mfc = new WepsFileChooser2(WepsFileTypes2.Management, coordinator.getProjectPath(), WepsFileChooser2.Action.Save);
            mfc.setCurrentDirectory(new TFile(coordinator.getProjectPath())); //new de.schlichtherle.truezip.file.File(ConfigData.INITIAL_DIR));
            tempFile = new TFile(coordinator.getProjectPath(), fileName);
            mfc.setSelectedFile(tempFile);
            mfc.setText(tempFile.getName());
        } else if (!usda.weru.util.ConfigData.getDefault().isFormatXMLAllowed()) {
            //xml not allowed!
            mfc = new WepsFileChooser2(WepsFileTypes2.Management, savePath, WepsFileChooser2.Action.Save);
            String tempFileName = Util.purgeExtensions(fileName, WepsFileTypes2.Management.getExtension());
            tempFile = new TFile(savePath, tempFileName);
            /*
            --- Old stuff, might need it in a few CI's ---
            mfc.setCurrentDirectory(new TFile(savePath));
            mfc.setDefaultDirectory(new TFile(savePath));
            mfc.setDefaultCurrentDirectory(savePath);
            */
            mfc.createBasicFileChooser(WepsFileChooser2.SelectionType.FILES_ONLY, null, false, "Select a file name and destination",null, tempFile);

        } else {
            mfc = WepsFileChooser2.create(WepsFileChooser2.Action.Save, new TFile(savePath), false, false, "", WepsFileTypes2.Management, WepsFileTypes2.XML);
            tempFile = new TFile(savePath, fileName);
            mfc.setCurrentDirectory(new TFile(savePath));
            mfc.setSelectedFile(tempFile);
            mfc.setText(tempFile.getName());
        }

        //set the tooltip of "home" button to "Home" instead of "Desktop"
        mfc.homeToolTip(mfc);

        mfc.setDialogTitle("Save a Management File");
        mfc.getPath().selectProjDir(); // Just select the project directory, easiest way
        mfc.enableFolderButtonsSave();
        mfc.setSelectedFile(tempFile); // sets selected file to current manfile name, not blank
        if (mfc.showDialog(this) != WepsFileChooser2.APPROVE_OPTION) {
            //            fileName = "";
            if(resort) coordinator.JMI_RtI_ActionPerformed(new ActionEvent(this, 2, ""));
            return;
        }
        TFile currentFile = new TFile(mfc.getSelectedFile());
        String newFileName = currentFile.getPath();
        TFile testFile = currentFile;
        TFile parentDir = new TFile(testFile.getParentFile());
        String name = testFile.getName();
        while (parentDir != null &amp;&amp; !parentDir.exists()) {
            name = parentDir.getName() + TFile.separator + name;
            parentDir = new TFile(parentDir.getParentFile());
        }

        char[] invalidChars = {'\\', '/', '&lt;', '&gt;', '|', '?', '*', '&amp;', '"', ':', '~', '`', '\''};
        //A single quote cannot be given as a character, so given as a separate string
        for (int i = 0; i &lt; name.length(); i++) {
            for (int j = 0; j &lt; invalidChars.length; j++) {
                if (name.charAt(i) == invalidChars[j]) {
                    JOptionPane.showMessageDialog(this, "INVALID CHARACTER : "
                            + invalidChars[j] + " \n\\ / &lt; &gt; | ? * &amp; \" ~ ` ' ",
                            " Cannot Save file ", JOptionPane.INFORMATION_MESSAGE);
                    saveCurrentAs();
                    if(resort) coordinator.JMI_RtI_ActionPerformed(new ActionEvent(this, 2, ""));
                    return;
                }
            }
        }
        if (!mfc.getSelectedFile().getParentFile().exists()) {

            JOptionPane.showMessageDialog(this, "WRONG PATH : " + newFileName + " doesn't exist.", " Cannot Save file ",
                    JOptionPane.INFORMATION_MESSAGE);
            saveCurrentAs();
            if(resort) coordinator.JMI_RtI_ActionPerformed(new ActionEvent(this, 2, ""));
            return;
        }
        //don't let a read only file save over itself or another read only file
        if(MCREWWindow.isReadOnly(newFileName)){
            JOptionPane.showMessageDialog(this, "Error : Can not write over a Readonly file", " Cannot Save file ",
                    JOptionPane.INFORMATION_MESSAGE);
            saveCurrentAs();
            return;
        }else{
            //save as will be asuccesful action, so reenable saves
            coordinator.toggleSaves(true);
            
            
        }

        // strip suffix that filechooser seems to want to add
        if (newFileName.toUpperCase().endsWith(".MAN")) {
            newFileName = newFileName.substring(0, newFileName.lastIndexOf('.'));
        } else if (newFileName.toUpperCase().endsWith(".XML")) {
            newFileName = newFileName.substring(0, newFileName.lastIndexOf('.'));
        }

        String savedFile = null; //save the name of the file along with .man or .xml extension.

        // create .xml file if filter specified if
        int result = -1;
        if (mfc.accept(new TFile("x.xml"))) {
            result = table.getData().writeDataFile(newFileName + ".xml");
            savedFile = newFileName + ".xml";
        }
       
        //MUST DO THE MAN SECOND!! Make sure we use it in the run data.
        // create .man file if filter specified if
        if (mfc.accept(new TFile("x.man"))) {
            result = table.getData().writeDataFile(newFileName + ".man");
            savedFile = newFileName + ".man";
        }

        if (result != 1) {
            JOptionPane.showMessageDialog(this, "The File location Specified is not writable\n " + newFileName, "Error",
                    JOptionPane.ERROR_MESSAGE);
            if(resort)coordinator.JMI_RtI_ActionPerformed(new ActionEvent(this, 2, ""));
            return;
        }

        fileName = new TFile(savedFile).getName();
        saveSuccess = true;

        try {
            table.getData().setWepsManFileNotes("");
            TFile fileDir = new TFile(savedFile);
            absFileName = fileDir.getCanonicalPath();
        } catch (IOException e) {
            System.err.println("Error in opening file " + e);
        }
        String message = null;
        //If Mcrew is not stand alone, append the message that mcrew will be closed
        changeRotFileName(savedFile);
        setDataChanged(false);
        if(resort)coordinator.JMI_RtI_ActionPerformed(new ActionEvent(this, 2, ""));
    }
    
    /**
     * Saves the table as the file defined by the input String (path)
     */
    @SuppressWarnings("fallthrough")
    public void saveCurrentWithName(String path) {
        boolean resort = table.getData().intView();
        switch(table.getData().getSave())
        {
            case NOSAVE:
                JOptionPane.showMessageDialog(null, "Unable to save:\nSome data out of limits.",
                "Management File", JOptionPane.INFORMATION_MESSAGE);
                return;
            case WARNSAVE:
                JOptionPane.showMessageDialog(null, "Warning:\nSome data out of recommended limits.",
                "Management File", JOptionPane.INFORMATION_MESSAGE);
                /*FallThrough*/
            default:
        }

        if(resort) coordinator.JMI_ItR_ActionPerformed(new ActionEvent(this, 2, ""));
        if (table.checkAllConditions() == false) {
            saveSuccess = false;
            if(resort) coordinator.JMI_RtI_ActionPerformed(new ActionEvent(this, 2, ""));
            return;
        }

        TFile currentFile = new TFile(path);

        String newFileName = currentFile.getPath();
        TFile testFile = currentFile;
        TFile parentDir = new TFile(testFile.getParentFile());
        String name = testFile.getName();
        while (parentDir != null &amp;&amp; !parentDir.exists()) {
            name = parentDir.getName() + TFile.separator + name;
            parentDir = new TFile(parentDir.getParentFile());
        }
        

        char[] invalidChars = {'\\', '/', '&lt;', '&gt;', '|', '?', '*', '&amp;', '"', ':', '~', '`', '\''};
        //A single quote cannot be given as a character, so given as a separate string
        for (int i = 0; i &lt; name.length(); i++) {
            for (int j = 0; j &lt; invalidChars.length; j++) {
                if (name.charAt(i) == invalidChars[j]) {
                    JOptionPane.showMessageDialog(this, "INVALID CHARACTER : "
                            + invalidChars[j] + " \n\\ / &lt; &gt; | ? * &amp; \" ~ ` ' ",
                            " Cannot Save file ", JOptionPane.INFORMATION_MESSAGE);
                    if(resort) coordinator.JMI_RtI_ActionPerformed(new ActionEvent(this, 2, ""));
                    return;
                }
            }
        }
        if(jOldFormatButton.isSelected()) {
            table.getData().setWriteVersionToOld(true);
            System.out.println("writing old version");
        } else {
            table.getData().setWriteVersionToOld(false);
            System.out.println("writing new version");
        }
        int result = table.getData().writeDataFile(currentFile.getAbsolutePath());

        if (result != 1) {
            JOptionPane.showMessageDialog(this, "The File location Specified is not writable\n " + newFileName, "Error",
                    JOptionPane.ERROR_MESSAGE);
            if(resort)coordinator.JMI_RtI_ActionPerformed(new ActionEvent(this, 2, ""));
            return;
        }

        fileName = currentFile.getName();
        saveSuccess = true;

        setDataChanged(false);
        if(resort)coordinator.JMI_RtI_ActionPerformed(new ActionEvent(this, 2, ""));
    }
    
    /**
     * Allows the user to choose a name and location, and saves the table with
     * said name and location.  The default location is set to the DB directory.
     */
    public void saveCurrentAsTemplate() {
        if (table.checkAllConditions() == false) {
            return; // user pressed cancel on some option, Don't save
        }

        String tempSaveAsPath = MCREWConfig.getDirectoryName(usda.weru.util.ConfigData.LocalManDB);
        String tempPath = MCREWConfig.getDirectoryName(XMLConstants.smanagement_template);
        TFile dirXML = null;
        if (tempSaveAsPath != null &amp;&amp; tempSaveAsPath.length() &gt; 0 &amp;&amp; MCREWConfig.getHasManTempSaveAsDir()) {
            dirXML = new TFile(tempSaveAsPath);
        } else {
            dirXML = new TFile(tempPath);
        }
        String dirXMLPath = dirXML.getAbsolutePath();

        TFile tempFile;
        tempFile = new TFile(dirXMLPath, fileName != null ? fileName : "Untitled");

        WepsFileChooser2 mfc = new WepsFileChooser2(WepsFileTypes2.Management, dirXMLPath, WepsFileChooser2.Action.Save);

        mfc.setCurrentDirectory(dirXML); //new de.schlichtherle.truezip.file.File(ConfigData.INITIAL_DIR));
        //set the tooltip of "home" button to "Home" instead of "Desktop"
        mfc.homeToolTip(mfc);
        mfc.setPersitSelectedFile(true);
        mfc.setSelectedFile(tempFile);
        mfc.setText(tempFile.getName());
        mfc.enableFolderButtonsSave();
        mfc.setDialogTitle("Save a Template Management File");
        TFile shaMan = new TFile(ConfigData.getDefault().getData(ConfigData.SharedManDB));
        //System.out.println("shaMan path: " + shaMan.getAbsolutePath());
        //System.out.println("Is shaMan a directory? " + shaMan.isDirectory());
        TFile sysMan = new TFile(MCREWConfig.getDirectoryName(XMLConstants.smanagement_template));
        //System.out.println("sysMan path: " + sysMan.getAbsolutePath());
        //System.out.println("Is sysMan a directory? " + sysMan.isDirectory());
        mfc.getPath().addItem(shaMan);
        mfc.getPath().addItem(sysMan);
        if (mfc.showDialog(this) != WepsFileChooser2.APPROVE_OPTION) {
            return;
        }
        TFile testFile = new TFile(mfc.getSelectedFile());
        TFile parentDir = new TFile(testFile.getParentFile());
        String name = testFile.getName();
        while (parentDir != null &amp;&amp; !parentDir.exists()) {
            name = parentDir.getName() + TFile.separator + name;
            parentDir = new TFile(parentDir.getParentFile());
        }

        char[] invalidChars = {'\\', '/', '&lt;', '&gt;', '|', '?', '*', '&amp;', '"', ':', '~', '`', '\''};
        //A single quote cannot be given as a character, so given as a separate string
        for (int i = 0; i &lt; name.length(); i++) {
            for (int j = 0; j &lt; invalidChars.length; j++) {
                if (name.charAt(i) == invalidChars[j]) {
                    JOptionPane.showMessageDialog(this, "INVALID CHARACTER : " + invalidChars[j] + " \n\\ / &lt; &gt; | ? * &amp; \" ~ ` '",
                            " Cannot Save file ", JOptionPane.INFORMATION_MESSAGE);
                    saveCurrentAsTemplate();
                    return;
                }
            }
        }

        TFile selectedFile = new TFile(mfc.getSelectedFile());
        if (!selectedFile.getParentFile().exists()) {

            JOptionPane.showMessageDialog(this, "WRONG PATH : " + selectedFile.getPath()
                    + " doesn't exist.", " Cannot Save file ",
                    JOptionPane.INFORMATION_MESSAGE);
            saveCurrentAsTemplate();
            return;
        }

        String newFileName = selectedFile.getPath();

        // strip suffix that filechooser seems to want to add
        if (newFileName.toUpperCase().endsWith(".MAN")) {
            newFileName = newFileName.substring(0, newFileName.lastIndexOf('.'));
        } else if (newFileName.toUpperCase().endsWith(".XML")) {
            newFileName = newFileName.substring(0, newFileName.lastIndexOf('.'));
        }

        String savedFile = null; //save the name of the file along with .man or .xml extension.

        // create .xml file if filter specified if
        int result = -1;
        if (mfc.accept(new TFile("x.xml"))) {
            result = table.getData().writeDataFile(newFileName + ".xml");
            savedFile = newFileName + ".xml";
        }

        //MUST DO THE MAN SECOND!! Make sure we use it in the run data.
        // create .man file if filter specified if
        if (mfc.accept(new TFile("x.man"))) {
            result = table.getData().writeDataFile(newFileName + ".man");
            savedFile = newFileName + ".man";
        }

        if (result != 1) {
            JOptionPane.showMessageDialog(this, "The File location Specified is not writable\n " + newFileName, "Error",
                    JOptionPane.ERROR_MESSAGE);
            return;
        }

        JOptionPane.showMessageDialog(this, savedFile, "Management file(s) template created", JOptionPane.INFORMATION_MESSAGE);

        //Comment out this line if you dont want to set the name of this file
        // to the rotation text field.
        //    RotationJT.setText(savedFilePath); // set the rotation file text area to last file saved
    }
    
    /**
     * Allows the user to choose a name and location, and saves the table with
     * said name and location.  The default location is set to the DB directory.
     */
    public void saveCurrentAsSharedTemplate() {
        if (table.checkAllConditions() == false) {
            return; // user pressed cancel on some option, Don't save
        }
        //String tempSaveAsPath = MCREWConfig.getDirectoryName(usda.weru.util.ConfigData.SharedManDB);
        //String tempPath = MCREWConfig.getDirectoryName(XMLConstants.smanagement_template);
        TFile dirXML = new TFile(Weps.getInstance().cd.getData(ConfigData.SharedManDB));
//        if (tempSaveAsPath != null &amp;&amp; tempSaveAsPath.length() &gt; 0 &amp;&amp; MCREWConfig.getHasManTempSaveAsDir()) {
//            dirXML = new TFile(tempSaveAsPath);
//        } else {
//            dirXML = new TFile(tempPath);
//        }
        String dirXMLPath = dirXML.getAbsolutePath();

        TFile tempFile;
        tempFile = new TFile(dirXMLPath, fileName != null ? fileName : "Untitled");

        WepsFileChooser2 mfc = new WepsFileChooser2(WepsFileTypes2.Management, dirXMLPath, WepsFileChooser2.Action.Save);

        mfc.setCurrentDirectory(dirXML); //new de.schlichtherle.truezip.file.File(ConfigData.INITIAL_DIR));
        //set the tooltip of "home" button to "Home" instead of "Desktop"
        mfc.homeToolTip(mfc);
        mfc.setPersitSelectedFile(true);
        mfc.setSelectedFile(tempFile);
        mfc.setText(tempFile.getName());
        mfc.enableFolderButtonsSave();
        mfc.setDialogTitle("Save a Template Management File");
        TFile locMan = new TFile(Weps.getInstance().cd.getData(ConfigData.LocalManDB));
        //System.out.println("shaMan path: " + shaMan.getAbsolutePath());
        //System.out.println("Is shaMan a directory? " + shaMan.isDirectory());
        TFile sysMan = new TFile(MCREWConfig.getDirectoryName(XMLConstants.smanagement_template));
        //System.out.println("sysMan path: " + sysMan.getAbsolutePath());
        //System.out.println("Is sysMan a directory? " + sysMan.isDirectory());
        mfc.getPath().addItem(locMan);
        mfc.getPath().addItem(sysMan);
        if (mfc.showDialog(this) != WepsFileChooser2.APPROVE_OPTION) {
            return;
        }
        TFile testFile = new TFile(mfc.getSelectedFile());
        TFile parentDir = new TFile(testFile.getParentFile());
        String name = testFile.getName();
        while (parentDir != null &amp;&amp; !parentDir.exists()) {
            name = parentDir.getName() + TFile.separator + name;
            parentDir = new TFile(parentDir.getParentFile());
        }

        char[] invalidChars = {'\\', '/', '&lt;', '&gt;', '|', '?', '*', '&amp;', '"', ':', '~', '`', '\''};
        //A single quote cannot be given as a character, so given as a separate string
        for (int i = 0; i &lt; name.length(); i++) {
            for (int j = 0; j &lt; invalidChars.length; j++) {
                if (name.charAt(i) == invalidChars[j]) {
                    JOptionPane.showMessageDialog(this, "INVALID CHARACTER : " + invalidChars[j] + " \n\\ / &lt; &gt; | ? * &amp; \" ~ ` '",
                            " Cannot Save file ", JOptionPane.INFORMATION_MESSAGE);
                    saveCurrentAsTemplate();
                    return;
                }
            }
        }

        TFile selectedFile = new TFile(mfc.getSelectedFile());
        if (!selectedFile.getParentFile().exists()) {

            JOptionPane.showMessageDialog(this, "WRONG PATH : " + selectedFile.getPath()
                    + " doesn't exist.", " Cannot Save file ",
                    JOptionPane.INFORMATION_MESSAGE);
            saveCurrentAsTemplate();
            return;
        }

        String newFileName = selectedFile.getPath();

        // strip suffix that filechooser seems to want to add
        if (newFileName.toUpperCase().endsWith(".MAN")) {
            newFileName = newFileName.substring(0, newFileName.lastIndexOf('.'));
        } else if (newFileName.toUpperCase().endsWith(".XML")) {
            newFileName = newFileName.substring(0, newFileName.lastIndexOf('.'));
        }

        String savedFile = null; //save the name of the file along with .man or .xml extension.

        // create .xml file if filter specified if
        int result = -1;
        if (mfc.accept(new TFile("x.xml"))) {
            result = table.getData().writeDataFile(newFileName + ".xml");
            savedFile = newFileName + ".xml";
        }

        //MUST DO THE MAN SECOND!! Make sure we use it in the run data.
        // create .man file if filter specified if
        if (mfc.accept(new TFile("x.man"))) {
            result = table.getData().writeDataFile(newFileName + ".man");
            savedFile = newFileName + ".man";
        }

        if (result != 1) {
            JOptionPane.showMessageDialog(this, "The File location Specified is not writable\n " + newFileName, "Error",
                    JOptionPane.ERROR_MESSAGE);
            return;
        }

        JOptionPane.showMessageDialog(this, savedFile, "Management file(s) template created", JOptionPane.INFORMATION_MESSAGE);

        //Comment out this line if you dont want to set the name of this file
        // to the rotation text field.
        //    RotationJT.setText(savedFilePath); // set the rotation file text area to last file saved
    }
    
    /**
     * Returns the path on which the file should save.
     */
    public String getSavePath() { return savePath; }
    public Table getTable() { return table; }
    
    public void doPrint() { table.doPrint(fileName); }
    
    @SuppressWarnings("fallthrough")
    public boolean checkSaveOnExit()
    {
        if (new TFile(absFileName).isDirectory()) {
            absFileName = "";
            fileName = "";
            filePathName = "";
        }
        if (table.getDataChanged()) 
        {
            int option = JOptionPane.showConfirmDialog(this, "Save current Management File ? ");
            switch (option) {
                case JOptionPane.CANCEL_OPTION:
                    return false;
                case JOptionPane.YES_OPTION:
                    switch(table.getData().getSave())
                    {
                        case NOSAVE:
                            JOptionPane.showMessageDialog(null, "Unable to save:\nSome data out of limits.",
                            "Management File", JOptionPane.INFORMATION_MESSAGE);
                            return false;
                        case WARNSAVE:
                            JOptionPane.showMessageDialog(null, "Warning:\nSome data out of recommended limits.",
                            "Management File", JOptionPane.INFORMATION_MESSAGE);
                            /*FallThrough*/
                        default:
                            if(!table.checkAllConditions()) return false;
                            if ((fileName == null) || (fileName.length() == 0) 
                                    || (fileName.equals(sUntitledFileName))) saveCurrentAs();
                            else saveCurrent(); 
                            break;
                    }
                case JOptionPane.NO_OPTION:
                    break;
            }
        }
        return true;
    }
    
    /**
     * Fires a property change.
     */
    public void firePropertyChange(String name, Object oldValue, Object newValue)
    {
        if(changes != null) changes.firePropertyChange(name, oldValue, newValue);
    }
    
    /**
     * Changes the icon to the right of rotation file name.
     * @param input 
     */
    void changeTableLabel(Icon input)
    {
        JL_iconHolder.setIcon(input);
        if(table.getData().intView()) 
        {
            JTB_RtI.setIcon(Resources.getIcon("MCREWRotToggle.png"));
            JTB_RtI.setToolTipText("&lt;html&gt;\n&lt;b&gt;MCREW Toolbar (Clock icon)&lt;/b&gt;\n&lt;p&gt;Switches MCREW to display in \"WEPS Rotation Mode\"&lt;/p&gt;");
            CSH.setHelpIDString(JTB_RtI, "mcrewclockbutton_html");
            JB_cycleBwd.setEnabled(false);
            JB_cycleFwd.setEnabled(false);
        }
        else 
        {
            JTB_RtI.setIcon(Resources.getIcon("MCREWIntToggle.png"));
            JTB_RtI.setToolTipText("&lt;html&gt;\n&lt;b&gt;MCREW Toolbar (Green plant icon)&lt;/b&gt;\n&lt;p&gt;Switches MCREW to display in \"WEPS Crop Rotation Mode\"&lt;/p&gt;");
            CSH.setHelpIDString(JTB_RtI, "mcrewplantbutton_html");
            JB_cycleBwd.setEnabled(true);
            JB_cycleFwd.setEnabled(true);
        }
    }
    
    /**
     * This method will color the Rotation Years field with the ill wrapping color.
     */
    public void setIllWrap() { JTF_rotYears.setBackground(new Color(255,64,64)); }
    
    /**
     * This method will color the Rotation Years field with the default color (white).
     */
    public void clearIllWrap() { JTF_rotYears.setBackground(Color.WHITE); }
    
    public boolean rotToIntSort()
    {
        if(table.checkAllConditions())
        {
            table.setPivoting(true);
            table.getData().rotToIntSort();
            table.setPivoting(false);
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public void intToRotSort() throws Exception
    {
        table.setPivoting(true);
        table.getData().intToRotSort();
        table.setPivoting(false);
    }
    
}
</pre></body></html>