001    package net.sf.tacos.components.timeline;
002    
003    import java.util.Calendar;
004    
005    import org.apache.tapestry.json.JSONObject;
006    import org.apache.tapestry.json.JSONLiteral;
007    
008    /**
009     * Simple bean holding event data for http://simile.mit.edu/timeline/
010     * dhtml control.
011     *
012     * @author Andreas Andreou, amplafi.com
013     */
014    public class TimelineEvent {
015        private Calendar start;
016        private Calendar end;
017        private String title;
018        private String link;
019        private String description;
020    
021        public Calendar getStart() {
022            return start;
023        }
024    
025        /**
026         * Sets the start of the event.
027         * If the given date is after the ending, they're swapped.
028         * @param start
029         */
030        public void setStart(Calendar start) {
031            this.start = start;
032            fixDates();
033        }
034    
035        public Calendar getEnd() {
036            return end;
037        }
038    
039        /**
040         * Sets the end of the event.
041         * If the given date is before the starting, they're swapped.
042         * @param end
043         */
044        public void setEnd(Calendar end) {
045            this.end = end;
046            fixDates();
047        }
048    
049        public String getTitle() {
050            return title;
051        }
052    
053        public void setTitle(String title) {
054            this.title = title;
055        }
056    
057        public String getLink() {
058            return link;
059        }
060    
061        public void setLink(String link) {
062            this.link = link;
063        }
064    
065        public String getDescription() {
066            return description;
067        }
068    
069        public void setDescription(String description) {
070            this.description = description;
071        }
072    
073        private void fixDates() {
074            if (start!=null && end!=null && start.after(end)) {
075                Calendar temp = start;
076                start = end;
077                end = temp;
078            }
079        }
080    
081        private JSONLiteral toDate(Calendar date) {
082            return new JSONLiteral("new Date(" + date.get(Calendar.YEAR)
083                    + "," + date.get(Calendar.MONTH)
084                    + "," + (date.get(Calendar.DAY_OF_MONTH) - 1)
085                    + ")");
086        }
087    
088        public JSONObject appendTo(JSONObject obj) {
089            JSONObject event = new JSONObject();
090            event.put("start", toDate(start));
091            if (end!=null) {
092                event.put("end", toDate(end));
093            }
094            event.put("title", title)
095                    .put("link", link)
096                    .put("description", (description==null) ? "" : description);
097    
098            obj.accumulate("events", event);
099            return obj;
100        }
101    }