Coverage Report - org.seasar.teeda.core.render.html.HtmlDataTableRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
HtmlDataTableRenderer
100%
142/142
94%
58/62
4.111
 
 1  
 /*
 2  
  * Copyright 2004-2011 the Seasar Foundation and the Others.
 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,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package org.seasar.teeda.core.render.html;
 17  
 
 18  
 import java.io.IOException;
 19  
 import java.util.ArrayList;
 20  
 import java.util.Iterator;
 21  
 import java.util.List;
 22  
 
 23  
 import javax.faces.component.UIColumn;
 24  
 import javax.faces.component.UIComponent;
 25  
 import javax.faces.component.html.HtmlDataTable;
 26  
 import javax.faces.context.FacesContext;
 27  
 import javax.faces.context.ResponseWriter;
 28  
 import javax.faces.internal.IgnoreAttribute;
 29  
 
 30  
 import org.seasar.teeda.core.JsfConstants;
 31  
 import org.seasar.teeda.core.render.AbstractRenderer;
 32  
 import org.seasar.teeda.core.util.LoopIterator;
 33  
 import org.seasar.teeda.core.util.RendererUtil;
 34  
 
 35  
 /**
 36  
  * @author manhole
 37  
  */
 38  57
 public class HtmlDataTableRenderer extends AbstractRenderer {
 39  
 
 40  
     public static final String COMPONENT_FAMILY = "javax.faces.Data";
 41  
 
 42  
     public static final String RENDERER_TYPE = "javax.faces.Table";
 43  
 
 44  57
     private final IgnoreAttribute ignoreComponent = new IgnoreAttribute();
 45  
     {
 46  57
         ignoreComponent.addAttributeName(JsfConstants.ID_ATTR);
 47  57
         ignoreComponent.addAttributeName(JsfConstants.VALUE_ATTR);
 48  57
         ignoreComponent.addAttributeName("rowData");
 49  57
         ignoreComponent.addAttributeName("rowIndex");
 50  57
         ignoreComponent.addAttributeName("rows");
 51  57
         ignoreComponent.addAttributeName("first");
 52  57
         ignoreComponent.addAttributeName("rowCount");
 53  57
         ignoreComponent.addAttributeName("header");
 54  57
         ignoreComponent.addAttributeName("footer");
 55  57
         ignoreComponent.addAttributeName("headerClass");
 56  57
         ignoreComponent.addAttributeName("footerClass");
 57  57
         ignoreComponent.addAttributeName("rowAvailable");
 58  57
         ignoreComponent.addAttributeName("rowClasses");
 59  57
         ignoreComponent.addAttributeName("columnClasses");
 60  57
         ignoreComponent.addAttributeName("var");
 61  57
     }
 62  
 
 63  
     public void encodeBegin(FacesContext context, UIComponent component)
 64  
             throws IOException {
 65  29
         assertNotNull(context, component);
 66  27
         if (!component.isRendered()) {
 67  1
             return;
 68  
         }
 69  26
         encodeHtmlDataTableBegin(context, (HtmlDataTable) component);
 70  26
     }
 71  
 
 72  
     protected void encodeHtmlDataTableBegin(FacesContext context,
 73  
             HtmlDataTable htmlDataTable) throws IOException {
 74  26
         ResponseWriter writer = context.getResponseWriter();
 75  26
         writer.startElement(JsfConstants.TABLE_ELEM, htmlDataTable);
 76  26
         RendererUtil.renderIdAttributeIfNecessary(writer, htmlDataTable,
 77  
                 getIdForRender(context, htmlDataTable));
 78  26
         renderRemainAttributes(htmlDataTable, writer, ignoreComponent);
 79  
 
 80  26
         final List columns = new ArrayList();
 81  26
         boolean isColumnHeaderExist = false;
 82  26
         boolean isColumnFooterExist = false;
 83  
         {
 84  26
             for (Iterator it = getRenderedChildrenIterator(htmlDataTable); it
 85  48
                     .hasNext();) {
 86  22
                 UIComponent child = (UIComponent) it.next();
 87  22
                 if (child instanceof UIColumn) {
 88  22
                     UIColumn column = (UIColumn) child;
 89  22
                     columns.add(column);
 90  22
                     if (!isColumnHeaderExist) {
 91  18
                         UIComponent columnHeader = column.getHeader();
 92  18
                         if (columnHeader != null && columnHeader.isRendered()) {
 93  7
                             isColumnHeaderExist = true;
 94  
                         }
 95  
                     }
 96  22
                     if (!isColumnFooterExist) {
 97  18
                         UIComponent columnFooter = column.getFooter();
 98  18
                         if (columnFooter != null && columnFooter.isRendered()) {
 99  7
                             isColumnFooterExist = true;
 100  
                         }
 101  
                     }
 102  
                 }
 103  
             }
 104  
         }
 105  
         // thead
 106  
         {
 107  26
             UIComponent tableHeader = toNullIfNotRendered(htmlDataTable
 108  
                     .getHeader());
 109  26
             if (tableHeader != null || isColumnHeaderExist) {
 110  10
                 writer.startElement(JsfConstants.THEAD_ELEM, tableHeader);
 111  
 
 112  10
                 if (tableHeader != null) {
 113  6
                     writer.startElement(JsfConstants.TR_ELEM, tableHeader);
 114  6
                     writer.startElement(JsfConstants.TH_ELEM, tableHeader);
 115  6
                     RendererUtil.renderAttribute(writer,
 116  
                             JsfConstants.COLSPAN_ATTR, new Integer(columns
 117  
                                     .size()));
 118  6
                     writer.writeAttribute(JsfConstants.SCOPE_ATTR,
 119  
                             JsfConstants.COLGROUP_VALUE, null);
 120  6
                     RendererUtil.renderAttribute(writer,
 121  
                             JsfConstants.CLASS_ATTR, htmlDataTable
 122  
                                     .getHeaderClass(),
 123  
                             JsfConstants.HEADER_CLASS_ATTR);
 124  6
                     encodeComponent(context, tableHeader);
 125  6
                     writer.endElement(JsfConstants.TH_ELEM);
 126  6
                     writer.endElement(JsfConstants.TR_ELEM);
 127  
                 }
 128  
 
 129  10
                 if (isColumnHeaderExist) {
 130  7
                     writer.startElement(JsfConstants.TR_ELEM, tableHeader);
 131  7
                     for (Iterator it = columns.iterator(); it.hasNext();) {
 132  12
                         writer.startElement(JsfConstants.TH_ELEM, tableHeader);
 133  12
                         writer.writeAttribute(JsfConstants.COLGROUP_ATTR,
 134  
                                 JsfConstants.COL_VALUE, null);
 135  12
                         RendererUtil.renderAttribute(writer,
 136  
                                 JsfConstants.CLASS_ATTR, htmlDataTable
 137  
                                         .getHeaderClass(),
 138  
                                 JsfConstants.HEADER_CLASS_ATTR);
 139  12
                         UIColumn column = (UIColumn) it.next();
 140  12
                         UIComponent columnHeader = column.getHeader();
 141  12
                         if (columnHeader != null) {
 142  12
                             encodeComponent(context, columnHeader);
 143  
                         }
 144  12
                         writer.endElement(JsfConstants.TH_ELEM);
 145  
                     }
 146  7
                     writer.endElement(JsfConstants.TR_ELEM);
 147  
                 }
 148  
 
 149  10
                 writer.endElement(JsfConstants.THEAD_ELEM);
 150  
             }
 151  
         }
 152  
         // tfoot
 153  
         {
 154  26
             UIComponent tableFooter = toNullIfNotRendered(htmlDataTable
 155  
                     .getFooter());
 156  26
             if (tableFooter != null || isColumnFooterExist) {
 157  10
                 writer.startElement(JsfConstants.TFOOT_ELEM, tableFooter);
 158  
 
 159  10
                 if (tableFooter != null) {
 160  6
                     writer.startElement(JsfConstants.TR_ELEM, tableFooter);
 161  6
                     writer.startElement(JsfConstants.TD_ELEM, tableFooter);
 162  6
                     RendererUtil.renderAttribute(writer,
 163  
                             JsfConstants.COLSPAN_ATTR, new Integer(columns
 164  
                                     .size()));
 165  6
                     RendererUtil.renderAttribute(writer,
 166  
                             JsfConstants.CLASS_ATTR, htmlDataTable
 167  
                                     .getFooterClass(),
 168  
                             JsfConstants.FOOTER_CLASS_ATTR);
 169  6
                     encodeComponent(context, tableFooter);
 170  6
                     writer.endElement(JsfConstants.TD_ELEM);
 171  6
                     writer.endElement(JsfConstants.TR_ELEM);
 172  
                 }
 173  
 
 174  10
                 if (isColumnFooterExist) {
 175  7
                     writer.startElement(JsfConstants.TR_ELEM, tableFooter);
 176  7
                     for (Iterator it = columns.iterator(); it.hasNext();) {
 177  12
                         writer.startElement(JsfConstants.TD_ELEM, tableFooter);
 178  12
                         RendererUtil.renderAttribute(writer,
 179  
                                 JsfConstants.CLASS_ATTR, htmlDataTable
 180  
                                         .getFooterClass(),
 181  
                                 JsfConstants.FOOTER_CLASS_ATTR);
 182  12
                         UIColumn column = (UIColumn) it.next();
 183  12
                         UIComponent columnFooter = column.getFooter();
 184  12
                         if (columnFooter != null) {
 185  12
                             encodeComponent(context, columnFooter);
 186  
                         }
 187  12
                         writer.endElement(JsfConstants.TD_ELEM);
 188  
                     }
 189  7
                     writer.endElement(JsfConstants.TR_ELEM);
 190  
                 }
 191  
 
 192  10
                 writer.endElement(JsfConstants.TFOOT_ELEM);
 193  
             }
 194  
         }
 195  26
     }
 196  
 
 197  
     public void encodeChildren(FacesContext context, UIComponent component)
 198  
             throws IOException {
 199  10
         assertNotNull(context, component);
 200  8
         if (!component.isRendered()) {
 201  1
             return;
 202  
         }
 203  7
         encodeHtmlDataTableChildren(context, (HtmlDataTable) component);
 204  7
     }
 205  
 
 206  
     protected void encodeHtmlDataTableChildren(FacesContext context,
 207  
             HtmlDataTable htmlDataTable) throws IOException {
 208  7
         ResponseWriter writer = context.getResponseWriter();
 209  
 
 210  7
         writer.startElement(JsfConstants.TBODY_ELEM, htmlDataTable);
 211  
 
 212  7
         LoopIterator rowClasses = toStyleLoopIterator(htmlDataTable
 213  
                 .getRowClasses());
 214  7
         LoopIterator columnClasses = toStyleLoopIterator(htmlDataTable
 215  
                 .getColumnClasses());
 216  
 
 217  7
         int start = htmlDataTable.getFirst();
 218  7
         int rows = htmlDataTable.getRows();
 219  7
         boolean allRow = true;
 220  7
         if (0 < rows) {
 221  2
             allRow = false;
 222  
         }
 223  7
         htmlDataTable.setRowIndex(start);
 224  7
         for (int rowIndex = start; ((allRow || 0 < rows) && htmlDataTable
 225  
                 .isRowAvailable());) {
 226  18
             encodeBodyRow(context, htmlDataTable, writer, rowClasses,
 227  
                     columnClasses);
 228  18
             rowIndex++;
 229  18
             rows--;
 230  18
             htmlDataTable.setRowIndex(rowIndex);
 231  
         }
 232  7
         writer.endElement(JsfConstants.TBODY_ELEM);
 233  7
     }
 234  
 
 235  
     private void encodeBodyRow(FacesContext context,
 236  
             HtmlDataTable htmlDataTable, ResponseWriter writer,
 237  
             Iterator rowClasses, LoopIterator columnClasses) throws IOException {
 238  18
         writer.startElement(JsfConstants.TR_ELEM, htmlDataTable);
 239  18
         if (rowClasses.hasNext()) {
 240  8
             RendererUtil.renderAttribute(writer, JsfConstants.CLASS_ATTR,
 241  
                     rowClasses.next(), JsfConstants.ROW_CLASSES_ATTR);
 242  
         }
 243  18
         columnClasses.reset();
 244  18
         for (Iterator itColumn = getRenderedChildrenIterator(htmlDataTable); itColumn
 245  57
                 .hasNext();) {
 246  39
             UIComponent col = (UIComponent) itColumn.next();
 247  39
             if (col instanceof UIColumn) {
 248  39
                 UIColumn column = (UIColumn) col;
 249  39
                 encodeBodyRowColumn(context, column, writer, columnClasses);
 250  
             }
 251  
         }
 252  18
         writer.endElement(JsfConstants.TR_ELEM);
 253  18
     }
 254  
 
 255  
     private void encodeBodyRowColumn(FacesContext context, UIColumn column,
 256  
             ResponseWriter writer, Iterator columnClasses) throws IOException {
 257  39
         writer.startElement(JsfConstants.TD_ELEM, column);
 258  39
         if (columnClasses.hasNext()) {
 259  26
             RendererUtil.renderAttribute(writer, JsfConstants.CLASS_ATTR,
 260  
                     columnClasses.next(), JsfConstants.COLUMN_CLASSES_ATTR);
 261  
         }
 262  39
         encodeDescendantComponent(context, column);
 263  39
         writer.endElement(JsfConstants.TD_ELEM);
 264  39
     }
 265  
 
 266  
     public void encodeEnd(FacesContext context, UIComponent component)
 267  
             throws IOException {
 268  5
         assertNotNull(context, component);
 269  3
         if (!component.isRendered()) {
 270  1
             return;
 271  
         }
 272  2
         encodeHtmlDataTableEnd(context, (HtmlDataTable) component);
 273  2
     }
 274  
 
 275  
     protected void encodeHtmlDataTableEnd(FacesContext context,
 276  
             HtmlDataTable htmlDataTable) throws IOException {
 277  2
         ResponseWriter writer = context.getResponseWriter();
 278  2
         writer.endElement(JsfConstants.TABLE_ELEM);
 279  2
     }
 280  
 
 281  
     public boolean getRendersChildren() {
 282  4
         return true;
 283  
     }
 284  
 
 285  
 }