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

import de.schlichtherle.truezip.file.TFile;
import java.awt.Component;
import java.awt.Cursor;
import static java.awt.Cursor.getPredefinedCursor;
import java.awt.HeadlessException;
import java.beans.PropertyChangeEvent;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
// import java.util.logging.Logger;
import org.apache.log4j.Logger;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import static javax.swing.JOptionPane.*;
import static javax.swing.JOptionPane.showConfirmDialog;
import javax.swing.ListSelectionModel;
import javax.swing.SwingWorker;
import javax.swing.filechooser.FileSystemView;
import usda.weru.remoteDataAccess.csip.CsipLmodUtil;
import usda.weru.remoteDataAccess.csip.CsipLmodUtil.lmodType;
import usda.weru.remoteDataAccess.remoteFiles.RemoteFile;
import usda.weru.remoteDataAccess.remoteFiles.dbFile.soil.mdb.MdbSoilFileDatabase;
import usda.weru.remoteDataAccess.remoteFiles.dbFile.soil.soilDataMart.SDMFileDatabase;
import usda.weru.remoteDataAccess.remoteFiles.inetFile.CrLmodFile;
import usda.weru.remoteDataAccess.remoteFiles.inetFile.CsipSoilFile;
import usda.weru.util.About;
import usda.weru.util.ConfigData;
import usda.weru.util.Util;
import static usda.weru.util.Util.getProperty;
import usda.weru.weps.RunFileData;
/**
 *
 * @author mhaas
 */


/* ===== NOTE FOR THOSE USING ===== 
    I added a method to generate a basic and functional WepsFileChooser2 instance that
    in its' parameters has/sets all the basic information to generate a functional
    fileChooser without much knowledge of the class. Also added documentation to some
    of the more commonly used methods. Just generate an instance of file chooser through 
    whatever constructor is most applicable:

    WepsFileChooser2 fileChooser = new WepsFileChooser2(your choice of constructor);
                               Ex: new WepsFileChooser2(WepsFileTypes2.File, "", WepsFileChooser2.Action.Select);

    fileChooser.createBasicFileChooser(null, null, null, null, null, null);

    int response = .showDialog(parentComponent); // This displays the filechooser and the integer corresponds to success of choice

    -----------------------------------------------------------------------------------
    Every single argument can be null and will still generate a functional file chooser.
    adding in actual arguments will fine-tune the file chooser to more closely fit your
    needed use. -CKM
*/

public class WepsFileChooser2 extends WepsFileChooser2_n {

    static final long serialVersionUID = 1L;

    private static final Logger LOGGER = Logger.getLogger(WepsFileChooser2.class);
        
    public static enum Action {

        Open("Open", "Open a "), Save("Save", "Save a "), Delete("Delete", "Delete a "), Create("Create", "Create a "), Select("Select", "Select a ");
        
        private final String labelTxt;
        private final String description;

        private Action(String labelTxt, String description) {
            this.labelTxt = labelTxt;
            this.description = description;
        }
        public String getLabelTxt() {
            return labelTxt;
        }
        public String getDescription() {
            return description;
        }
    }
    
    public static enum SelectionType {
        FILES_ONLY,
        DIRECTORIES_ONLY,
        FILES_AND_DIRECTORIES         
    }
    
