diff options
author | Ilia Motornyi <elmot@vaadin.com> | 2018-03-20 15:34:54 +0200 |
---|---|---|
committer | Teemu Suo-Anttila <tsuoanttila@users.noreply.github.com> | 2018-03-20 15:34:54 +0200 |
commit | ec314ee921b460d8401132bd661575120da60931 (patch) | |
tree | 61dc3e2ca066fdee83e995703f7795e3ea578f01 /client | |
parent | 803a453a28c00a38e69e9beabe3efe6590486b3f (diff) | |
download | vaadin-framework-ec314ee921b460d8401132bd661575120da60931.tar.gz vaadin-framework-ec314ee921b460d8401132bd661575120da60931.zip |
Fix date ranges for date and datetime fields (#10685)
Fixes #9765
Diffstat (limited to 'client')
6 files changed, 69 insertions, 64 deletions
diff --git a/client/src/main/java/com/vaadin/client/DateTimeService.java b/client/src/main/java/com/vaadin/client/DateTimeService.java index 4883d34caf..50d5f9ed2d 100644 --- a/client/src/main/java/com/vaadin/client/DateTimeService.java +++ b/client/src/main/java/com/vaadin/client/DateTimeService.java @@ -60,6 +60,17 @@ public class DateTimeService { setLocale(locale); } + /** + * Utility method to format positive int as zero-padded two-digits number + * + * @param i the value + * @return "00".."99" + * @since + */ + public static String asTwoDigits(int i) { + return (i < 10 ? "0" : "") + i; + } + public void setLocale(String locale) throws LocaleNotLoadedException { if (!LocaleService.getAvailableLocales().contains(locale)) { throw new LocaleNotLoadedException(locale); diff --git a/client/src/main/java/com/vaadin/client/ui/CalendarEntry.java b/client/src/main/java/com/vaadin/client/ui/CalendarEntry.java index d7bea84758..1efe0b6cf8 100644 --- a/client/src/main/java/com/vaadin/client/ui/CalendarEntry.java +++ b/client/src/main/java/com/vaadin/client/ui/CalendarEntry.java @@ -20,6 +20,8 @@ import java.util.Date; import com.vaadin.client.DateTimeService; +import static com.vaadin.client.DateTimeService.asTwoDigits; + public class CalendarEntry { private final String styleName; private Date start; @@ -137,8 +139,4 @@ public class CalendarEntry { return s; } - private static String asTwoDigits(int i) { - return (i < 10 ? "0" : "") + i; - } - } diff --git a/client/src/main/java/com/vaadin/client/ui/VAbstractCalendarPanel.java b/client/src/main/java/com/vaadin/client/ui/VAbstractCalendarPanel.java index 8810b26e30..c792d0a2cc 100644 --- a/client/src/main/java/com/vaadin/client/ui/VAbstractCalendarPanel.java +++ b/client/src/main/java/com/vaadin/client/ui/VAbstractCalendarPanel.java @@ -60,6 +60,8 @@ import com.vaadin.client.WidgetUtil; import com.vaadin.client.ui.aria.AriaHelper; import com.vaadin.shared.util.SharedUtil; +import static com.vaadin.client.DateTimeService.asTwoDigits; + /** * Abstract calendar panel to show and select a date using a resolution. The * class is parameterized by the date resolution enumeration type. @@ -719,21 +721,23 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> return true; } - Date valueDuplicate = (Date) date.clone(); - Date rangeStartDuplicate = (Date) rangeStart.clone(); + String dateStrResolution = dateStrResolution(date, minResolution); + return rangeStart.substring(0,dateStrResolution.length()) + .compareTo(dateStrResolution) <=0; + } - if (isYear(minResolution)) { - return valueDuplicate.getYear() >= rangeStartDuplicate.getYear(); + private String dateStrResolution(Date date, R minResolution) { + String dateStrResolution = (1900 + date.getYear()) + ""; + while (dateStrResolution.length() < 4) { + dateStrResolution = "0" + dateStrResolution; } - if (isMonth(minResolution)) { - valueDuplicate = clearDateBelowMonth(valueDuplicate); - rangeStartDuplicate = clearDateBelowMonth(rangeStartDuplicate); - } else { - valueDuplicate = clearDateBelowDay(valueDuplicate); - rangeStartDuplicate = clearDateBelowDay(rangeStartDuplicate); + if (!isYear(minResolution)) { + dateStrResolution += "-" + asTwoDigits(1 + date.getMonth()); + if (!isMonth(minResolution)) { + dateStrResolution += "-" + asTwoDigits(date.getDate()); + } } - - return !rangeStartDuplicate.after(valueDuplicate); + return dateStrResolution; } /** @@ -755,22 +759,9 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> return true; } - Date valueDuplicate = (Date) date.clone(); - Date rangeEndDuplicate = (Date) rangeEnd.clone(); - - if (isYear(minResolution)) { - return valueDuplicate.getYear() <= rangeEndDuplicate.getYear(); - } - if (isMonth(minResolution)) { - valueDuplicate = clearDateBelowMonth(valueDuplicate); - rangeEndDuplicate = clearDateBelowMonth(rangeEndDuplicate); - } else { - valueDuplicate = clearDateBelowDay(valueDuplicate); - rangeEndDuplicate = clearDateBelowDay(rangeEndDuplicate); - } - - return !rangeEndDuplicate.before(valueDuplicate); - + String dateStrResolution = dateStrResolution(date, minResolution); + return rangeEnd.substring(0,dateStrResolution.length()) + .compareTo(dateStrResolution) >= 0; } private static Date clearDateBelowMonth(Date date) { @@ -1691,14 +1682,32 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> * @param date */ private Date adjustDateToFitInsideRange(Date date) { - if (rangeStart != null && rangeStart.after(date)) { - date = (Date) rangeStart.clone(); - } else if (rangeEnd != null && rangeEnd.before(date)) { - date = (Date) rangeEnd.clone(); + if(!isAcceptedByRangeStart(date,resolution)) { + date = parseRangeString(rangeStart); + } else + if(!isAcceptedByRangeEnd(date,resolution)) { + date = parseRangeString(rangeEnd); } return date; } + private Date parseRangeString(String dateStr) { + if(dateStr == null || "".equals(dateStr)) return null; + int year = Integer.parseInt(dateStr.substring(0,4)) - 1900; + int month = parsePart(dateStr, 5, 2,1) - 1; + int day = parsePart(dateStr, 8, 2,1); + int hrs = parsePart(dateStr, 11, 2,0); + int min = parsePart(dateStr, 14, 2,0); + int sec = parsePart(dateStr, 17, 2,0); + + return new Date(year,month,day,hrs,min,sec); + } + + private int parsePart(String dateStr, int beginIndex, int length, int defValue) { + if(dateStr.length() < beginIndex + length) return defValue; + return Integer.parseInt(dateStr.substring(beginIndex, beginIndex + length)); + } + /** * Sets the data of the Panel. * @@ -1914,9 +1923,9 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> private static final String SUBPART_DAY = "day"; private static final String SUBPART_MONTH_YEAR_HEADER = "header"; - private Date rangeStart; + private String rangeStart; - private Date rangeEnd; + private String rangeEnd; @Override public String getSubPartName( @@ -2070,7 +2079,7 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> * @param newRangeStart * - the allowed range's start date */ - public void setRangeStart(Date newRangeStart) { + public void setRangeStart(String newRangeStart) { if (!SharedUtil.equals(rangeStart, newRangeStart)) { rangeStart = newRangeStart; if (initialRenderDone) { @@ -2088,7 +2097,7 @@ public abstract class VAbstractCalendarPanel<R extends Enum<R>> * @param newRangeEnd * - the allowed range's end date */ - public void setRangeEnd(Date newRangeEnd) { + public void setRangeEnd(String newRangeEnd) { if (!SharedUtil.equals(rangeEnd, newRangeEnd)) { rangeEnd = newRangeEnd; if (initialRenderDone) { diff --git a/client/src/main/java/com/vaadin/client/ui/VAbstractPopupCalendar.java b/client/src/main/java/com/vaadin/client/ui/VAbstractPopupCalendar.java index 4df4d0e325..bccf867d4e 100644 --- a/client/src/main/java/com/vaadin/client/ui/VAbstractPopupCalendar.java +++ b/client/src/main/java/com/vaadin/client/ui/VAbstractPopupCalendar.java @@ -599,7 +599,7 @@ public abstract class VAbstractPopupCalendar<PANEL extends VAbstractCalendarPane * @param rangeStart * - the allowed range's start date */ - public void setRangeStart(Date rangeStart) { + public void setRangeStart(String rangeStart) { calendar.setRangeStart(rangeStart); } @@ -610,7 +610,7 @@ public abstract class VAbstractPopupCalendar<PANEL extends VAbstractCalendarPane * @param rangeEnd * - the allowed range's end date */ - public void setRangeEnd(Date rangeEnd) { + public void setRangeEnd(String rangeEnd) { calendar.setRangeEnd(rangeEnd); } diff --git a/client/src/main/java/com/vaadin/client/ui/VDateTimeCalendarPanel.java b/client/src/main/java/com/vaadin/client/ui/VDateTimeCalendarPanel.java index 5e9732e091..5dae92564a 100644 --- a/client/src/main/java/com/vaadin/client/ui/VDateTimeCalendarPanel.java +++ b/client/src/main/java/com/vaadin/client/ui/VDateTimeCalendarPanel.java @@ -79,8 +79,6 @@ public class VDateTimeCalendarPanel /** * Constructs the ListBoxes and updates their value * - * @param redraw - * Should new instances of the listboxes be created */ private void buildTime() { clear(); @@ -89,11 +87,11 @@ public class VDateTimeCalendarPanel if (getDateTimeService().isTwelveHourClock()) { hours.addItem("12"); for (int i = 1; i < 12; i++) { - hours.addItem(asTwoDigits(i)); + hours.addItem(DateTimeService.asTwoDigits(i)); } } else { for (int i = 0; i < 24; i++) { - hours.addItem(asTwoDigits(i)); + hours.addItem(DateTimeService.asTwoDigits(i)); } } @@ -109,14 +107,14 @@ public class VDateTimeCalendarPanel if (getResolution().compareTo(DateTimeResolution.MINUTE) <= 0) { mins = createListBox(); for (int i = 0; i < 60; i++) { - mins.addItem(asTwoDigits(i)); + mins.addItem(DateTimeService.asTwoDigits(i)); } mins.addChangeHandler(this); } if (getResolution().compareTo(DateTimeResolution.SECOND) <= 0) { sec = createListBox(); for (int i = 0; i < 60; i++) { - sec.addItem(asTwoDigits(i)); + sec.addItem(DateTimeService.asTwoDigits(i)); } sec.addChangeHandler(this); } @@ -130,7 +128,7 @@ public class VDateTimeCalendarPanel if (getDateTimeService().isTwelveHourClock()) { h -= h < 12 ? 0 : 12; } - add(new VLabel(asTwoDigits(h))); + add(new VLabel(DateTimeService.asTwoDigits(h))); } else { add(hours); } @@ -139,7 +137,7 @@ public class VDateTimeCalendarPanel add(new VLabel(delimiter)); if (isReadonly()) { final int m = mins.getSelectedIndex(); - add(new VLabel(asTwoDigits(m))); + add(new VLabel(DateTimeService.asTwoDigits(m))); } else { add(mins); } @@ -148,7 +146,7 @@ public class VDateTimeCalendarPanel add(new VLabel(delimiter)); if (isReadonly()) { final int s = sec.getSelectedIndex(); - add(new VLabel(asTwoDigits(s))); + add(new VLabel(DateTimeService.asTwoDigits(s))); } else { add(sec); } @@ -308,10 +306,6 @@ public class VDateTimeCalendarPanel } } - private static String asTwoDigits(int i) { - return (i < 10 ? "0" : "") + i; - } - /** * Dispatches an event when the panel when time is changed. */ diff --git a/client/src/main/java/com/vaadin/client/ui/datefield/TextualDateConnector.java b/client/src/main/java/com/vaadin/client/ui/datefield/TextualDateConnector.java index 0702211ccb..08682ff13d 100644 --- a/client/src/main/java/com/vaadin/client/ui/datefield/TextualDateConnector.java +++ b/client/src/main/java/com/vaadin/client/ui/datefield/TextualDateConnector.java @@ -123,8 +123,8 @@ public abstract class TextualDateConnector<PANEL extends VAbstractCalendarPanel< super.onStateChanged(stateChangeEvent); getWidget().setTextFieldEnabled(getState().textFieldEnabled); - getWidget().setRangeStart(nullSafeDateClone(getState().rangeStart)); - getWidget().setRangeEnd(nullSafeDateClone(getState().rangeEnd)); + getWidget().setRangeStart(getState().rangeStart); + getWidget().setRangeEnd(getState().rangeEnd); getWidget().calendar.setDateStyles(getState().dateStyles); getWidget().calendar @@ -167,13 +167,6 @@ public abstract class TextualDateConnector<PANEL extends VAbstractCalendarPanel< getWidget().setTextFieldTabIndex(); } - private Date nullSafeDateClone(Date date) { - if (date != null) { - return (Date) date.clone(); - } - return null; - } - @Override protected void setWidgetStyleName(String styleName, boolean add) { super.setWidgetStyleName(styleName, add); |