/*
 * Copyright (c) 2006-2008 MiniMe. Code released under The MIT/X Window System
 * License. Full license text can be found in license.txt
 */
package minime.ui;

import javax.microedition.lcdui.Graphics;

import minime.Font;
import minime.Image;

/**
 * LabelPanel is a Label with a background Image. If the text is not null, it
 * shoud be displayed in the center of the image. Image can be null. LabelPanel
 * size if the bigest of each size (w is max(image.w, editfieldTitleLabel.w), h
 * is max(image.h, editfieldTitleLabel.h))
 * 
 * @author Lionel
 * 
 */
public class LabelPanel extends Label
{
    private Image bgImage;
    
    protected LabelPanel()
    {}
    /**
     * Constructs an instance of LabelPanel by given label instance and image.
     * 
     * @param l
     *            the label instance
     * @param i
     *            the image
     */
    public LabelPanel(Label l, Image i)
    {
        super(l);
        this.bgImage = i;
    }

    /**
     * Construct a LabelPanel instance with given Label instance and background
     * image resource index id, this id is used to get image from resource
     * image.If want to set image to null, imRscId should be
     * ResourceManager.NONE_RESOURCE.
     * 
     * @param l
     *            the Label instance
     * @param imRscId
     *            the background image resource id, if want to set image to
     *            null, imRscId should be ResourceManager.NONE_RESOURCE.
     */
    public LabelPanel(Label l, int imRscId)
    {
        this(l, new Image(imRscId));
    }

    public void renderImpl(Graphics gc)
    {
    	// draw the image centered
        if (bgImage != null)
            bgImage.draw(gc, (getWidth() - bgImage.getWidth()) / 2, (getHeight() - bgImage.getHeight()) / 2);
        // then draw the text
        super.renderImpl(gc);
    }

    public void layoutImp()
    {
        // call bgImage.getHeight() to force realized
        if (bgImage != null && (bgImage.getHeight()>-1))
        {
        	// set the drawable size ate the image size
            setHeight(bgImage.getHeight()) ; 
            setWidth(bgImage.getWidth()) ;
        }
        // then layout the text
        super.layoutImp(); 
    }

    /**
     * Sets the background image.
     * 
     * @param i
     *            the image.
     */
    public void setBgImage(Image i)
    {
        this.bgImage = i;
        setNeedLayout() ;
    }

    /**
     * Sets the image for LabelPanel, the image will be gotten from image
     * resource file(.bin) based on imRscId by ResrouceManager.
     * 
     * @param imRscId
     *            the image resource id.
     */
    public void setBgImage(int imRscId)
    {
    	setBgImage(new Image(imRscId));
    }
    
	public Image getBgImage() {
		return bgImage;
	}
}