    private static final Map&lt;String, WepsFileTypes2&gt; HT_propertyToType = new HashMap&lt;&gt;();
    static {
        //start new properties
        HT_propertyToType.put(ConfigData.opRecordsCRLMOD, WepsFileTypes2.Dir);
        HT_propertyToType.put(ConfigData.cropRecordsCRLMOD, WepsFileTypes2.Dir);
        HT_propertyToType.put(ConfigData.residueRecordsCRLMOD, WepsFileTypes2.Dir);
        HT_propertyToType.put(ConfigData.localResidueRecords, WepsFileTypes2.Dir);
        HT_propertyToType.put(ConfigData.cligenDataCRLMOD, WepsFileTypes2.CliDB);
        HT_propertyToType.put(ConfigData.cligenIndexCRLMOD, WepsFileTypes2.CliIdx);
        HT_propertyToType.put(ConfigData.prismNormalsCRLMOD, WepsFileTypes2.File);
        HT_propertyToType.put(ConfigData.windgenDataCRLMOD, WepsFileTypes2.WindDB);
        HT_propertyToType.put(ConfigData.windgenIndexCRLMOD, WepsFileTypes2.WindIdx);
        //end new properties
        HT_propertyToType.put(ConfigData.WepsExe, WepsFileTypes2.Executable);
        HT_propertyToType.put(ConfigData.CliExe, WepsFileTypes2.Executable);
        HT_propertyToType.put(ConfigData.WinExe, WepsFileTypes2.Executable);
        HT_propertyToType.put(ConfigData.WindInterp1EXE, WepsFileTypes2.Executable);
        HT_propertyToType.put(ConfigData.WindInterp2EXE, WepsFileTypes2.Executable);
        HT_propertyToType.put(ConfigData.SWEEPExe, WepsFileTypes2.Executable);
        HT_propertyToType.put(ConfigData.WinData, WepsFileTypes2.WindDB);
        HT_propertyToType.put(ConfigData.WinIndex, WepsFileTypes2.WindIdx);
        HT_propertyToType.put(ConfigData.CliData, WepsFileTypes2.CliDB);
        HT_propertyToType.put(ConfigData.CliIndex, WepsFileTypes2.CliIdx);
        HT_propertyToType.put(ConfigData.ManTemp, WepsFileTypes2.Dir);
        HT_propertyToType.put(ConfigData.LocalManDB, WepsFileTypes2.Dir);
        HT_propertyToType.put(ConfigData.ManSkel, WepsFileTypes2.Dir);
        HT_propertyToType.put(ConfigData.SystemOpDB, WepsFileTypes2.Dir);
        HT_propertyToType.put(ConfigData.SystemCropDB, WepsFileTypes2.Dir);
        HT_propertyToType.put(ConfigData.LocalSDMDB, WepsFileTypes2.Dir);
        HT_propertyToType.put(ConfigData.LocalCropDB, WepsFileTypes2.Dir);
        HT_propertyToType.put(ConfigData.SharedManDB, WepsFileTypes2.Dir);
        HT_propertyToType.put(ConfigData.LocalOpDB, WepsFileTypes2.Dir);
        HT_propertyToType.put(ConfigData.LocalSoilDB, WepsFileTypes2.Dir);
        HT_propertyToType.put(ConfigData.MCREW, WepsFileTypes2.Dir);
        HT_propertyToType.put(ConfigData.SoilDB, WepsFileTypes2.Dir);
        HT_propertyToType.put(ConfigData.ProjDir, WepsFileTypes2.Dir);
        HT_propertyToType.put(ConfigData.DefaultRunsLocationTemplate, WepsFileTypes2.Dir);
        HT_propertyToType.put(ConfigData.McrewDataConfigFile, WepsFileTypes2.Dir);
        HT_propertyToType.put(ConfigData.SkelTranslationFile, WepsFileTypes2.File);
        HT_propertyToType.put(ConfigData.ReportFileName, WepsFileTypes2.File);
        HT_propertyToType.put(ConfigData.DetailTableFilterFile, WepsFileTypes2.File);
        HT_propertyToType.put(ConfigData.GISData, WepsFileTypes2.Dir);
        HT_propertyToType.put(ConfigData.SoilOrganicFile, WepsFileTypes2.Soil);
        HT_propertyToType.put(ConfigData.BarriersFile, WepsFileTypes2.File);
        HT_propertyToType.put(ConfigData.CurrentProj, WepsFileTypes2.Project);
        HT_propertyToType.put(ConfigData.FuelDatabase, WepsFileTypes2.XML);
        HT_propertyToType.put(ConfigData.WindgenInterpolationBoundaryFile, WepsFileTypes2.Pol);
    }
    
    // Return values from file/directory selction
    public static final int APPROVE_OPTION = JFileChooser.APPROVE_OPTION;
    public static final int CANCEL_OPTION = JFileChooser.CANCEL_OPTION;
    public static final int ERROR_OPTION = JFileChooser.ERROR_OPTION;
    
    public static String propChangeCmdStrFetchCompleted = "WFC_MainDialog_fetchCompleted";
    
    protected Action action;
    protected File selectedFile;
    protected File[] selectedFiles;
    protected boolean allowNewDir;
    protected SelectionType selectionMode;
    protected boolean multiSelection;
    protected ArrayList &lt;WepsFileTypes2&gt; chooserTypeList;
    protected WepsFileTypes2 chooserType;
    protected boolean allowDirectorySelection;

    // Constructors from JFileChooser
    private WepsFileChooser2 () {
        super();
    }
    public WepsFileChooser2 (File currentDirectory) {
        super();
        //System.out.println("dir");
        init(null, Action.Select, currentDirectory, WepsFileTypes2.Any);
    }
    public WepsFileChooser2 (File currentDirectory, FileSystemView fsv) {
        super();
        //System.out.println("dir + fsv");
        init(null, Action.Select, currentDirectory, WepsFileTypes2.Any);
    }
    public WepsFileChooser2 (FileSystemView fsv) {
        super();
        //System.out.println("fsv");
        init(null, Action.Select, new File(System.getProperty("user.dir")), WepsFileTypes2.Any);
    }
    public WepsFileChooser2 (String currentDirectoryPath) {
        super();
        //System.out.println("path");
        TFile initialDir = new TFile(Util.parse(currentDirectoryPath));
        //System.out.println("Parsed file: " + initialDir.getAbsolutePath());
        init(null, Action.Select, initialDir, WepsFileTypes2.Any);
    }
    public WepsFileChooser2 (String currentDirectoryPath, FileSystemView fsv) {
        super();
        //System.out.println("path + fsv");
        init(null, Action.Select, new File(currentDirectoryPath), WepsFileTypes2.Any);
    }
    
