/*
 * @(#)NeilsJpegBrowser.java        1.0 01/12/03
*/

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;

/**
 * Use NeilsJpegBrowser to quickly browse all jpeg images in a folder.
 * The NeilsJpegBrowser class creates the Open File pop up window for the
 * JFileChooser, and passes the directory path to the SplitPane class.
 *
 * @version 	1.0 3 Dec 2001
 * @author 	Neil Moomey
 */


public class NeilsJpegBrowser extends JFrame {
    static private final String newline = "\n";

    public NeilsJpegBrowser() {
        super("NEIL'S JPEG BROWSER");

        //Create the log first because the action listeners need to refer to it.
        final JTextArea log = new JTextArea(5,20);
        log.setMargin(new Insets(5,5,5,5));
        log.setEditable(false);
        log.setText("JPEG BROWSER");
        log.setText("Please select a folder to browse.");
        JScrollPane logScrollPane = new JScrollPane(log);

        //Create a file chooser
        final JFileChooser fc = new JFileChooser();

        //Create the open button
        ImageIcon openIcon = new ImageIcon("images/open.gif");
        JButton openButton = new JButton("Open a File...", openIcon);
        openButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int returnVal = fc.showOpenDialog(NeilsJpegBrowser.this);

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                    //this is where the application opens a file.
                    //************
                    // Convert "C:\images\denali.jpg" into "C:/images/"
                    String path = file.getPath();
                    path=path.replace('\\','/');
                    int s2=path.lastIndexOf('/');  // This determines the postion of the last / char
                    path=path.substring(0,s2+1);

                    System.out.println("path =" + path);
 					JFrame frame2 = new JFrame("NEIL'S JPEG BROWSER");

					frame2.addWindowListener(new WindowAdapter() {
					public void windowClosing(WindowEvent e) {System.exit(0);}
					     });

					SplitPane SplitPane = new SplitPane(path);
					//SplitPane.folder2=path;
					frame2.getContentPane().add(SplitPane.getSplitPane());
					frame2.pack();
					frame2.setVisible(true);
                    //************
                    log.append(file.getPath() + " " + file.getName() + "." + newline);
                } else {
                    log.append("Open command cancelled by user." + newline);
                }
            }
        });

        //For layout purposes, put the buttons in a separate panel
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(openButton);

        //Add the buttons and the log to the frame
        Container contentPane = getContentPane();
        contentPane.add(buttonPanel, BorderLayout.NORTH);
        contentPane.add(logScrollPane, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        JFrame frame = new NeilsJpegBrowser();
        //System.out.println("Path = " + file.getPath());

        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        frame.pack();
        frame.setVisible(true);
    }
}


