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.BackwardEvent;
23 import com.vaadin.v7.ui.components.calendar.CalendarComponentEvents.BackwardHandler;
26 * Implements basic functionality needed to enable backwards navigation.
31 @SuppressWarnings("serial")
33 public class BasicBackwardHandler implements BackwardHandler {
39 * com.vaadin.addon.calendar.ui.CalendarComponentEvents.BackwardHandler#
41 * (com.vaadin.addon.calendar.ui.CalendarComponentEvents.BackwardEvent)
44 public void backward(BackwardEvent event) {
45 Date start = event.getComponent().getStartDate();
46 Date end = event.getComponent().getEndDate();
48 // calculate amount to move back
49 int durationInDays = (int) (((end.getTime()) - start.getTime())
50 / DateConstants.DAYINMILLIS);
52 // for week view durationInDays = -7, for day view durationInDays = -1
53 durationInDays = -durationInDays;
55 // set new start and end times
56 Calendar javaCalendar = event.getComponent().getInternalCalendar();
57 javaCalendar.setTime(start);
58 javaCalendar.add(Calendar.DATE, durationInDays);
59 Date newStart = javaCalendar.getTime();
61 javaCalendar.setTime(end);
62 javaCalendar.add(Calendar.DATE, durationInDays);
63 Date newEnd = javaCalendar.getTime();
65 if (start.equals(end)) { // day view
66 int firstDay = event.getComponent().getFirstVisibleDayOfWeek();
67 int lastDay = event.getComponent().getLastVisibleDayOfWeek();
68 int dayOfWeek = javaCalendar.get(Calendar.DAY_OF_WEEK);
70 // we suppose that 7 >= lastDay >= firstDay >= 1
71 while (!(firstDay <= dayOfWeek && dayOfWeek <= lastDay)) {
72 javaCalendar.add(Calendar.DATE, -1);
73 dayOfWeek = javaCalendar.get(Calendar.DAY_OF_WEEK);
76 newStart = javaCalendar.getTime();
77 newEnd = javaCalendar.getTime();
80 setDates(event, newStart, newEnd);
84 * Set the start and end dates for the event.
87 * The event that the start and end dates should be set
93 protected void setDates(BackwardEvent event, Date start, Date end) {
94 event.getComponent().setStartDate(start);
95 event.getComponent().setEndDate(end);