001package org.gwtbootstrap3.extras.fullcalendar.client.ui;
002
003/*
004 * #%L
005 * GwtBootstrap3
006 * %%
007 * Copyright (C) 2013 - 2015 GwtBootstrap3
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import com.google.gwt.core.client.JavaScriptObject;
024import com.google.gwt.core.client.JsArrayString;
025import com.google.gwt.core.client.JsDate;
026import com.google.gwt.i18n.client.DateTimeFormat;
027import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
028
029import java.util.Date;
030
031/**
032 * Represents and event on a FullCalendar
033 *
034 * @author Jeff Isenhart
035 * @see http://arshaw.com/fullcalendar/docs/event_data/Event_Object/
036 */
037public class Event implements IsJavaScriptObject {
038
039    private static final DateTimeFormat ISO_8601_FORMAT = DateTimeFormat.getFormat(PredefinedFormat.ISO_8601);
040    private static final DateTimeFormat RFC_2822_FORMAT = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822);
041    private JavaScriptObject event;
042
043    public Event(final String id, final String title) {
044        newEvent(id, title, true, true, true);
045    }
046
047    public Event(final JavaScriptObject jso) {
048        event = jso;
049    }
050
051    public Event(final String id, final String title, final boolean isEditable, final boolean isStartEditable, final boolean isDurationEditable) {
052        newEvent(id, title, isEditable, isStartEditable, isDurationEditable);
053    }
054
055    private native void newEvent(String eventId, String eventTitle, boolean isEditable, boolean isStartEditable, boolean isDurationEditable) /*-{
056        var theInstance = this;
057        theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event = {id: eventId,
058            title: eventTitle,
059            allDay: false,
060            editable: isEditable,
061            startEditable: isStartEditable,
062            durationEditable: isDurationEditable
063        };
064    }-*/;
065
066    public native String getId() /*-{
067        var theInstance = this;
068        return theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.id;
069    }-*/;
070
071    public native String getTitle() /*-{
072        var theInstance = this;
073        return theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.title;
074    }-*/;
075
076    public native void setTitle(String title) /*-{
077        var theInstance = this;
078        theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.title = title;
079    }-*/;
080
081    public native void setAllDay(boolean isAllDay) /*-{
082        var theInstance = this;
083        theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.allDay = isAllDay;
084    }-*/;
085
086    public native boolean isAllDay() /*-{
087        var theInstance = this;
088        return theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.allDay;
089    }-*/;
090
091    public void setStart(final Date d) {
092        if (d != null) {
093            setStart(getDateAsISO8601(d));
094        }
095    }
096
097    public native void setStart(String start) /*-{
098        var theInstance = this;
099        theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.start = start;
100    }-*/;
101
102    public native void setStart(final JavaScriptObject start) /*-{
103        var theInstance = this;
104        theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.start = start;
105    }-*/;
106
107    public native JsDate getStart() /*-{
108        var theInstance = this;
109        if (theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.start) {
110            return theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.start.toDate();
111        }
112        return null;
113
114    }-*/;
115
116    public Date getStartFromYearMonthDay() {
117        Date iso = null;
118        final String isoString = getISOStart();
119        if (isoString != null && isoString.length() >= 10) {
120            iso = DateTimeFormat.getFormat("yyyy-MM-dd").parse(isoString.substring(0, 10));
121        }
122        return iso;
123    }
124
125    public native String getISOStart() /*-{
126        var theInstance = this;
127        if (theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.start) {
128            return theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.start.toDate().toISOString();
129        }
130        return null;
131
132    }-*/;
133
134    public void setEnd(final Date d) {
135        if (d != null) {
136            setEnd(getDateAsISO8601(d));
137        }
138    }
139
140    public native void setEnd(String end) /*-{
141        var theInstance = this;
142        theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.end = end;
143    }-*/;
144
145    public native void setEnd(final JavaScriptObject end) /*-{
146        var theInstance = this;
147        theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.end = end;
148    }-*/;
149
150    public native JsDate getEnd() /*-{
151        var theInstance = this;
152        if (theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.end) {
153            return theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.end.toDate();
154        }
155        return null;
156    }-*/;
157
158    public native String getISOEnd() /*-{
159        var theInstance = this;
160        if (theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.end) {
161            return theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.end.toDate().toISOString();
162        }
163        return null;
164    }-*/;
165
166    public Date getEndFromYearMonthDay() {
167        Date iso = null;
168        final String isoString = getISOEnd();
169        if (isoString != null && isoString.length() >= 10) {
170            iso = DateTimeFormat.getFormat("yyyy-MM-dd").parse(isoString.substring(0, 10));
171        }
172        return iso;
173    }
174
175    public native void setUrl(String url) /*-{
176        var theInstance = this;
177        theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.url = url;
178    }-*/;
179
180    public native String getUrl() /*-{
181        var theInstance = this;
182        return theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.url;
183    }-*/;
184
185    public native void setClassName(String className) /*-{
186        var theInstance = this;
187        theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.className = className;
188    }-*/;
189
190    public native void setClassNames(JsArrayString classNames) /*-{
191        var theInstance = this;
192        theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.className = className;
193    }-*/;
194
195    public native void setEditable(boolean editable) /*-{
196        var theInstance = this;
197        theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.editable = editable;
198    }-*/;
199
200    public native boolean isEditable() /*-{
201        var theInstance = this;
202        if (theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.editable) {
203            return theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.editable;
204        }
205        return false;
206    }-*/;
207
208    public native void setStartEditable(boolean startEditable) /*-{
209        var theInstance = this;
210        theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.startEditable = startEditable;
211    }-*/;
212
213    public native boolean getStartEditable() /*-{
214        var theInstance = this;
215        if (theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.startEditable) {
216            return theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.startEditable;
217        }
218        return false;
219    }-*/;
220
221    public native void setDurationEditable(boolean durationEditable) /*-{
222        var theInstance = this;
223        theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.durationEditable = durationEditable;
224    }-*/;
225
226    public native boolean getDurationEditable() /*-{
227        var theInstance = this;
228        if (theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.durationEditable) {
229            return theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.durationEditable;
230        }
231        return false;
232    }-*/;
233    
234    public native void setRendering(String rendering) /*-{
235        var theInstance = this;
236        theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.rendering = rendering;
237    }-*/;
238    
239    public native String getRendering() /*-{
240        var theInstance = this;
241         if (theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.rendering) {
242            return theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.rendering;
243        }
244        return null;
245    }-*/;
246    
247    public native void setOverlap(boolean overlap) /*-{
248        var theInstance = this;
249        theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.overlap = overlap;
250    }-*/;
251    
252    public native boolean getOverlap() /*-{
253        var theInstance = this;
254        if (typeof theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.overlap != 'undefined') {
255            return theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.overlap;
256        }
257        return true;
258    }-*/;
259    
260    public native void setConstraint(String constraint) /*-{
261        var theInstance = this;
262        theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.constraint = constraint;
263    }-*/;
264    
265    public native void setConstraint(JavaScriptObject constraint) /*-{
266        var theInstance = this;
267        theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.constraint = constraint;
268    }-*/;
269    
270    private native JavaScriptObject getJSOSource() /*-{
271        var theInstance = this;
272        return theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.source;
273    }-*/;
274    
275    public EventSource getSource() {
276        return new EventSource(getJSOSource());
277    }
278
279    public native void setColor(String color) /*-{
280        var theInstance = this;
281        theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.color = color;
282    }-*/;
283
284    public native void setBackgroundColor(String bgColor) /*-{
285        var theInstance = this;
286        theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.backgroundColor = bgColor;
287    }-*/;
288
289    public native void setBorderColor(String borderColor) /*-{
290        var theInstance = this;
291        theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.borderColor = borderColor;
292    }-*/;
293
294    public native void setTextColor(String textColor) /*-{
295        var theInstance = this;
296        theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.textColor = textColor;
297    }-*/;
298
299    /**
300     * ************* non standard / add-on fields and methods *********************
301     */
302    public native void setDescription(String description) /*-{
303        var theInstance = this;
304        theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.description = description;
305    }-*/;
306
307    public native String getDescription() /*-{
308        var theInstance = this;
309        return theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.description;
310    }-*/;
311
312    public static String getDateAsRFC_2822(final Date d) {
313        return d == null ? "" : RFC_2822_FORMAT.format(d);
314    }
315
316    public static String getDateAsISO8601(final Date d) {
317        return d == null ? "" : ISO_8601_FORMAT.format(d);
318    }
319
320    public static String getDateAsUnixTimestamp(final Date d) {
321        if (d == null) {
322            return "";
323        }
324        final int iTimeStamp = (int) (d.getTime() * .001);
325        return "" + iTimeStamp;
326    }
327
328    @Override
329    public JavaScriptObject toJavaScript() {
330        return this.event;
331    }
332}