/*
 *  RTIbuilder
 *  Copyright (C) 2008-11  Universidade do Minho and Cultural Heritage Imaging
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 3 as published
 *  by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/*
 * LpMenu.java
 *
 * Created on 3/Jun/2009, 15:50:56
 */

package Plugin;

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class LPmenu extends javax.swing.JDialog {

	private File lp_file;
	private File project_folder;
	private boolean returned;


    /** Creates new form LpMenu */
    public LPmenu(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
		setResizable(false);
    }
 
	public void showLPMenu(){
		lp_file_warning.setText("");
		lp_file_tb.setText("");
		project_folder_warning.setText("");
		project_folder_tb.setText("");
		this.setVisible(true);
	}


    /** 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() {

        jPanel1 = new javax.swing.JPanel();
        lp_file_tb = new javax.swing.JTextField();
        lp_file_button = new javax.swing.JButton();
        project_folder_tb = new javax.swing.JTextField();
        browse_folder_button = new javax.swing.JButton();
        lp_label = new javax.swing.JLabel();
        project_label = new javax.swing.JLabel();
        lp_file_warning = new javax.swing.JLabel();
        project_folder_warning = new javax.swing.JLabel();
        ok_button = new javax.swing.JButton();
        cancel_button = new javax.swing.JButton();

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 100, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Light position file");
        setAlwaysOnTop(true);

        lp_file_button.setText("Browse");
        lp_file_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lp_file_buttonActionPerformed(evt);
            }
        });

        browse_folder_button.setText("Browse");
        browse_folder_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browse_folder_buttonActionPerformed(evt);
            }
        });

        lp_label.setText("Light Position File");

        project_label.setText("Project Folder");

        lp_file_warning.setForeground(new java.awt.Color(255, 0, 0));
        lp_file_warning.setText("File not found");

        project_folder_warning.setForeground(new java.awt.Color(255, 0, 0));
        project_folder_warning.setText("Folder not found");

        ok_button.setText("Ok");
        ok_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ok_buttonActionPerformed(evt);
            }
        });

        cancel_button.setText("Cancel");
        cancel_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancel_buttonActionPerformed(evt);
            }
        });

        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(lp_file_warning)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 324, Short.MAX_VALUE)
                                .add(ok_button, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 79, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(project_folder_warning)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(lp_label)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(lp_file_tb, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 317, Short.MAX_VALUE))
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(project_label)
                                .add(19, 19, 19)
                                .add(project_folder_tb, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 317, Short.MAX_VALUE)))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(lp_file_button, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
                            .add(browse_folder_button, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
                            .add(cancel_button, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 79, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lp_file_tb, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(lp_file_button)
                    .add(lp_label))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(lp_file_warning)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(project_label)
                    .add(project_folder_tb, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(browse_folder_button))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(project_folder_warning)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(ok_button, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(cancel_button, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

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

	private void lp_file_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lp_file_buttonActionPerformed
		// TODO add your handling code here:
            //Select a lp file
            File SelectedFile;
            //File chooser for selecting the lp file.
            JFileChooser jfc = new JFileChooser("Select LP file");
            jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
            int result = jfc.showOpenDialog(new JFrame());
            if (result == JFileChooser.APPROVE_OPTION) {
                if (jfc.getSelectedFile().isFile()) {
                    SelectedFile = jfc.getSelectedFile();
                    lp_file_tb.setText(SelectedFile.getAbsolutePath());
                } else {
                    System.err.println("Is not a file.");
                }
            }

	}//GEN-LAST:event_lp_file_buttonActionPerformed

	private void browse_folder_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browse_folder_buttonActionPerformed
		    //Select the project folder
            File SelectedFile;
            //File chooser for selecting the folder
            JFileChooser jfc = new JFileChooser("Select the project folder");
            jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            int result = jfc.showOpenDialog(new JFrame());
            if (result == JFileChooser.APPROVE_OPTION) {
                if (jfc.getSelectedFile().isDirectory()) {
                    SelectedFile = jfc.getSelectedFile();
                    project_folder_tb.setText(SelectedFile.getAbsolutePath());
                } else {
                    SelectedFile = jfc.getSelectedFile().getParentFile();
                    project_folder_tb.setText(SelectedFile.getAbsolutePath());
                    System.err.println("Is not a directory.");
                }
            }
	}//GEN-LAST:event_browse_folder_buttonActionPerformed

	private void cancel_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancel_buttonActionPerformed
		returned =  false;
		setVisible(false);
}//GEN-LAST:event_cancel_buttonActionPerformed

	private void ok_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ok_buttonActionPerformed
		// TODO add your handling code here:
		boolean error  =  false;
		File lp = new File(lp_file_tb.getText());
		if(!lp.exists()){
			error =  true;
			lp_file_warning.setText("File not found!");
		}
		else{
		    lp_file_warning.setText("");
			lp_file =  lp;
		}
		File folder = new File(project_folder_tb.getText());
		if(!folder.exists()){
			error =  true;
			project_folder_warning.setText("Folder not found!");
		}
		else{
			project_folder_warning.setText("");
			project_folder = folder;
		}

		returned = !error;
		if(!error){
			setVisible(false);
		}
	}//GEN-LAST:event_ok_buttonActionPerformed

    /**Get the lp file.
     *@return  a File object with the light position file.
     */
    public File getLPFile(){
        return lp_file;
    }

     /**Get the image folder.
     *@return  a File object with the images folder.
     */
    public File getProjectFolder(){
        return project_folder;
    }


    /**See if the lp file and folder where selected.
    *@return  a boolean that indicates if the lp and the project folder were defined.
    */
    public boolean isLPFileSelected(){
        return returned;
    }


    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                LPmenu dialog = new LPmenu(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton browse_folder_button;
    private javax.swing.JButton cancel_button;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton lp_file_button;
    private javax.swing.JTextField lp_file_tb;
    private javax.swing.JLabel lp_file_warning;
    private javax.swing.JLabel lp_label;
    private javax.swing.JButton ok_button;
    private javax.swing.JTextField project_folder_tb;
    private javax.swing.JLabel project_folder_warning;
    private javax.swing.JLabel project_label;
    // End of variables declaration//GEN-END:variables

}
