From: Matti Tahvonen Date: Thu, 30 Oct 2008 08:21:39 +0000 (+0000) Subject: Cleaned Button & CheckBox code, now ensures button has initial value false. X-Git-Tag: 6.7.0.beta1~3899 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=3b48b5012a287a8e33ed4b89f1205268c8690e32;p=vaadin-framework.git Cleaned Button & CheckBox code, now ensures button has initial value false. svn changeset:5767/svn branch:trunk --- diff --git a/src/com/itmill/toolkit/ui/Button.java b/src/com/itmill/toolkit/ui/Button.java index 9e51b81576..0812f261ff 100644 --- a/src/com/itmill/toolkit/ui/Button.java +++ b/src/com/itmill/toolkit/ui/Button.java @@ -32,6 +32,7 @@ public class Button extends AbstractField { * */ public Button() { + setValue(new Boolean(false)); setSwitchMode(false); } @@ -45,8 +46,8 @@ public class Button extends AbstractField { * the Button caption. */ public Button(String caption) { + this(); setCaption(caption); - setSwitchMode(false); } /** @@ -134,14 +135,7 @@ public class Button extends AbstractField { if (isSwitchMode()) { target.addAttribute("type", "switch"); } - boolean state; - try { - state = ((Boolean) getValue()).booleanValue(); - } catch (final NullPointerException e) { - state = false; - } - target.addVariable(this, "state", state); - + target.addVariable(this, "state", booleanValue()); } /** @@ -203,7 +197,9 @@ public class Button extends AbstractField { this.switchMode = switchMode; if (!switchMode) { setImmediate(true); - setValue(new Boolean(false)); + if (booleanValue()) { + setValue(new Boolean(false)); + } } } @@ -213,7 +209,13 @@ public class Button extends AbstractField { * @return True iff the button is pressed down or checked. */ public boolean booleanValue() { - return ((Boolean) getValue()).booleanValue(); + boolean state; + try { + state = ((Boolean) getValue()).booleanValue(); + } catch (final NullPointerException e) { + state = false; + } + return state; } /** diff --git a/src/com/itmill/toolkit/ui/CheckBox.java b/src/com/itmill/toolkit/ui/CheckBox.java index 58855c9a62..cd488b403a 100644 --- a/src/com/itmill/toolkit/ui/CheckBox.java +++ b/src/com/itmill/toolkit/ui/CheckBox.java @@ -25,9 +25,7 @@ public class CheckBox extends Button { * the initial state of the switch button */ public CheckBox(String caption, boolean initialState) { - setCaption(caption); - setValue(new Boolean(initialState)); - setSwitchMode(true); + super(caption, initialState); } /** @@ -39,8 +37,7 @@ public class CheckBox extends Button { * the click listener */ public CheckBox(String caption, ClickListener listener) { - setCaption(caption); - addListener(listener); + super(caption, listener); setSwitchMode(true); } @@ -62,8 +59,7 @@ public class CheckBox extends Button { * click events. */ public CheckBox(String caption, Object target, String methodName) { - setCaption(caption); - addListener(ClickEvent.class, target, methodName); + super(caption, target, methodName); setSwitchMode(true); } @@ -75,8 +71,7 @@ public class CheckBox extends Button { * @param dataSource */ public CheckBox(String caption, Property dataSource) { - setCaption(caption); - setPropertyDataSource(dataSource); + super(caption, dataSource); setSwitchMode(true); } @@ -91,8 +86,7 @@ public class CheckBox extends Button { */ public CheckBox(String caption) { - setCaption(caption); - setSwitchMode(true); + super(caption, false); } public void setSwitchMode(boolean switchMode) @@ -101,7 +95,7 @@ public class CheckBox extends Button { throw new UnsupportedOperationException( "CheckBox is always in switch mode (consider using a Button)"); } - this.switchMode = true; + super.setSwitchMode(true); } }