123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502 |
- /*
- @VaadinApache2LicenseForJavaFiles@
- */
-
- package com.vaadin.ui;
-
- import java.io.Serializable;
- import java.lang.reflect.Method;
- import java.util.Map;
-
- import com.vaadin.event.Action;
- import com.vaadin.event.FieldEvents;
- import com.vaadin.event.FieldEvents.BlurEvent;
- import com.vaadin.event.FieldEvents.BlurListener;
- import com.vaadin.event.FieldEvents.FocusEvent;
- import com.vaadin.event.FieldEvents.FocusListener;
- import com.vaadin.event.ShortcutAction;
- import com.vaadin.event.ShortcutAction.KeyCode;
- import com.vaadin.event.ShortcutAction.ModifierKey;
- import com.vaadin.event.ShortcutListener;
- import com.vaadin.terminal.gwt.client.ComponentState;
- import com.vaadin.terminal.gwt.client.MouseEventDetails;
- import com.vaadin.terminal.gwt.client.ui.ButtonConnector;
- import com.vaadin.terminal.gwt.client.ui.ButtonConnector.ButtonServerRpc;
- import com.vaadin.terminal.gwt.client.ui.ButtonState;
- import com.vaadin.terminal.gwt.server.RpcTarget;
- import com.vaadin.tools.ReflectTools;
- import com.vaadin.ui.ClientWidget.LoadStyle;
- import com.vaadin.ui.Component.Focusable;
-
- /**
- * A generic button component.
- *
- * @author Vaadin Ltd.
- * @version
- * @VERSION@
- * @since 3.0
- */
- @SuppressWarnings("serial")
- @ClientWidget(value = ButtonConnector.class, loadStyle = LoadStyle.EAGER)
- public class Button extends AbstractComponent implements
- FieldEvents.BlurNotifier, FieldEvents.FocusNotifier, Focusable,
- Action.ShortcutNotifier, RpcTarget {
-
- /**
- * Creates a new push button.
- */
- public Button() {
- // TODO take the implementation out of an anonymous class?
- registerRpcImplementation(new ButtonServerRpc() {
- public void click(MouseEventDetails mouseEventDetails) {
- fireClick(mouseEventDetails);
- }
-
- public void disableOnClick() {
- // Could be optimized so the button is not repainted because of
- // this (client side has already disabled the button)
- setEnabled(false);
- }
- }, ButtonServerRpc.class);
- }
-
- /**
- * Creates a new push button with the given caption.
- *
- * @param caption
- * the Button caption.
- */
- public Button(String caption) {
- this();
- setCaption(caption);
- }
-
- /**
- * Creates a new push button with a click listener.
- *
- * @param caption
- * the Button caption.
- * @param listener
- * the Button click listener.
- */
- public Button(String caption, ClickListener listener) {
- this(caption);
- addListener(listener);
- }
-
- /**
- * Invoked when the value of a variable has changed. Button listeners are
- * notified if the button is clicked.
- *
- * @param source
- * @param variables
- */
- @Override
- public void changeVariables(Object source, Map<String, Object> variables) {
- super.changeVariables(source, variables);
-
- if (variables.containsKey(FocusEvent.EVENT_ID)) {
- fireEvent(new FocusEvent(this));
- }
- if (variables.containsKey(BlurEvent.EVENT_ID)) {
- fireEvent(new BlurEvent(this));
- }
- }
-
- /**
- * Click event. This event is thrown, when the button is clicked.
- *
- * @author Vaadin Ltd.
- * @version
- * @VERSION@
- * @since 3.0
- */
- public class ClickEvent extends Component.Event {
-
- private final MouseEventDetails details;
-
- /**
- * New instance of text change event.
- *
- * @param source
- * the Source of the event.
- */
- public ClickEvent(Component source) {
- super(source);
- details = null;
- }
-
- /**
- * Constructor with mouse details
- *
- * @param source
- * The source where the click took place
- * @param details
- * Details about the mouse click
- */
- public ClickEvent(Component source, MouseEventDetails details) {
- super(source);
- this.details = details;
- }
-
- /**
- * Gets the Button where the event occurred.
- *
- * @return the Source of the event.
- */
- public Button getButton() {
- return (Button) getSource();
- }
-
- /**
- * Returns the mouse position (x coordinate) when the click took place.
- * The position is relative to the browser client area.
- *
- * @return The mouse cursor x position or -1 if unknown
- */
- public int getClientX() {
- if (null != details) {
- return details.getClientX();
- } else {
- return -1;
- }
- }
-
- /**
- * Returns the mouse position (y coordinate) when the click took place.
- * The position is relative to the browser client area.
- *
- * @return The mouse cursor y position or -1 if unknown
- */
- public int getClientY() {
- if (null != details) {
- return details.getClientY();
- } else {
- return -1;
- }
- }
-
- /**
- * Returns the relative mouse position (x coordinate) when the click
- * took place. The position is relative to the clicked component.
- *
- * @return The mouse cursor x position relative to the clicked layout
- * component or -1 if no x coordinate available
- */
- public int getRelativeX() {
- if (null != details) {
- return details.getRelativeX();
- } else {
- return -1;
- }
- }
-
- /**
- * Returns the relative mouse position (y coordinate) when the click
- * took place. The position is relative to the clicked component.
- *
- * @return The mouse cursor y position relative to the clicked layout
- * component or -1 if no y coordinate available
- */
- public int getRelativeY() {
- if (null != details) {
- return details.getRelativeY();
- } else {
- return -1;
- }
- }
-
- /**
- * Checks if the Alt key was down when the mouse event took place.
- *
- * @return true if Alt was down when the event occured, false otherwise
- * or if unknown
- */
- public boolean isAltKey() {
- if (null != details) {
- return details.isAltKey();
- } else {
- return false;
- }
- }
-
- /**
- * Checks if the Ctrl key was down when the mouse event took place.
- *
- * @return true if Ctrl was pressed when the event occured, false
- * otherwise or if unknown
- */
- public boolean isCtrlKey() {
- if (null != details) {
- return details.isCtrlKey();
- } else {
- return false;
- }
- }
-
- /**
- * Checks if the Meta key was down when the mouse event took place.
- *
- * @return true if Meta was pressed when the event occured, false
- * otherwise or if unknown
- */
- public boolean isMetaKey() {
- if (null != details) {
- return details.isMetaKey();
- } else {
- return false;
- }
- }
-
- /**
- * Checks if the Shift key was down when the mouse event took place.
- *
- * @return true if Shift was pressed when the event occured, false
- * otherwise or if unknown
- */
- public boolean isShiftKey() {
- if (null != details) {
- return details.isShiftKey();
- } else {
- return false;
- }
- }
- }
-
- /**
- * Interface for listening for a {@link ClickEvent} fired by a
- * {@link Component}.
- *
- * @author Vaadin Ltd.
- * @version
- * @VERSION@
- * @since 3.0
- */
- public interface ClickListener extends Serializable {
-
- public static final Method BUTTON_CLICK_METHOD = ReflectTools
- .findMethod(ClickListener.class, "buttonClick",
- ClickEvent.class);
-
- /**
- * Called when a {@link Button} has been clicked. A reference to the
- * button is given by {@link ClickEvent#getButton()}.
- *
- * @param event
- * An event containing information about the click.
- */
- 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,
- ClickListener.BUTTON_CLICK_METHOD);
- }
-
- /**
- * Removes the button click listener.
- *
- * @param listener
- * the Listener to be removed.
- */
- public void removeListener(ClickListener listener) {
- removeListener(ClickEvent.class, listener,
- ClickListener.BUTTON_CLICK_METHOD);
- }
-
- /**
- * Fires a click event to all listeners without any event details.
- *
- * In subclasses, override {@link #fireClick(MouseEventDetails)} instead of
- * this method.
- */
- protected void fireClick() {
- fireEvent(new Button.ClickEvent(this));
- }
-
- /**
- * Fires a click event to all listeners.
- *
- * @param details
- * MouseEventDetails from which keyboard modifiers and other
- * information about the mouse click can be obtained. If the
- * button was clicked by a keyboard event, some of the fields may
- * be empty/undefined.
- */
- protected void fireClick(MouseEventDetails details) {
- fireEvent(new Button.ClickEvent(this, details));
- }
-
- public void addListener(BlurListener listener) {
- addListener(BlurEvent.EVENT_ID, BlurEvent.class, listener,
- BlurListener.blurMethod);
- }
-
- public void removeListener(BlurListener listener) {
- removeListener(BlurEvent.EVENT_ID, BlurEvent.class, listener);
- }
-
- public void addListener(FocusListener listener) {
- addListener(FocusEvent.EVENT_ID, FocusEvent.class, listener,
- FocusListener.focusMethod);
- }
-
- public void removeListener(FocusListener listener) {
- removeListener(FocusEvent.EVENT_ID, FocusEvent.class, listener);
-
- }
-
- /*
- * Actions
- */
-
- protected ClickShortcut clickShortcut;
-
- private int tabIndex = 0;
-
- /**
- * Makes it possible to invoke a click on this button by pressing the given
- * {@link KeyCode} and (optional) {@link ModifierKey}s.<br/>
- * The shortcut is global (bound to the containing Window).
- *
- * @param keyCode
- * the keycode for invoking the shortcut
- * @param modifiers
- * the (optional) modifiers for invoking the shortcut, null for
- * none
- */
- public void setClickShortcut(int keyCode, int... modifiers) {
- if (clickShortcut != null) {
- removeShortcutListener(clickShortcut);
- }
- clickShortcut = new ClickShortcut(this, keyCode, modifiers);
- addShortcutListener(clickShortcut);
- getState().setClickShortcutKeyCode(clickShortcut.getKeyCode());
- }
-
- /**
- * Removes the keyboard shortcut previously set with
- * {@link #setClickShortcut(int, int...)}.
- */
- public void removeClickShortcut() {
- if (clickShortcut != null) {
- removeShortcutListener(clickShortcut);
- clickShortcut = null;
- getState().setClickShortcutKeyCode(0);
- }
- }
-
- /**
- * A {@link ShortcutListener} specifically made to define a keyboard
- * shortcut that invokes a click on the given button.
- *
- */
- public static class ClickShortcut extends ShortcutListener {
- protected Button button;
-
- /**
- * Creates a keyboard shortcut for clicking the given button using the
- * shorthand notation defined in {@link ShortcutAction}.
- *
- * @param button
- * to be clicked when the shortcut is invoked
- * @param shorthandCaption
- * the caption with shortcut keycode and modifiers indicated
- */
- public ClickShortcut(Button button, String shorthandCaption) {
- super(shorthandCaption);
- this.button = button;
- }
-
- /**
- * Creates a keyboard shortcut for clicking the given button using the
- * given {@link KeyCode} and {@link ModifierKey}s.
- *
- * @param button
- * to be clicked when the shortcut is invoked
- * @param keyCode
- * KeyCode to react to
- * @param modifiers
- * optional modifiers for shortcut
- */
- public ClickShortcut(Button button, int keyCode, int... modifiers) {
- super(null, keyCode, modifiers);
- this.button = button;
- }
-
- /**
- * Creates a keyboard shortcut for clicking the given button using the
- * given {@link KeyCode}.
- *
- * @param button
- * to be clicked when the shortcut is invoked
- * @param keyCode
- * KeyCode to react to
- */
- public ClickShortcut(Button button, int keyCode) {
- this(button, keyCode, null);
- }
-
- @Override
- public void handleAction(Object sender, Object target) {
- if (button.isEnabled() && !button.isReadOnly()) {
- button.fireClick();
- }
- }
- }
-
- /**
- * Determines if a button is automatically disabled when clicked. See
- * {@link #setDisableOnClick(boolean)} for details.
- *
- * @return true if the button is disabled when clicked, false otherwise
- */
- public boolean isDisableOnClick() {
- return getState().isDisableOnClick();
- }
-
- /**
- * Determines if a button is automatically disabled when clicked. If this is
- * set to true the button will be automatically disabled when clicked,
- * typically to prevent (accidental) extra clicks on a button.
- *
- * @param disableOnClick
- * true to disable button when it is clicked, false otherwise
- */
- public void setDisableOnClick(boolean disableOnClick) {
- getState().setDisableOnClick(disableOnClick);
- requestRepaint();
- }
-
- public int getTabIndex() {
- return tabIndex;
- }
-
- public void setTabIndex(int tabIndex) {
- this.tabIndex = tabIndex;
-
- }
-
- @Override
- public void focus() {
- // Overridden only to make public
- super.focus();
- }
-
- @Override
- protected ComponentState createState() {
- return new ButtonState();
- }
-
- @Override
- public ButtonState getState() {
- return (ButtonState) super.getState();
- }
- }
|