/*
 * WepsFileField.java
 *
 * Created on February 4, 2008, 5:13 PM
 */
package usda.weru.util;

import de.schlichtherle.truezip.file.TFile;
import java.io.File;
import javax.swing.JButton;
import org.jdesktop.beansbinding.Converter;
import usda.weru.util.wepsFileChooser2.WepsFileChooser2;
import usda.weru.util.wepsFileChooser2.WepsFileTypes2;
import usda.weru.weps.gui.RunExporter_n;

/**
 *
 * @author  Joseph Levin <joelevin@weru.ksu.edu>
 */
public class WepsFileField extends javax.swing.JPanel {

    private static final long serialVersionUID = 1L;

    /**
     *
     */
    public enum MODE {

        OPEN(WepsFileChooser2.Action.Open),
        SAVE(WepsFileChooser2.Action.Save),
        SELECT(WepsFileChooser2.Action.Select),
        DELETE(WepsFileChooser2.Action.Delete);
        
        private final WepsFileChooser2.Action c_mode;

        MODE(WepsFileChooser2.Action mode) {
            c_mode = mode;
        }

        public WepsFileChooser2.Action getFileChooserMode() {
            return c_mode;
        }
    }

    /**
     *
     */
    public enum TYPE {

        RUN(WepsFileTypes2.RunDir),
        DIRECTORY(WepsFileTypes2.Dir);
        
        private final WepsFileTypes2 c_id;

        TYPE(WepsFileTypes2 id) {
            c_id = id;
        }

        public WepsFileTypes2 getFileChooserType() {
            return c_id;
        }
    }

    /**
     *
     */
    public static final String PROP_FILE = "file";

    /**
     *
     */
    protected TFile c_file;

    /**
     *
     */
    protected TYPE c_fileType;

    /**
     *
     */
    protected MODE c_mode = MODE.SELECT;
     /**
     *
     */
    protected String defaultDir;

    /** Creates new form WepsFileField */
    public WepsFileField() {
        initComponents();
        c_file = null;
    }
    public WepsFileField(String dirPath){
        File dDir = new File(dirPath);
        dDir.mkdirs();
        TFile defaultDir = new TFile(dirPath);
        setFile(defaultDir);
        this.defaultDir = dirPath;
        initComponents();
        c_file = defaultDir;
    }
    

    /**
     *
     * @return
     */
    public TFile getFile() {
        return c_file;
    }

    /**
     *
     * @param file
     */
    public void setFile(TFile file) {
        if (file != null && file.getPath().compareTo("") == 0) {
            file = null;
        }
        TFile old = c_file;
        c_file = file;
        firePropertyChange(PROP_FILE, old, c_file);
    }

    /**
     *
     * @return
     */
    public MODE getMode() {
        return c_mode;
    }

    /**
     *
     * @param mode
     */
    public void setMode(MODE mode) {
        c_mode = mode;
    }

    /**
     *
     * @return
     */
    public TYPE getFileType() {
        return c_fileType;
    }

    /**
     *
     * @param type
     */
    public void setFileType(TYPE type) {
        c_fileType = type;
    }

    /**
     *
     */
    public static class BindingFileToString extends Converter<TFile, String> {

        /**
         *
         * @param file
         * @return
         */
        @Override
        public String convertForward(TFile file) {
            if (file != null) {
                return file.getPath();
            } else {
                return null;
            }
        }

        /**
         *
         * @param path
         * @return
         */
        @Override
        public TFile convertReverse(String path) {
            if (path != null) {
                return new TFile(path);
            } else {
                return null;
            }
        }
    }

    /**
     *
     */
    @Override
    public void requestFocus() {
        super.requestFocus();
        g_pathField.requestFocus();
    }

    /**
     *
     * @return
     */
    public JButton getButton() {
        return g_browseButton;
    }
    
    public void setText(String text){
        g_pathField.setText(text);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        g_pathField = new javax.swing.JTextField();
        g_browseButton = new javax.swing.JButton();

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${file}"), g_pathField, org.jdesktop.beansbinding.BeanProperty.create("text_ON_ACTION_OR_FOCUS_LOST"), "");
        binding.setConverter(new BindingFileToString());
        bindingGroup.addBinding(binding);

        g_pathField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                g_pathFieldActionPerformed(evt);
            }
        });

        g_browseButton.setText("...");
        g_browseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                g_browseButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(g_pathField, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
                .addGap(0, 0, 0)
                .addComponent(g_browseButton, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(g_browseButton, 0, 0, Short.MAX_VALUE)
            .addComponent(g_pathField)
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents
    private void g_browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_g_browseButtonActionPerformed
        String homePath = null;
        switch (c_fileType) {
            case RUN:
                homePath = c_file.getAbsolutePath();
                break;
            default:
                if (defaultDir != null){
                    homePath = defaultDir;
                }else{
                    homePath = Util.parse(ConfigData.getDefault().getData(ConfigData.ProjDir));
                    homePath += "/exported WEPS runs";
                }
                break;
        }
        WepsFileChooser2 wfc = new WepsFileChooser2(c_fileType.getFileChooserType(), homePath, c_mode.getFileChooserMode());
        wfc.setMultiSelectionEnabled(false);
        if (c_file != null) {
           // wfc.setSelectedFile(c_file);
        } else {
            wfc.setCurrentDirectory(homePath);
        }

        int result = wfc.showDialog(this);
        if (result == WepsFileChooser2.APPROVE_OPTION) {
            setFile(new TFile(wfc.getSelectedFile()));
        }
}//GEN-LAST:event_g_browseButtonActionPerformed

    private void g_pathFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_g_pathFieldActionPerformed
}//GEN-LAST:event_g_pathFieldActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton g_browseButton;
    private javax.swing.JTextField g_pathField;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
