123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- /*
- @ITMillApache2LicenseForJavaFiles@
- */
-
- package com.itmill.toolkit.ui;
-
- import java.io.IOException;
- import java.lang.reflect.Method;
- import java.util.Map;
-
- import com.itmill.toolkit.data.Property;
- import com.itmill.toolkit.terminal.PaintException;
- import com.itmill.toolkit.terminal.PaintTarget;
-
- /**
- * A generic button component.
- *
- * @author IT Mill Ltd.
- * @version
- * @VERSION@
- * @since 3.0
- */
- public class Button extends AbstractField {
-
- /* Private members */
-
- boolean switchMode = false;
-
- /**
- * Creates a new push button. The value of the push button is allways false
- * and they are immediate by default.
- *
- */
- public Button() {
- setSwitchMode(false);
- }
-
- /**
- * Creates a new push button.
- *
- * The value of the push button is allways false and they are immediate by
- * default.
- *
- * @param caption
- * the Button caption.
- */
- public Button(String caption) {
- setCaption(caption);
- setSwitchMode(false);
- }
-
- /**
- * Creates a new push button with click listener.
- *
- * @param caption
- * the Button caption.
- * @param listener
- * the Button click listener.
- */
- public Button(String caption, ClickListener listener) {
- this(caption);
- addListener(listener);
- }
-
- /**
- * Creates a new push button with a method listening button clicks. Using
- * this method is discouraged because it cannot be checked during
- * compilation. Use
- * {@link #Button(String, com.itmill.toolkit.ui.Button.ClickListener)}
- * instead. The method must have either no parameters, or only one parameter
- * of Button.ClickEvent type.
- *
- * @param caption
- * the Button caption.
- * @param target
- * the Object having the method for listening button clicks.
- * @param methodName
- * the name of the method in target object, that receives button
- * click events.
- */
- public Button(String caption, Object target, String methodName) {
- this(caption);
- addListener(ClickEvent.class, target, methodName);
- }
-
- /**
- * Creates a new switch button with initial value.
- *
- * @param state
- * the Initial state of the switch-button.
- * @param initialState
- */
- public Button(String caption, boolean initialState) {
- setCaption(caption);
- setValue(new Boolean(initialState));
- setSwitchMode(true);
- }
-
- /**
- * Creates a new switch button that is connected to a boolean property.
- *
- * @param state
- * the Initial state of the switch-button.
- * @param dataSource
- */
- public Button(String caption, Property dataSource) {
- setCaption(caption);
- setSwitchMode(true);
- setPropertyDataSource(dataSource);
- }
-
- /**
- * Gets component UIDL tag.
- *
- * @return the Component UIDL tag as string.
- */
- public String getTag() {
- return "button";
- }
-
- /**
- * Paints the content of this component.
- *
- * @param event
- * the PaintEvent.
- * @throws IOException
- * if the writing failed due to input/output error.
- * @throws PaintException
- * if the paint operation failed.
- */
- public void paintContent(PaintTarget target) throws PaintException {
- super.paintContent(target);
-
- if (isSwitchMode()) {
- target.addAttribute("type", "switch");
- }
- boolean state;
- try {
- state = ((Boolean) getValue()).booleanValue();
- } catch (final NullPointerException e) {
- state = false;
- }
- target.addVariable(this, "state", state);
-
- }
-
- /**
- * Invoked when the value of a variable has changed. Button listeners are
- * notified if the button is clicked.
- *
- * @param source
- * @param variables
- */
- public void changeVariables(Object source, Map variables) {
- super.changeVariables(source, variables);
-
- if (!isReadOnly() && variables.containsKey("state")) {
- // Gets the new and old button states
- final Boolean newValue = (Boolean) variables.get("state");
- final Boolean oldValue = (Boolean) getValue();
-
- if (isSwitchMode()) {
-
- // For switch button, the event is only sent if the
- // switch state is changed
- if (newValue != null && !newValue.equals(oldValue)
- && !isReadOnly()) {
- setValue(newValue);
- fireClick();
- }
- } else {
-
- // Only send click event if the button is pushed
- if (newValue.booleanValue()) {
- fireClick();
- }
-
- // If the button is true for some reason, release it
- if (oldValue.booleanValue()) {
- setValue(new Boolean(false));
- }
- }
- }
- }
-
- /**
- * Checks if it is switchMode.
- *
- * @return <code>true</code> if it is in Switch Mode, otherwise
- * <code>false</code>.
- */
- public boolean isSwitchMode() {
- return switchMode;
- }
-
- /**
- * Sets the switchMode.
- *
- * @param switchMode
- * The switchMode to set.
- */
- public void setSwitchMode(boolean switchMode) {
- this.switchMode = switchMode;
- if (!switchMode) {
- setImmediate(true);
- setValue(new Boolean(false));
- }
- }
-
- /**
- * Get the boolean value of the button state.
- *
- * @return True iff the button is pressed down or checked.
- */
- public boolean booleanValue() {
- return ((Boolean) getValue()).booleanValue();
- }
-
- /**
- * Sets immediate mode. Push buttons can not be set in non-immediate mode.
- *
- * @see com.itmill.toolkit.ui.AbstractComponent#setImmediate(boolean)
- */
- public void setImmediate(boolean immediate) {
- // Push buttons are always immediate
- super.setImmediate(!isSwitchMode() || immediate);
- }
-
- /**
- * The type of the button as a property.
- *
- * @see com.itmill.toolkit.data.Property#getType()
- */
- public Class getType() {
- return Boolean.class;
- }
-
- /* Click event */
-
- private static final Method BUTTON_CLICK_METHOD;
-
- /* Button style with no decorations. Looks like a link, acts like a button */
- public static final String STYLE_LINK = "link";
-
- static {
- try {
- BUTTON_CLICK_METHOD = ClickListener.class.getDeclaredMethod(
- "buttonClick", new Class[] { ClickEvent.class });
- } catch (final java.lang.NoSuchMethodException e) {
- // This should never happen
- throw new java.lang.RuntimeException(
- "Internal error finding methods in Button");
- }
- }
-
- /**
- * Click event. This event is thrown, when the button is clicked.
- *
- * @author IT Mill Ltd.
- * @version
- * @VERSION@
- * @since 3.0
- */
- public class ClickEvent extends Component.Event {
-
- /**
- * Serial generated by eclipse.
- */
- private static final long serialVersionUID = 3546647602931118393L;
-
- /**
- * New instance of text change event.
- *
- * @param source
- * the Source of the event.
- */
- public ClickEvent(Component source) {
- super(source);
- }
-
- /**
- * Gets the Button where the event occurred.
- *
- * @return the Source of the event.
- */
- public Button getButton() {
- return (Button) getSource();
- }
- }
-
- /**
- * Button click listener
- *
- * @author IT Mill Ltd.
- * @version
- * @VERSION@
- * @since 3.0
- */
- public interface ClickListener {
-
- /**
- * Button has been pressed.
- *
- * @param event
- * Button click event.
- */
- public void buttonClick(ClickEvent event);
- }
-
- /**
- * Adds the button click listener.
- *
- * @param listener
- * the Listener to be added.
- */
- public void addListener(ClickListener listener) {
- addListener(ClickEvent.class, listener, BUTTON_CLICK_METHOD);
- }
-
- /**
- * Removes the button click listener.
- *
- * @param listener
- * the Listener to be removed.
- */
- public void removeListener(ClickListener listener) {
- removeListener(ClickEvent.class, listener, BUTTON_CLICK_METHOD);
- }
-
- /**
- * Emits the options change event.
- */
- protected void fireClick() {
- fireEvent(new Button.ClickEvent(this));
- }
-
- }
|