]> source.dussan.org Git - vaadin-framework.git/commitdiff
Specify custom first day of week for Calendar #19227
authorLeonid Rozenblyum <lrozenblyum@gmail.com>
Wed, 4 Nov 2015 10:18:46 +0000 (12:18 +0200)
committerVaadin Code Review <review@vaadin.com>
Wed, 11 Nov 2015 10:09:45 +0000 (10:09 +0000)
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

server/src/com/vaadin/ui/Calendar.java
server/tests/src/com/vaadin/tests/server/component/calendar/CalendarBasicsTest.java
uitest/src/com/vaadin/tests/components/calendar/CalendarTest.java

index acddbe308bfc37aed5826fe47e795202473c5582..0fb6925ae2cd1e2db08247bd8c980e4b3828ce54 100644 (file)
@@ -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();
+    }
 }
index 95b4aecdb3d65a73d52b69563ab7902dd811cdaa..1592fb6c381614e96ef03e81ed92d879cfc14f1e 100644 (file)
@@ -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;
index a1bcca2e4e62a16f44d70582f8f2a88d089ef554..b56f92962a28ab7ba56096a4193f12a4e5f8acbf 100644 (file)
@@ -108,6 +108,7 @@ public class CalendarTest extends UI {
     private CheckBox hideWeekendsButton;
 
     private CheckBox readOnlyButton;
+    private ComboBox customFirstDayOfWeekSelect;
 
     private TextField captionField;
 
@@ -221,10 +222,12 @@ public class CalendarTest extends UI {
             setLocale(Locale.getDefault());
         }
 
-        // Initialize locale, timezone and timeformat selects.
+        // Initialize locale, timezone, timeformat and custom first day of week
+        // selects.
         localeSelect = createLocaleSelect();
         timeZoneSelect = createTimeZoneSelect();
         formatSelect = createCalendarFormatSelect();
+        customFirstDayOfWeekSelect = createCustomFirstDayOfWeekSelect();
 
         initCalendar();
         initLayoutContent();
@@ -359,6 +362,7 @@ public class CalendarTest extends UI {
         controlPanel.addComponent(localeSelect);
         controlPanel.addComponent(timeZoneSelect);
         controlPanel.addComponent(formatSelect);
+        controlPanel.addComponent(customFirstDayOfWeekSelect);
         controlPanel.addComponent(hideWeekendsButton);
         controlPanel.addComponent(readOnlyButton);
         controlPanel.addComponent(disabledButton);
@@ -770,6 +774,39 @@ public class CalendarTest extends UI {
         return s;
     }
 
+    private ComboBox createCustomFirstDayOfWeekSelect() {
+        ComboBox comboBox = new ComboBox("First day of week");
+
+        comboBox.addContainerProperty("caption", String.class, "");
+        comboBox.setItemCaptionPropertyId("caption");
+
+        comboBox.setImmediate(true);
+
+        Item defaultItem = comboBox.addItem(DEFAULT_ITEMID);
+        defaultItem.getItemProperty("caption").setValue("Default by locale");
+
+        Item sunday = comboBox.addItem(java.util.Calendar.SUNDAY);
+        sunday.getItemProperty("caption").setValue("Sunday");
+
+        Item monday = comboBox.addItem(java.util.Calendar.MONDAY);
+        monday.getItemProperty("caption").setValue("Monday");
+
+        comboBox.select(DEFAULT_ITEMID);
+        comboBox.setNullSelectionAllowed(false);
+
+        comboBox.addValueChangeListener(new ValueChangeListener() {
+
+            private static final long serialVersionUID = 1L;
+
+            @Override
+            public void valueChange(ValueChangeEvent event) {
+                updateCalendarFirstDayOfWeek(event.getProperty().getValue());
+            }
+        });
+
+        return comboBox;
+    }
+
     private ComboBox createLocaleSelect() {
         ComboBox s = new ComboBox("Locale");
         s.addContainerProperty("caption", String.class, "");
@@ -831,6 +868,20 @@ public class CalendarTest extends UI {
         calendarComponent.setTimeFormat(calFormat);
     }
 
+    private void updateCalendarFirstDayOfWeek(Object firstDayOfWeek) {
+        Integer firstDayOfWeekValue = null;
+        if (firstDayOfWeek instanceof Integer) {
+            firstDayOfWeekValue = (Integer) firstDayOfWeek;
+            calendarComponent.setFirstDayOfWeek(firstDayOfWeekValue);
+        } else {
+            // it means 'Default by locale has been selected'
+            calendarComponent.setFirstDayOfWeek(null);
+        }
+
+        // if we need we may update also field of this test 'calendar' which
+        // also keeps first day of week
+    }
+
     private String getLocaleItemCaption(Locale l) {
         String country = l.getDisplayCountry(getLocale());
         String language = l.getDisplayLanguage(getLocale());