From 3904bc38dc84a2428497469440a542c316d78ad1 Mon Sep 17 00:00:00 2001 From: Henri Sara Date: Wed, 8 Apr 2009 12:21:43 +0000 Subject: [PATCH] Merged #2829 to 6.0: Could not distinguish an empty DateField from a DateField containing invalid value svn changeset:7365/svn branch:6.0 --- .../terminal/gwt/client/ui/ITextualDate.java | 3 ++ src/com/itmill/toolkit/ui/DateField.java | 49 ++++++++++++++++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/com/itmill/toolkit/terminal/gwt/client/ui/ITextualDate.java b/src/com/itmill/toolkit/terminal/gwt/client/ui/ITextualDate.java index e68342dcf6..dcd411c129 100644 --- a/src/com/itmill/toolkit/terminal/gwt/client/ui/ITextualDate.java +++ b/src/com/itmill/toolkit/terminal/gwt/client/ui/ITextualDate.java @@ -155,6 +155,7 @@ public class ITextualDate extends IDateField implements Paintable, Field, ClientExceptionHandler.displayError(e.getMessage()); addStyleName(PARSE_ERROR_CLASSNAME); + // this is a hack that may eventually be removed client.updateVariable(id, "lastInvalidDateString", text .getText(), false); date = null; @@ -164,6 +165,8 @@ public class ITextualDate extends IDateField implements Paintable, Field, // remove possibly added invalid value indication removeStyleName(PARSE_ERROR_CLASSNAME); } + // always send the date string + client.updateVariable(id, "dateString", text.getText(), false); if (date != null) { showingDate = new Date(date.getTime()); diff --git a/src/com/itmill/toolkit/ui/DateField.java b/src/com/itmill/toolkit/ui/DateField.java index aaa53e3ee1..96ddc08b9e 100644 --- a/src/com/itmill/toolkit/ui/DateField.java +++ b/src/com/itmill/toolkit/ui/DateField.java @@ -107,6 +107,12 @@ public class DateField extends AbstractField { */ private String dateFormat; + /** + * Read-only content of an ITextualDate field - null for other types of date + * fields. + */ + private String dateString; + /* Constructors */ /** @@ -258,13 +264,23 @@ public class DateField extends AbstractField { || variables.containsKey("day") || variables.containsKey("hour") || variables.containsKey("min") - || variables.containsKey("sec") || variables - .containsKey("msec"))) { + || variables.containsKey("sec") + || variables.containsKey("msec") || variables + .containsKey("dateString"))) { // Old and new dates final Date oldDate = (Date) getValue(); + final String oldDateString = dateString; Date newDate = null; + // this enables analyzing invalid input on the server + Object o = variables.get("dateString"); + if (o != null) { + dateString = o.toString(); + } else { + dateString = null; + } + // Gets the new date in parts // Null values are converted to negative values. int year = variables.containsKey("year") ? (variables.get("year") == null ? -1 @@ -326,10 +342,39 @@ public class DateField extends AbstractField { && (newDate == null || !newDate.equals(oldDate))) { setValue(newDate, true); // Don't require a repaint, client // updates itself + } else if (dateString != null && !"".equals(dateString) + && !dateString.equals(oldDateString)) { + setValue(handleUnparsableDateString(dateString)); } } } + /** + * This method is called to handle the date string from the client if the + * client could not parse it as a Date. + * + * By default, null is returned. If an exception is thrown, the current + * value will not be modified. + * + * This can be overridden to handle conversions or to throw an exception, or + * to fire an event. + * + * The default behavior is likely to change in the next major version of the + * toolkit - a Property.ConversionException will be thrown. + * + * @param dateString + * @return parsed Date + * @throws Property.ConversionException + * to keep the old value and indicate an error + */ + protected Date handleUnparsableDateString(String dateString) + throws Property.ConversionException { + // TODO in the next major version, this should throw an exception to be + // consistent with other fields + // throw new Property.ConversionException(); + return null; + } + /* Property features */ /* -- 2.39.5