import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import java.util.zip.*;
import java.io.*;
import javax.swing.JPanel;
import javax.swing.*;
import javax.imageio.*;
// Refs: http://java.sun.com/docs/books/tutorial/uiswing/
//       http://doc.java.sun.com/DocWeb/api/javax.swing.JApplet
public class demoImageLib extends JApplet implements ActionListener {

    JList dataList;
    Vector vjpgs;
    ZipFile zf;
    JLabel pic;
    JSplitPane splitPane;
    Container content;
    
    void setUpFrame(int i) {
        content.removeAll();  // overkill, not really a good way to do this
        content.setBackground(Color.white);
        content.setLayout(new FlowLayout()); 
        setupSplitPane(i);
        content.add(splitPane);
        JButton copyButton = new JButton("don't click!");
        copyButton.addActionListener(this);
        content.add(copyButton);
        setContentPane(content);
    }
    
    public void init() {
    	content = getContentPane();
    	setUpFrame(0);
      }
    
    void setupSplitPane(int i) {
     // see http://java.sun.com/docs/books/tutorial/uiswing/components/splitpane.html
     // Step 1:  create a scroll page of image names
        try {
            zf = new ZipFile("imagelib.zip");
            ArrayList jpgs = Collections.list(zf.entries());
            vjpgs = new Vector(jpgs);
            } 
         catch (IOException e) {
            zf = null;
            vjpgs = new Vector();
            }
         dataList = new JList(vjpgs);
         dataList.addMouseListener(new myMouse());
         JScrollPane listScrollPane = new JScrollPane(dataList);
     // Step 2: create a scroll pane for a picture (just a JLabel)
     pic = new JLabel();
     // pic.setVerticalAlignment(SwingConstants.TOP);
     pic.setHorizontalTextPosition(SwingConstants.CENTER);
     pic.setVerticalTextPosition(SwingConstants.TOP);
     pic.setIcon(fetchImage(i));
     pic.setText(vjpgs.elementAt(i).toString());
     JPanel tp = new JPanel(new FlowLayout());
     tp.add(pic);
     JScrollPane pictureScrollPane = new JScrollPane(tp);
    	
     // Step 3: create a splitpane from the two previous step results
     splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                 listScrollPane, pictureScrollPane);
     // the following are not essential, but nice
     splitPane.setOneTouchExpandable(true);
     splitPane.setDividerLocation(150);
     Dimension minimumSize = new Dimension(200, 50);
     listScrollPane.setMinimumSize(minimumSize);
     minimumSize = new Dimension(2000,1000);
     pictureScrollPane.setMinimumSize(minimumSize);
     }

    public void actionPerformed(ActionEvent event) {
        System.out.println("ouch!");
        }
   
    ImageIcon fetchImage( int ind ) {
        ZipEntry ze = (ZipEntry)vjpgs.elementAt(ind);
        String ent = ze.toString();
        try { 
           InputStream ris = zf.getInputStream(ze);
           BufferedImage bim = ImageIO.read(ris);
           Image img = bim;  // upcast to an Image
           ImageIcon imic = new ImageIcon(img);  // convert to ImageIcon
           return imic;
           }
        catch (IOException e) {
           return null;
           }
        }
    
    // this is a typical Java trick:  instead of having to define
    // all the mouse events (mouseover, press, etc) -- we just use
    // the MouseAdapter class, which has empty methods for all these,
    // yet we override the one we care about
    class myMouse extends MouseAdapter {
    	public void mouseClicked(MouseEvent e) {
            int index = dataList.locationToIndex(e.getPoint());
            System.out.println("clicked on picture item "+index);
            // setUpFrame(index);
            pic.setText(vjpgs.elementAt(index).toString());
            pic.setIcon(fetchImage(index));
         }
    }
}
    