Button.java

Button.java/**
 * Copyright (c) 2013 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */

package com.nokia.example.statusshout.ui;

import java.io.IOException;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.nokia.mid.ui.CanvasGraphicsItem;

/**
 * A custom button. 
 */
public class Button extends CanvasGraphicsItem {
    // Constants
    private static final int DEFAULT_WIDTH = 100;
    private static final int DEFAULT_HEIGHT = 50;

    // Members
    private Listener listener;
    private Image unpressedImage;
    private Image pressedImage;
    private int width;
    private int height;
    private int x;
    private int y;
    private boolean pressed;
    private boolean disabled;

    /**
     * Constructor.
     * @param unpressedImage
     * @param pressedImage
     * @param listener
     */
    public Button(final Image unpressedImage,
                  final Image pressedImage,
                  final Listener listener)
    {
        super(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        construct(unpressedImage, pressedImage, listener);
    }

    /**
     * Constructor.
     * @param unpressedImageUri
     * @param pressedImageUri
     * @param listener 
     */
    public Button(final String unpressedImageUri,
                  final String pressedImageUri,
                  final Listener listener)
    {
        super(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        
        try {
            unpressedImage = Image.createImage(unpressedImageUri);
            pressedImage = Image.createImage(pressedImageUri);
        }
        catch (IOException e) {
        }
        
        construct(unpressedImage, pressedImage, listener);
    }

    /**
     * @see com.nokia.mid.ui.CanvasItem#setPosition(int, int)
     */
    public void setPosition(int x, int y) {
        super.setPosition(x, y);
        this.x = x;
        this.y = y;
    }

    /**
     * 
     * @param pressed
     */
    public void setPressed(boolean pressed) {
        this.pressed = pressed;
        repaint();
    }

    /** 
     * @return
     */
    public boolean getPressed() {
        return pressed;
    }

    /**
     * 
     * @param disabled
     */
    public void setIsDisabled(boolean disabled) {
        this.disabled = disabled;
        
        if (disabled && pressed) {
            pressed = false;
            repaint();
        }
    }

    public boolean getIsDisabled() {
        return disabled;
    }

    public boolean pointerPressed(int x, int y) {
        if (disabled) {
            return false;
        }
        
        boolean caught = false;
        
        if (x >= this.x && x <= this.x + width
                && y >= this.y && y <= this.y + height)
        {
            setPressed(true);
            
            if (listener != null) {
                listener.onPressedChanged(this, true);
            }
            
            caught = true;
        }
        
        return caught;
    }

    public boolean pointerDragged(int x, int y) {
        if (disabled) {
            return false;
        }
        
        boolean caught = false;
        
        if (x >= this.x && x <= this.x + width
                && y >= this.y && y <= this.y + height)
        {
            caught = true;
        }
        else if (pressed) {
            setPressed(false);
            
            if (listener != null) {
                listener.onPressedChanged(this,  false);
            }
        }
        
        return caught;
    }

    public boolean pointerReleased(int x, int y) {
        if (disabled) {
            return false;
        }
        
        boolean caught = false;
        
        if (x >= this.x && x <= this.x + width
                && y >= this.y && y <= this.y + height)
        {
            if (pressed && listener != null) {
                listener.onTapped(this);
            }
            
            caught = true;
        }
        
        if (pressed) {
            setPressed(false);
            
            if (listener != null) {
                listener.onPressedChanged(this, false);
            }
        }
        
        return caught;
    }

    /**
     * Paints the button.
     * @param graphics
     * @param x
     * @param y
     */
    public void paint(final Graphics graphics, final int x, final int y) {
        if (unpressedImage != null && (!pressed || (pressed && pressedImage == null))) {
            graphics.drawImage(unpressedImage, x, y, Graphics.TOP | Graphics.LEFT);
        }
        else if (pressed && pressedImage != null) {
            graphics.drawImage(pressedImage, x, y, Graphics.TOP | Graphics.LEFT);
        }
    }

    /**
     * @see com.nokia.mid.ui.CanvasGraphicsItem#paint(javax.microedition.lcdui.Graphics)
     */
    protected void paint(Graphics graphics) {
        paint(graphics, 0, 0);
    }

    /**
     * 
     * @param unpressedImage
     * @param pressedImage
     * @param listener
     * @throws NullPointerException
     */
    private void construct(final Image unpressedImage,
                           final Image pressedImage,
                           final Listener listener)
            throws NullPointerException
    {
        if (unpressedImage == null) {
            throw new NullPointerException("No image!");
        }
        
        this.unpressedImage = unpressedImage;
        this.pressedImage = pressedImage;
        this.listener = listener;
        
        width = unpressedImage.getWidth();
        height = unpressedImage.getHeight();
        this.setSize(width, height);
    }

    /**
     * Interface for button listeners.
     */
    public interface Listener {
        void onPressedChanged(Button button, boolean pressed);
        void onTapped(Button button);
    }
}