// file:$ISIP_IFC/util/demo/isip_comm_client_speaker_verification/LockPanel.java
// version: $Id: LockPanel.java 10572 2006-06-26 21:54:34Z rei5 $

// import necessary java libraries
//
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.font.TextLayout;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.image.*;
import java.util.Hashtable;
import java.net.MalformedURLException;


/**
 * LockPanel is the main drawing class.  draws three stop signs of
 * various sizes with different fonts.
 */
public class LockPanel extends JPanel {

    //-------------------------------------------------------------------------
    //
    // private members
    //
    //-------------------------------------------------------------------------
    private float size;
    private int edge = 45;	
    private static Image agif, clouds;
    private static int aw, ah, cw;
    private int x;
    private static Hashtable<Object,Object> cache;
    private long flag;
    private String[] names = 
    { 
	"open.gif", "close.gif"
    };
    
    /**
     * constructor of the class
     */
    public LockPanel() {
	setBackground(Color.white);
	setPreferredSize(new Dimension(120,120));
	cache = new Hashtable<Object, Object>(names.length);
        for (int i = 0; i < names.length; i++) {
            cache.put(names[i], getImage(names[i], this));
        }      
	flag = 1;
    }
    
    /**
     * get the image
     *
     * @param name name of the image
     * @param cmp component name
     */
    public static Image getImage(String name, Component cmp) {
        Image img = null;
        
	// check whether the hash table is empty or not
	//
	if (cache != null) {
            if ((img = (Image) cache.get(name)) != null) {
                return img;
            }
        }
        String dir = "./";
	img = cmp.getToolkit().createImage(dir + name);
        
	// create a mediatracker object
	//
        MediaTracker tracker = new MediaTracker(cmp);
        
	// add the image
	//
	tracker.addImage(img, 0);
        try {
            tracker.waitForID(0);
            if (tracker.isErrorAny()) {
                System.out.println("Error loading image " + name);
            }
        } catch (Exception ex) { ex.printStackTrace(); }
        return img;
    }


    /**
     * set the flag.
     *
     * @param im the value is used to differentiate between the
     * different images.
     */
    public void setFlag (long im) {
	flag = im;
	repaint();
    }

    /**
     * the paint method
     *
     * @param g default graphics
     */
    public void paintComponent(Graphics g) {
	
	// set the defualt looks
	// 
	super.paintComponent(g); 
	
	// set the color
	//
	setBackground(Color.white);
	drawDemo(g);
    }
    
    /**
     *  draw thw picture depending on the flag set
     * 
     * @param g default graphics
     */
    public void drawDemo(Graphics g) {

        Graphics2D g2 = (Graphics2D) g;

	// set dimensions
	// 
	Dimension d = getSize();
	int clientWidth = 100;
	int clientHeight = 125;

	// if the flag is set to 2 draw the opened lock image
	//
	if (flag == 2) {
	    clouds = getImage("../isip_comm_client_speaker_verification/images/open.gif", this);
	    g2.drawImage(clouds, -18 + edge,15 + edge, clientWidth, clientHeight, this);
	}
	
	// if the flag is set to 1 draw thw closed lock image
	//
	else {
	    clouds = getImage("../isip_comm_client_speaker_verification/images/close.gif", this);
	    g2.drawImage(clouds, -25 + edge, 25 + edge, clientWidth, clientHeight, this);
	}

    }  

}

















