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.

CalendarEvent.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 com.google.gwt.i18n.client.DateTimeFormat;
  19. import com.vaadin.v7.shared.ui.calendar.DateConstants;
  20. /**
  21. * A client side implementation of a calendar event.
  22. *
  23. * @since 7.1
  24. * @author Vaadin Ltd.
  25. */
  26. public class CalendarEvent {
  27. private int index;
  28. private String caption;
  29. private Date start, end;
  30. private String styleName;
  31. private Date startTime, endTime;
  32. private String description;
  33. private int slotIndex = -1;
  34. private boolean format24h;
  35. DateTimeFormat dateformatDate = DateTimeFormat.getFormat("h:mm a");
  36. DateTimeFormat dateformatDate24 = DateTimeFormat.getFormat("H:mm");
  37. private boolean allDay;
  38. /**
  39. * @see com.vaadin.addon.calendar.event.CalendarEvent#getStyleName()
  40. */
  41. public String getStyleName() {
  42. return styleName;
  43. }
  44. /**
  45. * @see com.vaadin.addon.calendar.event.CalendarEvent#getStart()
  46. */
  47. public Date getStart() {
  48. return start;
  49. }
  50. /**
  51. * @see com.vaadin.addon.calendar.event.CalendarEvent#getStyleName()
  52. * @param style
  53. */
  54. public void setStyleName(String style) {
  55. styleName = style;
  56. }
  57. /**
  58. * @see com.vaadin.addon.calendar.event.CalendarEvent#getStart()
  59. * @param start
  60. */
  61. public void setStart(Date start) {
  62. this.start = start;
  63. }
  64. /**
  65. * @see com.vaadin.addon.calendar.event.CalendarEvent#getEnd()
  66. * @return
  67. */
  68. public Date getEnd() {
  69. return end;
  70. }
  71. /**
  72. * @see com.vaadin.addon.calendar.event.CalendarEvent#getEnd()
  73. * @param end
  74. */
  75. public void setEnd(Date end) {
  76. this.end = end;
  77. }
  78. /**
  79. * Returns the start time of the event.
  80. *
  81. * @return Time embedded in the {@link Date} object
  82. */
  83. public Date getStartTime() {
  84. return startTime;
  85. }
  86. /**
  87. * Set the start time of the event.
  88. *
  89. * @param startTime
  90. * The time of the event. Use the time fields in the {@link Date}
  91. * object
  92. */
  93. public void setStartTime(Date startTime) {
  94. this.startTime = startTime;
  95. }
  96. /**
  97. * Get the end time of the event.
  98. *
  99. * @return Time embedded in the {@link Date} object
  100. */
  101. public Date getEndTime() {
  102. return endTime;
  103. }
  104. /**
  105. * Set the end time of the event.
  106. *
  107. * @param endTime
  108. * Time embedded in the {@link Date} object
  109. */
  110. public void setEndTime(Date endTime) {
  111. this.endTime = endTime;
  112. }
  113. /**
  114. * Get the (server side) index of the event.
  115. *
  116. * @return
  117. */
  118. public int getIndex() {
  119. return index;
  120. }
  121. /**
  122. * Get the index of the slot where the event in rendered.
  123. *
  124. * @return
  125. */
  126. public int getSlotIndex() {
  127. return slotIndex;
  128. }
  129. /**
  130. * Set the index of the slot where the event in rendered.
  131. *
  132. * @param index
  133. * The index of the slot
  134. */
  135. public void setSlotIndex(int index) {
  136. slotIndex = index;
  137. }
  138. /**
  139. * Set the (server side) index of the event.
  140. *
  141. * @param index
  142. * The index
  143. */
  144. public void setIndex(int index) {
  145. this.index = index;
  146. }
  147. /**
  148. * Get the caption of the event. The caption is the text displayed in the
  149. * calendar on the event.
  150. *
  151. * @return
  152. */
  153. public String getCaption() {
  154. return caption;
  155. }
  156. /**
  157. * Set the caption of the event. The caption is the text displayed in the
  158. * calendar on the event.
  159. *
  160. * @param caption
  161. * The visible caption of the event
  162. */
  163. public void setCaption(String caption) {
  164. this.caption = caption;
  165. }
  166. /**
  167. * Get the description of the event. The description is the text displayed
  168. * when hovering over the event with the mouse
  169. *
  170. * @return
  171. */
  172. public String getDescription() {
  173. return description;
  174. }
  175. /**
  176. * Set the description of the event. The description is the text displayed
  177. * when hovering over the event with the mouse
  178. *
  179. * @param description
  180. */
  181. public void setDescription(String description) {
  182. this.description = description;
  183. }
  184. /**
  185. * Does the event use the 24h time format.
  186. *
  187. * @param format24h
  188. * True if it uses the 24h format, false if it uses the 12h time
  189. * format
  190. */
  191. public void setFormat24h(boolean format24h) {
  192. this.format24h = format24h;
  193. }
  194. /**
  195. * Is the event an all day event.
  196. *
  197. * @param allDay
  198. * True if the event should be rendered all day
  199. */
  200. public void setAllDay(boolean allDay) {
  201. this.allDay = allDay;
  202. }
  203. /**
  204. * Is the event an all day event.
  205. *
  206. * @return
  207. */
  208. public boolean isAllDay() {
  209. return allDay;
  210. }
  211. /**
  212. * Get the time as a formatted string.
  213. *
  214. * @return
  215. */
  216. public String getTimeAsText() {
  217. if (format24h) {
  218. return dateformatDate24.format(startTime);
  219. } else {
  220. return dateformatDate.format(startTime);
  221. }
  222. }
  223. /**
  224. * Get the amount of milliseconds between the start and end of the event.
  225. *
  226. * @return
  227. */
  228. public long getRangeInMilliseconds() {
  229. return getEndTime().getTime() - getStartTime().getTime();
  230. }
  231. /**
  232. * Get the amount of minutes between the start and end of the event.
  233. *
  234. * @return
  235. */
  236. public long getRangeInMinutes() {
  237. return (getRangeInMilliseconds() / DateConstants.MINUTEINMILLIS);
  238. }
  239. /**
  240. * Get the amount of minutes for the event on a specific day. This is useful
  241. * if the event spans several days.
  242. *
  243. * @param targetDay
  244. * The date to check
  245. * @return
  246. */
  247. public long getRangeInMinutesForDay(Date targetDay) {
  248. long rangeInMinutesForDay = 0;
  249. // we must take into account that here can be not only 1 and 2 days, but
  250. // 1, 2, 3, 4... days first and last days - special cases all another
  251. // days between first and last - have range "ALL DAY"
  252. if (isTimeOnDifferentDays()) {
  253. if (targetDay.compareTo(getStart()) == 0) { // for first day
  254. rangeInMinutesForDay = DateConstants.DAYINMINUTES
  255. - (getStartTime().getTime() - getStart().getTime())
  256. / DateConstants.MINUTEINMILLIS;
  257. } else if (targetDay.compareTo(getEnd()) == 0) { // for last day
  258. rangeInMinutesForDay = (getEndTime().getTime()
  259. - getEnd().getTime()) / DateConstants.MINUTEINMILLIS;
  260. } else { // for in-between days
  261. rangeInMinutesForDay = DateConstants.DAYINMINUTES;
  262. }
  263. } else { // simple case - period is in one day
  264. rangeInMinutesForDay = getRangeInMinutes();
  265. }
  266. return rangeInMinutesForDay;
  267. }
  268. /**
  269. * Does the event span several days.
  270. *
  271. * @return
  272. */
  273. @SuppressWarnings("deprecation")
  274. public boolean isTimeOnDifferentDays() {
  275. boolean isSeveralDays = false;
  276. // if difference between start and end times is more than day - of
  277. // course it is not one day, but several days
  278. if (getEndTime().getTime()
  279. - getStartTime().getTime() > DateConstants.DAYINMILLIS) {
  280. isSeveralDays = true;
  281. } else { // if difference <= day ->
  282. isSeveralDays = (getStart().compareTo(getEnd()) != 0)
  283. && !((getEndTime().getHours() == 0
  284. && getEndTime().getMinutes() == 0));
  285. }
  286. return isSeveralDays;
  287. }
  288. }