001    package org.gwtbootstrap3.extras.fullcalendar.client.ui;
002    
003    /*
004     * #%L
005     * GwtBootstrap3
006     * %%
007     * Copyright (C) 2013 - 2014 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    
023    import com.google.gwt.core.client.JavaScriptObject;
024    import com.google.gwt.core.client.JsArrayString;
025    import com.google.gwt.core.client.JsDate;
026    import com.google.gwt.i18n.client.DateTimeFormat;
027    import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
028    
029    import 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     */
037    public class Event implements IsJavaScriptObject {
038    
039        private JavaScriptObject event;
040    
041        public Event(final String id, final String title) {
042            newEvent(id, title, true, true, true);
043        }
044    
045        public Event(final JavaScriptObject jso) {
046            event = jso;
047        }
048    
049        public Event(final String id, final String title, final boolean isEditable, final boolean isStartEditable, final boolean isDurationEditable) {
050            newEvent(id, title, isEditable, isStartEditable, isDurationEditable);
051        }
052    
053        private native void newEvent(String eventId, String eventTitle, boolean isEditable, boolean isStartEditable, boolean isDurationEditable) /*-{
054            var theInstance = this;
055            theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event = {id: eventId,
056                title: eventTitle,
057                allDay: false,
058                editable: isEditable,
059                startEditable: isStartEditable,
060                durationEditable: isDurationEditable
061            };
062        }-*/;
063    
064        public native String getId() /*-{
065            var theInstance = this;
066            return theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.id;
067        }-*/;
068    
069        public native String getTitle() /*-{
070            var theInstance = this;
071            return theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.title;
072        }-*/;
073    
074        public native void setTitle(String title) /*-{
075            var theInstance = this;
076            theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.title = title;
077        }-*/;
078    
079        public native void setAllDay(boolean isAllDay) /*-{
080            var theInstance = this;
081            theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.allDay = isAllDay;
082        }-*/;
083    
084        public native boolean isAllDay() /*-{
085            var theInstance = this;
086            return theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.allDay;
087        }-*/;
088    
089        public void setStart(final Date d) {
090            if (d != null) {
091                setStart(getDateAsISO8601(d));
092            }
093        }
094    
095        private native void setStart(String start) /*-{
096            var theInstance = this;
097            theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.start = start;
098        }-*/;
099    
100        public native void setStart(final JavaScriptObject start) /*-{
101            var theInstance = this;
102            theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.start = start;
103        }-*/;
104    
105        public native JsDate getStart() /*-{
106            var theInstance = this;
107            if (theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.start) {
108                return theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.start.toDate();
109            }
110            return null;
111    
112        }-*/;
113    
114        public Date getStartFromYearMonthDay() {
115            Date iso = null;
116            final String isoString = getISOStart();
117            if (isoString != null && isoString.length() >= 10) {
118                iso = DateTimeFormat.getFormat("yyyy-MM-dd").parse(isoString.substring(0, 10));
119            }
120            return iso;
121        }
122    
123        public native String getISOStart() /*-{
124            var theInstance = this;
125            if (theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.start) {
126                return theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.start.toDate().toISOString();
127            }
128            return null;
129    
130        }-*/;
131    
132        public void setEnd(final Date d) {
133            if (d != null) {
134                setEnd(getDateAsISO8601(d));
135            }
136        }
137    
138        private native void setEnd(String end) /*-{
139            var theInstance = this;
140            theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.end = end;
141        }-*/;
142    
143        public native void setEnd(final JavaScriptObject end) /*-{
144            var theInstance = this;
145            theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.end = end;
146        }-*/;
147    
148        public native JsDate getEnd() /*-{
149            var theInstance = this;
150            if (theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.end) {
151                return theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.end.toDate();
152            }
153            return null;
154        }-*/;
155    
156        public native String getISOEnd() /*-{
157            var theInstance = this;
158            if (theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.end) {
159                return theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.end.toDate().toISOString();
160            }
161            return null;
162        }-*/;
163    
164        public Date getEndFromYearMonthDay() {
165            Date iso = null;
166            final String isoString = getISOEnd();
167            if (isoString != null && isoString.length() >= 10) {
168                iso = DateTimeFormat.getFormat("yyyy-MM-dd").parse(isoString.substring(0, 10));
169            }
170            return iso;
171        }
172    
173        public native void setUrl(String url) /*-{
174            var theInstance = this;
175            theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.url = url;
176        }-*/;
177    
178        public native String getUrl() /*-{
179            var theInstance = this;
180            return theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.url;
181        }-*/;
182    
183        public native void setClassName(String className) /*-{
184            var theInstance = this;
185            theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.className = className;
186        }-*/;
187    
188        public native void setClassNames(JsArrayString classNames) /*-{
189            var theInstance = this;
190            theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.className = className;
191        }-*/;
192    
193        public native void setEditable(boolean editable) /*-{
194            var theInstance = this;
195            theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.editable = editable;
196        }-*/;
197    
198        public native boolean isEditable() /*-{
199            var theInstance = this;
200            if (theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.editable) {
201                return theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.editable;
202            }
203            return false;
204        }-*/;
205    
206        public native void setStartEditable(boolean startEditable) /*-{
207            var theInstance = this;
208            theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.startEditable = startEditable;
209        }-*/;
210    
211        public native boolean getStartEditable() /*-{
212            var theInstance = this;
213            if (theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.startEditable) {
214                return theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.startEditable;
215            }
216            return false;
217        }-*/;
218    
219        public native void setDurationEditable(boolean durationEditable) /*-{
220            var theInstance = this;
221            theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.durationEditable = durationEditable;
222        }-*/;
223    
224        public native boolean getDurationEditable() /*-{
225            var theInstance = this;
226            if (theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.durationEditable) {
227                return theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.durationEditable;
228            }
229            return false;
230        }-*/;
231    
232        public native void setColor(String color) /*-{
233            var theInstance = this;
234            theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.color = color;
235        }-*/;
236    
237        public native void setBackgroundColor(String bgColor) /*-{
238            var theInstance = this;
239            theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.backgroundColor = bgColor;
240        }-*/;
241    
242        public native void setBorderColor(String borderColor) /*-{
243            var theInstance = this;
244            theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.borderColor = borderColor;
245        }-*/;
246    
247        public native void setTextColor(String textColor) /*-{
248            var theInstance = this;
249            theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.textColor = textColor;
250        }-*/;
251    
252        /**
253         * ************* non standard / add-on fields and methods *********************
254         */
255        public native void setDescription(String description) /*-{
256            var theInstance = this;
257            theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.description = description;
258        }-*/;
259    
260        public native String getDescription() /*-{
261            var theInstance = this;
262            return theInstance.@org.gwtbootstrap3.extras.fullcalendar.client.ui.Event::event.description;
263        }-*/;
264    
265        public static String getDateAsRFC_2822(final Date d) {
266            return d == null ? "" : DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).format(d);
267        }
268    
269        public static String getDateAsISO8601(final Date d) {
270            return d == null ? "" : DateTimeFormat.getFormat(PredefinedFormat.ISO_8601).format(d);
271        }
272    
273        public static String getDateAsUnixTimestamp(final Date d) {
274            if (d == null) {
275                return "";
276            }
277            final int iTimeStamp = (int) (d.getTime() * .001);
278            return "" + iTimeStamp;
279        }
280    
281        @Override
282        public JavaScriptObject toJavaScript() {
283            return this.event;
284        }
285    }