    // Constructors from WepsFileChooser
    public WepsFileChooser2 (WepsFileTypes2 type, String homeDirPath, Action action) {
        super();
        //System.out.println("type + path + action");
        init(null, action, new File(homeDirPath),type);
    }
    
    // Constructors from WepsFileChooser
    public WepsFileChooser2 (WepsFileTypes2 type, File homeDir, Action action) {
        super();
        //System.out.println("type + dir + action");
        init(null, action, homeDir,type);
    }
    
    public WepsFileChooser2(String property, Action action) {
        try {
            //System.out.println("property + action");
            WepsFileTypes2 thisFiletype = HT_propertyToType.get(property);
            init(null, action, new File("."), thisFiletype);

        } catch (NullPointerException npe) {
            System.err.println("WFC: property not defined " + property);
            init(null, action, new File("."), WepsFileTypes2.File);
        }
    }
    
    // Create from WepsFileChooser
    public static WepsFileChooser2 create(Action action, TFile home, WepsFileTypes2 type) {
        //System.out.println("action + file + type");
        return create(action, home, false, false, null, type);
    }
    public static WepsFileChooser2 create(Action action, TFile home, boolean allFilter, boolean groupFilter,
                                          String groupName, WepsFileTypes2... types) {
        WepsFileChooser2 wfc = new WepsFileChooser2 ();
        wfc.init(null, action, home, types);
        wfc.setAcceptAllFileFilterUsed(allFilter);
        //System.out.println("action + file + filter1 + filter2 + name + type...s");
        return wfc;
    }
    
    /**
     * This function is designed to set up a fileChooser as quickly as possible to
     * accomplish selecting file(s) or directory(s)
     * @param type The type of file you wish to select from enum (File, dir or both) if null, will be both
     * @param filter A File filter that is used to only display allowed files, if null set to all files
     * @param setMultipleFileSelect Whether the user is allowed to select multiple files by highlighting, or only a single file.
     * if null, defaults to false
     * @param informationText The text that appears between top of fileChooser and path selection bar.
     * @param topText  The text that appears in the gray top bar, generally purpose of file chooser. If null
     * selection will simply display ("WEPS File Chooser")
     * Usually basic instructions on what to select. If null, will reflect type
     * @param initDirectory The initial directory that the fileChooser will be pointing to. If null, sets to DataBase directory
     */
    public void createBasicFileChooser(SelectionType type, javax.swing.filechooser.FileFilter filter, Boolean setMultipleFileSelect, String informationText,
            String topText, File initDirectory) {
        
        // set type
        if (type != null) {
            this.setFileSelectionMode(type);
        } else {
            this.setFileSelectionMode(SelectionType.FILES_AND_DIRECTORIES); // accept either
        }
        
        // filter
        if (filter != null) {
            this.setFileFilter(filter);
        } else {
            this.setFileFilter(new WepsFileFilter()); // accepts all files with no arg
        }
        
        // setMultiple 
        if (setMultipleFileSelect != null) {
            this.setMultiSelectionEnabled(setMultipleFileSelect);
        } else {
            this.setMultiSelectionEnabled(false); // default to false
        }
        
        // top file chooser text
        if (topText != null) {
            this.setDialogTitle(topText);
        } else {
            this.setDialogTitle("WEPS File Chooser");
        }
        
        // instruction text
        if (informationText != null) {
            this.setText(informationText);
        } else {
            // set based on type
            switch(this.selectionMode){
                    case FILES_ONLY:
                        this.setText("Select File");
                        break;
                    case DIRECTORIES_ONLY:
                        this.setText("Select Directory");
                        break;
                    default:
                        // both
                        this.setText("Select a File or Directory");
                        break;
            }         
        }
        
        if (initDirectory != null) {
            this.setCurrentDirectory(initDirectory);
            this.setDefaultDirectory(initDirectory);
        } else {
            /* THIS WAY USES XMLConstants TO SET ---
            String dir = MCREWConfig.getDirectoryName(XMLConstants.sDBdir);
            TFile ddir = new TFile(dir);
            this.setCurrentDirectory(ddir);
            */
            JCB_Path.selectDbDir();  // This sets the path and the display to the set db directory
            this.setDefaultDirectory( JCB_Path.getSelectedDir());
        }
        
    }
    

    public void setAllowDirectorySelection(boolean selectionChoice) {
        allowDirectorySelection = selectionChoice;
    }
    
