Переглянути джерело

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
tags/7.6.0.beta2
Leonid Rozenblyum 8 роки тому
джерело
коміт
91dcac26f6

+ 31
- 6
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();
}
}

+ 54
- 0
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;

+ 52
- 1
uitest/src/com/vaadin/tests/components/calendar/CalendarTest.java Переглянути файл

@@ -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());

Завантаження…
Відмінити
Зберегти