2 * Copyright 2000-2018 Vaadin Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
16 package com.vaadin.v7.ui.components.calendar.handler;
18 import java.util.Calendar;
19 import java.util.Date;
21 import com.vaadin.v7.shared.ui.calendar.DateConstants;
22 import com.vaadin.v7.ui.components.calendar.CalendarComponentEvents.ForwardEvent;
23 import com.vaadin.v7.ui.components.calendar.CalendarComponentEvents.ForwardHandler;
26 * Implements basic functionality needed to enable forward navigation.
31 @SuppressWarnings("serial")
33 public class BasicForwardHandler implements ForwardHandler {
38 * @see com.vaadin.addon.calendar.ui.CalendarComponentEvents.ForwardHandler#
40 * (com.vaadin.addon.calendar.ui.CalendarComponentEvents.ForwardEvent)
43 public void forward(ForwardEvent event) {
44 Date start = event.getComponent().getStartDate();
45 Date end = event.getComponent().getEndDate();
47 // calculate amount to move forward
48 int durationInDays = (int) (((end.getTime()) - start.getTime())
49 / DateConstants.DAYINMILLIS);
50 // for week view durationInDays = 7, for day view durationInDays = 1
53 // set new start and end times
54 Calendar javaCalendar = Calendar.getInstance();
55 javaCalendar.setTime(start);
56 javaCalendar.add(Calendar.DATE, durationInDays);
57 Date newStart = javaCalendar.getTime();
59 javaCalendar.setTime(end);
60 javaCalendar.add(Calendar.DATE, durationInDays);
61 Date newEnd = javaCalendar.getTime();
63 if (start.equals(end)) { // day view
64 int firstDay = event.getComponent().getFirstVisibleDayOfWeek();
65 int lastDay = event.getComponent().getLastVisibleDayOfWeek();
66 int dayOfWeek = javaCalendar.get(Calendar.DAY_OF_WEEK);
68 // we suppose that 7 >= lastDay >= firstDay >= 1
69 while (!(firstDay <= dayOfWeek && dayOfWeek <= lastDay)) {
70 javaCalendar.add(Calendar.DATE, 1);
71 dayOfWeek = javaCalendar.get(Calendar.DAY_OF_WEEK);
74 newStart = javaCalendar.getTime();
75 newEnd = javaCalendar.getTime();
78 setDates(event, newStart, newEnd);
82 * Set the start and end dates for the event.
85 * The event that the start and end dates should be set
91 protected void setDates(ForwardEvent event, Date start, Date end) {
92 event.getComponent().setStartDate(start);
93 event.getComponent().setEndDate(end);