import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import java.util.*;

/**
 * The SplitPane class creates a vector of jpeg file names from a directory
 * path, creates a split pane, and then displays the filenames on the left
 * and the image on the right.
 *
 * @version 1.0 3 Dec 2001
 * @author 	Neil Moomey
 */

//SplitPane itself is not a visible component.
public class SplitPane implements ListSelectionListener {
    private Vector imageNames;
    private JLabel picture;
    private JList list;
    private JSplitPane splitPane;
    private String folder;


    public SplitPane(String folder) {
        //Read image names

            this.folder=folder;
            System.out.println("folder = "+ folder);
            //SplitPane.folder = folder;


			String[] dir = new java.io.File(folder).list(); // Get list of filenames in this folder
			java.util.Arrays.sort(dir);		// Sort it (Data Structuring chapter))

			String lower;

		    // Create vector imageNames with jpeg filenames
			imageNames = new Vector();
			for (int i=0; i<dir.length; i++) {
				lower = dir[i].toLowerCase();

				if ((lower.endsWith(".jpg")) || (lower.endsWith(".jpeg"))) {
					imageNames.addElement(dir[i]);

				}
			}


			if (imageNames.size() == 0) {
				System.out.println("There are no jpegs in this folder.");
				System.exit(0);
			}

		    // Print out jpeg filenames
			for (int i=0; i<imageNames.size(); i++) {
				System.out.println(i + " " + imageNames.elementAt(i) + " is a jpg");
			}

        //Create the list of images and put it in a scroll pane
        list = new JList(imageNames);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setSelectedIndex(0);
        list.addListSelectionListener(this);
        JScrollPane listScrollPane = new JScrollPane(list);

        //Set up the picture label and put it in a scroll pane
        ImageIcon firstImage = new ImageIcon(folder +
                                     (String)imageNames.firstElement());
        picture = new JLabel(firstImage);
        picture.setPreferredSize(new Dimension(firstImage.getIconWidth(),
                                               firstImage.getIconHeight()));
        JScrollPane pictureScrollPane = new JScrollPane(picture);

        //Create a split pane with the two scroll panes in it
        splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                                   listScrollPane, pictureScrollPane);
        splitPane.setOneTouchExpandable(true);
        splitPane.setDividerLocation(150);

        //Provide minimum sizes for the two components in the split pane
        Dimension minimumSize = new Dimension(100, 50);
        listScrollPane.setMinimumSize(minimumSize);
        pictureScrollPane.setMinimumSize(minimumSize);

        //Provide a preferred size for the split pane
        splitPane.setPreferredSize(new Dimension(400, 200));
    }


    //Used by SplitPane2
    public JList getImageList() {
        return list;
    }

    public JSplitPane getSplitPane() {
        return splitPane;
    }

    public void valueChanged(ListSelectionEvent e) {

        if (e.getValueIsAdjusting())
            return;

        JList theList = (JList)e.getSource();
        if (theList.isSelectionEmpty()) {
            picture.setIcon(null);
        } else {
            int index = theList.getSelectedIndex();

            //System.out.println("folder = "+ this.folder);
            ImageIcon newImage = new ImageIcon(this.folder + (String)imageNames.elementAt(index));
            picture.setIcon(newImage);
            picture.setPreferredSize(new Dimension(newImage.getIconWidth(),
                                               newImage.getIconHeight() ));
            picture.revalidate();
        }
    }
}
