diff options
author | Sauli Tähkäpää <sauli@vaadin.com> | 2014-10-04 00:03:38 +0300 |
---|---|---|
committer | Anna Koskinen <anna@vaadin.com> | 2014-11-03 13:16:47 +0200 |
commit | eb94e2a9be2d8ce34620fdedc90425e05785cdd8 (patch) | |
tree | f2c7714b1d541c7b490b83754f7be9c124bf18ee | |
parent | dda24122a7a0a0491fc5cac69792fe37a826803e (diff) | |
download | vaadin-framework-eb94e2a9be2d8ce34620fdedc90425e05785cdd8.tar.gz vaadin-framework-eb94e2a9be2d8ce34620fdedc90425e05785cdd8.zip |
Fix VCalendar to use correct year of week. (#14783)
Change-Id: Id55ad5ed620bd5c187b70ae2a2d0a4c4adea382a
7 files changed, 110 insertions, 5 deletions
diff --git a/client/src/com/vaadin/client/ui/VCalendar.java b/client/src/com/vaadin/client/ui/VCalendar.java index f0f1bc89ca..c59a78108c 100644 --- a/client/src/com/vaadin/client/ui/VCalendar.java +++ b/client/src/com/vaadin/client/ui/VCalendar.java @@ -588,7 +588,7 @@ public class VCalendar extends Composite implements VHasDropHandler { int x = pos - (y * columns); if (x == 0 && daysCount > 7) { // Add week to weekToolbar for navigation - weekToolbar.addWeek(week, d.getYear()); + weekToolbar.addWeek(week, day.getYearOfWeek()); } final SimpleDayCell cell = new SimpleDayCell(this, y, x); cell.setMonthGrid(monthGrid); diff --git a/client/src/com/vaadin/client/ui/calendar/CalendarConnector.java b/client/src/com/vaadin/client/ui/calendar/CalendarConnector.java index cbf63768a3..8f5e9d9a59 100644 --- a/client/src/com/vaadin/client/ui/calendar/CalendarConnector.java +++ b/client/src/com/vaadin/client/ui/calendar/CalendarConnector.java @@ -675,7 +675,7 @@ public class CalendarConnector extends AbstractComponentConnector implements List<CalendarDay> list = new ArrayList<CalendarDay>(days.size()); for (CalendarState.Day day : days) { CalendarDay d = new CalendarDay(day.date, day.localizedDateFormat, - day.dayOfWeek, day.week); + day.dayOfWeek, day.week, day.yearOfWeek); list.add(d); } diff --git a/client/src/com/vaadin/client/ui/calendar/schedule/CalendarDay.java b/client/src/com/vaadin/client/ui/calendar/schedule/CalendarDay.java index 44b82f166f..0fceb6b6f9 100644 --- a/client/src/com/vaadin/client/ui/calendar/schedule/CalendarDay.java +++ b/client/src/com/vaadin/client/ui/calendar/schedule/CalendarDay.java @@ -27,14 +27,16 @@ public class CalendarDay { private String localizedDateFormat; private int dayOfWeek; private int week; + private int yearOfWeek; public CalendarDay(String date, String localizedDateFormat, int dayOfWeek, - int week) { + int week, int yearOfWeek) { super(); this.date = date; this.localizedDateFormat = localizedDateFormat; this.dayOfWeek = dayOfWeek; this.week = week; + this.yearOfWeek = yearOfWeek; } public String getDate() { @@ -52,4 +54,8 @@ public class CalendarDay { public int getWeek() { return week; } + + public int getYearOfWeek() { + return yearOfWeek; + } } diff --git a/server/src/com/vaadin/ui/Calendar.java b/server/src/com/vaadin/ui/Calendar.java index 63ac9fe35c..72ff6eb0e0 100644 --- a/server/src/com/vaadin/ui/Calendar.java +++ b/server/src/com/vaadin/ui/Calendar.java @@ -516,7 +516,8 @@ public class Calendar extends AbstractComponent implements day.date = df_date.format(date); day.localizedDateFormat = weeklyCaptionFormatter.format(date); day.dayOfWeek = getDowByLocale(currentCalendar); - day.week = currentCalendar.get(java.util.Calendar.WEEK_OF_YEAR); + day.week = getWeek(currentCalendar); + day.yearOfWeek = getYearOfWeek(currentCalendar); days.add(day); @@ -560,6 +561,23 @@ public class Calendar extends AbstractComponent implements state.actions = createActionsList(actionMap); } + private int getWeek(java.util.Calendar calendar) { + return calendar.get(java.util.Calendar.WEEK_OF_YEAR); + } + + private int getYearOfWeek(java.util.Calendar calendar) { + // Would use calendar.getWeekYear() but it's only available since 1.7. + int week = getWeek(calendar); + int month = calendar.get(java.util.Calendar.MONTH); + int year = calendar.get(java.util.Calendar.YEAR); + + if (week == 1 && month == java.util.Calendar.DECEMBER) { + return year + 1; + } + + return year; + } + private void setActionsForEachHalfHour( Map<CalendarDateRange, Set<Action>> actionMap, Date start, Date end, Action.Handler actionHandler) { @@ -1772,7 +1790,7 @@ public class Calendar extends AbstractComponent implements String[] splitted = event.split("w"); if (splitted.length == 2) { try { - int yr = 1900 + Integer.parseInt(splitted[0]); + int yr = Integer.parseInt(splitted[0]); int week = Integer.parseInt(splitted[1]); fireWeekClick(week, yr); } catch (NumberFormatException e) { diff --git a/shared/src/com/vaadin/shared/ui/calendar/CalendarState.java b/shared/src/com/vaadin/shared/ui/calendar/CalendarState.java index de48f1a06a..93bd05bc1e 100644 --- a/shared/src/com/vaadin/shared/ui/calendar/CalendarState.java +++ b/shared/src/com/vaadin/shared/ui/calendar/CalendarState.java @@ -44,6 +44,7 @@ public class CalendarState extends AbstractComponentState { public String localizedDateFormat; public int dayOfWeek; public int week; + public int yearOfWeek; } public static class Action implements java.io.Serializable { diff --git a/uitest/src/com/vaadin/tests/components/calendar/CalendarWeekSelection.java b/uitest/src/com/vaadin/tests/components/calendar/CalendarWeekSelection.java new file mode 100644 index 0000000000..c74f2a53e3 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/calendar/CalendarWeekSelection.java @@ -0,0 +1,39 @@ +package com.vaadin.tests.components.calendar; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Locale; + +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.Calendar; + +public class CalendarWeekSelection extends AbstractTestUI { + @Override + protected void setup(VaadinRequest request) { + Calendar calendar = new Calendar(); + calendar.setLocale(Locale.US); + + try { + calendar.setStartDate(new SimpleDateFormat("yyyy-MM-dd") + .parse("2013-12-15")); + calendar.setEndDate(new SimpleDateFormat("yyyy-MM-dd") + .parse("2014-01-15")); + } catch (ParseException e) { + e.printStackTrace(); + } + + addComponent(calendar); + } + + @Override + protected Integer getTicketNumber() { + return 14783; + } + + @Override + protected String getTestDescription() { + return "December 2013 - January 2014. Clicking the week 1 " + + "should open the week view for the first week of 2014."; + } +} diff --git a/uitest/src/com/vaadin/tests/components/calendar/CalendarWeekSelectionTest.java b/uitest/src/com/vaadin/tests/components/calendar/CalendarWeekSelectionTest.java new file mode 100644 index 0000000000..83f41994ae --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/calendar/CalendarWeekSelectionTest.java @@ -0,0 +1,41 @@ +package com.vaadin.tests.components.calendar; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.util.List; + +import org.junit.Test; +import org.openqa.selenium.WebElement; + +import com.vaadin.testbench.By; +import com.vaadin.tests.tb3.MultiBrowserTest; + +public class CalendarWeekSelectionTest extends MultiBrowserTest { + + @Test + public void correctYearIsSelected() { + openTestURL(); + + clickOnWeek("1"); + + assertThat(getFirstDayOfTheYear().getText(), is("Wednesday 1/1/14")); + } + + private WebElement getFirstDayOfTheYear() { + WebElement header = findElement(By.className("v-calendar-header-week")); + List<WebElement> headerElements = header.findElements(By.tagName("td")); + + // Wednesday is the first day of 2014. + return headerElements.get(4); + } + + private void clickOnWeek(String week) { + for (WebElement e : findElements(By.className("v-calendar-week-number"))) { + if (e.getText().equals(week)) { + e.click(); + break; + } + } + } +}
\ No newline at end of file |