/*
 * 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.Image;
import minime.core.ResourceManager;

/**
 * The EditFieldPanel is an editable text component with background image.
 * 
 * @author yishu
 * 
 */
public class EditFieldPanel extends EditField
{
    private Image bgImage;
    private Image selectedImage;
    private Image disabledImage;
    private Image img;

    /**
     * Creates an EditFieldPanel instance by given editField object,images
     * resource id.The background image, selected image are been created by
     * ResourceManager based on resource id.if want to set image to null, the id
     * must be ResourceManager.NONE_RESOURCE.
     * 
     * @param editField
     *            an editField object
     * @param bgImRscId
     *            the background image resource id.
     * @param selectedImRscId
     *            the selected image resource id.
     * @return created EditFieldPanel instance.
     * @throws NullPointerException
     *             if editField is null
     */
    public static EditFieldPanel createEditFieldPanel(EditField editField, int bgImRscId, int selectedImRscId)
            throws NullPointerException
    {
        return createEditFieldPanel(editField, bgImRscId, selectedImRscId, ResourceManager.NONE_RESOURCE);
    }

    /**
     * Creates an EditFieldPanel instance by given editField object, background
     * image, image for been selected, but it doesn't have disablabed image.
     * 
     * @param editField
     *            an editField object
     * @param bgImage
     *            the background image
     * @param selectedImage
     *            image for been selected
     * @param disabledImage
     *            image for been disabled
     * @return the created EditFieldPanel instance.
     * @throws NullPointerException
     *             if editField is null
     */
    public static EditFieldPanel createEditFieldPanel(EditField editField, Image bgImage, Image selectedImage)
            throws NullPointerException
    {
        return createEditFieldPanel(editField, bgImage, selectedImage, null);
    }

    /**
     * Creates an EditFieldPanel instance by given editField object,images
     * resource id.The background image, selected image and disabled image are
     * been created by ResourceManager based on resource id.if want to set image
     * to null, the id must be ResourceManager.NONE_RESOURCE.
     * 
     * @param editField
     *            an editField object
     * @param bgImRscId
     *            the background image resource id.
     * @param selectedImRscId
     *            the selected image resource id.
     * @param disabledImRscId
     *            the disabled image resource id.
     * @return the created EditFieldPanel instance.
     * @throws NullPointerException
     *             if editField is null
     */
    public static EditFieldPanel createEditFieldPanel(EditField editField, int bgImRscId, int selectedImRscId,
            int disabledImRscId) throws NullPointerException
    {

        return createEditFieldPanel(editField, new Image(ResourceManager.getImage(bgImRscId)), new Image(
                ResourceManager.getImage(selectedImRscId)), new Image(ResourceManager.getImage(disabledImRscId)));
    }

    /**
     * Creates an EditFieldPanel instance by given editField object, background
     * image, images for been selected and disabled.
     * 
     * @param editField
     *            an editField object
     * @param bgImage
     *            the background image
     * @param selectedImage
     *            image for been selected
     * @param disabledImage
     *            image for been disabled
     * @return the created EditFieldPanel instance.
     * @throws NullPointerException
     *             if editField is null
     */
    public static EditFieldPanel createEditFieldPanel(EditField editField, Image bgImage, Image selectedImage,
            Image disabledImage) throws NullPointerException
    {
        if (editField == null)
            throw new NullPointerException();

        EditFieldPanel e = new EditFieldPanel(editField);
        e.setImage(bgImage, selectedImage, disabledImage);
        return e;
    }

    /**
     * Creates an EditFieldPanel instance by given editField object, and it
     * doesn't have background image,selected image and disabled image.The
     * parameter should not be null.
     * 
     * @param editField
     *            an editField object
     */
    public EditFieldPanel(EditField editField)
    {
        super(editField);
        setImage(null, null, null);
    }

    /**
     * @see minime.Drawable#layout()
     */
    public void layoutImp()
    {
        super.layoutImp();
        // cal. the size of editfieldpanel
        if (bgImage != null)
        {
            if (bgImage.getHeight() > getHeight())
                setHeight(bgImage.getHeight());
            if (bgImage.getWidth() > getWidth())
                setWidth(bgImage.getWidth());
        }
        if (selectedImage != null && isSelected())
        {
            if (selectedImage.getHeight() > getHeight())
                setHeight(selectedImage.getHeight());
            if (selectedImage.getWidth() > getWidth())
                setWidth(selectedImage.getWidth());
        }
    }

    /**
     * @see minime.Drawable#renderImpl(Graphics)
     */
    public void renderImpl(Graphics gc)
    {
        // is is selected then draw the selected image, or draw background image
        if (isSelected())
            img = selectedImage;
        //\TODO EditField & EditFieldPanel need to manage disabling
//        else if (is(DISABLED))
//            img = disabledImage;
        else
            img = bgImage;
        if (img != null)
            img.draw(gc, 0, (getHeight() - img.getHeight()) / 2);
        super.renderImpl(gc);
    }

    /**
     * Sets the background image, selected image and disabled image with given
     * images.
     * 
     * @param bgImage
     *            the background image
     * @param selectedImage
     *            the selected image
     * @param disabledImage
     *            the disabled image
     */
    public void setImage(Image bgImage, Image selectedImage, Image disabledImage)
    {
        this.bgImage = bgImage;
        this.selectedImage = selectedImage;
        this.disabledImage = disabledImage;
    }

    /**
     * Sets the background image, selected image and disabled image. These
     * images are gotten from resource file(*.bin) based on resource index id,
     * if want to set image to null, the id must be
     * ResourceManager.NONE_RESOURCE.
     * 
     * @param bgImRscId
     *            the background image resource index id, bgImRscId must be
     *            ResourceManager.NONE_RESOURCE if background image set to null.
     * @param selectedImRscId
     *            the selected image resource index id, selectedImRscId must be
     *            ResourceManager.NONE_RESOURCE if selected image set to null.
     * @param disabledImRscId
     *            the disabled image resource index id, disabledImRscId must be
     *            ResourceManager.NONE_RESOURCE if disabled image set to null.
     */
    public void setImage(int bgImRscId, int selectedImRscId, int disabledImRscId)
    {
        setImage(new Image(ResourceManager.getImage(bgImRscId)), new Image(ResourceManager.getImage(selectedImRscId)),
                new Image(ResourceManager.getImage(disabledImRscId)));
    }
}