    protected void init (String message, Action action, File initialDir, WepsFileTypes2... types) {
        
        this.action = action;
        selectedFile = null;
        selectedFiles = null;
        allowNewDir = false;
        selectionMode = SelectionType.FILES_AND_DIRECTORIES;
        setMultiSelectionEnabled(false);
        
        setIconImage (About.getWeruIconImage());
        setLocationRelativeTo(null);
        
        addPropertyChangeListener(this);
        
        // load the initial paths into the paths combo
        if (initialDir != null) {
            //System.out.println("pre parsed directory: " + initialDir.getAbsolutePath());
            // check for replaceable parameters in the file name
            initialDir = new File(Util.parse(initialDir.getPath()));
            //initialDir = new File(Util.parse(initialDir.getAbsolutePath()));
            //System.out.println("post parsed directory: " + initialDir.getAbsolutePath());
            TFile tf = new TFile (initialDir.getAbsolutePath());
            if (!tf.exists()) {
                tf.mkdirs();
            }
            JCB_Path.addItem(tf);
            JCB_Path.setDefaultDir(tf);
        }
            
        for (java.nio.file.Path p : FileSystems.getDefault().getRootDirectories() ) {
            JCB_Path.addItem(new TFile(p.toString()));
        }
        
        JCB_FileTypes.addItem (new WepsFileFilter ("Default", types));
        
        chooserTypeList = new ArrayList&lt;&gt;();
        
        for (WepsFileTypes2 type : types) {
            //System.out.println("Type: " + type.getDescription());
            JCB_FileTypes.addItem (type.getFileFilter());
            chooserTypeList.add(type);
            // check if this type should allow the newDir button
            if (type.isEnableNewDir()) {
                allowNewDir = true;
            }
            if(type.isDirectory()) {
                //System.out.println("Allowing directory selection");
                setAllowDirectorySelection(true);
            } else {
                //System.out.println("Not allowing directory selection");
                setAllowDirectorySelection(false);
            }
            
        }
        chooserType = chooserTypeList.get(0);
        
        boolean crlmodLoad = ConfigData.checkParmValue(ConfigData.crlmodEnabled, "1");
        for (WepsFileTypes2 type : types) {
            if (type.isManagement()) {
                if(!action.equals(Action.Save)) { // adds the CrLmod Management to the dropdown of locations if it is an open action, not a save action
                    if(crlmodLoad) {
                        System.out.println("Enabling CrLmod databases");
                        JCB_Path.addItem(new CrLmodFile (lmodType.lmodManagement));
                    } else {
                        System.out.println("Not enabling CrLmod databases");
                    }
                }
                // Temp during dev? MEH.
                if (ConfigData.checkParmValue("CD-WS-csip-crlmod-Management-ShowMCacheItem","1")) {
                    JCB_Path.addItem(new TFile(About.getWepsCacheDir(), CsipLmodUtil.WepsCacheDirNameMan));
                }
                break;
            }
        }
        for (WepsFileTypes2 type : types) {
            if (type.isCrop()) {
                JCB_Path.addItem(new CrLmodFile (lmodType.lmodCrop));
                JCB_Path.addItem(new CrLmodFile (lmodType.lmodCropResidue));
                // Temp during dev? MEH.
                if (ConfigData.checkParmValue("CD-WS-csip-crlmod-Crop-ShowCacheItem","1")) {
                    JCB_Path.addItem(new TFile(About.getWepsCacheDir(), CsipLmodUtil.WepsCacheDirNameCrop));
                }
                break;
            }
        }
        for (WepsFileTypes2 type : types) {
            if (type.isOperation()) {
                JCB_Path.addItem(new CrLmodFile (lmodType.lmodOperation));
                // Temp during dev? MEH.
                if (ConfigData.checkParmValue("CD-WS-csip-crlmod-Operation-ShowCacheItem","1")) {
                    JCB_Path.addItem(new TFile(About.getWepsCacheDir(), CsipLmodUtil.WepsCacheDirNameOp));
                }
                break;
            }
        }
        for (WepsFileTypes2 type : types) {
            if (type.isSoil()) {
                JCB_Path.addItem(new SDMFileDatabase ());
                                
                CsipSoilFile csipSoilFile = new CsipSoilFile ();
                csipSoilFile.setLatLonToCurrentUserVal();
                JCB_Path.addItem(csipSoilFile);
                // Temp during dev? MEH.
                if (ConfigData.checkParmValue("CD-WS-csip-Soil-ShowCacheItem","1")) {
                    JCB_Path.addItem(new TFile(About.getWepsCacheDir(),CsipLmodUtil.WepsCacheDirNameSoil));
                }
                
                TFile file = new TFile (ConfigData.getDefault().getDataParsed(ConfigData.SoilDB),"WEPS_baseline.mdb");
                if (file.exists()) {
                    JCB_Path.addItem (new MdbSoilFileDatabase(file) );
                }
                
                break;
            }
        }

        //JB_Select.setText(action.getLabelTxt());
        setApproveButtonToolTipText(action.getDescription() + types[0].getButtonToolTip());
        
        // set default message if needed
        if (message == null || message.length() &lt;=  0) {
            message = action.getDescription() + types[0].getChooserTitle();
            if (message.contentEquals("Select a Any File")) {
                message = "Select a file";
            }
        }
        setMessage( ((message != null) &amp;&amp; (message.length() &gt; 0)) ? message :
                    action.getDescription() + types[0].getChooserTitle());
        
        if (action == Action.Save || action == Action.Create) {
            allowNewDir = true;
            setPersitSelectedFile (true);
        }
        allowNewDir(allowNewDir);
    }
    
    
    
