diff options
author | Olli Tietäväinen <ollit@vaadin.com> | 2017-08-10 15:57:27 +0300 |
---|---|---|
committer | Henri Sara <henri.sara@gmail.com> | 2017-08-10 15:57:27 +0300 |
commit | 66e68f1ef25804dabfb4b0e4cdd7d59c66522927 (patch) | |
tree | 47190488a16fc39a87305878516a58cf00d1dcc7 /client | |
parent | 24473c8deac75d9ac229252a81b534ba0e62b31c (diff) | |
download | vaadin-framework-66e68f1ef25804dabfb4b0e4cdd7d59c66522927.tar.gz vaadin-framework-66e68f1ef25804dabfb4b0e4cdd7d59c66522927.zip |
Enable setting non-selected default value for DateField (#9745)
Requested feature: allow setting DateField to a certain starting point without selecting a value.
Diffstat (limited to 'client')
3 files changed, 61 insertions, 0 deletions
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 a8c356a07c..9d9862ceae 100644 --- a/client/src/main/java/com/vaadin/client/ui/VAbstractPopupCalendar.java +++ b/client/src/main/java/com/vaadin/client/ui/VAbstractPopupCalendar.java @@ -391,6 +391,8 @@ public abstract class VAbstractPopupCalendar<PANEL extends VAbstractCalendarPane if (getCurrentDate() != null) { calendar.setDate((Date) getCurrentDate().clone()); + } else if (getDefaultDate() != null) { + calendar.setDate(getDefaultDate()); } else { calendar.setDate(new Date()); } 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 40fc9a8f8c..772f72c897 100644 --- a/client/src/main/java/com/vaadin/client/ui/VDateField.java +++ b/client/src/main/java/com/vaadin/client/ui/VDateField.java @@ -54,6 +54,12 @@ public abstract class VDateField<R extends Enum<R>> extends FlowPanel protected boolean enabled; /** + * The date that is displayed the date field before a value is selected. If + * null, display the current date. + */ + private Date defaultDate = null; + + /** * The date that is selected in the date field. Null if an invalid date is * specified. */ @@ -95,6 +101,18 @@ public abstract class VDateField<R extends Enum<R>> extends FlowPanel } /** + * Set the default date to open popup when no date is selected. + * + * @param date + * default date to show as the initial (non-selected) value when + * opening a popup with no value selected + * @since 8.1.2 + */ + public void setDefaultDate(Date date) { + this.defaultDate = date; + } + + /** * Set the current date using a map with date values. * <p> * The map contains integer representation of values per resolution. The @@ -108,6 +126,27 @@ public abstract class VDateField<R extends Enum<R>> extends FlowPanel setCurrentDate(getDate(dateValues)); } + /** + * Set the default date using a map with date values. + * + * @see #setCurrentDate(Map) + * @param defaultValues + * @since 8.1.2 + */ + public void setDefaultDate(Map<R, Integer> defaultValues) { + setDefaultDate(getDate(defaultValues)); + } + + /** + * Sets the default date when no date is selected. + * + * @return the default date + * @since 8.1.2 + */ + public Date getDefaultDate() { + return defaultDate; + } + public boolean isReadonly() { return readonly; } 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 c851c13679..21e85773de 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 @@ -77,6 +77,7 @@ public abstract class AbstractDateFieldConnector<R extends Enum<R>> + getWidget().resolutionAsString(), true); getWidget().setCurrentDate(getTimeValues(uidl)); + getWidget().setDefaultDate(getDefaultValues(uidl)); } private void updateResolution(UIDL uidl) { @@ -98,6 +99,25 @@ public abstract class AbstractDateFieldConnector<R extends Enum<R>> : -1)); } + /** + * Returns the default date (when no date is selected) components as a map + * from Resolution to the corresponding value. + * + * @param uidl + * UIDL with corresponding variables + * @return default date component map + * @since 8.1.2 + */ + protected Map<R, Integer> getDefaultValues(UIDL uidl) { + Stream<R> resolutions = getWidget().getResolutions(); + R resolution = getWidget().getCurrentResolution(); + return resolutions.collect(Collectors.toMap(Function.identity(), + res -> (resolution.compareTo(res) <= 0) + ? uidl.getIntVariable("default-" + + getWidget().getResolutionVariable(res)) + : -1)); + } + @SuppressWarnings("unchecked") @Override public VDateField<R> getWidget() { |