View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * @JSFRenderer
36   *   renderKitId = "HTML_BASIC" 
37   *   family = "javax.faces.Data"
38   *   type = "org.apache.myfaces.List"
39   * 
40   * @author Manfred Geiler (latest modification by $Author: lu4242 $)
41   * @version $Revision: 940151 $ $Date: 2010-05-01 21:02:26 -0500 (Sat, 01 May 2010) $
42   */
43  public class HtmlListRenderer
44          extends HtmlRenderer
45  {
46      //private static final Log log = LogFactory.getLog(HtmlListRenderer.class);
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         //Map requestMap = facesContext.getExternalContext().getRequestMap();
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         String rowIndexVar = getRowIndexVar(component);
137         String rowCountVar = getRowCountVar(component);
138 
139         if (rowCountVar != null)
140         {
141             requestMap.put(rowCountVar, new Integer(rowCount));
142         }
143         */
144 
145         if (layout != null && layout.equals(LAYOUT_GRID)){
146             // output table row
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                 if (rowIndexVar != null)
156                 {
157                     requestMap.put(rowIndexVar, new Integer(i));
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                         // output table cells
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                         // output table cells
190                         writer.endElement(HTML.TD_ELEM);
191                     }
192                 }
193 
194                 /*
195                 if (rowIndexVar != null)
196                 {
197                     requestMap.remove(rowIndexVar);
198                 }
199                 */
200             }
201         }
202         if (layout != null && layout.equals(LAYOUT_GRID)){
203             writer.endElement(HTML.TR_ELEM);
204         }
205         /*
206         if (rowCountVar != null)
207         {
208             requestMap.remove(rowCountVar);
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     private String getRowIndexVar(UIComponent component)
268     {
269         if (component instanceof HtmlDataList)
270         {
271             return ((HtmlDataList)component).getRowIndexVar();
272         }
273         else
274         {
275             return (String)component.getAttributes().get("rowIndexVar");
276         }
277     }
278 
279     private String getRowCountVar(UIComponent component)
280     {
281         if (component instanceof HtmlDataList)
282         {
283             return ((HtmlDataList)component).getRowCountVar();
284         }
285         else
286         {
287             return (String)component.getAttributes().get("rowCountVar");
288         }
289     }
290     */
291 
292 }