    public void closeChooser() {
        setVisible(false);
        dispose();
    }
    
    public WfcPathComboBox getPath() {
        return JCB_Path;
    }
    
    public File showChooser() {
        setVisible(true);
        return selectedFile;
    }
    
    public int showDialog(Component parent) {
        return showDialog(parent, null);
    }
    
    public int showDialog(Component parent, String approveButtonText) {
        
        if (approveButtonText != null) {
            setApproveButtonText(approveButtonText);
        }
        
        setLocationRelativeTo(parent);
        if (showChooser() != null) {
            return APPROVE_OPTION;
        } else {
            return ERROR_OPTION;
        }
    }
    
    public void homeToolTip(WepsFileChooser2 jfc) {
        java.util.Hashtable&lt;String, String&gt; ht = new java.util.Hashtable&lt;&gt;();
        ht.put("Desktop", "Home");
        Util.loadToolTips(jfc, ht);
    }
    
    /**
     * Changes the tooltip (Hover-over) text for the 'Select' button on the filechooser
     * @param toolTipText 
     */
    public void setApproveButtonToolTipText(String toolTipText) {
        JB_Select.setToolTipText(toolTipText);
    }
    
    /**
     * !!! This function currently does nothing !!!
     * If implemented, it would require the button to be dynamically resized 
     * according to the length of the String.
     * @param text The new text for the 'Select' Button
     */
    public void setApproveButtonText (String text) {
        //JB_Select.setText(text);
        //We would have to resize the button and throw off the layout to get additional text to fit here.
    }
    
    public boolean accept (File f) {
        return (JCB_FileTypes.getSelectedItem()).accept(f);
    }
    

    public void allowNewDir(boolean allowNewDir) {
        this.allowNewDir = allowNewDir;
        JB_NewDir.setVisible(allowNewDir);
    }
    
    // Legacy interface from WepsFileChooser.
    public void disableNewFolder(WepsFileChooser2 wfc) {
        allowNewDir(false);
    }
    
    public void setDefaultDirectory(File file) {
        JCB_Path.setDefaultDir(file);
    }
    
    public void setSelectedFile(File newFile) {
        if (newFile.getParentFile() == null) {
            newFile = new File (JCB_Path.getSelectedDir(),newFile.getName());
        } else {
            JCB_Path.addItem(newFile.getParentFile());
        }
        JTF_SelectedFile.setText(newFile.getName());
    }

    /**
     * Sets what type of selection is allowed from the SelectionType enum
     * [ FILES_ONLY, DIRECTORIES_ONLY, FILES_AND_DIRECTORIES ]
     * @param selectionMode - type of selection(s) permitted
     */
    public void setFileSelectionMode (SelectionType selectionMode) {
        this.selectionMode = selectionMode;
    }
    
    /**
     * Sets wether user can highlight and select multiple files/dir or only select a
     * single file/dir
     * @param multiSelection true - select multiple files
     */
    public void setMultiSelectionEnabled (boolean multiSelection) {
        this.multiSelection = multiSelection;
        if (multiSelection) {
            JT_dirList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        } else {
            JT_dirList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        }
    }
    
    public void setAcceptAllFileFilterUsed(boolean b) {
        JCB_FileTypes.setAllFileFilterUsed(b);
    }
    
    /**
     * This sets the text present at the light gray bar at the top of the fileChooser.
     * Typically used to describe the purpose of the fileChooser. 
     * @param dialogTitle 
     */
    public void setDialogTitle(String dialogTitle) {
        setTitle(dialogTitle);
    }
        
    public void setMessage (String message) {
        JL_CustomMessage.setText(message);
    }
    
    /**
     * Sets the large, bold font that appears between the top bar of the window
     * and the drop down menu bar that displays the path. Generally used to describe what 
     * action should be taking place in the fileChooser (Select File/Select Directory...etc)
     * @param text 
     */
    public void setText(String text) {
        setMessage(text);
    }
    
    /**
     * Adds a file filter that can be selected and applied on the user interface
     * from the drop down menu of available file filters
     * @param filter 
     */
    public void addChoosableFileFilter(javax.swing.filechooser.FileFilter filter) {
        WepsFileFilter wepsFilter = new WepsFileFilter(filter);
        JCB_FileTypes.addItem(wepsFilter);
    }
    
    /**
     * 
     * @return The currently selected file filter for this fileChooser
     */
    public javax.swing.filechooser.FileFilter getFileFilter() {
        return JCB_FileTypes.selectedItem;
    }
    
    /**
     * Applies and selects the filter, and adds to list of available file filters
     * @param filter Filter to be applied and added
     */
    public void setFileFilter(WepsFileFilter filter) {
        JCB_FileTypes.addItem(filter);
        JCB_FileTypes.setSelectedItem(filter);
    }
    
     /**
     * Applies and selects the filter, and adds to list of available file filters
     * @param filter Filter to be applied and added
     */
    public void setFileFilter(javax.swing.filechooser.FileFilter filter) {
        WepsFileFilter wepsFilter = new WepsFileFilter(filter);
        JCB_FileTypes.addItem(wepsFilter);
        JCB_FileTypes.setSelectedItem(wepsFilter);
    }
    
