Stephen Haberman has uploaded a new change for review.

  https://gwt-review.googlesource.com/3010


Change subject: Have SimpleCheckBox implement HasValue, fixes 4018.
......................................................................

Have SimpleCheckBox implement HasValue, fixes 4018.

Change-Id: I790120198cb22ff38e676a303782b719f87083c6
---
M user/src/com/google/gwt/user/client/ui/SimpleCheckBox.java
M user/test/com/google/gwt/user/client/ui/SimpleCheckBoxTest.java
2 files changed, 93 insertions(+), 2 deletions(-)



diff --git a/user/src/com/google/gwt/user/client/ui/SimpleCheckBox.java b/user/src/com/google/gwt/user/client/ui/SimpleCheckBox.java
index 4975554..9007b0d 100644
--- a/user/src/com/google/gwt/user/client/ui/SimpleCheckBox.java
+++ b/user/src/com/google/gwt/user/client/ui/SimpleCheckBox.java
@@ -21,7 +21,11 @@
 import com.google.gwt.editor.client.IsEditor;
 import com.google.gwt.editor.client.LeafValueEditor;
 import com.google.gwt.editor.client.adapters.TakesValueEditor;
-import com.google.gwt.user.client.TakesValue;
+import com.google.gwt.event.dom.client.ClickEvent;
+import com.google.gwt.event.dom.client.ClickHandler;
+import com.google.gwt.event.logical.shared.ValueChangeEvent;
+import com.google.gwt.event.logical.shared.ValueChangeHandler;
+import com.google.gwt.event.shared.HandlerRegistration;

 /**
  * A simple checkbox widget, with no label.
@@ -33,7 +37,7 @@
  * </ul>
  */
 public class SimpleCheckBox extends FocusWidget implements HasName,
-    TakesValue<Boolean>, IsEditor<LeafValueEditor<Boolean>> {
+    HasValue<Boolean>, IsEditor<LeafValueEditor<Boolean>> {

   /**
    * Creates a SimpleCheckBox widget that wraps an existing &lt;input
@@ -59,6 +63,7 @@
   }

   private LeafValueEditor<Boolean> editor;
+  private boolean valueChangeHandlerInitialized;

   /**
    * Creates a new simple checkbox.
@@ -84,6 +89,17 @@
     if (styleName != null) {
       setStyleName(styleName);
     }
+  }
+
+  @Override
+  public HandlerRegistration addValueChangeHandler(
+      ValueChangeHandler<Boolean> handler) {
+    // Is this the first value change handler? If so, time to add handlers
+    if (!valueChangeHandlerInitialized) {
+      ensureDomEventHandlers();
+      valueChangeHandlerInitialized = true;
+    }
+    return addHandler(handler, ValueChangeEvent.getType());
   }

   public LeafValueEditor<Boolean> asEditor() {
@@ -187,12 +203,45 @@
    * @param value true to check, false to uncheck; null value implies false
    */
   public void setValue(Boolean value) {
+    setValue(value, false);
+  }
+
+  /**
+   * Checks or unchecks the check box, firing {@link ValueChangeEvent} if
+   * appropriate.
+   * <p>
+ * Note that this <em>does not</em> set the value property of the checkbox
+   * input element wrapped by this widget. For access to that property, see
+   * {@link #setFormValue(String)}
+   *
+   * @param value true to check, false to uncheck; null value implies false
+   * @param fireEvents If true, and value has changed, fire a
+   *          {@link ValueChangeEvent}
+   */
+  @Override
+  public void setValue(Boolean value, boolean fireEvents) {
     if (value == null) {
       value = Boolean.FALSE;
     }

+    Boolean oldValue = getValue();
     getInputElement().setChecked(value);
     getInputElement().setDefaultChecked(value);
+    if (value.equals(oldValue)) {
+      return;
+    }
+    if (fireEvents) {
+      ValueChangeEvent.fire(this, value);
+    }
+  }
+
+  protected void ensureDomEventHandlers() {
+    addClickHandler(new ClickHandler() {
+      @Override
+      public void onClick(ClickEvent event) {
+        ValueChangeEvent.fire(SimpleCheckBox.this, getValue());
+      }
+    });
   }

   /**
diff --git a/user/test/com/google/gwt/user/client/ui/SimpleCheckBoxTest.java b/user/test/com/google/gwt/user/client/ui/SimpleCheckBoxTest.java
index 7df2ec6..75bed2f 100644
--- a/user/test/com/google/gwt/user/client/ui/SimpleCheckBoxTest.java
+++ b/user/test/com/google/gwt/user/client/ui/SimpleCheckBoxTest.java
@@ -15,6 +15,9 @@
  */
 package com.google.gwt.user.client.ui;

+import com.google.gwt.dom.client.ButtonElement;
+import com.google.gwt.event.logical.shared.ValueChangeEvent;
+import com.google.gwt.event.logical.shared.ValueChangeHandler;
 import com.google.gwt.junit.client.GWTTestCase;

 /**
@@ -22,11 +25,20 @@
  */
 public class SimpleCheckBoxTest extends GWTTestCase {

+  private static class Handler implements ValueChangeHandler<Boolean> {
+    Boolean received = null;
+
+    public void onValueChange(ValueChangeEvent<Boolean> event) {
+      received = event.getValue();
+    }
+  }
+
   @Override
   public String getModuleName() {
     return "com.google.gwt.user.UserTest";
   }

+  @SuppressWarnings("deprecation")
   public void testProperties() {
     SimpleCheckBox checkbox = new SimpleCheckBox();

@@ -50,4 +62,34 @@
     RootPanel.get().remove(checkbox);
     assertEquals(true, checkbox.isChecked());
   }
+
+  @SuppressWarnings("deprecation")
+  public void testValueChangeEvent() {
+    SimpleCheckBox cb = new SimpleCheckBox();
+    Handler h = new Handler();
+    cb.addValueChangeHandler(h);
+    RootPanel.get().add(cb);
+
+    cb.setChecked(false);
+    assertNull(h.received);
+    cb.setChecked(true);
+    assertNull(h.received);
+
+    cb.setValue(false);
+    assertNull(h.received);
+    cb.setValue(true);
+    assertNull(h.received);
+
+    cb.setValue(true, true);
+    assertNull(h.received);
+
+    cb.setValue(false, true);
+    assertFalse(h.received);
+
+    cb.setValue(true, true);
+    assertTrue(h.received);
+
+    cb.getElement().<ButtonElement> cast().click();
+    assertFalse(h.received);
+  }
 }

--
To view, visit https://gwt-review.googlesource.com/3010
To unsubscribe, visit https://gwt-review.googlesource.com/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I790120198cb22ff38e676a303782b719f87083c6
Gerrit-PatchSet: 1
Gerrit-Project: gwt
Gerrit-Branch: master
Gerrit-Owner: Stephen Haberman <[email protected]>

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors
--- You received this message because you are subscribed to the Google Groups "GWT Contributors" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to