Coverage Report - org.apache.myfaces.shared_impl.renderkit.html.HtmlGridRendererBase
 
Classes in this File Line Coverage Branch Coverage Complexity
HtmlGridRendererBase
0%
0/96
0%
0/66
4.143
 
 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.shared_impl.renderkit.html;
 20  
 
 21  
 import org.apache.commons.logging.Log;
 22  
 import org.apache.commons.logging.LogFactory;
 23  
 import org.apache.myfaces.shared_impl.renderkit.JSFAttr;
 24  
 import org.apache.myfaces.shared_impl.renderkit.RendererUtils;
 25  
 import org.apache.myfaces.shared_impl.util.ArrayUtils;
 26  
 import org.apache.myfaces.shared_impl.util.StringUtils;
 27  
 
 28  
 import javax.faces.component.UIComponent;
 29  
 import javax.faces.component.UIPanel;
 30  
 import javax.faces.component.html.HtmlPanelGrid;
 31  
 import javax.faces.context.FacesContext;
 32  
 import javax.faces.context.ResponseWriter;
 33  
 import java.io.IOException;
 34  
 import java.util.Iterator;
 35  
 
 36  
 /**
 37  
  * @author Martin Marinschek
 38  
  * @version $Revision: 618839 $ $Date: 2008-02-05 19:41:42 -0500 (Tue, 05 Feb 2008) $
 39  
  *          <p/>
 40  
  *          $Log: $
 41  
  */
 42  0
 public class HtmlGridRendererBase
 43  
         extends HtmlRenderer
 44  
 {
 45  0
     private static final Log log = LogFactory.getLog(HtmlGridRendererBase.class);
 46  
 
 47  
     public boolean getRendersChildren()
 48  
     {
 49  0
         return true;
 50  
     }
 51  
 
 52  
     public void encodeBegin(FacesContext facesContext, UIComponent component)
 53  
             throws IOException
 54  
     {
 55  
         // all work done in encodeEnd()
 56  0
     }
 57  
 
 58  
     public void encodeChildren(FacesContext context, UIComponent component)
 59  
         throws IOException
 60  
     {
 61  
         // all work done in encodeEnd()
 62  0
     }
 63  
 
 64  
     public void encodeEnd(FacesContext facesContext, UIComponent component)
 65  
             throws IOException
 66  
     {
 67  0
         RendererUtils.checkParamValidity(facesContext, component, UIPanel.class);
 68  
 
 69  
         int columns;
 70  0
         if (component instanceof HtmlPanelGrid)
 71  
         {
 72  0
             columns = ((HtmlPanelGrid)component).getColumns();
 73  
         }
 74  
         else
 75  
         {
 76  0
             Integer i = (Integer)component.getAttributes().get(org.apache.myfaces.shared_impl.renderkit.JSFAttr.COLUMNS_ATTR);
 77  0
             columns = i != null ? i.intValue() : 0;
 78  
         }
 79  
 
 80  0
         if (columns <= 0)
 81  
         {
 82  0
             if (log.isErrorEnabled())
 83  
             {
 84  0
                 log.error("Wrong columns attribute for PanelGrid " + component.getClientId(facesContext) + ": " + columns);
 85  
             }
 86  0
             columns = 1;
 87  
         }
 88  
 
 89  0
         ResponseWriter writer = facesContext.getResponseWriter();
 90  0
         writer.startElement(HTML.TABLE_ELEM, component);
 91  0
         HtmlRendererUtils.writeIdIfNecessary(writer, component, facesContext);
 92  0
         HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.TABLE_PASSTHROUGH_ATTRIBUTES);
 93  
 
 94  0
         writer.flush();
 95  
 
 96  0
         HtmlRendererUtils.renderTableCaption(facesContext, writer, component);
 97  
 
 98  
         // theader and tfooter are rendered before the tbody
 99  0
         renderHeaderOrFooter(facesContext, writer, component, columns, true);   //Header facet
 100  0
         renderHeaderOrFooter(facesContext, writer, component, columns, false);  //Footer facet
 101  
         
 102  0
         renderChildren(facesContext, writer, component, columns);
 103  
 
 104  0
         writer.endElement(HTML.TABLE_ELEM);
 105  0
     }
 106  
 
 107  
     
 108  
 
 109  
     protected void renderHeaderOrFooter(FacesContext context,
 110  
                                       ResponseWriter writer,
 111  
                                       UIComponent component,
 112  
                                       int columns,
 113  
                                       boolean header)
 114  
         throws IOException
 115  
     {
 116  0
         UIComponent facet = component.getFacet(header ? "header" : "footer");
 117  0
         if (facet == null) return;
 118  
 
 119  0
         HtmlRendererUtils.writePrettyLineSeparator(context);
 120  0
         writer.startElement(header ? org.apache.myfaces.shared_impl.renderkit.html.HTML.THEAD_ELEM : HTML.TFOOT_ELEM, component);
 121  0
         writer.startElement(HTML.TR_ELEM, component);
 122  0
         writer.startElement(header ? HTML.TH_ELEM : HTML.TD_ELEM, component);
 123  
 
 124  0
         String styleClass = (component instanceof HtmlPanelGrid)
 125  
             ? (header ?
 126  
                          ((HtmlPanelGrid)component).getHeaderClass() :
 127  
                          ((HtmlPanelGrid)component).getFooterClass())
 128  
             : (header ?
 129  
                          (String)component.getAttributes().get(JSFAttr.HEADER_CLASS_ATTR) :
 130  
                          (String)component.getAttributes().get(org.apache.myfaces.shared_impl.renderkit.JSFAttr.FOOTER_CLASS_ATTR));
 131  0
         if (styleClass != null)
 132  
         {
 133  0
             writer.writeAttribute(HTML.CLASS_ATTR, styleClass,
 134  
                                   header ? JSFAttr.HEADER_CLASS_ATTR : org.apache.myfaces.shared_impl.renderkit.JSFAttr.FOOTER_CLASS_ATTR);
 135  
         }
 136  
 
 137  0
         if (header)
 138  
         {
 139  0
             writer.writeAttribute(HTML.SCOPE_ATTR, HTML.SCOPE_COLGROUP_VALUE, null);
 140  
         }
 141  
 
 142  0
         writer.writeAttribute(HTML.COLSPAN_ATTR, Integer.toString(columns), null);
 143  
 
 144  0
         HtmlRendererUtils.writePrettyLineSeparator(context);
 145  0
         RendererUtils.renderChild(context, facet);
 146  
 
 147  0
         HtmlRendererUtils.writePrettyLineSeparator(context);
 148  0
         writer.endElement(header ? HTML.TH_ELEM : HTML.TD_ELEM);
 149  0
         writer.endElement(HTML.TR_ELEM);
 150  0
         writer.endElement(header ? HTML.THEAD_ELEM : HTML.TFOOT_ELEM);
 151  0
     }
 152  
 
 153  
     protected int childAttributes(FacesContext context,
 154  
             ResponseWriter writer,
 155  
             UIComponent component,
 156  
             int columnIndex)
 157  
         throws IOException
 158  
     {
 159  
         // subclasses can override this method to add attributes to the table cell <td> tag
 160  0
         return columnIndex;
 161  
     }
 162  
 
 163  
     protected void renderChildren(FacesContext context,
 164  
                                 ResponseWriter writer,
 165  
                                 UIComponent component,
 166  
                                 int columns)
 167  
         throws IOException
 168  
     {
 169  0
         writer.startElement(HTML.TBODY_ELEM, component);
 170  
 
 171  
         String columnClasses;
 172  
         String rowClasses;
 173  0
         if (component instanceof HtmlPanelGrid)
 174  
         {
 175  0
             columnClasses = ((HtmlPanelGrid)component).getColumnClasses();
 176  0
             rowClasses =  ((HtmlPanelGrid)component).getRowClasses();
 177  
         }
 178  
         else
 179  
         {
 180  0
             columnClasses = (String)component.getAttributes().get(org.apache.myfaces.shared_impl.renderkit.JSFAttr.COLUMN_CLASSES_ATTR);
 181  0
             rowClasses = (String)component.getAttributes().get(JSFAttr.ROW_CLASSES_ATTR);
 182  
         }
 183  
 
 184  0
         String[] columnClassesArray = (columnClasses == null)
 185  
             ? ArrayUtils.EMPTY_STRING_ARRAY
 186  
             : StringUtils.trim(StringUtils.splitShortString(columnClasses, ','));
 187  0
         int columnClassesCount = columnClassesArray.length;
 188  
 
 189  0
         String[] rowClassesArray = (rowClasses == null)
 190  
             ? org.apache.myfaces.shared_impl.util.ArrayUtils.EMPTY_STRING_ARRAY
 191  
             : StringUtils.trim(StringUtils.splitShortString(rowClasses, ','));
 192  0
         int rowClassesCount = rowClassesArray.length;
 193  
 
 194  0
         int childCount = getChildCount(component);
 195  0
         if (childCount > 0)
 196  
         {
 197  0
             int columnIndex = 0;
 198  0
             int rowClassIndex = 0;
 199  0
             boolean rowStarted = false;
 200  0
             for (Iterator it = getChildren(component).iterator(); it.hasNext(); )
 201  
             {
 202  0
                 UIComponent child = (UIComponent)it.next();
 203  0
                 if (child.isRendered())
 204  
                 {
 205  0
                     if (columnIndex == 0)
 206  
                     {
 207  
                         //start of new/next row
 208  0
                         if (rowStarted)
 209  
                         {
 210  
                             //do we have to close the last row?
 211  0
                             writer.endElement(HTML.TR_ELEM);
 212  0
                             HtmlRendererUtils.writePrettyLineSeparator(context);
 213  
                         }
 214  0
                         writer.startElement(HTML.TR_ELEM, component);
 215  0
                         if (rowClassIndex < rowClassesCount) {
 216  0
                             writer.writeAttribute(HTML.CLASS_ATTR, rowClassesArray[rowClassIndex], null);
 217  
                         }
 218  0
                         rowStarted = true;
 219  0
                         rowClassIndex++;
 220  0
                         if (rowClassIndex == rowClassesCount) {
 221  0
                             rowClassIndex = 0;
 222  
                         }
 223  
                     }
 224  
 
 225  0
                     writer.startElement(HTML.TD_ELEM, component);
 226  0
                     if (columnIndex < columnClassesCount)
 227  
                     {
 228  0
                         writer.writeAttribute(HTML.CLASS_ATTR, columnClassesArray[columnIndex], null);
 229  
                     }
 230  0
                     columnIndex = childAttributes(context, writer, child, columnIndex);
 231  0
                     RendererUtils.renderChild(context, child);
 232  0
                     writer.endElement(HTML.TD_ELEM);
 233  
 
 234  0
                     columnIndex++;
 235  0
                     if (columnIndex >= columns) {
 236  0
                         columnIndex = 0;
 237  
                     }
 238  
                 }
 239  0
             }
 240  
 
 241  0
             if (rowStarted)
 242  
             {
 243  0
                 if (columnIndex > 0)
 244  
                 {
 245  0
                     if (log.isWarnEnabled()) log.warn("PanelGrid " + component.getClientId(context) + " has not enough children. Child count should be a multiple of the columns attribute.");
 246  
                     //Render empty columns, so that table is correct
 247  0
                     for ( ; columnIndex < columns; columnIndex++)
 248  
                     {
 249  0
                         writer.startElement(HTML.TD_ELEM, component);
 250  0
                         if (columnIndex < columnClassesCount)
 251  
                         {
 252  0
                             writer.writeAttribute(HTML.CLASS_ATTR, columnClassesArray[columnIndex], null);
 253  
                         }
 254  0
                         writer.endElement(HTML.TD_ELEM);
 255  
                     }
 256  
                 }
 257  0
                 writer.endElement(HTML.TR_ELEM);
 258  0
                 HtmlRendererUtils.writePrettyLineSeparator(context);
 259  
             }
 260  
         }
 261  
 
 262  0
         writer.endElement(HTML.TBODY_ELEM);
 263  0
     }
 264  
 
 265  
 }