import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import java.util.*;

public class SplitPaneDemo extends JSplitPane
			   implements ListSelectionListener {
    static JFrame frame;
    static ResourceBundle imageResource;
    static Vector imageList;

    JLabel picture;
    JScrollPane pictureScrollPane;

    public SplitPaneDemo() {
	super(HORIZONTAL_SPLIT);
	setOneTouchExpandable(true);

        // Create the list of images and put it in a scroll pane
        JList listOfImages = new JList(imageList);
        listOfImages.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        listOfImages.setSelectedIndex(0);
        listOfImages.addListSelectionListener(this);
        JScrollPane listScrollPane = new JScrollPane(listOfImages);

        // Set up the picture label and put it in a scroll pane
        ImageIcon firstImage = new ImageIcon("images/" + 
				     (String)imageList.firstElement());
        picture = new JLabel(firstImage);
        picture.setPreferredSize(new Dimension(firstImage.getIconWidth(),
                                               firstImage.getIconHeight()));
        pictureScrollPane = new JScrollPane(picture);

	// Add the two scroll panes to this split pane
	setLeftComponent(listScrollPane);
	setRightComponent(pictureScrollPane);

	// Provide minimum sizes for the two components in the split pane
	Dimension minimumSize = new Dimension(100, 50);
	listScrollPane.setMinimumSize(minimumSize);
	pictureScrollPane.setMinimumSize(minimumSize);

	// Set the initial location and size of the divider
        setDividerLocation(150);
	setDividerSize(10);

	// Provide a preferred size for the split pane
	setPreferredSize(new Dimension(400, 200));
    }

    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();
            ImageIcon newImage = new ImageIcon("images/" + 
                                     (String)imageList.elementAt(index));
            picture.setIcon(newImage);
            picture.setPreferredSize(new Dimension(newImage.getIconWidth(),
                                               newImage.getIconHeight() ));
            picture.revalidate();
        }
    }

    protected static Vector parseList(String theStringList) {
	Vector v = new Vector(10);
        StringTokenizer tokenizer = new StringTokenizer(theStringList, " ");
        while (tokenizer.hasMoreTokens()) {
            String image = tokenizer.nextToken();
            v.addElement(image);
        }
	return v;
    }

    public static void main(String s[]) {

        try {
            UIManager.setLookAndFeel(
                "javax.swing.plaf.metal.MetalLookAndFeel");
        } catch (Exception e) {
            System.err.println("Couldn't use the metal "
                             + "look and feel: " + e);
        }

        // Read image names from a properties file
        try {
            imageResource = ResourceBundle.getBundle("imagenames");
            String imageListString = imageResource.getString("images");
            imageList = parseList(imageListString);
        } catch (MissingResourceException e) {
            System.err.println("Can't find list of image names.");
            System.exit(-1);
        }

        WindowListener l = new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        };
 
        frame = new JFrame("SplitPaneDemo");
        frame.addWindowListener(l);
        frame.getContentPane().add("Center", new SplitPaneDemo());
        frame.pack();
        frame.setVisible(true);
    }
}
