//import java.awt.*;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.AWTEventMulticaster;
import java.awt.AWTEvent;
import java.awt.Image;
import java.awt.Dimension;
import javax.swing.*;
import java.awt.event.*;
import java.io.File;

/**
 * an icon used for thumbnail sketches of a Pixmap
 * currently all icons are square rather than proportional
 * to the image being iconified
 *
 * @author Owen Astrachan
 */

public class PixIcon extends JPanel
{
    final static int SIZE = 60;

    /**
     * @param im the image for which a thumbnail is created
     * @param name URL/filename of image (displayed with thumbnail)
     *
     */
    
    public PixIcon(Image im,String name)
    {
	myImage = im;
	myD = new Dimension(getWidth()+1,getHeight()+15);
	myName = name;	
    }

    void setImage(Image im)
    {
        myImage = im;
    }

    /**
     * base dimension is width
     * @return scaled height for icon
     */
    public int getHeight()
    {
        return SIZE*
	  myImage.getHeight(this)/myImage.getWidth(this);
    }

    public int getWidth()
    {
        return SIZE;
    }

    /**
     * paints the thumbnail with associated name
     */
    
    public void paintComponent(Graphics g)
    {
	if (! ourFontIsSet)     // set font once (must be a better way!)
        {
            ourFontIsSet = true;
            ourMetric = g.getFontMetrics(ourFont);
        }

	super.paintComponent(g);
	
	// draw the scaled image as a thumbnail
	
	g.drawImage(myImage,0,0,getWidth(),getHeight(),
		    0,0,myImage.getWidth(this),myImage.getHeight(this),this);

	// find the filename, last component of full path
	
	String name =
	    myName.substring(myName.lastIndexOf(File.separator)+1);

	// draw the name
	
	g.setFont(ourFont);
	g.drawString(name,
		     (getWidth()-ourMetric.stringWidth(name))/2,
		     (getHeight()+ourMetric.getAscent()+1));
    }

    public Dimension getPreferredSize()
    {
	return myD;
    }

    public Dimension getMinimumSize()
    {
	return myD;
    }

    /**
     * if the thumbnail is clicked it will generate an action event
     */
    
    public void processMouseEvent(MouseEvent ev)
    {
	switch (ev.getID())
	{
	case MouseEvent.MOUSE_RELEASED:
	    break;
		
	case MouseEvent.MOUSE_PRESSED:
	    break;
	    
	case MouseEvent.MOUSE_CLICKED:
		
	    if (myListener != null)
	    {
		myListener.actionPerformed(
		    new ActionEvent(
			this,ActionEvent.ACTION_PERFORMED,myName));
	    }
	    break;
		
	case MouseEvent.MOUSE_ENTERED:
	    break;
		
	case MouseEvent.MOUSE_EXITED:
	    break;
	}
	super.processMouseEvent(ev);
    }

    /**
     * Adds an action listener to the icon
     * @param listener the action listener added
     */
    
    public void addActionListener(ActionListener listener)
    {
	myListener =
	    AWTEventMulticaster.add(myListener,listener);
	enableEvents(AWTEvent.MOUSE_EVENT_MASK);
    }

    /**
     * Removes an action listener from the icon
     * @param listener the action listener to remove
     */
    
    public void removeActionListner(ActionListener listener)
    {
	myListener = AWTEventMulticaster.remove(myListener,listener);
    }

    private String myName;
    private Image myImage;
    private Dimension myD;
    private ActionListener myListener;
    
    private static Font ourFont = new Font("SansSerif",Font.BOLD,10);
    private static boolean ourFontIsSet = false;
    private static FontMetrics ourMetric;
}
