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

/**
 * An object represents a button, it can have label and background image and
 * frame.
 * 
 * @author aaa
 * 
 */
public class Button extends LabelPanel
{
    private Image bgBitmapUp;
    private Image bgBitmapDown;
    private boolean keyPressed = false;

    /**
     * create a button with given background bitmaps for button up and down
     * 
     * @param bgBitmapUp
     *            background bitmap for button up
     * @param bgBitmapDown
     *            background bitmap for button down
     */
    public Button(Image bgBitmapUp, Image bgBitmapDown)
    {
        this(new Label(null, FontManager.getFont(FontManager.BUTTON_FONT), 0), bgBitmapUp, bgBitmapDown);
    }

    /**
     * create a button object with given button text, text color and background
     * bitmaps for button up and down
     * 
     * @param text
     *            the text of button
     * @param textColor
     *            color for text
     * @param bgBitmapUp
     *            background bitmap for button up
     * @param bgBitmapDown
     *            background bitmap for button down
     */
    public Button(String text, int textColor, Image bgBitmapUp, Image bgBitmapDown)
    {
        this(new Label(text, FontManager.getFont(FontManager.BUTTON_FONT), textColor), bgBitmapUp, bgBitmapDown);
    }

    /**
     * create a button object with given button text id, text color and
     * background bitmaps id for button up and down.The text and images will be
     * gotten from resource files based on index id.If want to set text or image
     * to null, set the id to ResourceManager.NONE_RESOURCE.
     * 
     * @param txRscId
     *            the label text resource index id, If want to set text to null,
     *            set the id to ResourceManager.NONE_RESOURCE.
     * @param textColor
     *            the text color
     * @param upImRscId
     *            background bitmap index id for button up.If want to set image
     *            to null, set the id to ResourceManager.NONE_RESOURCE.
     * @param downImRscId
     *            background bitmap index id for button down.If want to set
     *            image to null, set the id to ResourceManager.NONE_RESOURCE.
     */
    public Button(int txRscId, int textColor, int upImRscId, int downImRscId)
    {
        this(new Label(txRscId, FontManager.getFont(FontManager.BUTTON_FONT), textColor), 
                new Image(ResourceManager.getImage(upImRscId)), new Image(ResourceManager.getImage(downImRscId)));
    }

    /**
     * create a button object with given editfieldTitleLabel object and
     * background bitmaps for button up and down
     * 
     * @param l
     *            editfieldTitleLabel object
     * @param bgBitmapUp
     *            background bitmap for button up
     * @param bgBitmapDown
     *            background bitmap for button down
     */
    public Button(Label l, Image bgBitmapUp, Image bgBitmapDown)
    {
        super(l, bgBitmapUp);
        this.bgBitmapUp = bgBitmapUp;
        this.bgBitmapDown = bgBitmapDown;
    }

    public void layoutImp()
    {
        if (keyPressed)
        {
            setFrame(true);
            setBgImage(bgBitmapDown);
        }
        else
        {
            setFrame(false);
            setBgImage(bgBitmapUp);
        }
        // layout the labelPanel
        super.layoutImp();
    }

    /**
     * Sets the button pressed or not.
     * 
     * @param keyPressed
     *            the button pressed or not.
     */
    public void setKeyPressed(boolean keyPressed)
    {
        this.keyPressed = keyPressed;
        setNeedLayout() ;
    }
}
