KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > datalist > HtmlListRenderer


1 /*
2  * Copyright 2004 The Apache Software Foundation.
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 org.apache.myfaces.custom.datalist;
17
18 import org.apache.myfaces.renderkit.JSFAttr;
19 import org.apache.myfaces.renderkit.RendererUtils;
20 import org.apache.myfaces.renderkit.html.HTML;
21 import org.apache.myfaces.renderkit.html.HtmlRenderer;
22 import org.apache.myfaces.renderkit.html.HtmlRendererUtils;
23
24 import javax.faces.component.UIComponent;
25 import javax.faces.component.UIData;
26 import javax.faces.component.UIViewRoot;
27 import javax.faces.context.FacesContext;
28 import javax.faces.context.ResponseWriter;
29 import java.io.IOException JavaDoc;
30
31 /**
32  * @author Manfred Geiler (latest modification by $Author: matze $)
33  * @version $Revision: 1.8 $ $Date: 2004/10/13 11:50:57 $
34  * $Log: HtmlListRenderer.java,v $
35  * Revision 1.8 2004/10/13 11:50:57 matze
36  * renamed packages to org.apache
37  *
38  * Revision 1.7 2004/09/02 17:23:25 tinytoony
39  * fix for the span-element for other than the output-text
40  *
41  * Revision 1.6 2004/08/20 07:12:00 manolito
42  * rowIndexVar and rowCountVar now handled correctly by component itself instead of renderer
43  *
44  * Revision 1.5 2004/08/09 11:47:09 manolito
45  * CSS style support also for non OL or UL layout
46  *
47  * Revision 1.4 2004/08/09 09:10:39 manolito
48  * RFE #990814 - dataList component ignores styleClass attribute
49  *
50  */

