1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.custom.datalist;
20
21 import org.apache.myfaces.shared_tomahawk.renderkit.JSFAttr;
22 import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
23 import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
24 import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRenderer;
25 import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
26
27 import javax.faces.component.UIComponent;
28 import javax.faces.component.UIData;
29 import javax.faces.component.UIViewRoot;
30 import javax.faces.context.FacesContext;
31 import javax.faces.context.ResponseWriter;
32 import java.io.IOException;
33
34
35
36
37
38
39
40
41
42
43 public class HtmlListRenderer
44 extends HtmlRenderer
45 {
46
47
48 public static final String LAYOUT_SIMPLE = "simple";
49 public static final String LAYOUT_UL = "unorderedList";
50 public static final String LAYOUT_OL = "orderedList";
51 public static final String LAYOUT_GRID = "grid";
52
53 public boolean getRendersChildren()
54 {
55 return true;
56 }
57
58 public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) throws IOException
59 {
60 RendererUtils.checkParamValidity(facesContext, uiComponent, UIData.class);
61 ResponseWriter writer = facesContext.getResponseWriter();
62 String layout = getLayout(uiComponent);
63 if (layout != null)
64 {
65 if (! layout.equals(LAYOUT_SIMPLE)) {
66 HtmlRendererUtils.writePrettyLineSeparator(facesContext);
67 }
68 if (layout.equals(LAYOUT_UL))
69 {
70 writer.startElement(HTML.UL_ELEM, uiComponent);
71
72 writer.writeAttribute(HTML.ID_ATTR, uiComponent.getClientId(facesContext), null);
73
74 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent,
75 HTML.COMMON_PASSTROUGH_ATTRIBUTES);
76 }
77 else if (layout.equals(LAYOUT_OL))
78 {
79 writer.startElement(HTML.OL_ELEM, uiComponent);
80
81 writer.writeAttribute(HTML.ID_ATTR, uiComponent.getClientId(facesContext), null);
82
83 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent,
84 HTML.COMMON_PASSTROUGH_ATTRIBUTES);
85 }
86 else if (layout.equals(LAYOUT_GRID))
87 {
88 writer.startElement(HTML.TABLE_ELEM, uiComponent);
89
90 writer.writeAttribute(HTML.ID_ATTR, uiComponent.getClientId(facesContext), null);
91
92 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent,
93 HTML.COMMON_PASSTROUGH_ATTRIBUTES);
94 }
95 else
96 {
97 if (uiComponent.getId() != null && !uiComponent.getId().startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
98 {
99 writer.startElement(HTML.SPAN_ELEM, uiComponent);
100
101 writer.writeAttribute(HTML.ID_ATTR, uiComponent.getClientId(facesContext), null);
102
103 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.COMMON_PASSTROUGH_ATTRIBUTES);
104
105 }
106 else
107 {
108 HtmlRendererUtils.renderHTMLAttributesWithOptionalStartElement(writer, uiComponent,
109 HTML.SPAN_ELEM, HTML.COMMON_PASSTROUGH_ATTRIBUTES);
110 }
111 }
112 }
113 }
114
115 public void encodeChildren(FacesContext facesContext, UIComponent component) throws IOException
116 {
117 RendererUtils.checkParamValidity(facesContext, component, UIData.class);
118
119 UIData uiData = (UIData) component;
120 String layout = getLayout(component);
121
122
123 ResponseWriter writer = facesContext.getResponseWriter();
124
125 int first = uiData.getFirst();
126 int rows = uiData.getRows();
127 int rowCount = uiData.getRowCount();
128 if (rows <= 0)
129 {
130 rows = rowCount - first;
131 }
132 int last = first + rows;
133 if (last > rowCount) last = rowCount;
134
135
136
137
138
139
140
141
142
143
144
145 if (layout != null && layout.equals(LAYOUT_GRID)){
146
147 writer.startElement(HTML.TR_ELEM, null);
148 }
149 for (int i = first; i < last; i++)
150 {
151 uiData.setRowIndex(i);
152 if (uiData.isRowAvailable())
153 {
154
155
156
157
158
159
160
161 if (layout != null)
162 {
163 if (! layout.equals(LAYOUT_SIMPLE)) {
164 HtmlRendererUtils.writePrettyLineSeparator(facesContext);
165 }
166 if (layout.equals(LAYOUT_UL) || (layout.equals(LAYOUT_OL)))
167 {
168 writer.startElement(HTML.LI_ELEM, component);
169 HtmlRendererUtils.renderHTMLAttribute(writer, component, "itemStyleClass", HTML.STYLE_CLASS_ATTR);
170 HtmlRendererUtils.renderHTMLAttribute(writer, component, "itemOnClick", HTML.ONCLICK_ATTR);
171 }
172 else if (layout.equals(LAYOUT_GRID))
173 {
174
175 writer.startElement(HTML.TD_ELEM, null);
176 }
177 }
178
179 RendererUtils.renderChildren(facesContext, component);
180
181 if (layout != null)
182 {
183 if (layout.equals(LAYOUT_UL) || (layout.equals(LAYOUT_OL)))
184 {
185 writer.endElement(HTML.LI_ELEM);
186 }
187 else if (layout.equals(LAYOUT_GRID))
188 {
189
190 writer.endElement(HTML.TD_ELEM);
191 }
192 }
193
194
195
196
197
198
199
200 }
201 }
202 if (layout != null && layout.equals(LAYOUT_GRID)){
203 writer.endElement(HTML.TR_ELEM);
204 }
205
206
207
208
209
210
211 }
212
213
214 public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) throws IOException
215 {
216 RendererUtils.checkParamValidity(facesContext, uiComponent, UIData.class);
217 ResponseWriter writer = facesContext.getResponseWriter();
218 String layout = getLayout(uiComponent);
219 if (layout != null)
220 {
221 if (! layout.equals(LAYOUT_SIMPLE)) {
222 HtmlRendererUtils.writePrettyLineSeparator(facesContext);
223 }
224 if (layout.equals(LAYOUT_UL))
225 {
226 writer.endElement(HTML.UL_ELEM);
227 }
228 else if (layout.equals(LAYOUT_OL))
229 {
230 writer.endElement(HTML.OL_ELEM);
231 }
232 else if (layout.equals(LAYOUT_GRID))
233 {
234 writer.endElement(HTML.TABLE_ELEM);
235 }
236 else
237 {
238 if (uiComponent.getId() != null && !uiComponent.getId().startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
239 {
240 writer.endElement(HTML.SPAN_ELEM);
241 }
242 else
243 {
244 HtmlRendererUtils.renderOptionalEndElement(writer,
245 uiComponent,
246 HTML.SPAN_ELEM,
247 HTML.COMMON_PASSTROUGH_ATTRIBUTES);
248 }
249 }
250 }
251 }
252
253
254 private String getLayout(UIComponent component)
255 {
256 if (component instanceof HtmlDataList)
257 {
258 return ((HtmlDataList) component).getLayout();
259 }
260 else
261 {
262 return (String) component.getAttributes().get(JSFAttr.LAYOUT_ATTR);
263 }
264 }
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292 }