KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > renderkit > html > HtmlGridRenderer


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.renderkit.html;
17
18 import org.apache.myfaces.renderkit.JSFAttr;
19 import org.apache.myfaces.renderkit.RendererUtils;
20 import org.apache.myfaces.util.ArrayUtils;
21 import org.apache.myfaces.util.StringUtils;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 import javax.faces.component.UIComponent;
27 import javax.faces.component.UIPanel;
28 import javax.faces.component.html.HtmlPanelGrid;
29 import javax.faces.context.FacesContext;
30 import javax.faces.context.ResponseWriter;
31 import java.io.IOException JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 /**
35  * X-CHECKED: tlddoc h:panelGrid 1.0 final
36  *
37  * $Log: HtmlGridRenderer.java,v $
38  * Revision 1.16 2004/12/23 13:03:08 mmarinschek
39  * id's not rendered (or not conditionally rendered); changes in jslistener to support both ie and firefox now
40  *
41  * Revision 1.15 2004/11/21 12:39:09 mmarinschek
42  * better Error handling
43  *
44  * Revision 1.14 2004/10/13 11:51:00 matze
45  * renamed packages to org.apache
46  *
47  * Revision 1.13 2004/07/01 22:05:07 mwessendorf
48  * ASF switch
49  *
50  * Revision 1.12 2004/06/17 03:57:14 dave0000
51  * Fix order of nested if statements (columns <= 0 check) + misc
52  *
53  * Revision 1.11 2004/06/04 00:51:05 o_rossmueller
54  * fixed NPE
55  *
56  * Revision 1.10 2004/06/03 23:02:14 o_rossmueller
57  * fix for completion of last row
58  *
59  * Revision 1.9 2004/06/03 13:41:08 o_rossmueller
60  * fix #962084: render rowClasses
61  *
62  * Revision 1.8 2004/03/31 13:43:29 royalts
63  * no message
64  *
65  * Revision 1.7 2004/03/29 14:56:55 manolito
66  * refactoring for implementation and non-standard component split
67  *
68  * Revision 1.6 2004/03/25 12:42:24 manolito
69  * x-checked and corrected GridRenderer
70  *
71  * @author Manfred Geiler (latest modification by $Author: mmarinschek $)
72  * @author Thomas Spiegl
73  * @version $Revision: 1.16 $ $Date: 2004/12/23 13:03:08 $
74  */