51 public class HtmlListRenderer
52     extends HtmlRenderer
53 {
54     //private static final Log log = LogFactory.getLog(HtmlListRenderer.class);
55

56     public static final String JavaDoc LAYOUT_SIMPLE = "simple";
57     public static final String JavaDoc LAYOUT_UL = "unorderedList";
58     public static final String JavaDoc LAYOUT_OL = "orderedList";
59
60     public boolean getRendersChildren()
61     {
62         return true;
63     }
64
65     public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) throws IOException JavaDoc
66     {
67         RendererUtils.checkParamValidity(facesContext, uiComponent, UIData.class);
68         ResponseWriter writer = facesContext.getResponseWriter();
69         HtmlRendererUtils.writePrettyLineSeparator(facesContext);
70         String JavaDoc layout = getLayout(uiComponent);
71         if (layout != null)
72         {
73             if (layout.equals(LAYOUT_UL))
74             {
75                 writer.startElement(HTML.UL_ELEM, uiComponent);
76                 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent,
77                                                        HTML.COMMON_PASSTROUGH_ATTRIBUTES);
78             }
79             else if (layout.equals(LAYOUT_OL))
80             {
81                 writer.startElement(HTML.OL_ELEM, uiComponent);
82                 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent,
83                                                        HTML.COMMON_PASSTROUGH_ATTRIBUTES);
84             }
85             else
86             {
87                 if(uiComponent.getId()!=null && !uiComponent.getId().startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
88                 {
89                     writer.startElement(HTML.SPAN_ELEM, uiComponent);
90
91                     writer.writeAttribute(HTML.ID_ATTR, uiComponent.getClientId(facesContext),null);
92
93                     HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.COMMON_PASSTROUGH_ATTRIBUTES);
94
95                 }
96                 else
97                 {
98                     HtmlRendererUtils.renderHTMLAttributesWithOptionalStartElement(writer,uiComponent,
99                             HTML.SPAN_ELEM,HTML.COMMON_PASSTROUGH_ATTRIBUTES);
100                 }
101             }
102         }
103     }
104
105     public void encodeChildren(FacesContext facesContext, UIComponent component) throws IOException JavaDoc
106     {
107         RendererUtils.checkParamValidity(facesContext, component, UIData.class);
108
109         UIData uiData = (UIData)component;
110         String JavaDoc layout = getLayout(component);
111         //Map requestMap = facesContext.getExternalContext().getRequestMap();
112

113         ResponseWriter writer = facesContext.getResponseWriter();
114
115         int first = uiData.getFirst();
116         int rows = uiData.getRows();
117         int rowCount = uiData.getRowCount();
118         if (rows <= 0)
119         {
120             rows = rowCount - first;
121         }
122         int last = first + rows;
123         if (last > rowCount) last = rowCount;
124
125         /*
126         String rowIndexVar = getRowIndexVar(component);
127         String rowCountVar = getRowCountVar(component);
128
129         if (rowCountVar != null)
130         {
131             requestMap.put(rowCountVar, new Integer(rowCount));
132         }
133         */

134
135         for (int i = first; i < last; i++)
136         {
137             uiData.setRowIndex(i);
138             if (uiData.isRowAvailable())
139             {
140                 /*
141                 if (rowIndexVar != null)
142                 {
143                     requestMap.put(rowIndexVar, new Integer(i));
144                 }
145                 */

146
147                 HtmlRendererUtils.writePrettyLineSeparator(facesContext);
148                 if (layout != null && (layout.equals(LAYOUT_UL) || (layout.equals(LAYOUT_OL))))
149                 {
150                     writer.startElement(HTML.LI_ELEM, component);
151                 }
152
153                 RendererUtils.renderChildren(facesContext, component);
154
155                 if (layout != null && (layout.equals(LAYOUT_UL) || (layout.equals(LAYOUT_OL))))
156                 {
157                     writer.endElement(HTML.LI_ELEM);
158                 }
159
160                 /*
161                 if (rowIndexVar != null)
162                 {
163                     requestMap.remove(rowIndexVar);
164                 }
165                 */

166             }
167         }
168
169         /*
170         if (rowCountVar != null)
171         {
172             requestMap.remove(rowCountVar);
173         }
174         */

175     }
176
177
178     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) throws IOException JavaDoc
179     {
180         RendererUtils.checkParamValidity(facesContext, uiComponent, UIData.class);
181         ResponseWriter writer = facesContext.getResponseWriter();
182         HtmlRendererUtils.writePrettyLineSeparator(facesContext);
183         String JavaDoc layout = getLayout(uiComponent);
184         if (layout != null)
185         {
186             if (layout.equals(LAYOUT_UL))
187             {
188                 writer.endElement(HTML.UL_ELEM);
189             }
190             else if (layout.equals(LAYOUT_OL))
191             {
192                 writer.endElement(HTML.OL_ELEM);
193             }
194             else
195             {
196                 if(uiComponent.getId()!=null && !uiComponent.getId().startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
197                 {
198                     writer.endElement(HTML.SPAN_ELEM);
199                 }
200                 else
201                 {
202                     HtmlRendererUtils.renderOptionalEndElement(writer,
203                                                            uiComponent,
204                                                            HTML.SPAN_ELEM,
205                                                            HTML.COMMON_PASSTROUGH_ATTRIBUTES);
206                 }
207             }
208         }
209     }
210
211
212     private String JavaDoc getLayout(UIComponent component)
213     {
214         if (component instanceof HtmlDataList)
215         {
216             return ((HtmlDataList)component).getLayout();
217         }
218         else
219         {
220             return (String JavaDoc)component.getAttributes().get(JSFAttr.LAYOUT_ATTR);
221         }
222     }
223
224     /*
225     private String getRowIndexVar(UIComponent component)
226     {
227         if (component instanceof HtmlDataList)
228         {
229             return ((HtmlDataList)component).getRowIndexVar();
230         }
231         else
232         {
233             return (String)component.getAttributes().get("rowIndexVar");
234         }
235     }
236
237     private String getRowCountVar(UIComponent component)
238     {
239         if (component instanceof HtmlDataList)
240         {
241             return ((HtmlDataList)component).getRowCountVar();
242         }
243         else
244         {
245             return (String)component.getAttributes().get("rowCountVar");
246         }
247     }
248     */

249
250 }
251
Popular Tags