•Add events directly to the Calendar object using the addEvent()
•Use a Container as a data source
•Use the event provider mechanism
The easiest way to add and manage events in a calendar is to use the basic event management API in the Calendar. You can add events with addEvent() and remove them with the removeEvent().These methods will use the underlying event provider to write the modifications to the data source.
For example, the following adds a two-hour event starting from the current time. The standard Java GregorianCalendar provides various ways to manipulate date and time.
// Add a short event
GregorianCalendar start = new GregorianCalendar();
GregorianCalendar end |
= new GregorianCalendar(); |
end.add(java.util.Calendar.HOUR, 2); |
calendar.addEvent(new |
BasicEvent("Calendar study", |
"Learning how |
to use Vaadin Calendar", |
start.getTime(), end.getTime()));
Calendar uses by default a BasicEventProvider, which keeps the events in memory in an internal reprensetation.
This adds a new event that lasts for 3 hours. As the BasicEventProvider and BasicEvent implement some optional event interfaces provided by the calendar package, there is no need to refresh the calendar. Just create events, set their properties and add them to the Event Provider.
18.3.3. Getting Events from a Container
You can use any Vaadin Container that implements the Indexed interface as the data source for calendar events. The Calendar will listen to change events from the container as well as write changes to the container. You can attach a container to a Calendar with setContainerDataSource().
In the following example, we bind a BeanItemContainer that contains built-in BasicEvent events to a calendar.
// Create the calendar
Calendar calendar = new Calendar("Bound Calendar");
//Use a container of built-in BasicEvents final BeanItemContainer<BasicEvent> container =
new BeanItemContainer<BasicEvent>(BasicEvent.class);
//Create a meeting in the container
container.addBean(new BasicEvent("The Event", "Single Event", new GregorianCalendar(2012,1,14,12,00).getTime(), new GregorianCalendar(2012,1,14,14,00).getTime()));
//The container must be ordered by the start time. You
//have to sort the BIC every time after you have added
//or modified events.
container.sort(new Object[]{"start"}, new boolean[]{true});
calendar.setContainerDataSource(container, "caption", "description", "start", "end", "styleName");