75 public class HtmlGridRenderer
76     extends HtmlRenderer
77 {
78     private static final Log log = LogFactory.getLog(HtmlGridRenderer.class);
79
80     public boolean getRendersChildren()
81     {
82         return true;
83     }
84
85     public void encodeBegin(FacesContext facesContext, UIComponent component)
86             throws IOException JavaDoc
87     {
88         // all work done in encodeEnd()
89
}
90
91     public void encodeChildren(FacesContext context, UIComponent component)
92         throws IOException JavaDoc
93     {
94         // all work done in encodeEnd()
95
}
96
97     public void encodeEnd(FacesContext facesContext, UIComponent component)
98             throws IOException JavaDoc
99     {
100         RendererUtils.checkParamValidity(facesContext, component, UIPanel.class);
101
102         int columns;
103         if (component instanceof HtmlPanelGrid)
104         {
105             columns = ((HtmlPanelGrid)component).getColumns();
106         }
107         else
108         {
109             Integer JavaDoc i = (Integer JavaDoc)component.getAttributes().get(JSFAttr.COLUMNS_ATTR);
110             columns = i != null ? i.intValue() : 0;
111         }
112
113         if (columns <= 0)
114         {
115             if (log.isErrorEnabled())
116             {
117                 log.error("Wrong columns attribute for PanelGrid " + component.getClientId(facesContext) + ": " + columns);
118             }
119             columns = 1;
120         }
121
122         ResponseWriter writer = facesContext.getResponseWriter();
123         writer.startElement(HTML.TABLE_ELEM, component);
124         HtmlRendererUtils.writeIdIfNecessary(writer, component, facesContext);
125         HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.TABLE_PASSTHROUGH_ATTRIBUTES);
126
127         writer.flush();
128
129         renderHeaderOrFooter(facesContext, writer, component, columns, true); //Header facet
130

131         renderChildren(facesContext, writer, component, columns);
132
133         renderHeaderOrFooter(facesContext, writer, component, columns, false); //Footer facet
134

135         writer.endElement(HTML.TABLE_ELEM);
136     }
137
138
139     private void renderHeaderOrFooter(FacesContext context,
140                                       ResponseWriter writer,
141                                       UIComponent component,
142                                       int columns,
143                                       boolean header)
144         throws IOException JavaDoc
145     {
146         UIComponent facet = component.getFacet(header ? "header" : "footer");
147         if (facet == null) return;
148
149         HtmlRendererUtils.writePrettyLineSeparator(context);
150         writer.startElement(header ? HTML.THEAD_ELEM : HTML.TFOOT_ELEM, component);
151         writer.startElement(HTML.TR_ELEM, component);
152         writer.startElement(header ? HTML.TH_ELEM : HTML.TD_ELEM, component);
153
154         String JavaDoc styleClass = (component instanceof HtmlPanelGrid)
155             ? (header ?
156                          ((HtmlPanelGrid)component).getHeaderClass() :
157                          ((HtmlPanelGrid)component).getFooterClass())
158             : (header ?
159                          (String JavaDoc)component.getAttributes().get(JSFAttr.HEADER_CLASS_ATTR) :
160                          (String JavaDoc)component.getAttributes().get(JSFAttr.FOOTER_CLASS_ATTR));
161         if (styleClass != null)
162         {
163             writer.writeAttribute(HTML.CLASS_ATTR, styleClass,
164                                   header ? JSFAttr.HEADER_CLASS_ATTR : JSFAttr.FOOTER_CLASS_ATTR);
165         }
166
167         if (header)
168         {
169             writer.writeAttribute(HTML.SCOPE_ATTR, HTML.SCOPE_COLGROUP_VALUE, null);
170         }
171
172         writer.writeAttribute(HTML.COLSPAN_ATTR, Integer.toString(columns), null);
173
174         HtmlRendererUtils.writePrettyLineSeparator(context);
175         RendererUtils.renderChild(context, facet);
176
177         HtmlRendererUtils.writePrettyLineSeparator(context);
178         writer.endElement(header ? HTML.TH_ELEM : HTML.TD_ELEM);
179         writer.endElement(HTML.TR_ELEM);
180         writer.endElement(header ? HTML.THEAD_ELEM : HTML.TFOOT_ELEM);
181     }
182
183
184     private void renderChildren(FacesContext context,
185                                 ResponseWriter writer,
186                                 UIComponent component,
187                                 int columns)
188         throws IOException JavaDoc
189     {
190         try
191         {
192             writer.startElement(HTML.TBODY_ELEM, component);
193
194             String JavaDoc columnClasses;
195             String JavaDoc rowClasses;
196             if (component instanceof HtmlPanelGrid)
197             {
198                 columnClasses = ((HtmlPanelGrid)component).getColumnClasses();
199                 rowClasses = ((HtmlPanelGrid)component).getRowClasses();
200             }
201             else
202             {
203                 columnClasses = (String JavaDoc)component.getAttributes().get(JSFAttr.COLUMN_CLASSES_ATTR);
204                 rowClasses = (String JavaDoc)component.getAttributes().get(JSFAttr.ROW_CLASSES_ATTR);
205             }
206
207             String JavaDoc[] columnClassesArray = (columnClasses == null)
208                 ? ArrayUtils.EMPTY_STRING_ARRAY
209                 : StringUtils.trim(StringUtils.splitShortString(columnClasses, ','));
210             int columnClassesCount = columnClassesArray.length;
211
212             String JavaDoc[] rowClassesArray = (rowClasses == null)
213                 ? ArrayUtils.EMPTY_STRING_ARRAY
214                 : StringUtils.trim(StringUtils.splitShortString(rowClasses, ','));
215             int rowClassesCount = rowClassesArray.length;
216
217             int childCount = component.getChildCount();
218             if (childCount > 0)
219             {
220                 int columnIndex = 0;
221                 int rowClassIndex = 0;
222                 boolean rowStarted = false;
223                 for (Iterator JavaDoc it = component.getChildren().iterator(); it.hasNext(); )
224                 {
225                     UIComponent child = (UIComponent)it.next();
226                     if (child.isRendered())
227                     {
228                         if (columnIndex == 0)
229                         {
230                             //start of new/next row
231
if (rowStarted)
232                             {
233                                 //do we have to close the last row?
234
writer.endElement(HTML.TR_ELEM);
235                                 HtmlRendererUtils.writePrettyLineSeparator(context);
236                             }
237                             writer.startElement(HTML.TR_ELEM, component);
238                             if (rowClassIndex < rowClassesCount) {
239                                 writer.writeAttribute(HTML.CLASS_ATTR, rowClassesArray[rowClassIndex], null);
240                             }
241                             rowStarted = true;
242                             rowClassIndex++;
243                             if (rowClassIndex == rowClassesCount) {
244                                 rowClassIndex = 0;
245                             }
246                         }
247
248                         writer.startElement(HTML.TD_ELEM, component);
249                         if (columnIndex < columnClassesCount)
250                         {
251                             writer.writeAttribute(HTML.CLASS_ATTR, columnClassesArray[columnIndex], null);
252                         }
253                         RendererUtils.renderChild(context, child);
254                         writer.endElement(HTML.TD_ELEM);
255
256                         columnIndex++;
257                         if (columnIndex >= columns) {
258                             columnIndex = 0;
259                         }
260                     }
261                 }
262
263                 if (rowStarted)
264                 {
265                     if (columnIndex > 0)
266                     {
267                         if (log.isWarnEnabled()) log.warn("PanelGrid " + component.getClientId(context) + " has not enough children. Child count should be a multiple of the columns attribute.");
268                         //Render empty columns, so that table is correct
269
for ( ; columnIndex < columns; columnIndex++)
270                         {
271                             writer.startElement(HTML.TD_ELEM, component);
272                             if (columnIndex < columnClassesCount)
273                             {
274                                 writer.writeAttribute(HTML.CLASS_ATTR, columnClassesArray[columnIndex], null);
275                             }
276                             writer.endElement(HTML.TD_ELEM);
277                         }
278                     }
279                     writer.endElement(HTML.TR_ELEM);
280                     HtmlRendererUtils.writePrettyLineSeparator(context);
281                 }
282             }
283
284             writer.endElement(HTML.TBODY_ELEM);
285         }
286         catch(IOException JavaDoc ex)
287         {
288             throw ex;
289         }
290         catch(RuntimeException JavaDoc ex2)
291         {
292             log.error("Exception while rendering children of panel-grid.",ex2);
293             throw ex2;
294         }
295     }
296
297 }
298
Popular Tags