From: Artur Signell Date: Mon, 18 Jul 2011 13:08:21 +0000 (+0000) Subject: #6444 Ability to disable button on click X-Git-Tag: 6.7.0.beta1~182 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=1bbad422177eac5a795e0b63cfbd788f9e99d864;p=vaadin-framework.git #6444 Ability to disable button on click svn changeset:19820/svn branch:6.7 --- diff --git a/src/com/vaadin/terminal/gwt/client/ui/VButton.java b/src/com/vaadin/terminal/gwt/client/ui/VButton.java index 46492a707c..1cb2a92e2e 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VButton.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VButton.java @@ -35,6 +35,8 @@ public class VButton extends FocusWidget implements Paintable, ClickHandler, public static final String CLASSNAME = "v-button"; private static final String CLASSNAME_PRESSED = "v-pressed"; + public static final String ATTR_DISABLE_ON_CLICK = "dc"; + // mouse movement is checked before synthesizing click event on mouseout protected static int MOVE_THRESHOLD = 3; protected int mousedownX = 0; @@ -63,6 +65,8 @@ public class VButton extends FocusWidget implements Paintable, ClickHandler, private int tabIndex = 0; + private boolean disableOnClick = false; + /* * BELOW PRIVATE MEMBERS COPY-PASTED FROM GWT CustomButton */ @@ -129,6 +133,8 @@ public class VButton extends FocusWidget implements Paintable, ClickHandler, // Set text setText(uidl.getStringAttribute("caption")); + disableOnClick = uidl.hasAttribute(ATTR_DISABLE_ON_CLICK); + // handle error if (uidl.hasAttribute("error")) { if (errorIndicatorElement == null) { @@ -354,6 +360,11 @@ public class VButton extends FocusWidget implements Paintable, ClickHandler, if (BrowserInfo.get().isSafari()) { VButton.this.setFocus(true); } + if (disableOnClick) { + setEnabled(false); + client.updateVariable(id, "disabledOnClick", true, false); + } + client.updateVariable(id, "state", true, false); // Add mouse details diff --git a/src/com/vaadin/terminal/gwt/client/ui/VNativeButton.java b/src/com/vaadin/terminal/gwt/client/ui/VNativeButton.java index 49b878f611..8ea2e1a688 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VNativeButton.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VNativeButton.java @@ -54,6 +54,8 @@ public class VNativeButton extends Button implements Paintable, ClickHandler, private HandlerRegistration focusHandlerRegistration; private HandlerRegistration blurHandlerRegistration; + private boolean disableOnClick = false; + public VNativeButton() { setStyleName(CLASSNAME); @@ -75,6 +77,8 @@ public class VNativeButton extends Button implements Paintable, ClickHandler, return; } + disableOnClick = uidl.hasAttribute(VButton.ATTR_DISABLE_ON_CLICK); + focusHandlerRegistration = EventHelper.updateFocusHandler(this, client, focusHandlerRegistration); blurHandlerRegistration = EventHelper.updateBlurHandler(this, client, @@ -201,6 +205,10 @@ public class VNativeButton extends Button implements Paintable, ClickHandler, if (BrowserInfo.get().isSafari()) { VNativeButton.this.setFocus(true); } + if (disableOnClick) { + setEnabled(false); + client.updateVariable(id, "disabledOnClick", true, false); + } // Add mouse details MouseEventDetails details = new MouseEventDetails( diff --git a/src/com/vaadin/ui/Button.java b/src/com/vaadin/ui/Button.java index 122d155e0f..16314f94c3 100644 --- a/src/com/vaadin/ui/Button.java +++ b/src/com/vaadin/ui/Button.java @@ -43,6 +43,8 @@ public class Button extends AbstractField implements FieldEvents.BlurNotifier, boolean switchMode = false; + boolean disableOnClick = false; + /** * Creates a new push button. The value of the push button is false and it * is immediate by default. @@ -148,6 +150,9 @@ public class Button extends AbstractField implements FieldEvents.BlurNotifier, } target.addVariable(this, "state", booleanValue()); + if (isDisableOnClick()) { + target.addAttribute(VButton.ATTR_DISABLE_ON_CLICK, true); + } if (clickShortcut != null) { target.addAttribute("keycode", clickShortcut.getKeyCode()); } @@ -164,6 +169,12 @@ public class Button extends AbstractField implements FieldEvents.BlurNotifier, public void changeVariables(Object source, Map variables) { super.changeVariables(source, variables); + if (variables.containsKey("disabledOnClick")) { + // Could be optimized so the button is not repainted because of this + // (client side has already disabled the button) + setEnabled(false); + } + if (!isReadOnly() && variables.containsKey("state")) { // Gets the new and old button states final Boolean newValue = (Boolean) variables.get("state"); @@ -651,4 +662,27 @@ public class Button extends AbstractField implements FieldEvents.BlurNotifier, } } + /** + * 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 disableOnClick; + } + + /** + * 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) { + this.disableOnClick = disableOnClick; + requestRepaint(); + } + } diff --git a/src/com/vaadin/ui/CheckBox.java b/src/com/vaadin/ui/CheckBox.java index 93138e10c0..b9ae236cb0 100644 --- a/src/com/vaadin/ui/CheckBox.java +++ b/src/com/vaadin/ui/CheckBox.java @@ -104,4 +104,10 @@ public class CheckBox extends Button { super.setSwitchMode(true); } + @Override + public void setDisableOnClick(boolean disableOnClick) { + throw new UnsupportedOperationException( + "CheckBox does not support disable on click"); + } + } diff --git a/tests/src/com/vaadin/tests/components/button/ButtonDisableOnClick.html b/tests/src/com/vaadin/tests/components/button/ButtonDisableOnClick.html new file mode 100644 index 0000000000..850d553b5b --- /dev/null +++ b/tests/src/com/vaadin/tests/components/button/ButtonDisableOnClick.html @@ -0,0 +1,157 @@ + + + + + + +New Test + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
New Test
open/run/com.vaadin.tests.components.button.Buttons2?restartApplication
mouseClickvaadin=runcomvaadintestscomponentsbuttonButtons2::PID_Smenu#item037,10
mouseClickvaadin=runcomvaadintestscomponentsbuttonButtons2::Root/VOverlay[0]/VMenuBar[0]#item328,7
mouseClickvaadin=runcomvaadintestscomponentsbuttonButtons2::Root/VOverlay[1]/VMenuBar[0]#item435,8
mouseClickvaadin=runcomvaadintestscomponentsbuttonButtons2::PID_Smenu#item039,10
mouseClickvaadin=runcomvaadintestscomponentsbuttonButtons2::Root/VOverlay[0]/VMenuBar[0]#item433,7
mouseClickvaadin=runcomvaadintestscomponentsbuttonButtons2::Root/VOverlay[1]/VMenuBar[0]#item122,4
assertTextvaadin=runcomvaadintestscomponentsbuttonButtons2::PID_SLog_row_01. Command: /Disable on click(true)
clickvaadin=runcomvaadintestscomponentsbuttonButtons2::PID_StestComponent/domChild[0]/domChild[0]
assertCSSClassvaadin=runcomvaadintestscomponentsbuttonButtons2::PID_StestComponentv-disabled
assertTextvaadin=runcomvaadintestscomponentsbuttonButtons2::PID_SLog_row_02. ClickEvent
clickvaadin=runcomvaadintestscomponentsbuttonButtons2::PID_StestComponent/domChild[0]/domChild[0]
assertTextvaadin=runcomvaadintestscomponentsbuttonButtons2::PID_SLog_row_02. ClickEvent
mouseClickvaadin=runcomvaadintestscomponentsbuttonButtons2::PID_Smenu#item035,13
mouseClickvaadin=runcomvaadintestscomponentsbuttonButtons2::Root/VOverlay[0]/VMenuBar[0]#item034,12
mouseClickvaadin=runcomvaadintestscomponentsbuttonButtons2::Root/VOverlay[1]/VMenuBar[0]#item115,13
mouseClickvaadin=runcomvaadintestscomponentsbuttonButtons2::PID_Smenu#item043,4
mouseClickvaadin=runcomvaadintestscomponentsbuttonButtons2::Root/VOverlay[0]/VMenuBar[0]#item037,8
mouseClickvaadin=runcomvaadintestscomponentsbuttonButtons2::Root/VOverlay[1]/VMenuBar[0]#item133,6
assertTextvaadin=runcomvaadintestscomponentsbuttonButtons2::PID_SLog_row_04. Command: /Enabled(true)
assertNotCSSClassvaadin=runcomvaadintestscomponentsbuttonButtons2::PID_StestComponentv-disabled
mouseClickvaadin=runcomvaadintestscomponentsbuttonButtons2::PID_Smenu#item025,9
mouseClickvaadin=runcomvaadintestscomponentsbuttonButtons2::Root/VOverlay[0]/VMenuBar[0]#item439,13
mouseClickvaadin=runcomvaadintestscomponentsbuttonButtons2::Root/VOverlay[1]/VMenuBar[0]#item136,3
clickvaadin=runcomvaadintestscomponentsbuttonButtons2::PID_StestComponent/domChild[0]/domChild[0]
assertTextvaadin=runcomvaadintestscomponentsbuttonButtons2::PID_SLog_row_06. ClickEvent
assertNotCSSClassvaadin=runcomvaadintestscomponentsbuttonButtons2::PID_StestComponentv-disabled
+ + diff --git a/tests/src/com/vaadin/tests/components/button/Buttons2.java b/tests/src/com/vaadin/tests/components/button/Buttons2.java index 491e19919c..c18e736082 100644 --- a/tests/src/com/vaadin/tests/components/button/Buttons2.java +++ b/tests/src/com/vaadin/tests/components/button/Buttons2.java @@ -19,6 +19,13 @@ public class Buttons2 extends AbstractFieldTest implements } }; + private Command disableOnClickCommand = new Command() { + + public void execute(T c, Boolean value, Object data) { + c.setDisableOnClick(value); + } + }; + private Command clickListenerCommand = new Command() { public void execute(T c, Boolean value, Object data) { @@ -42,6 +49,8 @@ public class Buttons2 extends AbstractFieldTest implements createBooleanAction("Switch mode", CATEGORY_FEATURES, false, switchModeCommand); + createBooleanAction("Disable on click", CATEGORY_FEATURES, false, + disableOnClickCommand); addClickListener(CATEGORY_LISTENERS); } diff --git a/tests/src/com/vaadin/tests/components/nativebutton/NativeButtonDisableOnClick.html b/tests/src/com/vaadin/tests/components/nativebutton/NativeButtonDisableOnClick.html new file mode 100644 index 0000000000..75eab71892 --- /dev/null +++ b/tests/src/com/vaadin/tests/components/nativebutton/NativeButtonDisableOnClick.html @@ -0,0 +1,156 @@ + + + + + + +New Test + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
New Test
open/run/com.vaadin.tests.components.nativebutton.NativeButtonTest?restartApplication
mouseClickvaadin=runcomvaadintestscomponentsnativebuttonNativeButtonTest::PID_Smenu#item037,10
mouseClickvaadin=runcomvaadintestscomponentsnativebuttonNativeButtonTest::Root/VOverlay[0]/VMenuBar[0]#item328,7
mouseClickvaadin=runcomvaadintestscomponentsnativebuttonNativeButtonTest::Root/VOverlay[1]/VMenuBar[0]#item435,8
mouseClickvaadin=runcomvaadintestscomponentsnativebuttonNativeButtonTest::PID_Smenu#item039,10
mouseClickvaadin=runcomvaadintestscomponentsnativebuttonNativeButtonTest::Root/VOverlay[0]/VMenuBar[0]#item433,7
mouseClickvaadin=runcomvaadintestscomponentsnativebuttonNativeButtonTest::Root/VOverlay[1]/VMenuBar[0]#item122,4
assertTextvaadin=runcomvaadintestscomponentsnativebuttonNativeButtonTest::PID_SLog_row_01. Command: /Disable on click(true)
clickvaadin=runcomvaadintestscomponentsnativebuttonNativeButtonTest::PID_StestComponent
assertCSSClassvaadin=runcomvaadintestscomponentsnativebuttonNativeButtonTest::PID_StestComponentv-disabled
assertTextvaadin=runcomvaadintestscomponentsnativebuttonNativeButtonTest::PID_SLog_row_02. ClickEvent
clickvaadin=runcomvaadintestscomponentsnativebuttonNativeButtonTest::PID_StestComponent
assertTextvaadin=runcomvaadintestscomponentsnativebuttonNativeButtonTest::PID_SLog_row_02. ClickEvent
mouseClickvaadin=runcomvaadintestscomponentsnativebuttonNativeButtonTest::PID_Smenu#item035,13
mouseClickvaadin=runcomvaadintestscomponentsnativebuttonNativeButtonTest::Root/VOverlay[0]/VMenuBar[0]#item034,12
mouseClickvaadin=runcomvaadintestscomponentsnativebuttonNativeButtonTest::Root/VOverlay[1]/VMenuBar[0]#item115,13
mouseClickvaadin=runcomvaadintestscomponentsnativebuttonNativeButtonTest::PID_Smenu#item043,4
mouseClickvaadin=runcomvaadintestscomponentsnativebuttonNativeButtonTest::Root/VOverlay[0]/VMenuBar[0]#item037,8
mouseClickvaadin=runcomvaadintestscomponentsnativebuttonNativeButtonTest::Root/VOverlay[1]/VMenuBar[0]#item133,6
assertTextvaadin=runcomvaadintestscomponentsnativebuttonNativeButtonTest::PID_SLog_row_04. Command: /Enabled(true)
assertNotCSSClassvaadin=runcomvaadintestscomponentsnativebuttonNativeButtonTest::PID_StestComponentv-disabled
mouseClickvaadin=runcomvaadintestscomponentsnativebuttonNativeButtonTest::PID_Smenu#item025,9
mouseClickvaadin=runcomvaadintestscomponentsnativebuttonNativeButtonTest::Root/VOverlay[0]/VMenuBar[0]#item439,13
mouseClickvaadin=runcomvaadintestscomponentsnativebuttonNativeButtonTest::Root/VOverlay[1]/VMenuBar[0]#item136,3
clickvaadin=runcomvaadintestscomponentsnativebuttonNativeButtonTest::PID_StestComponent
assertTextvaadin=runcomvaadintestscomponentsnativebuttonNativeButtonTest::PID_SLog_row_06. ClickEvent
assertNotCSSClassvaadin=runcomvaadintestscomponentsnativebuttonNativeButtonTest::PID_StestComponentv-disabled
+ +