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.

WeeklyLongEvents.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.client.ui.calendar.schedule;
  17. import java.util.Date;
  18. import java.util.List;
  19. import com.google.gwt.user.client.ui.HorizontalPanel;
  20. import com.vaadin.v7.client.ui.VCalendar;
  21. /**
  22. *
  23. * @since 7.1
  24. * @author Vaadin Ltd.
  25. *
  26. */
  27. public class WeeklyLongEvents extends HorizontalPanel implements HasTooltipKey {
  28. public static final int EVENT_HEIGTH = 15;
  29. public static final int EVENT_MARGIN = 1;
  30. private int rowCount = 0;
  31. private VCalendar calendar;
  32. private boolean undefinedWidth;
  33. public WeeklyLongEvents(VCalendar calendar) {
  34. setStylePrimaryName("v-calendar-weekly-longevents");
  35. this.calendar = calendar;
  36. }
  37. public void addDate(Date d) {
  38. DateCellContainer dcc = new DateCellContainer();
  39. dcc.setDate(d);
  40. dcc.setCalendar(calendar);
  41. add(dcc);
  42. }
  43. public void setWidthPX(int width) {
  44. if (getWidgetCount() == 0) {
  45. return;
  46. }
  47. undefinedWidth = (width < 0);
  48. updateCellWidths();
  49. }
  50. public void addEvents(List<CalendarEvent> events) {
  51. for (CalendarEvent e : events) {
  52. addEvent(e);
  53. }
  54. }
  55. public void addEvent(CalendarEvent calendarEvent) {
  56. updateEventSlot(calendarEvent);
  57. int dateCount = getWidgetCount();
  58. Date from = calendarEvent.getStart();
  59. Date to = calendarEvent.getEnd();
  60. boolean started = false;
  61. for (int i = 0; i < dateCount; i++) {
  62. DateCellContainer dc = (DateCellContainer) getWidget(i);
  63. Date dcDate = dc.getDate();
  64. int comp = dcDate.compareTo(from);
  65. int comp2 = dcDate.compareTo(to);
  66. WeeklyLongEventsDateCell eventLabel = dc
  67. .getDateCell(calendarEvent.getSlotIndex());
  68. eventLabel.setStylePrimaryName("v-calendar-event");
  69. if (comp >= 0 && comp2 <= 0) {
  70. eventLabel.setEvent(calendarEvent);
  71. eventLabel.setCalendar(calendar);
  72. eventLabel.addStyleDependentName("all-day");
  73. if (comp == 0) {
  74. eventLabel.addStyleDependentName("start");
  75. }
  76. if (comp2 == 0) {
  77. eventLabel.addStyleDependentName("end");
  78. }
  79. if (!started && comp > 0 && comp2 <= 0) {
  80. eventLabel.addStyleDependentName("continued-from");
  81. } else if (i == (dateCount - 1)) {
  82. eventLabel.addStyleDependentName("continued-to");
  83. }
  84. final String extraStyle = calendarEvent.getStyleName();
  85. if (extraStyle != null && !extraStyle.isEmpty()) {
  86. eventLabel.addStyleDependentName(extraStyle + "-all-day");
  87. }
  88. if (!started) {
  89. if (calendar.isEventCaptionAsHtml()) {
  90. eventLabel.setHTML(calendarEvent.getCaption());
  91. } else {
  92. eventLabel.setText(calendarEvent.getCaption());
  93. }
  94. started = true;
  95. }
  96. }
  97. }
  98. }
  99. private void updateEventSlot(CalendarEvent e) {
  100. boolean foundFreeSlot = false;
  101. int slot = 0;
  102. while (!foundFreeSlot) {
  103. if (isSlotFree(slot, e.getStart(), e.getEnd())) {
  104. e.setSlotIndex(slot);
  105. foundFreeSlot = true;
  106. } else {
  107. slot++;
  108. }
  109. }
  110. }
  111. private boolean isSlotFree(int slot, Date start, Date end) {
  112. int dateCount = getWidgetCount();
  113. // Go over all dates this week
  114. for (int i = 0; i < dateCount; i++) {
  115. DateCellContainer dc = (DateCellContainer) getWidget(i);
  116. Date dcDate = dc.getDate();
  117. int comp = dcDate.compareTo(start);
  118. int comp2 = dcDate.compareTo(end);
  119. // check if the date is in the range we need
  120. if (comp >= 0 && comp2 <= 0) {
  121. // check if the slot is taken
  122. if (dc.hasEvent(slot)) {
  123. return false;
  124. }
  125. }
  126. }
  127. return true;
  128. }
  129. public int getRowCount() {
  130. return rowCount;
  131. }
  132. public void updateCellWidths() {
  133. int cells = getWidgetCount();
  134. if (cells <= 0) {
  135. return;
  136. }
  137. int cellWidth = -1;
  138. // if width is undefined, use the width of the first cell
  139. // otherwise use distributed sizes
  140. if (undefinedWidth) {
  141. cellWidth = calendar.getWeekGrid().getDateCellWidth()
  142. - calendar.getWeekGrid().getDateSlotBorder();
  143. }
  144. for (int i = 0; i < cells; i++) {
  145. DateCellContainer dc = (DateCellContainer) getWidget(i);
  146. if (undefinedWidth) {
  147. dc.setWidth(cellWidth + "px");
  148. } else {
  149. dc.setWidth(
  150. calendar.getWeekGrid().getDateCellWidths()[i] + "px");
  151. }
  152. }
  153. }
  154. @Override
  155. public String getTooltipKey() {
  156. return null;
  157. }
  158. }