/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
package com.sun.dtv.lwuit;
import com.sun.dtv.lwuit.geom.Dimension;
import com.sun.dtv.lwuit.events.ActionEvent;
import com.sun.dtv.lwuit.events.ActionListener;
import com.sun.dtv.lwuit.plaf.Border;
import com.sun.dtv.lwuit.plaf.UIManager;
/**
* Button is the base class for several UI widgets allowing clickability.
* It has 3 states: rollover, pressed and the default state it
* can also have ActionListeners that react when the Button is clicked.
*
* @author Chen Fishbein
*/
public class Button extends Label {
/**
* Indicates the rollover state of a button which is equivalent to focused for
* most uses
*/
public static final int STATE_ROLLOVER = 0;
/**
* Indicates the pressed state of a button
*/
public static final int STATE_PRESSED = 1;
/**
* Indicates the default state of a button which is neither pressed nor focused
*/
public static final int STATE_DEAFULT = 2;
private static String id = "Button";
private EventDispatcher dispatcher = new EventDispatcher();
private int state = STATE_DEAFULT;
private Image pressedIcon;
private Image rolloverIcon;
private Command cmd;
/**
* Constructs a button with an empty string for its text.
*/
public Button() {
this("");
}
/**
* Constructs a button with the specified text.
*
* @param text label appearing on the button
*/
public Button(String text) {
this(text, null);
}
/**
* Allows binding a command to a button for ease of use
*
* @param cmd command whose text would be used for the button and would recive action events
* from the button
*/
public Button(Command cmd) {
this(cmd.getCommandName(), cmd.getIcon());
addActionListener(cmd);
this.cmd = cmd;
}
/**
* Constructs a button with the specified image.
*
* @param icon appearing on the button
*/
public Button(Image icon) {
this("", icon);
}
/**
* Constructor a button with text and image
*
* @param text label appearing on the button
* @param icon image appearing on the button
*/
public Button(String text, Image icon) {
super(text);
setFocusable(true);
setIcon(icon);
this.pressedIcon = icon;
this.rolloverIcon = icon;
}
/**
* @inheritDoc
*/
void focusGainedInternal() {
state = STATE_ROLLOVER;
}
/**
* @inheritDoc
*/
void focusLostInternal() {
state = STATE_DEAFULT;
}
/**
* @inheritDoc
*/
protected String getUIID() {
return id;
}
/**
* Returns the button state
*
* @return One of STATE_ROLLOVER, STATE_DEAFULT, STATE_PRESSED
*/
public int getState() {
return state;
}
/**
* Indicates the icon that is displayed on the button when the button is in
* pressed state
*
* @return icon used
* @see #STATE_PRESSED
*/
public Image getPressedIcon() {
return pressedIcon;
}
/**
* Indicates the icon that is displayed on the button when the button is in
* rolled over state
*
* @return icon used
* @see #STATE_ROLLOVER
*/
public Image getRolloverIcon() {
return rolloverIcon;
}
/**
* Indicates the icon that is displayed on the button when the button is in
* rolled over state
*
* @param rolloverIcon icon to use
* @see #STATE_ROLLOVER
*/
public void setRolloverIcon(Image rolloverIcon) {
this.rolloverIcon = rolloverIcon;
setShouldCalcPreferredSize(true);
repaint();
}
/**
* Indicates the icon that is displayed on the button when the button is in
* pressed state
*
* @param pressedIcon icon used
* @see #STATE_PRESSED
*/
public void setPressedIcon(Image pressedIcon) {
this.pressedIcon = pressedIcon;
setShouldCalcPreferredSize(true);
repaint();
}
/**
* Adds a listener to the button which will cause an event to dispatch on click
*
* @param l implementation of the action listener interface
*/
public void addActionListener(ActionListener l){
dispatcher.addListener(l);
}
/**
* Removes the given action listener from the button
*
* @param l implementation of the action listener interface
*/
public void removeActionListener(ActionListener l){
dispatcher.removeListener(l);
}
/**
* @inheritDoc
*/
void fireActionEvent(){
super.fireActionEvent();
if(cmd != null) {
dispatcher.fireActionEvent(new ActionEvent(cmd));
} else {
dispatcher.fireActionEvent(new ActionEvent(this));
}
}
/**
* Invoked to change the state of the button to the pressed state
*/
void pressed(){
if(isEnabled()) {
state=STATE_PRESSED;
repaint();
}
}
/**
* Invoked to change the state of the button to the released state
*/
void released(){
if(isEnabled()) {
state=STATE_ROLLOVER;
repaint();
fireActionEvent();
}
}
/**
* @inheritDoc
*/
public void keyPressed(int keyCode) {
if (Display.getInstance().getGameAction(keyCode) == Display.GAME_FIRE){
pressed();
}
}
/**
* @inheritDoc
*/
public void keyReleased(int keyCode) {
if (Display.getInstance().getGameAction(keyCode) == Display.GAME_FIRE){
released();
}
}
/**
* @inheritDoc
*/
protected void fireClicked() {
pressed();
released();
}
/**
* @inheritDoc
*/
protected boolean isSelectableInteraction() {
return true;
}
/**
* @inheritDoc
*/
public void pointerPressed(int x, int y) {
pressed();
}
/**
* @inheritDoc
*/
public void pointerReleased(int x, int y) {
released();
}
/**
* @inheritDoc
*/
public void paint(Graphics g) {
UIManager.getInstance().getLookAndFeel().drawButton(g, this);
}
/**
* @inheritDoc
*/
protected Dimension calcPreferredSize(){
return UIManager.getInstance().getLookAndFeel().getButtonPreferredSize(this);
}
/**
* @inheritDoc
*/
protected Border getBorder() {
if(getState() != STATE_PRESSED){
return super.getBorder();
} else {
return getStyle().getBorder().createPressedVersion();
}
}
public String toString(){
return this.getText();
}
}