1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
75 if (MachoBar.getMachoBar(cycle) != null)
76 {
77 throw new ApplicationRuntimeException("MachoBar components should NOT nest.");
78 }
79
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
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
128 smallAssetlist.clear();
129 bigAssetlist.clear();
130 }
131 }