Reviewers: rjrjr,

Description:
Allow DateBox to fire events with null date values for invalid and empty
input
strings.


Please review this at http://gwt-code-reviews.appspot.com/1552803/

Affected files:
  M user/src/com/google/gwt/user/datepicker/client/DateBox.java


Index: user/src/com/google/gwt/user/datepicker/client/DateBox.java
===================================================================
--- user/src/com/google/gwt/user/datepicker/client/DateBox.java (revision 10654) +++ user/src/com/google/gwt/user/datepicker/client/DateBox.java (working copy)
@@ -229,7 +229,7 @@
     }

     public void onValueChange(ValueChangeEvent<Date> event) {
-      setValue(parseDate(false), event.getValue(), true);
+      setValue(parseDate(false), event.getValue(), true, true);
       hideDatePicker();
       preventDatePickerPopup();
       box.setFocus(true);
@@ -252,6 +252,7 @@
   private LeafValueEditor<Date> editor;
   private Format format;
   private boolean allowDPShow = true;
+  private boolean fireNullValues = false;

   /**
    * Create a date box with a new {@link DatePicker}.
@@ -325,6 +326,14 @@
   }

   /**
+ * Returns true iff the date box will fire {@code ValueChangeEvents} with a
+   * date value of {@code null} for invalid or empty string values.
+   */
+  public boolean getFireNullValues() {
+    return fireNullValues;
+  }
+
+  /**
* Gets the format instance used to control formatting and parsing of this
    * {@link DateBox}.
    *
@@ -395,6 +404,14 @@
     box.setEnabled(enabled);
   }

+  /**
+ * Sets whether or not the date box will fire {@code ValueChangeEvents} with a
+   * date value of {@code null} for invalid or empty string values.
+   */
+  public void setFireNullValues(boolean fireNullValues) {
+    this.fireNullValues = fireNullValues;
+  }
+
   /**
* Explicitly focus/unfocus this widget. Only one widget can have focus at a
    * time, and the widget that does will receive all keyboard events.
@@ -448,7 +465,7 @@
   }

   public void setValue(Date date, boolean fireEvents) {
-    setValue(picker.getValue(), date, fireEvents);
+    setValue(picker.getValue(), date, fireEvents, true);
   }

   /**
@@ -480,14 +497,17 @@
     });
   }

-  private void setValue(Date oldDate, Date date, boolean fireEvents) {
+ private void setValue(Date oldDate, Date date, boolean fireEvents, boolean updateText) {
     if (date != null) {
       picker.setCurrentMonth(date);
     }
     picker.setValue(date, false);
-    format.reset(this, false);
-    box.setText(getFormat().format(this, date));
-
+
+    if (updateText) {
+      format.reset(this, false);
+      box.setText(getFormat().format(this, date));
+    }
+
     if (fireEvents) {
       DateChangeEvent.fireIfNotEqualDates(this, oldDate, date);
     }
@@ -495,8 +515,8 @@

   private void updateDateFromTextBox() {
     Date parsedDate = parseDate(true);
-    if (parsedDate != null) {
-      setValue(picker.getValue(), parsedDate, true);
+    if (fireNullValues || (parsedDate != null)) {
+      setValue(picker.getValue(), parsedDate, true, false);
     }
   }
 }


--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to