diff options
author | Ahmed Ashour <asashour@yahoo.com> | 2017-10-20 10:54:25 +0200 |
---|---|---|
committer | Péter Török <31210544+torok-peter@users.noreply.github.com> | 2017-10-20 11:54:25 +0300 |
commit | 3929d0ac6f8e3d7370412d1d181db1b2361d13e3 (patch) | |
tree | e59252649924608b756d2dbfac8ad3fb116cec7b /client | |
parent | afb9d3b6c1d143b59506e08c3f27eb088555801c (diff) | |
download | vaadin-framework-3929d0ac6f8e3d7370412d1d181db1b2361d13e3.tar.gz vaadin-framework-3929d0ac6f8e3d7370412d1d181db1b2361d13e3.zip |
AbstractDateField.formatDate() to be abstract (#10186)
* AbstractDateField.formatDate to be abstract
Remove invalidDateString paramter, as it can be deduced
* Fix test
* Remove AbstractDateField.getResolutionVariable
VAbstractTextualDate: rename updateDateVariables() to updateBufferedResolutions()
* Revert to use fixed value of MONDAY.
* release notes
* updateAndSendBufferedValues()
* Missed call to updateAndSendBufferedValues();
* release note
Diffstat (limited to 'client')
11 files changed, 205 insertions, 248 deletions
diff --git a/client/src/main/java/com/vaadin/client/DateTimeService.java b/client/src/main/java/com/vaadin/client/DateTimeService.java index 7367d686b6..fb4aaf130d 100644 --- a/client/src/main/java/com/vaadin/client/DateTimeService.java +++ b/client/src/main/java/com/vaadin/client/DateTimeService.java @@ -602,7 +602,6 @@ public class DateTimeService { } return date; - } private static Logger getLogger() { diff --git a/client/src/main/java/com/vaadin/client/LocaleService.java b/client/src/main/java/com/vaadin/client/LocaleService.java index 88d07ee836..39c77d4697 100644 --- a/client/src/main/java/com/vaadin/client/LocaleService.java +++ b/client/src/main/java/com/vaadin/client/LocaleService.java @@ -65,81 +65,72 @@ public class LocaleService { throws LocaleNotLoadedException { if (cache.containsKey(locale)) { return cache.get(locale).monthNames; - } else { - throw new LocaleNotLoadedException(locale); } + throw new LocaleNotLoadedException(locale); } public static String[] getShortMonthNames(String locale) throws LocaleNotLoadedException { if (cache.containsKey(locale)) { return cache.get(locale).shortMonthNames; - } else { - throw new LocaleNotLoadedException(locale); } + throw new LocaleNotLoadedException(locale); } public static String[] getDayNames(String locale) throws LocaleNotLoadedException { if (cache.containsKey(locale)) { return cache.get(locale).dayNames; - } else { - throw new LocaleNotLoadedException(locale); } + throw new LocaleNotLoadedException(locale); } public static String[] getShortDayNames(String locale) throws LocaleNotLoadedException { if (cache.containsKey(locale)) { return cache.get(locale).shortDayNames; - } else { - throw new LocaleNotLoadedException(locale); } + throw new LocaleNotLoadedException(locale); } public static int getFirstDayOfWeek(String locale) throws LocaleNotLoadedException { if (cache.containsKey(locale)) { return cache.get(locale).firstDayOfWeek; - } else { - throw new LocaleNotLoadedException(locale); } + throw new LocaleNotLoadedException(locale); } public static String getDateFormat(String locale) throws LocaleNotLoadedException { if (cache.containsKey(locale)) { return cache.get(locale).dateFormat; - } else { - throw new LocaleNotLoadedException(locale); } + throw new LocaleNotLoadedException(locale); } public static boolean isTwelveHourClock(String locale) throws LocaleNotLoadedException { if (cache.containsKey(locale)) { return cache.get(locale).twelveHourClock; - } else { - throw new LocaleNotLoadedException(locale); } + throw new LocaleNotLoadedException(locale); } public static String getClockDelimiter(String locale) throws LocaleNotLoadedException { if (cache.containsKey(locale)) { return cache.get(locale).hourMinuteDelimiter; - } else { - throw new LocaleNotLoadedException(locale); } + throw new LocaleNotLoadedException(locale); } public static String[] getAmPmStrings(String locale) throws LocaleNotLoadedException { if (cache.containsKey(locale)) { return new String[] { cache.get(locale).am, cache.get(locale).pm }; - } else { - throw new LocaleNotLoadedException(locale); } + throw new LocaleNotLoadedException(locale); } public static void addLocales(List<LocaleData> localeDatas) { diff --git a/client/src/main/java/com/vaadin/client/ui/AbstractConnector.java b/client/src/main/java/com/vaadin/client/ui/AbstractConnector.java index 849f977b53..7f58bf35a3 100644 --- a/client/src/main/java/com/vaadin/client/ui/AbstractConnector.java +++ b/client/src/main/java/com/vaadin/client/ui/AbstractConnector.java @@ -336,9 +336,7 @@ public abstract class AbstractConnector OnStateChangeMethod method = propertyMethods.get(j); if (invokedMethods.add(method)) { - method.invoke(stateChangeEvent); - } } } @@ -361,7 +359,6 @@ public abstract class AbstractConnector VConsole.log( "Unregistered connector " + Util.getConnectorString(this)); } - } /** @@ -447,11 +444,7 @@ public abstract class AbstractConnector return false; } - if (getParent() == null) { - return true; - } else { - return getParent().isEnabled(); - } + return getParent() == null || getParent().isEnabled(); } @Override @@ -474,21 +467,20 @@ public abstract class AbstractConnector * Gets the URL for a resource that has been added by the server-side * connector using * {@link com.vaadin.terminal.AbstractClientConnector#setResource(String, com.vaadin.terminal.Resource)} - * with the same key. <code>null</code> is returned if no corresponding - * resource is found. + * with the same key. {@code null} is returned if no corresponding resource + * is found. * * @param key * a string identifying the resource. - * @return the resource URL as a string, or <code>null</code> if no - * corresponding resource is found. + * @return the resource URL as a string, or {@code null} if no corresponding + * resource is found. */ public String getResourceUrl(String key) { URLReference urlReference = getState().resources.get(key); if (urlReference == null) { return null; - } else { - return urlReference.getURL(); } + return urlReference.getURL(); } /* 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 85594484b3..c0f8413530 100644 --- a/client/src/main/java/com/vaadin/client/ui/VAbstractPopupCalendar.java +++ b/client/src/main/java/com/vaadin/client/ui/VAbstractPopupCalendar.java @@ -231,16 +231,15 @@ public abstract class VAbstractPopupCalendar<PANEL extends VAbstractCalendarPane R resolution = getCurrentResolution(); if (currentDate == null || newDate.getTime() != currentDate.getTime()) { setCurrentDate((Date) newDate.clone()); - bufferedResolutions.put( - calendar.getResolution(calendar::isYear).name(), + bufferedResolutions.put(calendar.getResolution(calendar::isYear), newDate.getYear() + 1900); if (!calendar.isYear(resolution)) { bufferedResolutions.put( - calendar.getResolution(calendar::isMonth).name(), + calendar.getResolution(calendar::isMonth), newDate.getMonth() + 1); if (!calendar.isMonth(resolution)) { bufferedResolutions.put( - calendar.getResolution(calendar::isDay).name(), + calendar.getResolution(calendar::isDay), newDate.getDate()); } } diff --git a/client/src/main/java/com/vaadin/client/ui/VAbstractTextualDate.java b/client/src/main/java/com/vaadin/client/ui/VAbstractTextualDate.java index edbc863188..8206f2f862 100644 --- a/client/src/main/java/com/vaadin/client/ui/VAbstractTextualDate.java +++ b/client/src/main/java/com/vaadin/client/ui/VAbstractTextualDate.java @@ -121,16 +121,15 @@ public abstract class VAbstractTextualDate<R extends Enum<R>> protected String createFormatString() { if (isYear(getCurrentResolution())) { return "yyyy"; // force full year - } else { - try { - String frmString = LocaleService.getDateFormat(currentLocale); - return cleanFormat(frmString); - } catch (LocaleNotLoadedException e) { - // TODO should die instead? Can the component survive - // without format string? - VConsole.error(e); - return null; - } + } + try { + String frmString = LocaleService.getDateFormat(currentLocale); + return cleanFormat(frmString); + } catch (LocaleNotLoadedException e) { + // TODO should die instead? Can the component survive + // without format string? + VConsole.error(e); + return null; } } @@ -203,7 +202,7 @@ public abstract class VAbstractTextualDate<R extends Enum<R>> /** * Sets the time zone for the field. - * + * * @param timeZone * the new time zone to use * @since 8.2 @@ -243,8 +242,6 @@ public abstract class VAbstractTextualDate<R extends Enum<R>> VConsole.log(e); addStyleName(getStylePrimaryName() + PARSE_ERROR_CLASSNAME); - // this is a hack that may eventually be removed - bufferedInvalidDateString = true; setDate(null); } } else { @@ -255,28 +252,40 @@ public abstract class VAbstractTextualDate<R extends Enum<R>> // always send the date string bufferedDateString = text.getText(); - updateDateVariables(); + updateAndSendBufferedValues(); + } + + /** + * Updates the {@link VDateField#bufferedResolutions bufferedResolutions}, + * then {@link #sendBufferedValues() sends} the values to the server. + * + * @since + */ + protected final void updateAndSendBufferedValues() { + updateBufferedResolutions(); + sendBufferedValues(); } /** - * Updates variables to send a response to the server. + * Updates {@link VDateField#bufferedResolutions bufferedResolutions} before + * sending a response to the server. * <p> * The method can be overridden by subclasses to provide a custom logic for * date variables to avoid overriding the {@link #onChange(ChangeEvent)} * method. - * + * + * <p> + * Note that this method should not send the buffered values, but use + * {@link #updateAndSendBufferedValues()} instead + * * @since */ - protected void updateDateVariables() { - // Update variables - // (only the smallest defining resolution needs to be - // immediate) + protected void updateBufferedResolutions() { Date currentDate = getDate(); - bufferedResolutions.put( - getResolutions().filter(this::isYear).findFirst().get().name(), - currentDate != null ? currentDate.getYear() + 1900 : null); - if (isYear(getCurrentResolution())) { - sendBufferedValues(); + if (currentDate != null) { + bufferedResolutions.put( + getResolutions().filter(this::isYear).findFirst().get(), + currentDate.getYear() + 1900); } } @@ -432,13 +441,12 @@ public abstract class VAbstractTextualDate<R extends Enum<R>> * @since 8.1 */ public void setISODate(String isoDate) { - if (isoDate == null) { - setDate(null); - } else { - Date date = getIsoFormatter().parse(isoDate); - setDate(date); + Date date = null; + if (isoDate != null) { + date = getIsoFormatter().parse(isoDate); } - updateDateVariables(); + setDate(date); + updateAndSendBufferedValues(); } /** @@ -454,16 +462,14 @@ public abstract class VAbstractTextualDate<R extends Enum<R>> Date date = getDate(); if (date == null) { return null; - } else { - return getIsoFormatter().format(date); } + return getIsoFormatter().format(date); } private DateTimeFormat getIsoFormatter() { if (supportsTime()) { return DateTimeFormat.getFormat(ISO_DATE_TIME_PATTERN); - } else { - return DateTimeFormat.getFormat(ISO_DATE_PATTERN); } + return DateTimeFormat.getFormat(ISO_DATE_PATTERN); } } diff --git a/client/src/main/java/com/vaadin/client/ui/VDateField.java b/client/src/main/java/com/vaadin/client/ui/VDateField.java index 971248cee9..af1b018130 100644 --- a/client/src/main/java/com/vaadin/client/ui/VDateField.java +++ b/client/src/main/java/com/vaadin/client/ui/VDateField.java @@ -20,6 +20,7 @@ import java.util.Date; import java.util.HashMap; import java.util.Locale; import java.util.Map; +import java.util.stream.Collectors; import java.util.stream.Stream; import com.google.gwt.user.client.ui.FlowPanel; @@ -66,13 +67,13 @@ public abstract class VDateField<R extends Enum<R>> extends FlowPanel * A temporary holder of the time units (resolutions), which would be sent * to the server through {@link #sendBufferedValues()}. * - * The key is the resolution name e.g. "HOUR", "MINUTE". + * The key is the resolution. * * The value can be {@code null}. * * @since */ - protected Map<String, Integer> bufferedResolutions = new HashMap<>(); + protected Map<R, Integer> bufferedResolutions = new HashMap<>(); /** * A temporary holder of the date string, which would be sent to the server @@ -83,14 +84,6 @@ public abstract class VDateField<R extends Enum<R>> extends FlowPanel protected String bufferedDateString; /** - * A temporary holder of whether the date string was invalid or not, which - * would be sent to the server through {@link #sendBufferedValues()}. - * - * @since - */ - protected boolean bufferedInvalidDateString; - - /** * The date that is displayed the date field before a value is selected. If * null, display the current date. */ @@ -269,16 +262,16 @@ public abstract class VDateField<R extends Enum<R>> extends FlowPanel } /** - * Sends the {@link #bufferedDateString}, {@link #bufferedInvalidDateString} - * and {@link #bufferedResolutions} to the server, and clears their values. + * Sends the {@link #bufferedDateString} and {@link #bufferedResolutions} to + * the server, and clears their values. * * @since */ public void sendBufferedValues() { - rpc.update(bufferedDateString, bufferedInvalidDateString, - new HashMap<>(bufferedResolutions)); + rpc.update(bufferedDateString, + bufferedResolutions.entrySet().stream().collect(Collectors + .toMap(e -> e.getKey().name(), e -> e.getValue()))); bufferedDateString = null; - bufferedInvalidDateString = false; bufferedResolutions.clear(); } diff --git a/client/src/main/java/com/vaadin/client/ui/VDateFieldCalendar.java b/client/src/main/java/com/vaadin/client/ui/VDateFieldCalendar.java index 69e6d923ce..6e727ee640 100644 --- a/client/src/main/java/com/vaadin/client/ui/VDateFieldCalendar.java +++ b/client/src/main/java/com/vaadin/client/ui/VDateFieldCalendar.java @@ -15,6 +15,10 @@ */ package com.vaadin.client.ui; +import static com.vaadin.shared.ui.datefield.DateResolution.DAY; +import static com.vaadin.shared.ui.datefield.DateResolution.MONTH; +import static com.vaadin.shared.ui.datefield.DateResolution.YEAR; + import java.util.Date; import java.util.Map; @@ -31,7 +35,7 @@ public class VDateFieldCalendar extends VAbstractDateFieldCalendar<VDateCalendarPanel, DateResolution> { public VDateFieldCalendar() { - super(GWT.create(VDateCalendarPanel.class), DateResolution.YEAR); + super(GWT.create(VDateCalendarPanel.class), YEAR); } /** @@ -53,16 +57,14 @@ public class VDateFieldCalendar DateResolution resolution = getCurrentResolution(); if (currentDate == null || date2.getTime() != currentDate.getTime()) { setCurrentDate((Date) date2.clone()); - bufferedResolutions.put(DateResolution.YEAR.name(), + bufferedResolutions.put(YEAR, // Java Date uses the year aligned to 1900 (no to zero). // So we should add 1900 to get a correct year aligned to 0. date2.getYear() + 1900); - if (resolution.compareTo(DateResolution.YEAR) < 0) { - bufferedResolutions.put(DateResolution.MONTH.name(), - date2.getMonth() + 1); - if (resolution.compareTo(DateResolution.MONTH) < 0) { - bufferedResolutions.put(DateResolution.DAY.name(), - date2.getDate()); + if (resolution.compareTo(YEAR) < 0) { + bufferedResolutions.put(MONTH, date2.getMonth() + 1); + if (resolution.compareTo(MONTH) < 0) { + bufferedResolutions.put(DAY, date2.getDate()); } } sendBufferedValues(); @@ -71,8 +73,7 @@ public class VDateFieldCalendar @Override public void setCurrentResolution(DateResolution resolution) { - super.setCurrentResolution( - resolution == null ? DateResolution.YEAR : resolution); + super.setCurrentResolution(resolution == null ? YEAR : resolution); } @Override @@ -82,7 +83,7 @@ public class VDateFieldCalendar @Override public boolean isYear(DateResolution resolution) { - return DateResolution.YEAR.equals(resolution); + return YEAR.equals(resolution); } @Override diff --git a/client/src/main/java/com/vaadin/client/ui/VDateTimeFieldCalendar.java b/client/src/main/java/com/vaadin/client/ui/VDateTimeFieldCalendar.java index 6e74bf45e7..a635b8b615 100644 --- a/client/src/main/java/com/vaadin/client/ui/VDateTimeFieldCalendar.java +++ b/client/src/main/java/com/vaadin/client/ui/VDateTimeFieldCalendar.java @@ -15,6 +15,13 @@ */ package com.vaadin.client.ui; +import static com.vaadin.shared.ui.datefield.DateTimeResolution.DAY; +import static com.vaadin.shared.ui.datefield.DateTimeResolution.HOUR; +import static com.vaadin.shared.ui.datefield.DateTimeResolution.MINUTE; +import static com.vaadin.shared.ui.datefield.DateTimeResolution.MONTH; +import static com.vaadin.shared.ui.datefield.DateTimeResolution.SECOND; +import static com.vaadin.shared.ui.datefield.DateTimeResolution.YEAR; + import java.util.Date; import java.util.Map; @@ -32,8 +39,7 @@ public class VDateTimeFieldCalendar extends VAbstractDateFieldCalendar<VDateTimeCalendarPanel, DateTimeResolution> { public VDateTimeFieldCalendar() { - super(GWT.create(VDateTimeCalendarPanel.class), - DateTimeResolution.MINUTE); + super(GWT.create(VDateTimeCalendarPanel.class), MINUTE); } @Override @@ -49,23 +55,17 @@ public class VDateTimeFieldCalendar extends DateTimeResolution resolution = getCurrentResolution(); if (currentDate == null || date2.getTime() != currentDate.getTime()) { setCurrentDate((Date) date2.clone()); - addBufferedResolution(DateTimeResolution.YEAR, - date2.getYear() + 1900); - if (resolution.compareTo(DateTimeResolution.YEAR) < 0) { - addBufferedResolution(DateTimeResolution.MONTH, - date2.getMonth() + 1); - if (resolution.compareTo(DateTimeResolution.MONTH) < 0) { - addBufferedResolution(DateTimeResolution.DAY, - date2.getDate()); - if (resolution.compareTo(DateTimeResolution.DAY) < 0) { - addBufferedResolution(DateTimeResolution.HOUR, - date2.getHours()); - if (resolution.compareTo(DateTimeResolution.HOUR) < 0) { - addBufferedResolution(DateTimeResolution.MINUTE, - date2.getMinutes()); - if (resolution - .compareTo(DateTimeResolution.MINUTE) < 0) { - addBufferedResolution(DateTimeResolution.SECOND, + bufferedResolutions.put(YEAR, date2.getYear() + 1900); + if (resolution.compareTo(YEAR) < 0) { + bufferedResolutions.put(MONTH, date2.getMonth() + 1); + if (resolution.compareTo(MONTH) < 0) { + bufferedResolutions.put(DAY, date2.getDate()); + if (resolution.compareTo(DAY) < 0) { + bufferedResolutions.put(HOUR, date2.getHours()); + if (resolution.compareTo(HOUR) < 0) { + bufferedResolutions.put(MINUTE, date2.getMinutes()); + if (resolution.compareTo(MINUTE) < 0) { + bufferedResolutions.put(SECOND, date2.getSeconds()); } } @@ -76,23 +76,17 @@ public class VDateTimeFieldCalendar extends } } - private void addBufferedResolution(DateTimeResolution resolution, - Integer value) { - bufferedResolutions.put(resolution.name(), value); - } - @Override public String resolutionAsString() { - if (getCurrentResolution().compareTo(DateTimeResolution.DAY) >= 0) { + if (getCurrentResolution().compareTo(DAY) >= 0) { return getResolutionVariable(getCurrentResolution()); - } else { - return "full"; } + return "full"; } @Override public boolean isYear(DateTimeResolution resolution) { - return DateTimeResolution.YEAR.equals(resolution); + return YEAR.equals(resolution); } @Override diff --git a/client/src/main/java/com/vaadin/client/ui/VPopupCalendar.java b/client/src/main/java/com/vaadin/client/ui/VPopupCalendar.java index 65c47f8c6c..970ccfb71a 100644 --- a/client/src/main/java/com/vaadin/client/ui/VPopupCalendar.java +++ b/client/src/main/java/com/vaadin/client/ui/VPopupCalendar.java @@ -15,6 +15,10 @@ */ package com.vaadin.client.ui; +import static com.vaadin.shared.ui.datefield.DateResolution.DAY; +import static com.vaadin.shared.ui.datefield.DateResolution.MONTH; +import static com.vaadin.shared.ui.datefield.DateResolution.YEAR; + import java.util.Date; import java.util.Map; @@ -32,7 +36,7 @@ public class VPopupCalendar extends VAbstractPopupCalendar<VDateCalendarPanel, DateResolution> { public VPopupCalendar() { - super(GWT.create(VDateCalendarPanel.class), DateResolution.YEAR); + super(GWT.create(VDateCalendarPanel.class), YEAR); } @Override @@ -47,24 +51,23 @@ public class VPopupCalendar @Override public void setCurrentResolution(DateResolution resolution) { - super.setCurrentResolution( - resolution == null ? DateResolution.YEAR : resolution); + super.setCurrentResolution(resolution == null ? YEAR : resolution); } public static Date makeDate(Map<DateResolution, Integer> dateValues) { - if (dateValues.get(DateResolution.YEAR) == null) { + if (dateValues.get(YEAR) == null) { return null; } Date date = new Date(2000 - 1900, 0, 1); - Integer year = dateValues.get(DateResolution.YEAR); + Integer year = dateValues.get(YEAR); if (year != null) { date.setYear(year - 1900); } - Integer month = dateValues.get(DateResolution.MONTH); + Integer month = dateValues.get(MONTH); if (month != null) { date.setMonth(month - 1); } - Integer day = dateValues.get(DateResolution.DAY); + Integer day = dateValues.get(DAY); if (day != null) { date.setDate(day); } @@ -73,7 +76,7 @@ public class VPopupCalendar @Override public boolean isYear(DateResolution resolution) { - return DateResolution.YEAR.equals(resolution); + return YEAR.equals(resolution); } @Override @@ -82,31 +85,27 @@ public class VPopupCalendar } @Override - protected void updateDateVariables() { - super.updateDateVariables(); - DateResolution resolution = getCurrentResolution(); - // Update variables - // (only the smallest defining resolution needs to be - // immediate) + protected void updateBufferedResolutions() { + super.updateBufferedResolutions(); Date currentDate = getDate(); - if (resolution.compareTo(DateResolution.MONTH) <= 0) { - bufferedResolutions.put(DateResolution.MONTH.name(), - currentDate != null ? currentDate.getMonth() + 1 : null); - } - if (resolution.compareTo(DateResolution.DAY) <= 0) { - bufferedResolutions.put(DateResolution.DAY.name(), - currentDate != null ? currentDate.getDate() : null); + if (currentDate != null) { + DateResolution resolution = getCurrentResolution(); + if (resolution.compareTo(MONTH) <= 0) { + bufferedResolutions.put(MONTH, currentDate.getMonth() + 1); + } + if (resolution.compareTo(DAY) <= 0) { + bufferedResolutions.put(DAY, currentDate.getDate()); + } } - sendBufferedValues(); } @Override protected String cleanFormat(String format) { // Remove unnecessary d & M if resolution is too low - if (getCurrentResolution().compareTo(DateResolution.DAY) > 0) { + if (getCurrentResolution().compareTo(DAY) > 0) { format = format.replaceAll("d", ""); } - if (getCurrentResolution().compareTo(DateResolution.MONTH) > 0) { + if (getCurrentResolution().compareTo(MONTH) > 0) { format = format.replaceAll("M", ""); } return super.cleanFormat(format); diff --git a/client/src/main/java/com/vaadin/client/ui/VPopupTimeCalendar.java b/client/src/main/java/com/vaadin/client/ui/VPopupTimeCalendar.java index ff740efbca..39ae704651 100644 --- a/client/src/main/java/com/vaadin/client/ui/VPopupTimeCalendar.java +++ b/client/src/main/java/com/vaadin/client/ui/VPopupTimeCalendar.java @@ -15,6 +15,13 @@ */ package com.vaadin.client.ui; +import static com.vaadin.shared.ui.datefield.DateTimeResolution.DAY; +import static com.vaadin.shared.ui.datefield.DateTimeResolution.HOUR; +import static com.vaadin.shared.ui.datefield.DateTimeResolution.MINUTE; +import static com.vaadin.shared.ui.datefield.DateTimeResolution.MONTH; +import static com.vaadin.shared.ui.datefield.DateTimeResolution.SECOND; +import static com.vaadin.shared.ui.datefield.DateTimeResolution.YEAR; + import java.util.Date; import java.util.Map; @@ -36,8 +43,7 @@ public class VPopupTimeCalendar extends VAbstractPopupCalendar<VDateTimeCalendarPanel, DateTimeResolution> { public VPopupTimeCalendar() { - super(GWT.create(VDateTimeCalendarPanel.class), - DateTimeResolution.MINUTE); + super(GWT.create(VDateTimeCalendarPanel.class), MINUTE); } @Override @@ -47,45 +53,43 @@ public class VPopupTimeCalendar extends @Override public String resolutionAsString() { - if (getCurrentResolution().compareTo(DateTimeResolution.DAY) >= 0) { + if (getCurrentResolution().compareTo(DAY) >= 0) { return getResolutionVariable(getCurrentResolution()); - } else { - return "full"; } + return "full"; } @Override public void setCurrentResolution(DateTimeResolution resolution) { - super.setCurrentResolution( - resolution == null ? DateTimeResolution.MINUTE : resolution); + super.setCurrentResolution(resolution == null ? MINUTE : resolution); } public static Date makeDate(Map<DateTimeResolution, Integer> dateValues) { - if (dateValues.get(DateTimeResolution.YEAR) == null) { + if (dateValues.get(YEAR) == null) { return null; } Date date = new Date(2000 - 1900, 0, 1); - Integer year = dateValues.get(DateTimeResolution.YEAR); + Integer year = dateValues.get(YEAR); if (year != null) { date.setYear(year - 1900); } - Integer month = dateValues.get(DateTimeResolution.MONTH); + Integer month = dateValues.get(MONTH); if (month != null) { date.setMonth(month - 1); } - Integer day = dateValues.get(DateTimeResolution.DAY); + Integer day = dateValues.get(DAY); if (day != null) { date.setDate(day); } - Integer hour = dateValues.get(DateTimeResolution.HOUR); + Integer hour = dateValues.get(HOUR); if (hour != null) { date.setHours(hour); } - Integer minute = dateValues.get(DateTimeResolution.MINUTE); + Integer minute = dateValues.get(MINUTE); if (minute != null) { date.setMinutes(minute); } - Integer second = dateValues.get(DateTimeResolution.SECOND); + Integer second = dateValues.get(SECOND); if (second != null) { date.setSeconds(second); } @@ -94,7 +98,7 @@ public class VPopupTimeCalendar extends @Override public boolean isYear(DateTimeResolution resolution) { - return DateTimeResolution.YEAR.equals(resolution); + return YEAR.equals(resolution); } @Override @@ -103,38 +107,27 @@ public class VPopupTimeCalendar extends } @Override - protected void updateDateVariables() { - super.updateDateVariables(); - DateTimeResolution resolution = getCurrentResolution(); - // (only the smallest defining resolution needs to be - // immediate) + protected void updateBufferedResolutions() { + super.updateBufferedResolutions(); Date currentDate = getDate(); - if (resolution.compareTo(DateTimeResolution.MONTH) <= 0) { - addBufferedResolution(DateTimeResolution.MONTH, - currentDate != null ? currentDate.getMonth() + 1 : null); - } - if (resolution.compareTo(DateTimeResolution.DAY) <= 0) { - addBufferedResolution(DateTimeResolution.DAY, - currentDate != null ? currentDate.getDate() : null); - } - if (resolution.compareTo(DateTimeResolution.HOUR) <= 0) { - addBufferedResolution(DateTimeResolution.HOUR, - currentDate != null ? currentDate.getHours() : null); - } - if (resolution.compareTo(DateTimeResolution.MINUTE) <= 0) { - addBufferedResolution(DateTimeResolution.MINUTE, - currentDate != null ? currentDate.getMinutes() : null); - } - if (resolution.compareTo(DateTimeResolution.SECOND) <= 0) { - addBufferedResolution(DateTimeResolution.SECOND, - currentDate != null ? currentDate.getSeconds() : null); + if (currentDate != null) { + DateTimeResolution resolution = getCurrentResolution(); + if (resolution.compareTo(MONTH) <= 0) { + bufferedResolutions.put(MONTH, currentDate.getMonth() + 1); + } + if (resolution.compareTo(DAY) <= 0) { + bufferedResolutions.put(DAY, currentDate.getDate()); + } + if (resolution.compareTo(HOUR) <= 0) { + bufferedResolutions.put(HOUR, currentDate.getHours()); + } + if (resolution.compareTo(MINUTE) <= 0) { + bufferedResolutions.put(MINUTE, currentDate.getMinutes()); + } + if (resolution.compareTo(SECOND) <= 0) { + bufferedResolutions.put(SECOND, currentDate.getSeconds()); + } } - sendBufferedValues(); - } - - private void addBufferedResolution(DateTimeResolution resolutionToAdd, - Integer value) { - bufferedResolutions.put(resolutionToAdd.name(), value); } @Override @@ -144,16 +137,12 @@ public class VPopupTimeCalendar extends super.updateValue(newDate); DateTimeResolution resolution = getCurrentResolution(); if (currentDate == null || newDate.getTime() != currentDate.getTime()) { - if (resolution.compareTo(DateTimeResolution.DAY) < 0) { - bufferedResolutions.put(DateTimeResolution.HOUR.name(), - newDate.getHours()); - if (resolution.compareTo(DateTimeResolution.HOUR) < 0) { - bufferedResolutions.put(DateTimeResolution.MINUTE.name(), - newDate.getMinutes()); - if (resolution.compareTo(DateTimeResolution.MINUTE) < 0) { - bufferedResolutions.put( - DateTimeResolution.SECOND.name(), - newDate.getSeconds()); + if (resolution.compareTo(DAY) < 0) { + bufferedResolutions.put(HOUR, newDate.getHours()); + if (resolution.compareTo(HOUR) < 0) { + bufferedResolutions.put(MINUTE, newDate.getMinutes()); + if (resolution.compareTo(MINUTE) < 0) { + bufferedResolutions.put(SECOND, newDate.getSeconds()); } } } @@ -164,50 +153,45 @@ public class VPopupTimeCalendar extends protected String createFormatString() { if (isYear(getCurrentResolution())) { return "yyyy"; // force full year - } else { - - try { - String frmString = LocaleService.getDateFormat(currentLocale); - frmString = cleanFormat(frmString); - // String delim = LocaleService - // .getClockDelimiter(currentLocale); - if (getCurrentResolution() - .compareTo(DateTimeResolution.HOUR) <= 0) { - if (dts.isTwelveHourClock()) { - frmString += " hh"; - } else { - frmString += " HH"; - } - if (getCurrentResolution() - .compareTo(DateTimeResolution.MINUTE) <= 0) { - frmString += ":mm"; - if (getCurrentResolution() - .compareTo(DateTimeResolution.SECOND) <= 0) { - frmString += ":ss"; - } - } - if (dts.isTwelveHourClock()) { - frmString += " aaa"; + } + try { + String frmString = LocaleService.getDateFormat(currentLocale); + frmString = cleanFormat(frmString); + // String delim = LocaleService + // .getClockDelimiter(currentLocale); + if (getCurrentResolution().compareTo(HOUR) <= 0) { + if (dts.isTwelveHourClock()) { + frmString += " hh"; + } else { + frmString += " HH"; + } + if (getCurrentResolution().compareTo(MINUTE) <= 0) { + frmString += ":mm"; + if (getCurrentResolution().compareTo(SECOND) <= 0) { + frmString += ":ss"; } } - - return frmString; - } catch (LocaleNotLoadedException e) { - // TODO should die instead? Can the component survive - // without format string? - VConsole.error(e); - return null; + if (dts.isTwelveHourClock()) { + frmString += " aaa"; + } } + + return frmString; + } catch (LocaleNotLoadedException e) { + // TODO should die instead? Can the component survive + // without format string? + VConsole.error(e); + return null; } } @Override protected String cleanFormat(String format) { // Remove unnecessary d & M if resolution is too low - if (getCurrentResolution().compareTo(DateTimeResolution.DAY) > 0) { + if (getCurrentResolution().compareTo(DAY) > 0) { format = format.replaceAll("d", ""); } - if (getCurrentResolution().compareTo(DateTimeResolution.MONTH) > 0) { + if (getCurrentResolution().compareTo(MONTH) > 0) { format = format.replaceAll("M", ""); } return super.cleanFormat(format); diff --git a/client/src/main/java/com/vaadin/client/ui/datefield/AbstractDateFieldConnector.java b/client/src/main/java/com/vaadin/client/ui/datefield/AbstractDateFieldConnector.java index 2aad416343..b7849938af 100644 --- a/client/src/main/java/com/vaadin/client/ui/datefield/AbstractDateFieldConnector.java +++ b/client/src/main/java/com/vaadin/client/ui/datefield/AbstractDateFieldConnector.java @@ -54,10 +54,9 @@ public abstract class AbstractDateFieldConnector<R extends Enum<R>> Stream<R> resolutions = widget.getResolutions(); R resolution = widget.getCurrentResolution(); return resolutions.collect(Collectors.toMap(Function.identity(), - res -> res == null ? null - : (resolution.compareTo(res) <= 0) - ? stateResolutions.get(res.name()) - : null)); + res -> resolution.compareTo(res) <= 0 + ? stateResolutions.get(res.name()) + : null)); } /** @@ -68,14 +67,14 @@ public abstract class AbstractDateFieldConnector<R extends Enum<R>> * @since */ protected Map<R, Integer> getDefaultValues() { + VDateField<R> widget = getWidget(); Map<String, Integer> stateResolutions = getState().resolutions; - Stream<R> resolutions = getWidget().getResolutions(); - R resolution = getWidget().getCurrentResolution(); + Stream<R> resolutions = widget.getResolutions(); + R resolution = widget.getCurrentResolution(); return resolutions.collect(Collectors.toMap(Function.identity(), - res -> res == null ? null - : (resolution.compareTo(res) <= 0) - ? stateResolutions.get("default-" + res.name()) - : null)); + res -> resolution.compareTo(res) <= 0 + ? stateResolutions.get("default-" + res.name()) + : null)); } @SuppressWarnings("unchecked") |