2 * Copyright 2000-2018 Vaadin Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
16 package com.vaadin.v7.ui.components.calendar.event;
18 import java.util.ArrayList;
19 import java.util.Date;
20 import java.util.List;
22 import com.vaadin.v7.ui.components.calendar.event.CalendarEvent.EventChangeEvent;
23 import com.vaadin.v7.ui.components.calendar.event.CalendarEventProvider.EventSetChangeNotifier;
27 * Simple implementation of
28 * {@link com.vaadin.addon.calendar.event.CalendarEventProvider
29 * CalendarEventProvider}. Use {@link #addEvent(CalendarEvent)} and
30 * {@link #removeEvent(CalendarEvent)} to add / remove events.
34 * {@link com.vaadin.addon.calendar.event.CalendarEventProvider.EventSetChangeNotifier
35 * EventSetChangeNotifier} and
36 * {@link com.vaadin.addon.calendar.event.CalendarEvent.EventChangeListener
37 * EventChangeListener} are also implemented, so the Calendar is notified when
38 * an event is added, changed or removed.
44 @SuppressWarnings("serial")
46 public class BasicEventProvider implements CalendarEditableEventProvider,
47 EventSetChangeNotifier, CalendarEvent.EventChangeListener {
49 protected List<CalendarEvent> eventList = new ArrayList<CalendarEvent>();
51 private List<EventSetChangeListener> listeners = new ArrayList<EventSetChangeListener>();
57 * com.vaadin.addon.calendar.event.CalendarEventProvider#getEvents(java.
58 * util.Date, java.util.Date)
61 public List<CalendarEvent> getEvents(Date startDate, Date endDate) {
62 List<CalendarEvent> activeEvents = new ArrayList<CalendarEvent>();
64 for (CalendarEvent ev : eventList) {
65 long from = startDate.getTime();
66 long to = endDate.getTime();
68 if (ev.getStart() != null && ev.getEnd() != null) {
69 long f = ev.getStart().getTime();
70 long t = ev.getEnd().getTime();
71 // Select only events that overlaps with startDate and
73 if ((f <= to && f >= from) || (t >= from && t <= to)
74 || (f <= from && t >= to)) {
84 * Does this event provider container this event.
87 * The event to check for
88 * @return If this provider has the event then true is returned, else false
90 public boolean containsEvent(BasicEvent event) {
91 return eventList.contains(event);
97 * @see com.vaadin.addon.calendar.ui.CalendarComponentEvents.
98 * EventSetChangeNotifier #addListener
99 * (com.vaadin.addon.calendar.ui.CalendarComponentEvents.
100 * EventSetChangeListener )
103 public void addEventSetChangeListener(EventSetChangeListener listener) {
104 listeners.add(listener);
111 * @see com.vaadin.addon.calendar.ui.CalendarComponentEvents.
112 * EventSetChangeNotifier #removeListener
113 * (com.vaadin.addon.calendar.ui.CalendarComponentEvents.
114 * EventSetChangeListener )
117 public void removeEventSetChangeListener(EventSetChangeListener listener) {
118 listeners.remove(listener);
122 * Fires a eventsetchange event. The event is fired when either an event is
123 * added or removed to the event provider
125 protected void fireEventSetChange() {
126 EventSetChangeEvent event = new EventSetChangeEvent(this);
128 for (EventSetChangeListener listener : listeners) {
129 listener.eventSetChange(event);
137 * com.vaadin.addon.calendar.ui.CalendarComponentEvents.EventChangeListener
139 * (com.vaadin.addon.calendar.ui.CalendarComponentEvents.EventSetChange)
142 public void eventChange(EventChangeEvent changeEvent) {
143 // naive implementation
144 fireEventSetChange();
151 * com.vaadin.addon.calendar.event.CalendarEditableEventProvider#addEvent
152 * (com.vaadin.addon.calendar.event.CalendarEvent)
155 public void addEvent(CalendarEvent event) {
156 eventList.add(event);
157 if (event instanceof BasicEvent) {
158 ((BasicEvent) event).addEventChangeListener(this);
160 fireEventSetChange();
167 * com.vaadin.addon.calendar.event.CalendarEditableEventProvider#removeEvent
168 * (com.vaadin.addon.calendar.event.CalendarEvent)
171 public void removeEvent(CalendarEvent event) {
172 eventList.remove(event);
173 if (event instanceof BasicEvent) {
174 ((BasicEvent) event).removeEventChangeListener(this);
176 fireEventSetChange();