summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorLeonid Rozenblyum <lrozenblyum@gmail.com>2015-11-04 12:18:46 +0200
committerVaadin Code Review <review@vaadin.com>2015-11-11 10:09:45 +0000
commit91dcac26f683314d176a7763f53dad158c36e3df (patch)
tree7c95e3458ae0db6150b3af53bd8fd320f020a4e2 /server
parentd18d2fb7edd7901bab603bbce02d561808300ab3 (diff)
downloadvaadin-framework-91dcac26f683314d176a7763f53dad158c36e3df.tar.gz
vaadin-framework-91dcac26f683314d176a7763f53dad158c36e3df.zip
Specify custom first day of week for Calendar #19227
Added possibility to provide first day of week independent of Locale. (the Calendar code has been reformatted according to Eclipse 'Save action'). Unit-tests added. UI test extended. Change-Id: I3e3531228c139ce2014a1227e47c12e7896a6f87
Diffstat (limited to 'server')
-rw-r--r--server/src/com/vaadin/ui/Calendar.java37
-rw-r--r--server/tests/src/com/vaadin/tests/server/component/calendar/CalendarBasicsTest.java54
2 files changed, 85 insertions, 6 deletions
diff --git a/server/src/com/vaadin/ui/Calendar.java b/server/src/com/vaadin/ui/Calendar.java
index acddbe308b..0fb6925ae2 100644
--- a/server/src/com/vaadin/ui/Calendar.java
+++ b/server/src/com/vaadin/ui/Calendar.java
@@ -220,6 +220,8 @@ public class Calendar extends AbstractComponent implements
*/
private CalendarServerRpcImpl rpc = new CalendarServerRpcImpl();
+ private Integer customFirstDayOfWeek;
+
/**
* Returns the logger for the calendar
*/
@@ -427,6 +429,10 @@ public class Calendar extends AbstractComponent implements
} else {
currentCalendar = java.util.Calendar.getInstance(getLocale());
}
+
+ if (customFirstDayOfWeek != null) {
+ currentCalendar.setFirstDayOfWeek(customFirstDayOfWeek);
+ }
}
private void setupCalendarEvents() {
@@ -1281,9 +1287,8 @@ public class Calendar extends AbstractComponent implements
/*
* (non-Javadoc)
*
- * @see
- * com.vaadin.addon.calendar.ui.CalendarEvents.EventChangeListener#eventChange
- * (com.vaadin.addon.calendar.ui.CalendarEvents.EventChange)
+ * @see com.vaadin.addon.calendar.ui.CalendarEvents.EventChangeListener#
+ * eventChange (com.vaadin.addon.calendar.ui.CalendarEvents.EventChange)
*/
@Override
public void eventSetChange(EventSetChangeEvent changeEvent) {
@@ -1440,9 +1445,8 @@ public class Calendar extends AbstractComponent implements
/*
* (non-Javadoc)
*
- * @see
- * com.vaadin.addon.calendar.ui.CalendarComponentEvents.CalendarEventNotifier
- * #getHandler(java.lang.String)
+ * @see com.vaadin.addon.calendar.ui.CalendarComponentEvents.
+ * CalendarEventNotifier #getHandler(java.lang.String)
*/
@Override
public EventListener getHandler(String eventId) {
@@ -1992,4 +1996,25 @@ public class Calendar extends AbstractComponent implements
customAttributes.add("end-date");
return customAttributes;
}
+
+ /**
+ * Allow setting first day of week depending on Locale. Set to null if you
+ * want first day of week depend on locale
+ *
+ * @since
+ * @param dayOfWeek
+ */
+ public void setFirstDayOfWeek(Integer dayOfWeek) {
+ int minimalSupported = java.util.Calendar.SUNDAY;
+ int maximalSupported = java.util.Calendar.SATURDAY;
+ if (dayOfWeek != null
+ && (dayOfWeek < minimalSupported || dayOfWeek > maximalSupported)) {
+ throw new IllegalArgumentException(
+ String.format(
+ "Day of week must be between %s and %s. Actually received: %s",
+ minimalSupported, maximalSupported, dayOfWeek));
+ }
+ customFirstDayOfWeek = dayOfWeek;
+ markAsDirty();
+ }
}
diff --git a/server/tests/src/com/vaadin/tests/server/component/calendar/CalendarBasicsTest.java b/server/tests/src/com/vaadin/tests/server/component/calendar/CalendarBasicsTest.java
index 95b4aecdb3..1592fb6c38 100644
--- a/server/tests/src/com/vaadin/tests/server/component/calendar/CalendarBasicsTest.java
+++ b/server/tests/src/com/vaadin/tests/server/component/calendar/CalendarBasicsTest.java
@@ -216,6 +216,60 @@ public class CalendarBasicsTest {
calendar.isClientChangeAllowed());
}
+ // regression test to ensure old functionality is not broken
+ @Test
+ public void defaultFirstDayOfWeek() {
+ Calendar calendar = new Calendar();
+ calendar.setLocale(Locale.GERMAN);
+ // simulating consequences of markAsDirty
+ calendar.beforeClientResponse(true);
+ assertEquals(java.util.Calendar.MONDAY, calendar.getInternalCalendar()
+ .getFirstDayOfWeek());
+ }
+
+ @Test
+ public void customFirstDayOfWeek() {
+ Calendar calendar = new Calendar();
+ calendar.setLocale(Locale.GERMAN);
+ calendar.setFirstDayOfWeek(java.util.Calendar.SUNDAY);
+
+ // simulating consequences of markAsDirty
+ calendar.beforeClientResponse(true);
+ assertEquals(java.util.Calendar.SUNDAY, calendar.getInternalCalendar()
+ .getFirstDayOfWeek());
+ }
+
+ @Test
+ public void customFirstDayOfWeekCanSetEvenBeforeLocale() {
+ Calendar calendar = new Calendar();
+ calendar.setFirstDayOfWeek(java.util.Calendar.SUNDAY);
+
+ calendar.setLocale(Locale.GERMAN);
+ // simulating consequences of markAsDirty
+ calendar.beforeClientResponse(true);
+ assertEquals(java.util.Calendar.SUNDAY, calendar.getInternalCalendar()
+ .getFirstDayOfWeek());
+ }
+
+ @Test
+ public void customFirstDayOfWeekSetNullRestoresDefault() {
+ Calendar calendar = new Calendar();
+ calendar.setLocale(Locale.GERMAN);
+ calendar.setFirstDayOfWeek(java.util.Calendar.SUNDAY);
+ calendar.setFirstDayOfWeek(null);
+ // simulating consequences of markAsDirty
+ calendar.beforeClientResponse(true);
+ assertEquals(java.util.Calendar.MONDAY, calendar.getInternalCalendar()
+ .getFirstDayOfWeek());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void customFirstDayOfWeekValidation() {
+ Calendar calendar = new Calendar();
+ int someWrongDayOfWeek = 10;
+ calendar.setFirstDayOfWeek(someWrongDayOfWeek);
+ }
+
private static class TestCalendar extends Calendar {
TestCalendar(boolean connectorEnabled) {
isConnectorEnabled = connectorEnabled;