You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BasicForwardHandler.java 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  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
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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
  14. * the License.
  15. */
  16. package com.vaadin.v7.ui.components.calendar.handler;
  17. import java.util.Calendar;
  18. import java.util.Date;
  19. import com.vaadin.v7.shared.ui.calendar.DateConstants;
  20. import com.vaadin.v7.ui.components.calendar.CalendarComponentEvents.ForwardEvent;
  21. import com.vaadin.v7.ui.components.calendar.CalendarComponentEvents.ForwardHandler;
  22. /**
  23. * Implements basic functionality needed to enable forward navigation.
  24. *
  25. * @since 7.1
  26. * @author Vaadin Ltd.
  27. */
  28. @SuppressWarnings("serial")
  29. @Deprecated
  30. public class BasicForwardHandler implements ForwardHandler {
  31. /*
  32. * (non-Javadoc)
  33. *
  34. * @see com.vaadin.addon.calendar.ui.CalendarComponentEvents.ForwardHandler#
  35. * forward
  36. * (com.vaadin.addon.calendar.ui.CalendarComponentEvents.ForwardEvent)
  37. */
  38. @Override
  39. public void forward(ForwardEvent event) {
  40. Date start = event.getComponent().getStartDate();
  41. Date end = event.getComponent().getEndDate();
  42. // calculate amount to move forward
  43. int durationInDays = (int) (((end.getTime()) - start.getTime())
  44. / DateConstants.DAYINMILLIS);
  45. // for week view durationInDays = 7, for day view durationInDays = 1
  46. durationInDays++;
  47. // set new start and end times
  48. Calendar javaCalendar = Calendar.getInstance();
  49. javaCalendar.setTime(start);
  50. javaCalendar.add(Calendar.DATE, durationInDays);
  51. Date newStart = javaCalendar.getTime();
  52. javaCalendar.setTime(end);
  53. javaCalendar.add(Calendar.DATE, durationInDays);
  54. Date newEnd = javaCalendar.getTime();
  55. if (start.equals(end)) { // day view
  56. int firstDay = event.getComponent().getFirstVisibleDayOfWeek();
  57. int lastDay = event.getComponent().getLastVisibleDayOfWeek();
  58. int dayOfWeek = javaCalendar.get(Calendar.DAY_OF_WEEK);
  59. // we suppose that 7 >= lastDay >= firstDay >= 1
  60. while (!(firstDay <= dayOfWeek && dayOfWeek <= lastDay)) {
  61. javaCalendar.add(Calendar.DATE, 1);
  62. dayOfWeek = javaCalendar.get(Calendar.DAY_OF_WEEK);
  63. }
  64. newStart = javaCalendar.getTime();
  65. newEnd = javaCalendar.getTime();
  66. }
  67. setDates(event, newStart, newEnd);
  68. }
  69. /**
  70. * Set the start and end dates for the event
  71. *
  72. * @param event
  73. * The event that the start and end dates should be set
  74. * @param start
  75. * The start date
  76. * @param end
  77. * The end date
  78. */
  79. protected void setDates(ForwardEvent event, Date start, Date end) {
  80. event.getComponent().setStartDate(start);
  81. event.getComponent().setEndDate(end);
  82. }
  83. }