View Javadoc
1   /*
2    * Copyright 2004-2005 Andreas Andreou
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.tapfx.components.machobar;
17  
18  import java.util.Map;
19  import java.util.HashMap;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.apache.hivemind.ApplicationRuntimeException;
24  import org.apache.tapestry.IMarkupWriter;
25  import org.apache.tapestry.IRequestCycle;
26  import org.apache.tapestry.IScript;
27  import org.apache.tapestry.PageRenderSupport;
28  import org.apache.tapestry.TapestryUtils;
29  
30  import net.sf.tapfx.components.TapFXBaseComponent;
31  
32  /***
33   * @author Andreas Andreou
34   * @version $Id: MachoBar.java,v 1.3 2005/11/07 03:25:23 andyhot Exp $
35   */
36  public abstract class MachoBar extends TapFXBaseComponent
37  {
38      private static final String MACHO_BAR_CONTROL = MachoBar.class.getName();
39      public abstract int getSize();
40      public abstract int getLargeSize();
41      public abstract String getIdPrefix();    
42      private List smallAssetlist = new ArrayList();
43      private List bigAssetlist = new ArrayList();
44      
45      public abstract IScript getScript();
46  
47      public static MachoBar getMachoBar(IRequestCycle cycle)
48      {
49          return (MachoBar) cycle.getAttribute(MACHO_BAR_CONTROL);
50      }
51  
52      public int getHeight()
53      {
54          return getLargeSize() + 10;
55      }
56  
57      public int getItemsCount()
58      {
59          return smallAssetlist.size();
60      }
61  
62      public void addAssetUrl(String url)
63      {
64          smallAssetlist.add(url);
65      }
66  
67      public void addLargeAssetUrl(String url)
68      {
69          bigAssetlist.add(url);
70      }
71  
72      protected void setupComponent(IRequestCycle cycle)
73      { 
74          // check if there's another MachoBar in the page
75          if (MachoBar.getMachoBar(cycle) != null)
76          {
77              throw new ApplicationRuntimeException("MachoBar components should NOT nest.");
78          }
79          // store ourself in the cycle
80          cycle.setAttribute(MACHO_BAR_CONTROL, this);
81      }
82  
83      protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
84      {
85          super.renderComponent(writer, cycle);
86  
87          StringBuffer sb = new StringBuffer(100);
88          sb.append("b=new Array(")
89                  .append(smallAssetlist.size() + 1)
90                  .append(");");
91          sb.append("aa=new Array(")
92                  .append(smallAssetlist.size())
93                  .append(");");
94          sb.append("small=new Array(\"null\"");
95          for (int i=0; i<smallAssetlist.size(); i++)
96          {
97              String url = (String) smallAssetlist.get(i);
98              sb.append(",\"")
99                      .append(url)
100                     .append("\"");
101         }
102         sb.append(");");
103         sb.append("s=new Array(\"null\"");
104         for (int i=0; i<bigAssetlist.size(); i++)
105         {
106             String url = (String) bigAssetlist.get(i);
107             sb.append(",\"")
108                     .append(url)
109                     .append("\"");
110         }
111         sb.append(");");
112         // add the javascript
113         Map symbols = new HashMap();
114         symbols.put("small", "" + getSize());
115         symbols.put("big", "" + getLargeSize());
116         symbols.put("imgsSmall", smallAssetlist);
117         symbols.put("initJS", sb.toString());
118         
119         PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, this);
120         getScript().execute(cycle, pageRenderSupport, symbols);
121     }
122     
123     protected void cleanupAfterRender(IRequestCycle cycle)
124     {
125         super.cleanupAfterRender(cycle);
126         cycle.removeAttribute(MACHO_BAR_CONTROL);
127         // release resources
128         smallAssetlist.clear();
129         bigAssetlist.clear();        
130     }    
131 }