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  import org.apache.myfaces.shared_tomahawk.renderkit.html.util.ResourceUtils;
27  
28  import javax.faces.component.UIComponent;
29  import javax.faces.component.UIData;
30  import javax.faces.component.UIViewRoot;
31  import javax.faces.component.behavior.ClientBehavior;
32  import javax.faces.component.behavior.ClientBehaviorHolder;
33  import javax.faces.context.FacesContext;
34  import javax.faces.context.ResponseWriter;
35  import java.io.IOException;
36  import java.util.List;
37  import java.util.Map;
38  
39  /**
40   * @JSFRenderer
41   *   renderKitId = "HTML_BASIC" 
42   *   family = "javax.faces.Data"
43   *   type = "org.apache.myfaces.List"
44   * 
45   * @author Manfred Geiler (latest modification by $Author: lu4242 $)
46   * @version $Revision: 659874 $ $Date: 2008-05-24 15:59:15 -0500 (sáb, 24 may 2008) $
47   */
48  public class HtmlListRenderer
49          extends HtmlRenderer
50  {
51      //private static final Log log = LogFactory.getLog(HtmlListRenderer.class);
52  
53      public static final String LAYOUT_SIMPLE = "simple";
54      public static final String LAYOUT_UL = "unorderedList";
55      public static final String LAYOUT_OL = "orderedList";
56      public static final String LAYOUT_GRID = "grid";
57  
58      @Override
59      public void decode(FacesContext context, UIComponent component)
60      {
61          super.decode(context, component);
62          
63          HtmlRendererUtils.decodeClientBehaviors(context, component);
64      }
65  
66      public boolean getRendersChildren()
67      {
68          return true;
69      }
70  
71      public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) throws IOException
72      {
73          RendererUtils.checkParamValidity(facesContext, uiComponent, UIData.class);
74          ResponseWriter writer = facesContext.getResponseWriter();
75          String layout = getLayout(uiComponent);
76          
77          Map<String, List<ClientBehavior>> behaviors = null;
78          if (uiComponent instanceof ClientBehaviorHolder)
79          {
80              behaviors = ((ClientBehaviorHolder) uiComponent).getClientBehaviors();
81              if (!behaviors.isEmpty())
82              {
83                  ResourceUtils.renderDefaultJsfJsInlineIfNecessary(facesContext, facesContext.getResponseWriter());
84              }
85          }
86          
87          if (layout != null)
88          {
89              if (! layout.equals(LAYOUT_SIMPLE)) {
90                  HtmlRendererUtils.writePrettyLineSeparator(facesContext);
91              }
92              if (layout.equals(LAYOUT_UL))
93              {
94                  writer.startElement(HTML.UL_ELEM, uiComponent);
95  
96                  writer.writeAttribute(HTML.ID_ATTR, uiComponent.getClientId(facesContext), null);
97  
98                  if (behaviors != null && !behaviors.isEmpty())
99                  {
100                     HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.UNIVERSAL_ATTRIBUTES);
101                     HtmlRendererUtils.renderBehaviorizedEventHandlers(facesContext, writer, uiComponent, behaviors);
102                 }
103                 else
104                 {
105                     HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent,
106                             HTML.COMMON_PASSTROUGH_ATTRIBUTES);
107                 }
108             }
109             else if (layout.equals(LAYOUT_OL))
110             {
111                 writer.startElement(HTML.OL_ELEM, uiComponent);
112 
113                 writer.writeAttribute(HTML.ID_ATTR, uiComponent.getClientId(facesContext), null);
114 
115                 if (behaviors != null && !behaviors.isEmpty())
116                 {
117                     HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.UNIVERSAL_ATTRIBUTES);
118                     HtmlRendererUtils.renderBehaviorizedEventHandlers(facesContext, writer, uiComponent, behaviors);
119                 }
120                 else
121                 {
122                     HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent,
123                             HTML.COMMON_PASSTROUGH_ATTRIBUTES);
124                 }
125             }
126             else if (layout.equals(LAYOUT_GRID))
127             {
128                 writer.startElement(HTML.TABLE_ELEM, uiComponent);
129 
130                 writer.writeAttribute(HTML.ID_ATTR, uiComponent.getClientId(facesContext), null);
131 
132                 if (behaviors != null && !behaviors.isEmpty())
133                 {
134                     HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.UNIVERSAL_ATTRIBUTES);
135                     HtmlRendererUtils.renderBehaviorizedEventHandlers(facesContext, writer, uiComponent, behaviors);
136                 }
137                 else
138                 {
139                     HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent,
140                             HTML.COMMON_PASSTROUGH_ATTRIBUTES);
141                 }
142             }
143             else
144             {
145                 if (behaviors != null && !behaviors.isEmpty())
146                 {
147                     writer.startElement(HTML.SPAN_ELEM, uiComponent);
148                     writer.writeAttribute(HTML.ID_ATTR, uiComponent.getClientId(facesContext), null);
149                     HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.UNIVERSAL_ATTRIBUTES);
150                     HtmlRendererUtils.renderBehaviorizedEventHandlers(facesContext, writer, uiComponent, behaviors);
151                 }
152                 else
153                 {
154                     if (uiComponent.getId() != null && !uiComponent.getId().startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
155                     {
156                         writer.startElement(HTML.SPAN_ELEM, uiComponent);
157 
158                         writer.writeAttribute(HTML.ID_ATTR, uiComponent.getClientId(facesContext), null);
159 
160                         HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.COMMON_PASSTROUGH_ATTRIBUTES);
161                     }
162                     else
163                     {
164                         HtmlRendererUtils.renderHTMLAttributesWithOptionalStartElement(writer, uiComponent,
165                                 HTML.SPAN_ELEM, HTML.COMMON_PASSTROUGH_ATTRIBUTES);
166                     }
167                 }
168             }
169         }
170     }
171 
172     public void encodeChildren(FacesContext facesContext, UIComponent component) throws IOException
173     {
174         RendererUtils.checkParamValidity(facesContext, component, UIData.class);
175 
176         UIData uiData = (UIData) component;
177         String layout = getLayout(component);
178         //Map requestMap = facesContext.getExternalContext().getRequestMap();
179 
180         ResponseWriter writer = facesContext.getResponseWriter();
181 
182         int first = uiData.getFirst();
183         int rows = uiData.getRows();
184         int rowCount = uiData.getRowCount();
185         if (rows <= 0)
186         {
187             rows = rowCount - first;
188         }
189         int last = first + rows;
190         if (last > rowCount) last = rowCount;
191 
192         /*
193         String rowIndexVar = getRowIndexVar(component);
194         String rowCountVar = getRowCountVar(component);
195 
196         if (rowCountVar != null)
197         {
198             requestMap.put(rowCountVar, new Integer(rowCount));
199         }
200         */
201 
202         if (layout != null && layout.equals(LAYOUT_GRID)){
203             // output table row
204             writer.startElement(HTML.TR_ELEM, null);
205         }
206         for (int i = first; i < last; i++)
207         {
208             uiData.setRowIndex(i);
209             if (uiData.isRowAvailable())
210             {
211                 /*
212                 if (rowIndexVar != null)
213                 {
214                     requestMap.put(rowIndexVar, new Integer(i));
215                 }
216                 */
217 
218                 if (layout != null)
219                 {
220                     if (! layout.equals(LAYOUT_SIMPLE)) {
221                         HtmlRendererUtils.writePrettyLineSeparator(facesContext);
222                     }
223                     if (layout.equals(LAYOUT_UL) || (layout.equals(LAYOUT_OL)))
224                     {
225                         writer.startElement(HTML.LI_ELEM, component);
226                         HtmlRendererUtils.renderHTMLAttribute(writer, component, "itemStyleClass", HTML.STYLE_CLASS_ATTR);
227                         HtmlRendererUtils.renderHTMLAttribute(writer, component, "itemOnClick", HTML.ONCLICK_ATTR);
228                     }
229                     else if (layout.equals(LAYOUT_GRID))
230                     {
231                         // output table cells
232                         writer.startElement(HTML.TD_ELEM, null);
233                     }
234                 }
235 
236                 RendererUtils.renderChildren(facesContext, component);
237 
238                 if (layout != null)
239                 {
240                     if (layout.equals(LAYOUT_UL) || (layout.equals(LAYOUT_OL)))
241                     {
242                         writer.endElement(HTML.LI_ELEM);
243                     }
244                     else if (layout.equals(LAYOUT_GRID))
245                     {
246                         // output table cells
247                         writer.endElement(HTML.TD_ELEM);
248                     }
249                 }
250 
251                 /*
252                 if (rowIndexVar != null)
253                 {
254                     requestMap.remove(rowIndexVar);
255                 }
256                 */
257             }
258         }
259         if (layout != null && layout.equals(LAYOUT_GRID)){
260             writer.endElement(HTML.TR_ELEM);
261         }
262         /*
263         if (rowCountVar != null)
264         {
265             requestMap.remove(rowCountVar);
266         }
267         */
268     }
269 
270 
271     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) throws IOException
272     {
273         RendererUtils.checkParamValidity(facesContext, uiComponent, UIData.class);
274         ResponseWriter writer = facesContext.getResponseWriter();
275         String layout = getLayout(uiComponent);
276         if (layout != null)
277         {
278             if (! layout.equals(LAYOUT_SIMPLE)) {
279                 HtmlRendererUtils.writePrettyLineSeparator(facesContext);
280             }
281             if (layout.equals(LAYOUT_UL))
282             {
283                 writer.endElement(HTML.UL_ELEM);
284             }
285             else if (layout.equals(LAYOUT_OL))
286             {
287                 writer.endElement(HTML.OL_ELEM);
288             }
289             else if (layout.equals(LAYOUT_GRID))
290             {
291                 writer.endElement(HTML.TABLE_ELEM);
292             }
293             else
294             {
295                 Map<String, List<ClientBehavior>> behaviors = null;
296                 if (uiComponent instanceof ClientBehaviorHolder)
297                 {
298                     behaviors = ((ClientBehaviorHolder) uiComponent).getClientBehaviors();
299                 }
300                 
301                 if (behaviors != null && !behaviors.isEmpty())
302                 {
303                     writer.endElement(HTML.SPAN_ELEM);
304                 }
305                 else
306                 {
307                     if (uiComponent.getId() != null && !uiComponent.getId().startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
308                     {
309                         writer.endElement(HTML.SPAN_ELEM);
310                     }
311                     else
312                     {
313                         HtmlRendererUtils.renderOptionalEndElement(writer,
314                                 uiComponent,
315                                 HTML.SPAN_ELEM,
316                                 HTML.COMMON_PASSTROUGH_ATTRIBUTES);
317                     }
318                 }
319             }
320         }
321     }
322 
323 
324     private String getLayout(UIComponent component)
325     {
326         if (component instanceof HtmlDataList)
327         {
328             return ((HtmlDataList) component).getLayout();
329         }
330         else
331         {
332             return (String) component.getAttributes().get(JSFAttr.LAYOUT_ATTR);
333         }
334     }
335 
336     /*
337     private String getRowIndexVar(UIComponent component)
338     {
339         if (component instanceof HtmlDataList)
340         {
341             return ((HtmlDataList)component).getRowIndexVar();
342         }
343         else
344         {
345             return (String)component.getAttributes().get("rowIndexVar");
346         }
347     }
348 
349     private String getRowCountVar(UIComponent component)
350     {
351         if (component instanceof HtmlDataList)
352         {
353             return ((HtmlDataList)component).getRowCountVar();
354         }
355         else
356         {
357             return (String)component.getAttributes().get("rowCountVar");
358         }
359     }
360     */
361 
362 }