View Javadoc

1   package net.sf.tacos.components.timeline;
2   
3   import java.util.Collection;
4   import java.util.Iterator;
5   
6   import org.apache.tapestry.BaseComponent;
7   import org.apache.tapestry.IAsset;
8   import org.apache.tapestry.IRequestCycle;
9   import org.apache.tapestry.html.Shell;
10  import org.apache.tapestry.json.JSONObject;
11  
12  /**
13   * Timeline component, based on http://simile.mit.edu/timeline/
14   *
15   * @author Andreas Andreou, amplafi.com
16   */
17  public abstract class Timeline extends BaseComponent {
18  
19      public abstract Collection getEvents();
20  
21      public abstract String getEventsUrl();
22      
23      public abstract IAsset getTimelineAsset();
24      
25      public abstract IAsset getSimileAjaxAsset();    
26  
27      protected void prepareForRender(IRequestCycle cycle)
28      {
29          super.prepareForRender(cycle);
30  
31          if (!cycle.isRewinding()) 
32          {
33              Shell shell = Shell.get(cycle);
34              if (getSimileAjaxAsset()!=null)
35              {
36                  addScriptToShell(shell, getSimileAjaxAsset().buildURL());
37              }
38  
39              addScriptToShell(shell, getTimelineAsset().buildURL());
40          }
41      }
42  
43      public String getEventsJson()
44      {
45          JSONObject json = new JSONObject();
46          for (Iterator iterator = getEvents().iterator(); iterator.hasNext();)
47          {
48              TimelineEvent evt = (TimelineEvent) iterator.next();
49              evt.appendTo(json);
50          }
51          return json.toString(2);
52      }
53      
54      private void addScriptToShell(Shell shell, String url) {
55          shell.includeAdditionalContent(
56                  "<script src=\"" + url + "\" type=\"text/javascript\"></script>");
57      }
58  }