From a71b2e31c0a783a5a715824fd243ff88d65dabbf Mon Sep 17 00:00:00 2001 From: Daniil Tsarev Date: Wed, 29 Mar 2017 20:47:02 +0400 Subject: [PATCH] PL-8731 Ability to change Calendar day names and months --- .../cuba/gui/components/Calendar.java | 26 ++++++++ .../cuba/web/gui/components/WebCalendar.java | 62 +++++++++++++++++-- 2 files changed, 84 insertions(+), 4 deletions(-) diff --git a/modules/gui/src/com/haulmont/cuba/gui/components/Calendar.java b/modules/gui/src/com/haulmont/cuba/gui/components/Calendar.java index ffa697c77e..2c43815106 100644 --- a/modules/gui/src/com/haulmont/cuba/gui/components/Calendar.java +++ b/modules/gui/src/com/haulmont/cuba/gui/components/Calendar.java @@ -22,7 +22,10 @@ import com.haulmont.cuba.gui.components.calendar.CalendarEventProvider; import com.haulmont.cuba.gui.data.CollectionDatasource; import javax.annotation.Nullable; +import java.time.DayOfWeek; +import java.time.Month; import java.util.Date; +import java.util.Map; import java.util.TimeZone; public interface Calendar extends Component.BelongToFrame, Component.HasCaption, Component.HasIcon { @@ -133,6 +136,29 @@ public interface Calendar extends Component.BelongToFrame, Component.HasCaption, */ boolean isNavigationButtonsVisible(); + /** + * @return {@link DayOfWeek} values matched to localized day names + */ + Map getDayNames(); + + /** + * Sets localized Calendar day names. + * + * @param dayNames {@link DayOfWeek} values matched to localized day names + */ + void setDayNames(Map dayNames); + + /** + * @return {@link Month} values matched to localized month names + */ + Map getMonthNames(); + + /** + * Sets localized Calendar month names. + * + * @param monthNames {@link Month} values matched to localized month names + */ + void setMonthNames(Map monthNames); void addDateClickListener(CalendarDateClickListener listener); void removeDateClickListener(CalendarDateClickListener listener); diff --git a/modules/web/src/com/haulmont/cuba/web/gui/components/WebCalendar.java b/modules/web/src/com/haulmont/cuba/web/gui/components/WebCalendar.java index 48020675cb..621f61a897 100644 --- a/modules/web/src/com/haulmont/cuba/web/gui/components/WebCalendar.java +++ b/modules/web/src/com/haulmont/cuba/web/gui/components/WebCalendar.java @@ -16,6 +16,7 @@ package com.haulmont.cuba.web.gui.components; +import com.haulmont.bali.util.Preconditions; import com.haulmont.cuba.core.entity.Entity; import com.haulmont.cuba.core.global.AppBeans; import com.haulmont.cuba.core.global.Messages; @@ -28,9 +29,12 @@ import com.haulmont.cuba.web.gui.components.calendar.CalendarEventProviderWrappe import com.haulmont.cuba.web.gui.components.calendar.CalendarEventWrapper; import com.haulmont.cuba.web.toolkit.ui.CubaCalendar; import com.vaadin.ui.components.calendar.CalendarComponentEvents; +import org.apache.commons.collections.CollectionUtils; -import java.util.Date; -import java.util.TimeZone; +import java.time.DayOfWeek; +import java.time.Month; +import java.util.*; +import java.util.stream.Collectors; public class WebCalendar extends WebAbstractComponent implements Calendar { private CollectionDatasource datasource; @@ -45,7 +49,7 @@ public class WebCalendar extends WebAbstractComponent implements C component = createComponent(); Messages messages = AppBeans.get(Messages.NAME); - String [] monthNamesShort = new String[12]; + String[] monthNamesShort = new String[12]; monthNamesShort[0] = messages.getMainMessage("calendar.januaryCaption"); monthNamesShort[1] = messages.getMainMessage("calendar.februaryCaption"); monthNamesShort[2] = messages.getMainMessage("calendar.marchCaption"); @@ -60,7 +64,7 @@ public class WebCalendar extends WebAbstractComponent implements C monthNamesShort[11] = messages.getMainMessage("calendar.decemberCaption"); component.setMonthNamesShort(monthNamesShort); - String [] dayNamesShort = new String[7]; + String[] dayNamesShort = new String[7]; dayNamesShort[0] = messages.getMainMessage("calendar.sundayCaption"); dayNamesShort[1] = messages.getMainMessage("calendar.mondayCaption"); dayNamesShort[2] = messages.getMainMessage("calendar.tuesdayCaption"); @@ -500,4 +504,54 @@ public class WebCalendar extends WebAbstractComponent implements C removeStyleName("navbuttons-disabled"); } } + + @Override + public Map getDayNames() { + List days = Arrays.asList(component.getDayNamesShort()); + + return days.stream().collect(Collectors.toMap( + (String d) -> DayOfWeek.of(days.indexOf(d) + 1), + d -> d + )); + } + + @Override + public void setDayNames(Map dayNames) { + Preconditions.checkNotNullArgument(dayNames); + + if (!CollectionUtils.isEqualCollection(Arrays.asList(DayOfWeek.values()), dayNames.keySet())) { + throw new IllegalArgumentException("Day names map doesn't contain all required values"); + } + + String[] days = Arrays.stream(DayOfWeek.values()) + .map(dayNames::get) + .toArray(String[]::new); + + component.setDayNamesShort(days); + } + + @Override + public Map getMonthNames() { + List months = Arrays.asList(component.getMonthNamesShort()); + + return months.stream().collect(Collectors.toMap( + (String m) -> Month.of(months.indexOf(m) + 1), + m -> m + )); + } + + @Override + public void setMonthNames(Map monthNames) { + Preconditions.checkNotNullArgument(monthNames); + + if (!CollectionUtils.isEqualCollection(Arrays.asList(Month.values()), monthNames.keySet())) { + throw new IllegalArgumentException("Month names map doesn't contain all required values"); + } + + String[] months = Arrays.stream(Month.values()) + .map(monthNames::get) + .toArray(String[]::new); + + component.setMonthNamesShort(months); + } }