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.

components-calendar.asciidoc 41KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  1. ---
  2. title: Calendar
  3. order: 30
  4. layout: page
  5. ---
  6. [[components.calendar]]
  7. = [classname]#Calendar#
  8. ifdef::web[]
  9. [.sampler]
  10. image:{live-demo-image}[alt="Live Demo", link="http://demo.vaadin.com/sampler/#ui/data-input/dates/dates-calendar"]
  11. endif::web[]
  12. The [classname]#Calendar# component allows organizing and displaying calendar
  13. events. The main features of the calendar include:
  14. * Monthly, weekly, and daily views
  15. * Two types of events: all-day events and events with a time range
  16. * Add events directly, from a [classname]#Container#, or with an event provider
  17. * Control the range of the visible dates
  18. * Selecting and editing date or time range by dragging
  19. * Drag and drop events to calendar
  20. * Support for localization and timezones
  21. User interaction with the calendar elements, such as date and week captions as
  22. well as events, is handled with event listeners. Also date/time range
  23. selections, event dragging, and event resizing can be listened by the server.
  24. The weekly view has navigation buttons to navigate forward and backward in time.
  25. These actions are also listened by the server. Custom navigation can be
  26. implemented using event handlers
  27. ifdef::web[]
  28. , as described in
  29. <<components.calendar.customizing>>
  30. endif::web[]
  31. .
  32. The data source of a calendar can be practically anything, as its events are
  33. queried dynamically by the component. You can bind the calendar to a Vaadin
  34. container, or to any other data source by implementing an __event provider__.
  35. The [classname]#Calendar# has undefined size by default and you usually want to
  36. give it a fixed or relative size, for example as follows.
  37. [source, java]
  38. ----
  39. Calendar cal = new Calendar("My Calendar");
  40. cal.setWidth("600px");
  41. cal.setHeight("300px");
  42. ----
  43. After creating the calendar, you need to set a time range for it, which also
  44. controls the view mode, and set up the data source for calendar events.
  45. [[components.calendar.daterange]]
  46. == Date Range and View Mode
  47. The Vaadin Calendar has two types of views that are shown depending on the date
  48. range of the calendar. The __weekly view__ displays a week by default. It can
  49. show anything between one to seven days a week, and is also used as a single-day
  50. view. The view mode is determined from the __date range__ of the calendar,
  51. defined by a start and an end date. Calendar will be shown in a __monthly view__
  52. when the date range is over than one week (seven days) long. The date range is
  53. always calculated in an accuracy of one millisecond.
  54. [[figure.components.calendar.daterange.monthly]]
  55. .Monthly view with All-Day and Normal Events
  56. image::img/calendar-monthly.png[]
  57. The monthly view, shown in <<figure.components.calendar.daterange.monthly>>, can
  58. easily be used to control all types of events, but it is best suited for events
  59. that last for one or more days. You can drag the events to move them. In the
  60. figure, you can see two longer events that are highlighted with a blue and green
  61. background color. Other markings are shorter day events that last less than a 24
  62. hours. These events can not be moved by dragging in the monthly view.
  63. [[figure.components.calendar.daterange.weekly]]
  64. .Weekly View
  65. image::img/calendar-weekly.png[]
  66. In <<figure.components.calendar.daterange.weekly>>, you can see four normal day
  67. events and also all-day events at the top of the time line grid.
  68. In the following, we set the calendar to show only one day, which is the current
  69. day.
  70. [source, java]
  71. ----
  72. cal.setStartDate(new Date());
  73. cal.setEndDate(new Date());
  74. ----
  75. Notice that although the range we set above is actually zero time long, the
  76. calendar still renders the time from 00:00 to 23:59. This is normal, as the
  77. Vaadin Calendar is guaranteed to render at least the date range provided, but
  78. may expand it. This behaviour is important to notice when we implement our own
  79. event providers.
  80. [[components.calendar.events]]
  81. == Calendar Events
  82. All occurrences in a calendar are represented as __events__. You have three ways
  83. to manage the calendar events:
  84. * Add events directly to the [classname]#Calendar# object using the [methodname]#addEvent()#
  85. * Use a [interfacename]#Container# as a data source
  86. * Use the __event provider__ mechanism
  87. You can add events with [methodname]#addEvent()# and remove them with the
  88. [methodname]#removeEvent()#. These methods will use the underlying event
  89. provider to write the modifications to the data source.
  90. [[components.calendar.events.types]]
  91. === Event Interfaces and Providers
  92. Events are handled though the [interfacename]#CalendarEvent# interface. The
  93. concrete class of the event depends on the specific
  94. [classname]#CalendarEventProvider# used in the calendar.
  95. By default, [classname]#Calendar# uses a [classname]#BasicEventProvider# to
  96. provide events, which uses [classname]#BasicEvent# instances.
  97. Calendar does not depend on any particular data source implementation. Events
  98. are queried by the [classname]#Calendar# from the provider that just has to
  99. implement the [interfacename]#CalendarEventProvider# interface. It is up to the
  100. event provider that [classname]#Calendar# gets the correct events.
  101. You can bind any Vaadin [classname]#Container# to a calendar, in which case a
  102. [classname]#ContainerEventProvider# is used transparently. The container must be
  103. ordered by start date and time of the events. See
  104. <<dummy/../../../framework/datamodel/datamodel-container#datamodel.container,"Collecting
  105. Items in Containers">> for basic information about containers.
  106. [[components.calendar.events.details]]
  107. === Event Types
  108. A calendar event requires a start time and an end time. These are the only
  109. mandatory properties. In addition, an event can also be set as an all-day event
  110. by setting the [literal]#++all-day++# property of the event. You can also set
  111. the [literal]#++description++# of an event, which is displayed as a tooltip in
  112. the user interface.
  113. If the [literal]#++all-day++# field of the event is [literal]#++true++#, then
  114. the event is always rendered as an all-day event. In the monthly view, this
  115. means that no start time is displayed in the user interface and the event has an
  116. colored background. In the weekly view, all-day events are displayed in the
  117. upper part of the screen, and rendered similarly to the monthly view. In
  118. addition, when the time range of an event is 24 hours or longer, it is rendered
  119. as an all-day event in the monthly view.
  120. When the time range of an event is equal or less than 24 hours, with the
  121. accuracy of one millisecond, the event is considered as a normal day event.
  122. Normal event has a start and end times that may be on different days.
  123. [[components.calendar.events.basic]]
  124. === Basic Events
  125. The easiest way to add and manage events in a calendar is to use the __basic
  126. event__ management API. Calendar uses by default a
  127. [classname]#BasicEventProvider#, which keeps the events in memory in an internal
  128. reprensetation.
  129. For example, the following adds a two-hour event starting from the current time.
  130. The standard Java [classname]#GregorianCalendar# provides various ways to
  131. manipulate date and time.
  132. [source, java]
  133. ----
  134. // Add a two-hour event
  135. GregorianCalendar start = new GregorianCalendar();
  136. GregorianCalendar end = new GregorianCalendar();
  137. end.add(java.util.Calendar.HOUR, 2);
  138. calendar.addEvent(new BasicEvent("Calendar study",
  139. "Learning how to use Vaadin Calendar",
  140. start.getTime(), end.getTime()));
  141. ----
  142. See the http://demo.vaadin.com/book-examples-vaadin7/book#calendar.monthlyview[on-line example, window="_blank"].
  143. This adds a new event that lasts for 3 hours. As the BasicEventProvider and
  144. BasicEvent implement some optional event interfaces provided by the calendar
  145. package, there is no need to refresh the calendar. Just create events, set their
  146. properties and add them to the Event Provider.
  147. [[components.calendar.container]]
  148. == Getting Events from a Container
  149. You can use any Vaadin [interfacename]#Container# that implements the
  150. [interfacename]#Indexed# interface as the data source for calendar events. The
  151. [classname]#Calendar# will listen to change events from the container as well as
  152. write changes to the container. You can attach a container to a
  153. [classname]#Calendar# with [methodname]#setContainerDataSource()#.
  154. In the following example, we bind a [classname]#BeanItemContainer# that contains
  155. built-in [classname]#BasicEvent# events to a calendar.
  156. [source, java]
  157. ----
  158. // Create the calendar
  159. Calendar calendar = new Calendar("Bound Calendar");
  160. // Use a container of built-in BasicEvents
  161. final BeanItemContainer<BasicEvent> container =
  162. new BeanItemContainer<BasicEvent>(BasicEvent.class);
  163. // Create a meeting in the container
  164. container.addBean(new BasicEvent("The Event", "Single Event",
  165. new GregorianCalendar(2012,1,14,12,00).getTime(),
  166. new GregorianCalendar(2012,1,14,14,00).getTime()));
  167. // The container must be ordered by the start time. You
  168. // have to sort the BIC every time after you have added
  169. // or modified events.
  170. container.sort(new Object[]{"start"}, new boolean[]{true});
  171. calendar.setContainerDataSource(container, "caption",
  172. "description", "start", "end", "styleName");
  173. ----
  174. See the http://demo.vaadin.com/book-examples-vaadin7/book#calendar.beanitemcontainer[on-line example, window="_blank"].
  175. The container must either use the default property IDs for event data, as
  176. defined in the [interfacename]#CalendarEvent# interface, or provide them as
  177. parameters for the [methodname]#setContainerDataSource()# method, as we did in
  178. the example above.
  179. [[components.calendar.container.sorting]]
  180. === Keeping the Container Ordered
  181. The events in the container __must__ be kept ordered by their start date/time.
  182. Failing to do so may and will result in the events not showing in the calendar
  183. properly.
  184. Ordering depends on the container. With some containers, such as
  185. [classname]#BeanItemContainer#, you have to sort the container explicitly every
  186. time after you have added or modified events, usually with the
  187. [methodname]#sort()# method, as we did in the example above. Some container,
  188. such as [classname]#JPAContainer#, keep the in container automatically order if
  189. you provide a sorting rule.
  190. For example, you could order a [classname]#JPAContainer# by the following rule,
  191. assuming that the start date/time is held in the [literal]#++startDate++#
  192. property:
  193. [source, java]
  194. ----
  195. // The container must be ordered by start date. For JPAContainer
  196. // we can just set up sorting once and it will stay ordered.
  197. container.sort(new String[]{"startDate"}, new boolean[]{true});
  198. ----
  199. See the http://demo.vaadin.com/book-examples-vaadin7/book#calendar.jpacontainer[on-line example, window="_blank"].
  200. ifdef::web[]
  201. [[components.calendar.container.customization]]
  202. === Delegation of Event Management
  203. Setting a container as the calendar data source with
  204. [methodname]#setContainerDataSource()# automatically switches to
  205. [classname]#ContainerEventProvider#. You can manipulate the event data through
  206. the API in [classname]#Calendar# and the user can move and resize event through
  207. the user interface. The event provider delegates all such calendar operations to
  208. the container.
  209. If you add events through the [classname]#Calendar# API, notice that you may be
  210. unable to create events of the type held in the container or adding them
  211. requires some container-specific operations. In such case, you may need to
  212. customize the [methodname]#addEvent()# method.
  213. For example, [classname]#JPAContainer# requires adding new items with
  214. [methodname]#addEntity()#. You could first add the entity to the container or
  215. entity manager directly and then pass it to the [methodname]#addEvent()#. That
  216. does not, however, work if the entity class does not implement
  217. [interfacename]#CalendarEvent#. This is actually the case always if the property
  218. names differ from the ones defined in the interface. You could handle creating
  219. the underlying entity objects in the [methodname]#addEvent()# as follows:
  220. [source, java]
  221. ----
  222. // Create a JPAContainer
  223. final JPAContainer<MyCalendarEvent> container =
  224. JPAContainerFactory.make(MyCalendarEvent.class,
  225. "book-examples");
  226. // Customize the event provider for adding events
  227. // as entities
  228. ContainerEventProvider cep =
  229. new ContainerEventProvider(container) {
  230. @Override
  231. public void addEvent(CalendarEvent event) {
  232. MyCalendarEvent entity = new MyCalendarEvent(
  233. event.getCaption(), event.getDescription(),
  234. event.getStart(), event.getEnd(),
  235. event.getStyleName());
  236. container.addEntity(entity);
  237. }
  238. }
  239. // Set the container as the data source
  240. calendar.setEventProvider(cep);
  241. // Now we can add events to the database through the calendar
  242. BasicEvent event = new BasicEvent("The Event", "Single Event",
  243. new GregorianCalendar(2012,1,15,12,00).getTime(),
  244. new GregorianCalendar(2012,1,15,14,00).getTime());
  245. calendar.addEvent(event);
  246. ----
  247. endif::web[]
  248. ifdef::web[]
  249. [[components.calendar.eventprovider]]
  250. == Implementing an Event Provider
  251. If the two simple ways of storing and managing events for a calendar are not
  252. enough, you may need to implement a custom event provider. It is the most
  253. flexible way of providing events. You need to attach the event provider to the
  254. [classname]#Calendar# using the [methodname]#setEventProvider()# method.
  255. Event queries are done by asking the event provider for all the events between
  256. two given dates. The range of these dates is guaranteed to be at least as long
  257. as the start and end dates set for the component. The component can, however,
  258. ask for a longer range to ensure correct rendering. In particular, all start
  259. dates are expanded to the start of the day, and all end dates are expanded to
  260. the end of the day.
  261. [[components.calendar.eventprovider.customevents]]
  262. === Custom Events
  263. An event provider could use the built-in [classname]#BasicEvent#, but it is
  264. usually more proper to define a custom event type that is bound directly to the
  265. data source. Custom events may be useful for some other purposes as well, such
  266. as when you need to add extra information to an event or customize how it is
  267. acquired.
  268. Custom events must implement the [interfacename]#CalendarEvent# interface or
  269. extend an existing event class. The built-in [classname]#BasicEvent# class
  270. should serve as a good example of implementing simple events. It keeps the data
  271. in member variables.
  272. [source, java]
  273. ----
  274. public class BasicEvent
  275. implements CalendarEventEditor, EventChangeNotifier {
  276. ...
  277. public String getCaption() {
  278. return caption;
  279. }
  280. public String getDescription() {
  281. return description;
  282. }
  283. public Date getEnd() {
  284. return end;
  285. }
  286. public Date getStart() {
  287. return start;
  288. }
  289. public String getStyleName() {
  290. return styleName;
  291. }
  292. public boolean isAllDay() {
  293. return isAllDay;
  294. }
  295. public void setCaption(String caption) {
  296. this.caption = caption;
  297. fireEventChange();
  298. }
  299. public void setDescription(String description) {
  300. this.description = description;
  301. fireEventChange();
  302. }
  303. public void setEnd(Date end) {
  304. this.end = end;
  305. fireEventChange();
  306. }
  307. public void setStart(Date start) {
  308. this.start = start;
  309. fireEventChange();
  310. }
  311. public void setStyleName(String styleName) {
  312. this.styleName = styleName;
  313. fireEventChange();
  314. }
  315. public void setAllDay(boolean isAllDay) {
  316. this.isAllDay = isAllDay;
  317. fireEventChange();
  318. }
  319. public void addEventChangeListener(
  320. EventChangeListener listener) {
  321. ...
  322. }
  323. public void removeListener(EventChangeListener listener) {
  324. ...
  325. }
  326. protected void fireEventChange() {...}
  327. }
  328. ----
  329. You may have noticed that there was some additional code in the
  330. [classname]#BasicEvent# that was not in the [interfacename]#CalendarEvent#
  331. interface. Namely [classname]#BasicEvent# also implements two additional
  332. interfaces:
  333. [interfacename]#CalendarEditor#:: This interface defines setters for all the fields, and is required for some of
  334. the default handlers to work.
  335. [interfacename]#EventChangeNotifier#:: This interface adds the possibility to listen for changes in the event, and
  336. enables the [classname]#Calendar# to render the changes immediately.
  337. The start time and end time are mandatory, but caption, description, and style
  338. name are not. The style name is used as a part of the CSS class name for the
  339. HTML DOM element of the event.
  340. In addition to the basic event interfaces, you can enhance the functionality of
  341. your event and event provider classes by using the [classname]#EventChange# and
  342. [classname]#EventSetChange# events. They let the [classname]#Calendar# component
  343. to know about changes in events and update itself accordingly. The
  344. [classname]#BasicEvent# and [classname]#BasicEventProvider# examples given
  345. earlier include a simple implementation of these interfaces.
  346. [[components.calendar.eventprovider.eventprovider]]
  347. === Implementing the Event Provider
  348. An event provider needs to implement the [interfacename]#CalendarEventProvider#
  349. interface. It has only one method to be implemented. Whenever the calendar is
  350. painted, [methodname]#getEvents(Date, Date)# method is called and it must return
  351. a list of events between the given start and end time.
  352. The following example implementation returns only one example event. The event
  353. starts from the current time and is five hours long.
  354. [source, java]
  355. ----
  356. public class MyEventProvider implements CalendarEventProvider{
  357. public List<Event> getEvents(Date startDate, Date endDate){
  358. List<Event> events = new ArrayList<Event>();
  359. GregorianCalendar cal = new GregorianCalendar();
  360. cal.setTime(new Date());
  361. Date start = cal.getTime();
  362. cal.add(GregorianCalendar.HOUR, 5);
  363. Date end = cal.getTime();
  364. BasicEvent event = new BasicEvent();
  365. event.setCaption("My Event");
  366. event.setDescription("My Event Description");
  367. event.setStart(start);
  368. event.setEnd(end);
  369. events.add(event);
  370. return events;
  371. }
  372. }
  373. ----
  374. It is important to notice that the [classname]#Calendar# may query for dates
  375. beyond the range defined by start date and end date. Particularly, it may expand
  376. the date range to make sure the user interface is rendered correctly.
  377. endif::web[]
  378. ifdef::web[]
  379. [[components.calendar.appearance]]
  380. == Styling a Calendar
  381. Configuring the appearance of the Vaadin Calendar component is one of the basic
  382. tasks. At the least, you need to consider its sizing in your user interface. You
  383. also quite probably want to use some color or colors for events.
  384. [[components.calendar.appearance.sizing]]
  385. === Sizing
  386. The Calendar supports component sizing as usual for defined (fixed or relative)
  387. sizes. When using an undefined size for the calendar, all the sizes come from
  388. CSS. In addition, when the height is undefined, a scrollbar is displayed in the
  389. weekly view to better fit the cells to the user interface.
  390. Below is a list of style rules that define the size of a Calendar with undefined
  391. size (these are the defaults):
  392. [source, css]
  393. ----
  394. .v-calendar-month-sizedheight .v-calendar-month-day {
  395. height: 100px;
  396. }
  397. .v-calendar-month-sizedwidth .v-calendar-month-day {
  398. width: 100px;
  399. }
  400. .v-calendar-header-month-Hsized .v-calendar-header-day {
  401. width: 101px;
  402. }
  403. /* for IE */
  404. .v-ie6 .v-calendar-header-month-Hsized .v-calendar-header-day {
  405. width: 104px;
  406. }
  407. /* for others */
  408. .v-calendar-header-month-Hsized td:first-child {
  409. padding-left: 21px;
  410. }
  411. .v-calendar-header-day-Hsized {
  412. width: 200px;
  413. }
  414. .v-calendar-week-numbers-Vsized .v-calendar-week-number {
  415. height: 100px;
  416. line-height: 100px;
  417. }
  418. .v-calendar-week-wrapper-Vsized {
  419. height: 400px;
  420. overflow-x: hidden !important;
  421. }
  422. .v-calendar-times-Vsized .v-calendar-time {
  423. height: 38px;
  424. }
  425. .v-calendar-times-Hsized .v-calendar-time {
  426. width: 42px;
  427. }
  428. .v-calendar-day-times-Vsized .v-slot,.v-calendar-day-times-Vsized .v-slot-even {
  429. height: 18px;
  430. }
  431. .v-calendar-day-times-Hsized, .v-calendar-day-times-Hsized .v-slot,.v-calendar-day-times-Hsized .v-slot-even {
  432. width: 200px;
  433. }
  434. ----
  435. [[components.calendar.appearance.event-style]]
  436. === Event Style
  437. Events can be styled with CSS by setting them a __style name suffix__. The
  438. suffix is retrieved with the [methodname]#getStyleName()# method in
  439. [interfacename]#CalendarEvent#. If you use [classname]#BasicEvent# events, you
  440. can set the suffix with [methodname]#setStyleName()#.
  441. [source, java]
  442. ----
  443. BasicEvent event = new BasicEvent("Wednesday Wonder", ... );
  444. event.setStyleName("mycolor");
  445. calendar.addEvent(event);
  446. ----
  447. Suffix [literal]#++mycolor++# would create
  448. [literal]#++v-calendar-event-mycolor++# class for regular events and
  449. [literal]#++v-calendar-event-mycolor-add-day++# for all-day events. You could
  450. style the events with the following rules:
  451. [source, css]
  452. ----
  453. .v-calendar .v-calendar-event-mycolor {}
  454. .v-calendar .v-calendar-event-mycolor-all-day {}
  455. .v-calendar .v-calendar-event-mycolor .v-calendar-event-caption {}
  456. .v-calendar .v-calendar-event-mycolor .v-calendar-event-content {}
  457. ----
  458. endif::web[]
  459. ifdef::web[]
  460. [[components.calendar.visible-hours-days]]
  461. == Visible Hours and Days
  462. As we saw in <<components.calendar.daterange>>, you can set the range of dates
  463. that are shown by the Calendar. But what if you wanted to show the entire month
  464. but hide the weekends? Or show only hours from 8 to 16 in the weekly view? The
  465. [methodname]#setVisibleDays()# and [methodname]#setVisibleHours()# methods allow
  466. you to do that.
  467. [source, java]
  468. ----
  469. calendar.setVisibleDays(1,5); // Monday to Friday
  470. calendar.setVisibleHours(0,15); // Midnight until 4 pm
  471. ----
  472. After the above settings, only weekdays from Monday to Friday would be shown.
  473. And when the calendar is in the weekly view, only the time range from 00:00 to
  474. 16:00 would be shown.
  475. Note that the excluded times are never shown so you should take care when
  476. setting the date range. If the date range contains only dates / times that are
  477. excluded, nothing will be displayed. Also note that even if a date is not
  478. rendered because these settings, the event provider may still be queried for
  479. events for that date.
  480. endif::web[]
  481. ifdef::web[]
  482. [[components.calendar.drag-and-drop]]
  483. == Drag and Drop
  484. Vaadin Calendar can act as a drop target for drag and drop, described in
  485. <<dummy/../../../framework/advanced/advanced-dragndrop#advanced.dragndrop,"Drag
  486. and Drop">>. With the functionality, the user could drag events, for example,
  487. from a table to a calendar.
  488. To support dropping, a [classname]#Calendar# must have a drop handler. When the
  489. drop handler is set, the days in the monthly view and the time slots in the
  490. weekly view can receive drops. Other locations, such as day names in the weekly
  491. view, can not currently receive drops.
  492. Calendar uses its own implementation of [interfacename]#TargetDetails#:
  493. [classname]#CalendarTargetdetails#. It holds information about the the drop
  494. location, which in the context of [classname]#Calendar# means the date and time.
  495. The drop target location can be retrieved via the [methodname]#getDropTime()#
  496. method. If the drop is done in the monthly view, the returned date does not have
  497. exact time information. If the drop happened in the weekly view, the returned
  498. date also contains the start time of the slot.
  499. Below is a short example of creating a drop handler and using the drop
  500. information to create a new event:
  501. [source, java]
  502. ----
  503. private Calendar createDDCalendar() {
  504. Calendar calendar = new Calendar();
  505. calendar.setDropHandler(new DropHandler() {
  506. public void drop(DragAndDropEvent event) {
  507. CalendarTargetDetails details =
  508. (CalendarTargetDetails) event.getTargetDetails();
  509. TableTransferable transferable =
  510. (TableTransferable) event.getTransferable();
  511. createEvent(details, transferable);
  512. removeTableRow(transferable);
  513. }
  514. public AcceptCriterion getAcceptCriterion() {
  515. return AcceptAll.get();
  516. }
  517. });
  518. return calendar;
  519. }
  520. protected void createEvent(CalendarTargetDetails details,
  521. TableTransferable transferable) {
  522. Date dropTime = details.getDropTime();
  523. java.util.Calendar timeCalendar = details.getTargetCalendar()
  524. .getInternalCalendar();
  525. timeCalendar.setTime(dropTime);
  526. timeCalendar.add(java.util.Calendar.MINUTE, 120);
  527. Date endTime = timeCalendar.getTime();
  528. Item draggedItem = transferable.getSourceComponent().
  529. getItem(transferable.getItemId());
  530. String eventType = (String)draggedItem.
  531. getItemProperty("type").getValue();
  532. String eventDescription = "Attending: "
  533. + getParticipantString(
  534. (String[]) draggedItem.
  535. getItemProperty("participants").getValue());
  536. BasicEvent newEvent = new BasicEvent();
  537. newEvent.setAllDay(!details.hasDropTime());
  538. newEvent.setCaption(eventType);
  539. newEvent.setDescription(eventDescription);
  540. newEvent.setStart(dropTime);
  541. newEvent.setEnd(endTime);
  542. BasicEventProvider ep = (BasicEventProvider) details
  543. .getTargetCalendar().getEventProvider();
  544. ep.addEvent(newEvent);
  545. }
  546. ----
  547. endif::web[]
  548. ifdef::web[]
  549. [[components.calendar.contextmenu]]
  550. == Using the Context Menu
  551. Vaadin Calendar allows the use of context menu (mouse right-click) to manage
  552. events. As in other context menus in Vaadin, the menu items are handled in
  553. Vaadin as __actions__ by an __action handler__. To enable a context menu, you
  554. have to implement a Vaadin [interfacename]#Action.Handler# and add it to the
  555. calendar with [methodname]#addActionHandler()#.
  556. An action handler must implement two methods: [methodname]#getActions()# and
  557. [methodname]#handleAction()#. The [methodname]#getActions()# is called for each
  558. day displayed in the calendar view. It should return a list of allowed actions
  559. for that day, that is, the items of the context menu. The [parameter]#target#
  560. parameter is the context of the click - a [classname]#CalendarDateRange# that
  561. spans over the day. The [parameter]#sender# is the [classname]#Calendar# object.
  562. The [methodname]#handleActions()# receives the target context in the
  563. [parameter]#target#. If the context menu was opened on an event, the target is
  564. the [interfacename]#Event# object, otherwise it is a
  565. [classname]#CalendarDateRange#.
  566. endif::web[]
  567. ifdef::web[]
  568. [[components.calendar.localization]]
  569. == Localization and Formatting
  570. [[components.calendar.localization.locale]]
  571. === Setting the Locale and Time Zone
  572. Month and weekday names are shown in the language of the locale setting of the
  573. [classname]#Calendar#. The translations are acquired from the standard Java
  574. locale data. By default, [classname]#Calendar# uses the system default locale
  575. for its internal calendar, but you can change it with
  576. [methodname]#setLocale(Locale locale)#. Setting the locale will update also
  577. other location specific date and time settings, such as the first day of the
  578. week, time zone, and time format. However, time zone and time format can be
  579. overridden by settings in the [classname]#Calendar#.
  580. For example, the following would set the language to US English:
  581. [source, java]
  582. ----
  583. cal.setLocale(Locale.US);
  584. ----
  585. The locale defines the default time zone. You can change it with the
  586. [methodname]#setTimeZone()# method, which takes a
  587. [classname]#java.util.TimeZone# object as its parameter. Setting timezone to
  588. null will reset timezone to the locale default.
  589. For example, the following would set the Finnish time zone, which is EET
  590. [source, java]
  591. ----
  592. cal.setTimeZone(TimeZone.getTimeZone("Europe/Helsinki"));
  593. ----
  594. [[components.calendar.localization.datecaption]]
  595. === Time and Date Caption Format
  596. The time may be shown either in 24 or 12 hour format. The default format is
  597. defined by the locale, but you can change it with the
  598. [methodname]#setTimeFormat()# method. Giving a [literal]#++null++# setting will
  599. reset the time format to the locale default.
  600. [source, java]
  601. ----
  602. cal.setTimeFormat(TimeFormat.Format12H);
  603. ----
  604. You can change the format of the date captions in the week view with the
  605. [methodname]#setWeeklyCaptionFormat(String dateFormatPattern)# method. The date
  606. format pattern should follow the format of the standard Java
  607. [classname]#java.text.SimpleDateFormat# class.
  608. For example:
  609. [source, java]
  610. ----
  611. cal.setWeeklyCaptionFormat("dd-MM-yyyy");
  612. ----
  613. endif::web[]
  614. ifdef::web[]
  615. [[components.calendar.customizing]]
  616. == Customizing the Calendar
  617. In this section, we give a tutorial for how to make various basic customizations
  618. of the Vaadin Calendar. The event provider and styling was described earlier, so
  619. now we concentrate on other features of the Calendar API.
  620. [[components.calendar.customizing.overview]]
  621. === Overview of Handlers
  622. Most of the handlers related to calendar events have sensible default handlers.
  623. These are found in the [package]#com.vaadin.ui.handler# package. The default
  624. handlers and their functionalities are described below.
  625. * [classname]#BasicBackwardHandler#. Handles clicking the back-button of the weekly view so that the viewed month is changed to the previous one.
  626. * [classname]#BasicForwardHandler#. Handles clicking the forward-button of the weekly view so that the viewed month is changed to the next one.
  627. * [classname]#BasicWeekClickHandler#. Handles clicking the week numbers int the monthly view so that the viewable date range is changed to the clicked week.
  628. * [classname]#BasicDateClickHandler#. Handles clicking the dates on both the monthly view and the weekly view. Changes the viewable date range so that only the clicked day is visible.
  629. * [classname]#BasicEventMoveHandler#. Handles moving the events in both monthly view and the weekly view. Events can be moved and their start and end dates are changed correctly, but only if the event implements [classname]#CalendarEventEditor# (implemented by [classname]#BasicEvent#).
  630. * [classname]#BasicEventResizeHandler#. Handles resizing the events in the weekly view. Events can be resized and their start and end dates are changed correctly, but only if the event implements [classname]#CalendarEventEditor# (implemented by the [classname]#BasicEvent#).
  631. All of these handlers are automatically set when creating a new
  632. [classname]#Calendar#. If you wish to disable some of the default functionality,
  633. you can simply set the corresponding handler to [literal]#++null++#. This will
  634. prevent the functionality from ever appearing on the user interface. For
  635. example, if you set the [classname]#EventMoveHandler# to [literal]#++null++#,
  636. the user will be unable to move events in the browser.
  637. [[components.calendar.customizing.creating]]
  638. === Creating a Calendar
  639. Let us first create a new [classname]#Calendar# instance. Here we use our own
  640. event provider, the [classname]#MyEventProvider# described in
  641. <<components.calendar.eventprovider.eventprovider>>.
  642. [source, java]
  643. ----
  644. Calendar cal = new Calendar(new MyEventProvider());
  645. ----
  646. This initializes the Calendar. To customize the viewable date range, we must set
  647. a start and end date to it.
  648. There is only one visible event in the timeline, starting from the current time.
  649. That is what our event provider passes to the client.//TODO See the figure
  650. 3.
  651. It would be nice to also be able to control the navigation forward and backward.
  652. The default navigation is provided by the default handlers, but perhaps we want
  653. to restrict the users so they can only navigate dates in the current year. Maybe
  654. we also want to pose some other restrictions to the clicking week numbers and
  655. dates.
  656. These restrictions and other custom logic can be defined with custom handlers.
  657. You can find the handlers in the [package]#com.vaadin.addon.calendar.ui.handler#
  658. package and they can be easily extended. Note that if you don not want to extend
  659. the default handlers, you are free to implement your own. The interfaces are
  660. described in [interfacename]#CalendarComponentEvents#.
  661. endif::web[]
  662. [[components.calendar.navigation]]
  663. == Backward and Forward Navigation
  664. Vaadin Calendar has only limited built-in navigation support. The weekly view
  665. has navigation buttons in the top left and top right
  666. corners.
  667. // TODO See the figure 4.
  668. You can handle backward and forward navigation with a
  669. [interfacename]#BackwardListener# and [interfacename]#ForwardListener#.
  670. [source, java]
  671. ----
  672. cal.setHandler(new BasicBackwardHandler() {
  673. protected void setDates(BackwardEvent event,
  674. Date start, Date end) {
  675. java.util.Calendar calendar = event.getComponent()
  676. .getInternalCalendar();
  677. if (isThisYear(calendar, end)
  678. && isThisYear(calendar, start)) {
  679. super.setDates(event, start, end);
  680. }
  681. }});
  682. ----
  683. The forward navigation handler can be implemented in the same way. The example
  684. handler restricts the dates to the current year.
  685. ifdef::web[]
  686. [[components.calendar.dateclick]]
  687. == Date Click Handling
  688. By default, clicking a date either in month or week view switches to single-day
  689. view, while clicking on the date header in the day view has no effect. The date
  690. click event is handled by a [interfacename]#DateClickHandler#.
  691. The following example handles click events on the date header in the day view to
  692. zoom out to the week view. For other clicks it applies the default behavior; in
  693. the week view clicking on a day switches to the day view.
  694. [source, java]
  695. ----
  696. calendar.setHandler(new BasicDateClickHandler() {
  697. public void dateClick(DateClickEvent event) {
  698. Calendar cal = event.getComponent();
  699. // Check if the current range is already one day long
  700. long currentCalDateRange = cal.getEndDate().getTime() -
  701. cal.getStartDate().getTime();
  702. // From one-day view, zoom out to week view
  703. if (currentCalDateRange <= DateConstants.DAYINMILLIS) {
  704. // Change the date range to the current week
  705. GregorianCalendar weekstart = new GregorianCalendar();
  706. GregorianCalendar weekend = new GregorianCalendar();
  707. weekstart.setTime(event.getDate());
  708. weekend.setTime(event.getDate());
  709. weekstart.setFirstDayOfWeek(java.util.Calendar.SUNDAY);
  710. weekstart.set(java.util.Calendar.HOUR_OF_DAY, 0);
  711. weekstart.set(java.util.Calendar.DAY_OF_WEEK,
  712. java.util.Calendar.SUNDAY);
  713. weekend.set(java.util.Calendar.HOUR_OF_DAY, 23);
  714. weekend.set(java.util.Calendar.DAY_OF_WEEK,
  715. java.util.Calendar.SATURDAY);
  716. cal.setStartDate(weekstart.getTime());
  717. cal.setEndDate(weekend.getTime());
  718. Notification.show("Custom zoom to week");
  719. } else {
  720. // Default behavior, change date range to one day
  721. super.dateClick(event);
  722. }
  723. }
  724. });
  725. ----
  726. endif::web[]
  727. ifdef::web[]
  728. [[components.calendar.weekclick]]
  729. == Handling Week Clicks
  730. The monthly view displays week numbers for each week row on the left side of the
  731. date grid. The week number are clickable and you can handle the click events by
  732. setting a [interfacename]#WeekClickHandler# for the [classname]#Calendar#
  733. object. The default handler changes the date range to be the clicked week.
  734. In the following example, we add a week click handler that changes the date
  735. range of the calendar to one week only if the start and end dates of the week
  736. are in the current month.
  737. [source, java]
  738. ----
  739. cal.setHandler(new BasicWeekClickHandler() {
  740. protected void setDates(WeekClick event,
  741. Date start, Date end) {
  742. java.util.Calendar calendar = event.getComponent()
  743. .getInternalCalendar();
  744. if (isThisMonth(calendar, start)
  745. && isThisMonth(calendar, end)) {
  746. super.setDates(event, start, end);
  747. }
  748. }
  749. });
  750. ----
  751. endif::web[]
  752. ifdef::web[]
  753. [[components.calendar.eventclick]]
  754. == Handling Event Clicks
  755. The calendar events in all views are are clickable. There is no default handler.
  756. Just like the date and week click handlers, event click handling is enabled by
  757. setting an [interfacename]#EventClickHandler# for the [classname]#Calendar#
  758. object.
  759. You can get hold of the clicked event by the [methodname]#getCalendarEvent()#
  760. method in the [classname]#EventClick# object passed to the handler, as shown in
  761. the following example.
  762. [source, java]
  763. ----
  764. cal.setHandler(new EventClickHandler() {
  765. public void eventClick(EventClick event) {
  766. BasicEvent e = (BasicEvent) event.getCalendarEvent();
  767. // Do something with it
  768. new Notification("Event clicked: " + e.getCaption(),
  769. e.getDescription()).show(Page.getCurrent());
  770. }
  771. });
  772. ----
  773. endif::web[]
  774. ifdef::web[]
  775. [[components.calendar.eventdrag]]
  776. == Event Dragging
  777. The user can drag an event to change its position in time. The default handler
  778. sets the start and end time of the event accordingly. You can do many things
  779. with a custom move handler, such as restrict moving events.
  780. In the following example, we add a [interfacename]#EventMoveHandler# to a
  781. [classname]#Calendar#. The event handler updates the new position to the
  782. datasource, but only if the new dates are in the current month. This requires
  783. making some changes to the event provider class.
  784. [source, java]
  785. ----
  786. cal.setHandler(new BasicEventMoveHandler() {
  787. private java.util.Calendar javaCalendar;
  788. public void eventMove(MoveEvent event) {
  789. javaCalendar = event.getComponent().getInternalCalendar();
  790. super.eventMove(event);
  791. }
  792. protected void setDates(CalendarEventEditor event,
  793. Date start, Date end) {
  794. if (isThisMonth(javaCalendar, start)
  795. && isThisMonth(javaCalendar, end)) {
  796. super.setDates(event, start, end);
  797. }
  798. }
  799. });
  800. ----
  801. For the above example to work, the example event provider presented earlier
  802. needs to be changed slightly so that it doesn't always create a new event when
  803. [methodname]#getEvents()# is called.
  804. [source, java]
  805. ----
  806. public static class MyEventProvider
  807. implements CalendarEventProvider {
  808. private List<CalendarEvent> events =
  809. new ArrayList<CalendarEvent>();
  810. public MyEventProvider() {
  811. events = new ArrayList<CalendarEvent>();
  812. GregorianCalendar cal = new GregorianCalendar();
  813. cal.setTime(new Date());
  814. Date start = cal.getTime();
  815. cal.add(GregorianCalendar.HOUR, 5);
  816. Date end = cal.getTime();
  817. BasicEvent event = new BasicEvent();
  818. event.setCaption("My Event");
  819. event.setDescription("My Event Description");
  820. event.setStart(start);
  821. event.setEnd(end);
  822. events.add(event);
  823. }
  824. public void addEvent(CalendarEvent BasicEvent) {
  825. events.add(BasicEvent);
  826. }
  827. public List<CalendarEvent> getEvents(Date startDate,
  828. Date endDate) {
  829. return events;
  830. }
  831. }
  832. ----
  833. After these changes, the user can move events around as earlier, but dropping an
  834. event, the start and end dates are checked by the server. Note that as the
  835. server-side must move the event in order for it to render to the place it was
  836. dropped. The server can also reject moves by not doing anything when the event
  837. is received.
  838. endif::web[]
  839. ifdef::web[]
  840. [[components.calendar.dragselection]]
  841. == Handling Drag Selection
  842. Drag selection works both in the monthly and weekly views. To listen for drag
  843. selection, you can add a [interfacename]#RangeSelectListener# to the
  844. [classname]#Calendar#. There is no default handler for range select.
  845. In the code example below, we create an new event when any date range is
  846. selected. Drag selection opens a window where the user is asked for a caption
  847. for the new event. After confirming, the new event is be passed to the event
  848. provider and calendar is updated. Note that as our example event provider and
  849. event classes do not implement the event change interface, we must refresh the
  850. [classname]#Calendar# manually after changing the events.
  851. [source, java]
  852. ----
  853. cal.setHandler(new RangeSelectHandler() {
  854. public void rangeSelect(RangeSelectEvent event) {
  855. BasicEvent calendarEvent = new BasicEvent();
  856. calendarEvent.setStart(event.getStart());
  857. calendarEvent.setEnd(event.getEnd());
  858. // Create popup window and add a form in it.
  859. VerticalLayout layout = new VerticalLayout();
  860. layout.setMargin(true);
  861. layout.setSpacing(true);
  862. final Window w = new Window(null, layout);
  863. ...
  864. // Wrap the calendar event to a BeanItem
  865. // and pass it to the form
  866. final BeanItem<CalendarEvent> item =
  867. new BeanItem<CalendarEvent>(myEvent);
  868. final Form form = new Form();
  869. form.setItemDataSource(item);
  870. ...
  871. layout.addComponent(form);
  872. HorizontalLayout buttons = new HorizontalLayout();
  873. buttons.setSpacing(true);
  874. buttons.addComponent(new Button("OK", new ClickListener() {
  875. public void buttonClick(ClickEvent event) {
  876. form.commit();
  877. // Update event provider's data source
  878. provider.addEvent(item.getBean());
  879. UI.getCurrent().removeWindow(w);
  880. }
  881. }));
  882. ...
  883. }
  884. });
  885. ----
  886. endif::web[]
  887. ifdef::web[]
  888. [[components.calendar.eventresizing]]
  889. == Resizing Events
  890. The user can resize an event by dragging from both ends to change its start or
  891. end time. This offers a convenient way to change event times without the need to
  892. type anything. The default resize handler sets the start and end time of the
  893. event according to the resize.
  894. In the example below, we set a custom handler for resize events. The handler
  895. prevents any event to be resized over 12 hours in length. Note that this does
  896. not prevent the user from resizing an event over 12 hours in the client. The
  897. resize will just be corrected by the server.
  898. [source, java]
  899. ----
  900. cal.setHandler(new BasicEventResizeHandler() {
  901. private static final long twelveHoursInMs = 12*60*60*1000;
  902. protected void setDates(CalendarEventEditor event,
  903. Date start, Date end) {
  904. long eventLength = end.getTime() - start.getTime();
  905. if (eventLength <= twelveHoursInMs) {
  906. super.setDates(event, start, end);
  907. }
  908. }
  909. });
  910. ----
  911. endif::web[]