001    package net.sf.tacos.components.timeline;
002    
003    import java.util.Collection;
004    import java.util.Iterator;
005    
006    import org.apache.tapestry.BaseComponent;
007    import org.apache.tapestry.IAsset;
008    import org.apache.tapestry.IRequestCycle;
009    import org.apache.tapestry.html.Shell;
010    import org.apache.tapestry.json.JSONObject;
011    
012    /**
013     * Timeline component, based on http://simile.mit.edu/timeline/
014     *
015     * @author Andreas Andreou, amplafi.com
016     */
017    public abstract class Timeline extends BaseComponent {
018    
019        public abstract Collection getEvents();
020    
021        public abstract String getEventsUrl();
022        
023        public abstract IAsset getTimelineAsset();
024        
025        public abstract IAsset getSimileAjaxAsset();    
026    
027        protected void prepareForRender(IRequestCycle cycle)
028        {
029            super.prepareForRender(cycle);
030    
031            if (!cycle.isRewinding()) 
032            {
033                Shell shell = Shell.get(cycle);
034                if (getSimileAjaxAsset()!=null)
035                {
036                    addScriptToShell(shell, getSimileAjaxAsset().buildURL());
037                }
038    
039                addScriptToShell(shell, getTimelineAsset().buildURL());
040            }
041        }
042    
043        public String getEventsJson()
044        {
045            JSONObject json = new JSONObject();
046            for (Iterator iterator = getEvents().iterator(); iterator.hasNext();)
047            {
048                TimelineEvent evt = (TimelineEvent) iterator.next();
049                evt.appendTo(json);
050            }
051            return json.toString(2);
052        }
053        
054        private void addScriptToShell(Shell shell, String url) {
055            shell.includeAdditionalContent(
056                    "<script src=\"" + url + "\" type=\"text/javascript\"></script>");
057        }
058    }