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.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.tapestry.*;
26  import org.apache.tapestry.engine.IScriptSource;
27  import org.apache.tapestry.html.Body;
28  
29  import net.sf.tapfx.components.TapFXBaseComponent;
30  
31  /***
32   * @author Andreas Andreou
33   */
34  public abstract class MachoBar extends TapFXBaseComponent
35  {
36      private static final Log log =
37              LogFactory.getLog(MachoBar.class);
38      private static final String MACHO_BAR_CONTROL =
39              "net.sf.tapfx.components.machobar.MachoBar";
40      public abstract String getSize();
41      public abstract String getLargeSize();
42      public abstract String getIdPrefix();
43      private IScript script = null;
44      private List smallAssetlist = new ArrayList();
45      private List bigAssetlist = new ArrayList();
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 getItemLargeSize() + 10;
55      }
56  
57      public int getItemLargeSize()
58      {
59          int size;
60          try
61          {
62              size = Integer.parseInt(getLargeSize());
63          }
64          catch (NumberFormatException e)
65          {
66              log.debug(e);
67              size = 70;
68          }
69          return size;
70      }
71  
72      public int getItemSize()
73      {
74          int size;
75          try
76          {
77              size = Integer.parseInt(getSize());
78          }
79          catch (NumberFormatException e)
80          {
81              log.debug(e);
82              size = 35;
83          }
84          return size;
85      }
86  
87      public int getItemsCount()
88      {
89          return smallAssetlist.size();
90      }
91  
92      public void addAssetUrl(String url)
93      {
94          smallAssetlist.add(url);
95      }
96  
97      public void addLargeAssetUrl(String url)
98      {
99          bigAssetlist.add(url);
100     }
101 
102     protected void setupComponent(IRequestCycle cycle)
103     {
104         // check if there's another MachoBar in the page
105         if (MachoBar.getMachoBar(cycle) != null)
106         {
107             throw Tapestry.createRequiredParameterException(this,
108                     "You can only have one @MachoBar in a page");
109         }
110         // get body and check that it exists
111         Body body = Body.get(cycle);
112         if (body == null)
113         {
114             throw Tapestry.createRequiredParameterException(this,
115                     "You need to provide a @Body around this component");
116         }
117         // store ourself in the cycle
118         cycle.setAttribute(MACHO_BAR_CONTROL, this);
119         // initialize stuff
120         smallAssetlist.clear();
121         bigAssetlist.clear();
122     }
123 
124     public String getPrefix()
125     {
126         String prefix = getIdPrefix();
127         if (prefix == null)
128         {
129             prefix = "";
130         }
131         return prefix;
132     }
133 
134     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
135     {
136         super.renderComponent(writer, cycle);
137         // add the javascript
138         if (script == null)
139         {
140             IScriptSource source = cycle.getEngine().getScriptSource();
141             IResourceLocation location =
142                     getSpecification().getLocation().getResourceLocation();
143             IResourceLocation script_location =
144                     location.getRelativeLocation("MachoBar.script");
145             script = source.getScript(script_location);
146         }
147         StringBuffer sb = new StringBuffer(100);
148         sb.append("b=new Array(")
149                 .append(smallAssetlist.size() + 1)
150                 .append(");");
151         sb.append("aa=new Array(")
152                 .append(smallAssetlist.size())
153                 .append(");");
154         sb.append("small=new Array(\"null\"");
155         for (int i=0; i<smallAssetlist.size(); i++)
156         {
157             String url = (String) smallAssetlist.get(i);
158             sb.append(",\"")
159                     .append(url)
160                     .append("\"");
161         }
162         sb.append(");");
163         sb.append("s=new Array(\"null\"");
164         for (int i=0; i<bigAssetlist.size(); i++)
165         {
166             String url = (String) bigAssetlist.get(i);
167             sb.append(",\"")
168                     .append(url)
169                     .append("\"");
170         }
171         sb.append(");");
172         Map symbols = new HashMap();
173         symbols.put("small", "" + getItemSize());
174         symbols.put("big", "" + getItemLargeSize());
175         symbols.put("imgsSmall", smallAssetlist);
176         symbols.put("initJS", sb.toString());
177         script.execute(cycle, Body.get(cycle), symbols);
178     }
179 }