import java.util.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/** An instance is a JPanel that contains a thumbnail of
    an image and a title for it 
 */
public class ThumbnailPanel extends JPanel {
    
    
    /** The width of the thumbnail (in pixels),
        the width of the padding after each thumbnail */
    public static final int SIZE= 60;
    public static final int WIDTH_PADDING= 10;
    
    private static Font labelFont = new Font("Helvetica",Font.BOLD,9);
    
    private Image im;   // the image
    private String f;   // the label for it (just the file name, not the path)
    private ImageHandler imageHand; // the image handler that oversees everything
    
    private FontMetrics labelFontMetrics; // the font metrics (null if not set)
    
    /** Constructor: a thumbnail of image v. fpath is the path for the image file */
    public ThumbnailPanel(Image v, String fpath, ImageHandler imageHand) {
        this.im= v;
        this.imageHand= imageHand;
        this.f= fpath;
        
        // Remove all the the file name from f
        int k= this.f.lastIndexOf("/");
        if (k != -1)
            this.f= this.f.substring(k+1);
        
        // Register a mouse listener with this panel
        addMouseListener(new LocalMouseListener());
    }
    
    /** = the height of the thumbnail */
    public int newGetHeight() {
        if (im == null)
           return SIZE;
                           
        return SIZE  * im.getHeight(this)/im.getWidth(this);
    }
    
    /** set the image to im */
    public void setImage(Image im) {
        this.im= im;
    }
    
    /** = the width of the thumbnail */
    public int newGetWidth() {
        return SIZE;
    }
    
    /** paint the image and label f on the panel. The label is red
        if this thumbnail is highlighted and black otherwise. */
    public void paint(Graphics g) {
        // set the font if not already set
        if (labelFontMetrics == null) { 
            labelFontMetrics = g.getFontMetrics(labelFont);
        }
        
        // draw the thumbnail
        g.drawImage(im,0,0,newGetWidth(),newGetHeight(),
                    0,0,im.getWidth(this),im.getHeight(this),this);
        
        // draw label f. Make it red if this is the higlighted thumbnail
        // and black otherwise
        g.setFont(labelFont);
        Color c= g.getColor();
        if (this == imageHand.getHighlighted()) {
            g.setColor(Color.RED);
        }
        g.drawString(f,
                     /*(newGetWidth()-labelFontMetrics.stringWidth(f))/2,*/0,
                     (newGetHeight()+labelFontMetrics.getAscent()+1));
        g.setColor(c);
    }
    
    /** = preferred size of this JPanel */
    public Dimension getPreferredSize() {
        int width= newGetWidth()+WIDTH_PADDING;
        return new Dimension(width, newGetHeight()+15);
    }
    
    /** = minimum size of this JPanel */
    public Dimension getMinimumSize() {
        return new Dimension(newGetWidth()+10, newGetHeight()+15);
    }
    
    
    /** Local class with a method to process mouse click in this panel*/
    class LocalMouseListener extends MouseInputAdapter {
        public void mouseClicked(MouseEvent e) {
            imageHand.highlightThumbnail((ThumbnailPanel) e.getSource());
        } 
    }
    
    
}