    /**
     * Sets the directory the fileChooser is currently pointing at.
     * @param dir 
     */
    public void setCurrentDirectory(File dir) {
        JCB_Path.addAndSelectItem(new TFile (dir.getAbsolutePath()));
    }
    
    /**
     * Adds and selects File 'dir', but removes all previous file paths that 
     * appear in drop down.
     * @param dir 
     */
    public void setCurrentDirectory_deleteOtherItems(File dir) {
        JCB_Path.deleteAll();
        JCB_Path.addAndSelectItem(new TFile (dir.getAbsolutePath()));
    }
    
    /**
     * Sets the default current directory for the fileChooser 
     * @param name 
     */
    public void setDefaultCurrentDirectory(String name) {
        TFile dir = lookupDefaultDirectory(name);
        if (dir != null) {
            setCurrentDirectory(dir);
        }
    }
    
    public void setAccessory(JComponent newAccessory) {
        JP_Accessory.add(newAccessory);
    }
    
    // From JFileChooser.
    public void updateUI() {
        
    }
            
    // From WepsFileChooser.
    public void approveSelection() {
        try {
            TFile dir = new TFile(getSelectedFile().getAbsolutePath());
            TFile runfile = dir;
            
            if (action == Action.Save || action == Action.Create) {		// any file name is accepted for new
                if (dir.exists()) {
                    JOptionPane.showMessageDialog(this, dir.getName()
                            + " already exists.\nPlease choose another file name.", "WEPS", JOptionPane.WARNING_MESSAGE);
                    return;
                } else if (dir.getInnerArchive() != null) {
                    //This file would be in an archive, Naughty You!!
                    JOptionPane.showMessageDialog(this, "Archives are read-only.\n"
                            + "Please choose another file location.", "WEPS", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                closeChooser();
            } else {
                if (runfile != null) {
                    //If type is PROJDIR/DIR/RUNDIR means that we are just selecting
                    //a (project) directory here and this is not a file type
                    if ((chooserType == WepsFileTypes2.ProjDir || chooserType == WepsFileTypes2.Dir
                            || chooserType == WepsFileTypes2.RunDir) &amp;&amp; runfile.canRead()) {
                        closeChooser();
                    } //If the type is SOILDB and the currently selected filter is
                    //soilSilter, then approve the selection (directory) - neha
                    else if (chooserType == WepsFileTypes2.SoilDB) {
                        closeChooser();
                    } else if (chooserType == WepsFileTypes2.Management &amp;&amp; runfile.canRead()
                            &amp;&amp; (runfile.getName().toLowerCase().endsWith(chooserType.getExtension()))
                            || runfile.getName().toLowerCase().endsWith(WepsFileTypes2.XML.getExtension())) {
                        closeChooser();
                    } else if (chooserType == WepsFileTypes2.Project
                            &amp;&amp; runfile.getName().toLowerCase().endsWith(RunFileData.ProjectSuffix)
                            &amp;&amp; dir.getInnerArchive() != null) {
                        JOptionPane.showMessageDialog(this,
                                "Opening projects inside an archive is not allowed.\n"
                                + "Please choose a different project.", "WEPS", JOptionPane.WARNING_MESSAGE);
                        return;
                    } else if (chooserType == WepsFileTypes2.Error) {
                        //new code
                        closeChooser();
                    } else if (runfile.canRead() &amp;&amp; runfile.getName().toLowerCase().endsWith(chooserType.getExtension())) {
                        closeChooser();
                    }
                }
            }
            //If the user selects a Directory and hits "Select" button, then that
            //Directory should be opened by setting the current dir to that dir
            // except when we want to SELECT A DIRECTORY- neha
            if (action == Action.Select &amp;&amp; runfile.canRead() &amp;&amp; runfile.isDirectory() &amp;&amp; !typeIsDirectory(chooserType)) {
                setCurrentDirectory(runfile);

            }
        } catch (HeadlessException e) {
            //System.err.println("WepsFileChooser:approveSelection:"+e);
        }
    }
    
    private boolean typeIsDirectory(WepsFileTypes2 type) {
        return type == WepsFileTypes2.ProjDir || type == WepsFileTypes2.Dir || type == WepsFileTypes2.RunDir;
    }
    
    /**
     * Returns the currently selected file/directory
     * @return 
     */
    public File getSelectedFile() {
        return selectedFile;
    }
    
    /**
     * Returns the currently selected file/directory
     * @return 
     */
    public TFile getSelectedTFile() {
        return (selectedFile == null) ? null : new TFile(selectedFile.getAbsolutePath());
    }
    
    /**
     * 
     * @return a list of the currently selected files. This list can be populated
     * by enabling multiSelect
     */
    public File[] getSelectedFiles () {
        selectedFiles = JT_dirList.getCurrentSelections();
        return selectedFiles;
    }
    
    // From WepsFileChooser
    public static WepsFileTypes2 getType(String pTypeName) {

        if (pTypeName == null) {
            //System.err.println("WepsFileChooser:getType: " + " NullPointerException");
            return WepsFileTypes2.Error;
        }

        if (!pTypeName.startsWith(".")) {
            pTypeName = "." + pTypeName;
        }

        for(WepsFileTypes2 wft : WepsFileTypes2.values()) {
            if (wft.getExtension().contains(pTypeName)) {
                return wft;
            }
        }
        
        return WepsFileTypes2.values()[0];
    }
        
    @Override
    protected void doSelectButtonAction () {
        switch (action) {
            case Open:
            case Select:
            case Delete:
                doSelectFile();
                break;
            case Save:
            case Create:
                doNewFile();
                break;
        }
    }
    
    protected void fileTypesAction() {
        JCB_FileTypes.repaint();
        JCB_FileTypes.revalidate();
        String oldName = "";
        String description = JCB_FileTypes.selectedItem.description;
        // System.out.println("Description: " + description);
        LOGGER.info("Filename Description is: " + description);
        String extension = "";
        if(description.contains(".") &amp;&amp; description.contains(")")) {
            extension = description.substring(description.lastIndexOf('.'), description.lastIndexOf(')'));
            // System.out.println("Extension: " + extension);
            LOGGER.info("Filename Extension is: " + extension);
        } else {
            extension = "";
            // System.out.println("No Extension");
            LOGGER.info("No filename Extension specified");
        }
        //System.out.println("Extension: " + extension);
        if(JTF_SelectedFile.getText().contains(".")) {
            oldName = JTF_SelectedFile.getText().substring(0, JTF_SelectedFile.getText().lastIndexOf('.'));
            //System.out.println("oldName Period: " + oldName);
        } else {
            oldName = JTF_SelectedFile.getText();
            //System.out.println("oldName No Period: " + oldName);
        }
        JTF_SelectedFile.setText(oldName + extension);
        //System.out.println("New Name: " + JTF_SelectedFile.getText());
    }
    
        
    private boolean remoteOverride = false;
    
    /**
     * Fired when 'Select' button is hit on a file. Global variables hold the location
     * and String name of the selected file, and are used to instantiate the selected 
     * file.
     */
    protected void doSelectFile () {
        selectedFile = new TFile (JCB_Path.getSelectedDir(), JTF_SelectedFile.getText());
        //selectedFile = Util.fixPath(selectedFile); //fixPath (DEPRECATED)
        if(!allowDirectorySelection &amp;&amp; selectedFile.isDirectory()) {
            System.err.println("Directory selected.");
            //custom title, warning icon
            JOptionPane.showMessageDialog(null,
                "You have attempted to select a directory.",
                "Directory Selected",
                JOptionPane.WARNING_MESSAGE);
            return;
        }
        //System.out.println("CMZ Templates: " + ConfigData.getDefault().get)
        //System.err.println("selectedFile: " + selectedFile.getAbsolutePath());
        if(selectedFile.getAbsolutePath().contains("CrLmod Managements") || selectedFile.getAbsolutePath().contains("CrLmod Operations")) {
            String partialPath = selectedFile.getAbsolutePath().substring(selectedFile.getAbsolutePath().indexOf("CrLmod"));
            System.out.println("Partial Path: " + partialPath);
            String[] parts = partialPath.split("\\\\");
            for(String s : parts) {
                //System.out.println("Part: " + s);
            }
            ArrayList&lt;CrLmodFile&gt; files = new ArrayList&lt;CrLmodFile&gt;();
            CrLmodFile rootFile;
            if(partialPath.contains("Managements")) {
                rootFile = new CrLmodFile(parts[0], lmodType.lmodManagement);
            } else if(partialPath.contains("Operations")) {
                rootFile = new CrLmodFile(parts[0], lmodType.lmodOperation);
            } else {
                System.out.println("Unknown CrLmod Type.");
                return;
            }
            rootFile.listFiles();
            //System.out.println("RootFile Name: " + rootFile.getName());
            for(int i = 1; i &lt; parts.length; i++) {
                if(i == 1) {
                    CrLmodFile firstChild = new CrLmodFile(rootFile, parts[i]);
                    //System.out.println("FirstChild Name: " + firstChild.getName());
                    files.add(firstChild);
                } else {
                    CrLmodFile laterChild = new CrLmodFile(files.get(i - 2), parts[i]);
                    //System.out.println("LaterChild Name: " + laterChild.getName());
                    files.add(laterChild);
                }
            }
//            System.out.println("Number of Files: " + files.size());
            if(files.size() &gt; 0) {
                selectedFile = files.get(files.size() - 1);
            } else {
                JOptionPane.showMessageDialog(null,
                    "Remote file not found from available data.",
                    "Remote File Error",
                    JOptionPane.ERROR_MESSAGE);
                return;
            }
            remoteOverride = true;
        }
        if (selectedFile != null &amp;&amp; checkSelectedFileType(selectedFile)) {
            try {
                if ((selectedFile instanceof RemoteFile &amp;&amp; selectedFile.isFile()) || remoteOverride) {
                    //System.err.println("Loading a remote file!");
                    //System.err.println("selectedFile Name: " + selectedFile.getName());
                    //System.out.println("selectedFile Path: " + selectedFile.getAbsolutePath());
                    mainDlg.getContentPane().setCursor(getPredefinedCursor(Cursor.WAIT_CURSOR));
                    (new SwingWorker &lt;File, Void&gt; () {
                        @Override
                        protected File doInBackground() throws Exception {
                            File newFile = null;
                            try {
                                System.out.println("Starting the download.");
                                newFile = ((RemoteFile)selectedFile).downloadFile();
                                System.out.println("Download complete.");
                            } catch (IOException e) {
                                int j = 1;
                                System.err.println("Download failed.");
                            }
                            return newFile;
                        }        

                        @Override
                        protected void done() {
                           try {
                                mainDlg.getContentPane().setCursor(Cursor.getDefaultCursor());
                                selectedFile = get();
                                if (selectedFile != null) {
                                    mainDlg.firePropertyChange(WepsFileChooser2.propChangeCmdStrFetchCompleted, 0, 1);
                                }
                           } catch (Exception ignore) {
                           }
                       }
                    }).execute();
                } else {
                    if (selectedFile instanceof TFile) {
                        if (((TFile)selectedFile).isEntry()) {
                            // If the file is in an archive ( isEntry() ), then make a new file copied out from the archive
                            File newFile = new File (getProperty("project.directory"), selectedFile.getName());
                            ((TFile)selectedFile).cp_r(newFile);
                            selectedFile = newFile;
                        }
                    }
                    // Otherwise, it should already be a file on disk, just return it.

                    if (selectedFile != null &amp;&amp; selectedFile.exists()) {
                        File asFile = new File (selectedFile.getAbsolutePath());
                        switch (selectionMode) {
                            case FILES_ONLY:
                                System.out.println("Is it files only?");
                                if (!asFile.isFile()) {
                                    showMessageDialog(this, "Please select file, not a directory", "Invalid selection", JOptionPane.ERROR_MESSAGE);
                                } else {
                                    close();
                                }
                                break;
                            case DIRECTORIES_ONLY:
                                if (!asFile.isDirectory()) {
                                    showMessageDialog(this, "Please select directory, not a file", "Invalid selection", JOptionPane.ERROR_MESSAGE);
                                } else {
                                    close();
                                }
                                break;
                            case FILES_AND_DIRECTORIES:
                            default:
                                close();
                        }
                    } else {
                        //showMessageDialog(this, "Please make a selection", "No selection", JOptionPane.ERROR_MESSAGE);
                        close();
                    }
                }

            } catch (Exception ex) {
                // Logger.getLogger(WepsFileChooser2.class.getName()).log(Level.SEVERE, null, ex);
                LOGGER.error("doSelectFile Exception " + ex);
            }
        }
    }

    protected boolean checkSelectedFileType(File selectedFile) {
        boolean retVal = true;
        WepsFileFilter defaultFilter = JCB_FileTypes.getDefaultItem();
        
        if (defaultFilter != null &amp;&amp; !defaultFilter.accept(selectedFile)) {
            int userVal;
            userVal = showConfirmDialog(this, "The file you selected does not match the requested file type.  Continue and use this file anyway?", 
                                              "File types mismatch", JOptionPane.YES_NO_OPTION,  JOptionPane.WARNING_MESSAGE); 
            retVal = (userVal == JOptionPane.YES_OPTION);
        }
        return retVal;
    }
    
    protected void doNewFile () {
        if (JTF_SelectedFile.getText().length() &gt; 0) {
            selectedFile = new File (JCB_Path.getSelectedDir(), JTF_SelectedFile.getText());
            if (selectedFile.exists()) {
                int response = showConfirmDialog(this, "File already exists. Replace?", "Warning", JOptionPane.WARNING_MESSAGE);
                if (response != YES_OPTION &amp;&amp; response != OK_OPTION) {
                    selectedFile = null;
                }
            }
        }
        close();
    }
    
    // From WepsFileChooser
    public TFile lookupDefaultDirectory(String name) {

        //a few tricky cases, when MCREW_CFG should be the parent
        if (ConfigData.SkelTranslationFile.equals(name) || ConfigData.McrewDataConfigFile.equals(name)) {
            String path = ConfigData.getDefault().getDataParsed(ConfigData.MCREW);
            if (path != null) {
                return new TFile(path);
            }
        } else {
            ConfigData.FileConfigurationOption option = ConfigData.getDefault().getFileConfigurationOption(name);
            if (option != null) {
                String path = option.defaultLocation();
                if (path != null &amp;&amp; path.length() &gt; 0) {
                    return new TFile(path);
                }
            }
        }

        return null;
    }


    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(WepsFileChooser2.propChangeCmdStrFetchCompleted)) {
            close();
        } else {
            super.propertyChange(evt);
        }
    }

}
</pre></body></html>