/*
 * STUDENTS.................this class should NOT be changed
 * 
 * 
 * 
 * DO NOT CHANGE THIS FILE
 * */
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.*;

public class FileIndexer extends JPanel
                             implements ActionListener {
    static private final String newline = "\n";
    JButton openButton;
    JTextArea log;
    JFileChooser fc;
    FolderIndexer fIndexer;
    
    public FileIndexer() {
        super(new BorderLayout());
        //Create the log first, because the action listeners
        //need to refer to it.
        log = new JTextArea(22, 100);
        log.setMargin(new Insets(5,5,5,5));
        log.setEditable(false);
        JScrollPane logScrollPane = new JScrollPane(log);
        

        //Create a file chooser
        fc = new JFileChooser();

        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        openButton = new JButton("Find a Folder");
        openButton.addActionListener(this);

       
        //For layout purposes, put the buttons in a separate panel
        JPanel buttonPanel = new JPanel(); //use FlowLayout
        buttonPanel.add(openButton);
     
        //Add the buttons and the log to this panel.
        add(buttonPanel, BorderLayout.PAGE_START);
        add(logScrollPane, BorderLayout.CENTER);
    }

    public void actionPerformed(ActionEvent e) {

        //Handle open button action.
        if (e.getSource() == openButton) {
        	 int returnVal = fc.showOpenDialog(FileIndexer.this);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                //This is where a real application would open the file.
                log.append("Opening: " + file.getName() + "." + newline);
                
                File  filePath =file.getAbsoluteFile();
                
               
                if( filePath.isDirectory() == false){
                	log.append(filePath + " is not a valid directory.");
                	
                }
                else if(filePath.getPath().length() > 3){
                	fIndexer = new FolderIndexer(filePath );
                    log.append(fIndexer.getMessage());
                
                }
                else{
                	log.append("Your folder path is too short and could be just the C drive which is prohibited for safety reasons. \n Navigate to your desktop or documents. ");
                	
                }
                 
                
            } else {
                log.append("Open command cancelled by user." + newline);
            }
            log.setCaretPosition(log.getDocument().getLength());


        } 
    }



  
public static void doAsStatic(){
	  //Schedule a job for the event dispatch thread:
    //creating and showing this application's GUI.
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            //Turn off metal's use of bold fonts
            UIManager.put("swing.boldMetal", Boolean.FALSE); 
            JFrame frame = new JFrame("Index A Folder");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            //Add content to the window.
            frame.add(new FileIndexer());

            //Display the window.
            frame.pack();
            frame.setVisible(true);;
        }
    });
}	

    public void start() {
    	FileIndexer.doAsStatic(); 
    }
    
    
}
