Wednesday, March 27, 2013

Image Button

public class ImageButton extends Field {
    /* Implementation details of ImageButton :   
     */
   
    private Bitmap backgroundImage = null;
   


    private Bitmap pressedImage = null;
   
    private int width = 0;
   
    private boolean focusable = true;
   
    private boolean selected = false;
   
    /**
     * <p>This method can be used for getting the selected.</p>
     * @return The selected.
     */
    public boolean isSelected() {
        return selected; // returning selected.
    }

    /**
     * <p>This method can be used for setting the selected.</p>
     * @param selected The selected to set.
     */
    public void setSelected(boolean selected) {
        this.selected = selected; // Assigning to this.selected.
        invalidate();
    }

    /**
     * <p>This method can be used for setting the focusable.</p>
     * @param focusable The focusable to set.
     */
    public void setFocusable(boolean focusable) {
        this.focusable = focusable; // Assigning to this.focusable.
    }

    /**
     * <p>This method can be used for getting the width.</p>
     * @return The width.
     */
    public int getWidthAssigned() {
        return width; // returning width.
    }

    /**
     * <p>This method can be used for setting the width.</p>
     * @param width The width to set.
     */
    public void setWidthAssigned(int width) {
        this.width = width; // Assigning to this.width.
    }

    /**
     * <p>This method can be used for getting the hieght.</p>
     * @return The hieght.
     */
    public int getHeightAssigned() {
        return height; // returning hieght.
    }

    /**
     * <p>This method can be used for setting the hieght.</p>
     * @param hieght The hieght to set.
     */
    public void setHeightAssigned(int height) {
        this.height = height; // Assigning to this.hieght.
    }

    private int height = 0;
   
   
    /**
     * Constructor for ImageButton.
     */
    public ImageButton(Bitmap backgroundImage, Bitmap pressedImage, int width, int height, long style) {
        super(style);
        this.backgroundImage = backgroundImage;
        this.pressedImage = pressedImage;
        this.width = width;
        this.height = height;
    }

   
    public int getPreferredHeight() {
        if(getHeightAssigned() <= 0)
            return this.backgroundImage.getHeight();
        return getHeightAssigned();
    }

    public int getPreferredWidth() {
        if(getWidthAssigned() <= 0)
            return this.backgroundImage.getWidth();
        return getWidthAssigned();
    }

    /* (non-Javadoc)
     * @see net.rim.device.api.ui.Field#layout(int, int)
     */
    protected void layout(int width, int height) {
        setExtent(Math.min(width, getPreferredWidth()),
                Math.min(height, getPreferredHeight()));
    }

    /* (non-Javadoc)
     * @see net.rim.device.api.ui.Field#paint(net.rim.device.api.ui.Graphics)
     */
    protected void paint(Graphics graphics) {
        if ((isFocus() || isSelected()) && pressedImage != null) {
            graphics.drawBitmap(0, (getPreferredHeight() - pressedImage.getHeight())/2, getPreferredWidth(), getPreferredHeight(),
                    pressedImage, 0, 0);
        } else if (backgroundImage != null) {
            graphics.drawBitmap(0, (getPreferredHeight() - pressedImage.getHeight())/2, getPreferredWidth(), getPreferredHeight(),
                    backgroundImage, 0, 0);
        } else {
            graphics.setColor(Color.WHITE);
            graphics.fillRoundRect(1, 1, getWidth() - 2, getHeight() - 2, 12,
                    12);
        }
    }
   
    protected void drawFocus(Graphics graphics, boolean on) {
    }
   
    /* (non-Javadoc)
     * @see net.rim.device.api.ui.Field#isFocusable()
     */
    public boolean isFocusable() {
        return focusable;
    }
   
    protected void onFocus(int direction) {
        //  int fieldIndex = getLastFocusedCBIndex();
       //     CustomField field = (CustomField)getField(fieldIndex);
           
        super.onFocus(direction);
        invalidate();
    }

    protected void onUnfocus() {
        super.onUnfocus();
        invalidate();
    }
   
    protected boolean navigationClick(int status, int time) {
        fieldChangeNotify(0);
        return true;
    }
   
    protected boolean keyChar(char character, int status, int time) {
        if (character == Keypad.KEY_ENTER) {
            fieldChangeNotify(0);
            return true;
        }
        return super.keyChar(character, status, time);
    }

    /**
     * <p>This is the method for .</p>
     */
    public void focusLost() {
        onUnfocus();
        invalidate();
    }
}

No comments:

Post a Comment