From edc3b08defa815d9f65a9589c7a252ad99ea40fa Mon Sep 17 00:00:00 2001 From: Anna Koskinen Date: Thu, 28 Dec 2017 13:49:39 +0200 Subject: Allow setting custom styles to DateField calendar date cells (#10305) Fixes #10304 --- .../main/java/com/vaadin/ui/AbstractDateField.java | 68 ++++++++++++++++++++++ 1 file changed, 68 insertions(+) (limited to 'server') diff --git a/server/src/main/java/com/vaadin/ui/AbstractDateField.java b/server/src/main/java/com/vaadin/ui/AbstractDateField.java index 5c176f3a55..781848cb19 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractDateField.java +++ b/server/src/main/java/com/vaadin/ui/AbstractDateField.java @@ -27,6 +27,7 @@ import java.util.Date; import java.util.HashMap; import java.util.Locale; import java.util.Map; +import java.util.Map.Entry; import java.util.Objects; import java.util.Optional; import java.util.Set; @@ -802,4 +803,71 @@ public abstract class AbstractDateField + * Sets a custom style name for the given date's calendar cell. Setting the + * style name will override any previous style names that have been set for + * that date, but can contain several actual style names separated by space. + * Setting the custom style name {@code null} will only remove the previous + * custom style name. + *

+ *

+ * This logic is entirely separate from {@link #setStyleName(String)} + *

+ *

+ * Usage examples:
+ * {@code setDateStyle(LocalDate.now(), "teststyle");}
+ * {@code setDateStyle(LocalDate.now(), "teststyle1 teststyle2");} + *

+ * + * @param date + * which date cell to modify + * @param styleName + * the custom style name(s) for given date, {@code null} to clear + * custom style name(s) + */ + public void setDateStyle(LocalDate date, String styleName) { + if (date != null) { + if (styleName != null) { + getState().dateStyles.put(date.toString(), styleName); + } else { + getState().dateStyles.remove(date.toString()); + } + } + } + + /** + * Returns the custom style name that corresponds with the given date's + * calendar cell. + * + * @param date + * which date cell's custom style name(s) to return + * @return the corresponding style name(s), if any, {@code null} otherwise + * + * @see {@link #setDateStyle(LocalDate, String)} + */ + public String getDateStyle(LocalDate date) { + if (date == null) { + return null; + } + return getState(false).dateStyles.get(date.toString()); + } + + /** + * Returns a map from dates to custom style names in each date's calendar + * cell. + * + * @return map from dates to custom style names in each date's calendar cell + * + * @see {@link #setDateStyle(LocalDate, String)} + */ + public Map getDateStyles() { + HashMap hashMap = new HashMap<>(); + for (Entry entry : getState(false).dateStyles + .entrySet()) { + hashMap.put(LocalDate.parse(entry.getKey()), entry.getValue()); + } + return hashMap; + } } -- cgit v1.2.3