/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProjectWindow.java
 *
 * Created on Jan 23, 2011, 3:41:40 PM
 */

package ajcon.UI;

import java.util.ArrayList;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import org.jdesktop.application.Action;


public class ProjectWindow extends javax.swing.JFrame {

    private MainWindow vMainWindow;
    private JFileChooser chooser;

    /** Creates new form ProjectWindow */
    public ProjectWindow(MainWindow mw) {
        vMainWindow = mw;
        initComponents();

        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    /** 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("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(550, 200));
        setResizable(false);

        jLabel1.setText("Project Name:");
        jLabel1.setName("jLabel1"); // NOI18N

        jTextField1.setName("jTextField1"); // NOI18N

        jLabel2.setText("Project Source Folder:");
        jLabel2.setName("jLabel2"); // NOI18N

        jTextField2.setName("jTextField2"); // NOI18N

        jLabel3.setText("Output Destination Folder:");
        jLabel3.setName("jLabel3"); // NOI18N

        jTextField3.setName("jTextField3"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(ProjectWindow.class, this);
        jButton1.setAction(actionMap.get("buttonSourceFolderSelectClickedAction")); // NOI18N
        jButton1.setText("Choose...");
        jButton1.setName("jButton1"); // NOI18N

        jButton2.setAction(actionMap.get("buttonDestionationFolderSelectClickedAction")); // NOI18N
        jButton2.setText("Choose...");
        jButton2.setName("jButton2"); // NOI18N

        jButton3.setAction(actionMap.get("buttonConfirmProjectClickedAction")); // NOI18N
        jButton3.setText("Confirm");
        jButton3.setName("jButton3"); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 533, Short.MAX_VALUE)
                    .add(jLabel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 533, Short.MAX_VALUE)
                    .add(jLabel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 533, Short.MAX_VALUE)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 426, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(layout.createSequentialGroup()
                        .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 425, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18)
                        .add(jButton1))
                    .add(layout.createSequentialGroup()
                        .add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 425, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jButton3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jButton2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jLabel2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jButton1))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jLabel3)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jButton2))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jButton3)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void buttonSourceFolderSelectClickedAction(){
        chooser = new JFileChooser();
        chooser.setCurrentDirectory(new java.io.File("."));
        chooser.setDialogTitle("Select Source Folder");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        chooser.setAcceptAllFileFilterUsed(false);

        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            jTextField2.setText(chooser.getSelectedFile().toString());
            jTextField1.setText(chooser.getSelectedFile().toString().substring(chooser.getSelectedFile().toString().lastIndexOf("\\")+1, chooser.getSelectedFile().toString().length()));
        }
    }
    
    @Action
    public void buttonDestionationFolderSelectClickedAction() {
        chooser = new JFileChooser();
        chooser.setCurrentDirectory(new java.io.File("."));
        chooser.setDialogTitle("Select Destination Folder");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        
        chooser.setAcceptAllFileFilterUsed(false);
        

        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            jTextField3.setText(chooser.getSelectedFile().toString());
        }
    }

    @Action
    public void buttonConfirmProjectClickedAction() {
        int tn = vMainWindow.getTotalProjectNumber();

        if(jTextField2.getText().trim().compareTo("")==0
                || jTextField3.getText().trim().compareTo("")==0)
            return;
        
        if(tn >= 5){
            System.out.println("No more slot is available");
        }
        else{
            ArrayList<JCheckBox> jc = vMainWindow.getCheckboxes();
            ArrayList<JProgressBar> jpb = vMainWindow.getProgressBar();
            ArrayList<JLabel> jlpn= vMainWindow.getProjectNames();
            ArrayList<JLabel> jlsf = vMainWindow.getSourceFolders();
            ArrayList<JLabel> jldf= vMainWindow.getDestinationFolders();

            jlpn.get(tn).setText(jTextField1.getText());
            jlpn.get(tn).setVisible(true);

            jlsf.get(tn).setText(jTextField2.getText());
            jlsf.get(tn).setVisible(true);
            
            jldf.get(tn).setText(jTextField3.getText());
            jldf.get(tn).setVisible(true);

            jc.get(tn).setVisible(true);
            jpb.get(tn).setVisible(true);

            
            tn = tn+1;
            vMainWindow.setTotalProjectNumber(tn);
            vMainWindow.getProgressBar().get(tn-1).setValue(0);
            vMainWindow.getProgress().add(tn);
            vMainWindow.getLogs().set(tn-1, "Logs for project: "+jTextField1.getText());

            
            this.dispose();
        }
        
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration//GEN-END:variables

}
