Coverage Report - org.seasar.teeda.core.render.html.HtmlPanelGridRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
HtmlPanelGridRenderer
100%
98/98
100%
24/24
2.364
 
 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.Iterator;
 20  
 
 21  
 import javax.faces.component.UIComponent;
 22  
 import javax.faces.component.html.HtmlPanelGrid;
 23  
 import javax.faces.context.FacesContext;
 24  
 import javax.faces.context.ResponseWriter;
 25  
 import javax.faces.internal.IgnoreAttribute;
 26  
 
 27  
 import org.seasar.teeda.core.JsfConstants;
 28  
 import org.seasar.teeda.core.render.AbstractRenderer;
 29  
 import org.seasar.teeda.core.util.LoopIterator;
 30  
 import org.seasar.teeda.core.util.RendererUtil;
 31  
 
 32  
 /**
 33  
  * @author manhole
 34  
  */
 35  43
 public class HtmlPanelGridRenderer extends AbstractRenderer {
 36  
 
 37  
     public static final String COMPONENT_FAMILY = "javax.faces.Panel";
 38  
 
 39  
     public static final String RENDERER_TYPE = "javax.faces.Grid";
 40  
 
 41  43
     private final IgnoreAttribute ignoreComponent = new IgnoreAttribute();
 42  
     {
 43  43
         ignoreComponent.addAttributeName(JsfConstants.ID_ATTR);
 44  43
         ignoreComponent.addAttributeName(JsfConstants.HEADER_CLASS_ATTR);
 45  43
         ignoreComponent.addAttributeName(JsfConstants.FOOTER_CLASS_ATTR);
 46  43
         ignoreComponent.addAttributeName(JsfConstants.COLUMNS_ATTR);
 47  43
         ignoreComponent.addAttributeName(JsfConstants.COLUMN_CLASSES_ATTR);
 48  43
         ignoreComponent.addAttributeName(JsfConstants.ROW_CLASSES_ATTR);
 49  43
     }
 50  
 
 51  
     public void encodeBegin(FacesContext context, UIComponent component)
 52  
             throws IOException {
 53  18
         assertNotNull(context, component);
 54  16
         if (!component.isRendered()) {
 55  1
             return;
 56  
         }
 57  15
         encodeHtmlPanelGridBegin(context, (HtmlPanelGrid) component);
 58  15
     }
 59  
 
 60  
     protected void encodeHtmlPanelGridBegin(FacesContext context,
 61  
             HtmlPanelGrid htmlPanelGrid) throws IOException {
 62  15
         ResponseWriter writer = context.getResponseWriter();
 63  15
         writer.startElement(JsfConstants.TABLE_ELEM, htmlPanelGrid);
 64  15
         RendererUtil.renderIdAttributeIfNecessary(writer, htmlPanelGrid,
 65  
                 getIdForRender(context, htmlPanelGrid));
 66  15
         renderRemainAttributes(htmlPanelGrid, writer, ignoreComponent);
 67  
 
 68  
         // thead
 69  
         {
 70  15
             UIComponent tableHeader = toNullIfNotRendered(htmlPanelGrid
 71  
                     .getFacet("header"));
 72  15
             if (tableHeader != null) {
 73  4
                 writer.startElement(JsfConstants.THEAD_ELEM, tableHeader);
 74  
 
 75  4
                 writer.startElement(JsfConstants.TR_ELEM, tableHeader);
 76  4
                 writer.startElement(JsfConstants.TH_ELEM, tableHeader);
 77  4
                 RendererUtil.renderAttribute(writer, JsfConstants.COLSPAN_ATTR,
 78  
                         new Integer(getColumns(htmlPanelGrid)));
 79  4
                 writer.writeAttribute(JsfConstants.SCOPE_ATTR,
 80  
                         JsfConstants.COLGROUP_VALUE, null);
 81  4
                 RendererUtil.renderAttribute(writer, JsfConstants.CLASS_ATTR,
 82  
                         htmlPanelGrid.getHeaderClass(),
 83  
                         JsfConstants.HEADER_CLASS_ATTR);
 84  4
                 encodeComponent(context, tableHeader);
 85  4
                 writer.endElement(JsfConstants.TH_ELEM);
 86  4
                 closeTr(writer);
 87  
 
 88  4
                 writer.endElement(JsfConstants.THEAD_ELEM);
 89  
             }
 90  
         }
 91  
         // tfoot
 92  
         {
 93  15
             UIComponent tableFooter = toNullIfNotRendered(htmlPanelGrid
 94  
                     .getFacet("footer"));
 95  15
             if (tableFooter != null) {
 96  4
                 writer.startElement(JsfConstants.TFOOT_ELEM, tableFooter);
 97  
 
 98  4
                 writer.startElement(JsfConstants.TR_ELEM, tableFooter);
 99  4
                 writer.startElement(JsfConstants.TD_ELEM, tableFooter);
 100  4
                 RendererUtil.renderAttribute(writer, JsfConstants.COLSPAN_ATTR,
 101  
                         new Integer(getColumns(htmlPanelGrid)));
 102  4
                 RendererUtil.renderAttribute(writer, JsfConstants.CLASS_ATTR,
 103  
                         htmlPanelGrid.getFooterClass(),
 104  
                         JsfConstants.FOOTER_CLASS_ATTR);
 105  4
                 encodeComponent(context, tableFooter);
 106  4
                 writer.endElement(JsfConstants.TD_ELEM);
 107  4
                 closeTr(writer);
 108  
 
 109  4
                 writer.endElement(JsfConstants.TFOOT_ELEM);
 110  
             }
 111  
         }
 112  15
     }
 113  
 
 114  
     public void encodeChildren(FacesContext context, UIComponent component)
 115  
             throws IOException {
 116  7
         assertNotNull(context, component);
 117  5
         if (!component.isRendered()) {
 118  1
             return;
 119  
         }
 120  4
         encodeHtmlPanelGridChildren(context, (HtmlPanelGrid) component);
 121  4
     }
 122  
 
 123  
     protected void encodeHtmlPanelGridChildren(FacesContext context,
 124  
             HtmlPanelGrid htmlPanelGrid) throws IOException {
 125  4
         ResponseWriter writer = context.getResponseWriter();
 126  4
         writer.startElement(JsfConstants.TBODY_ELEM, htmlPanelGrid);
 127  
 
 128  4
         LoopIterator rowClasses = toStyleLoopIterator(htmlPanelGrid
 129  
                 .getRowClasses());
 130  4
         LoopIterator columnClasses = toStyleLoopIterator(htmlPanelGrid
 131  
                 .getColumnClasses());
 132  
 
 133  4
         final int maxColumns = getColumns(htmlPanelGrid);
 134  4
         final int initialPosition = 0;
 135  4
         int position = initialPosition;
 136  4
         boolean trTagOpening = false;
 137  4
         for (Iterator itChild = getRenderedChildrenIterator(htmlPanelGrid); itChild
 138  34
                 .hasNext();) {
 139  30
             UIComponent child = (UIComponent) itChild.next();
 140  30
             if (position == initialPosition) {
 141  9
                 openTr(writer, htmlPanelGrid, rowClasses, columnClasses);
 142  9
                 trTagOpening = true;
 143  
             }
 144  30
             encodeBodyRowColumn(context, child, writer, columnClasses);
 145  30
             position++;
 146  30
             if (maxColumns <= position) {
 147  7
                 position = initialPosition;
 148  7
                 closeTr(writer);
 149  7
                 trTagOpening = false;
 150  
             }
 151  
         }
 152  4
         if (trTagOpening) {
 153  2
             closeTr(writer);
 154  2
             trTagOpening = false;
 155  
         }
 156  
 
 157  4
         writer.endElement(JsfConstants.TBODY_ELEM);
 158  4
     }
 159  
 
 160  
     private void openTr(ResponseWriter writer, HtmlPanelGrid htmlPanelGrid,
 161  
             LoopIterator rowClasses, LoopIterator columnClasses)
 162  
             throws IOException {
 163  9
         writer.startElement(JsfConstants.TR_ELEM, htmlPanelGrid);
 164  9
         if (rowClasses.hasNext()) {
 165  8
             RendererUtil.renderAttribute(writer, JsfConstants.CLASS_ATTR,
 166  
                     rowClasses.next(), JsfConstants.ROW_CLASSES_ATTR);
 167  
         }
 168  9
         columnClasses.reset();
 169  9
     }
 170  
 
 171  
     private void closeTr(ResponseWriter writer) throws IOException {
 172  17
         writer.endElement(JsfConstants.TR_ELEM);
 173  17
     }
 174  
 
 175  
     private void encodeBodyRowColumn(FacesContext context,
 176  
             UIComponent component, ResponseWriter writer, Iterator columnClasses)
 177  
             throws IOException {
 178  30
         writer.startElement(JsfConstants.TD_ELEM, component);
 179  30
         if (columnClasses.hasNext()) {
 180  29
             RendererUtil.renderAttribute(writer, JsfConstants.CLASS_ATTR,
 181  
                     columnClasses.next(), JsfConstants.COLUMN_CLASSES_ATTR);
 182  
         }
 183  30
         encodeComponent(context, component);
 184  30
         writer.endElement(JsfConstants.TD_ELEM);
 185  30
     }
 186  
 
 187  
     public void encodeEnd(FacesContext context, UIComponent component)
 188  
             throws IOException {
 189  5
         assertNotNull(context, component);
 190  3
         if (!component.isRendered()) {
 191  1
             return;
 192  
         }
 193  2
         encodeHtmlPanelGridEnd(context, (HtmlPanelGrid) component);
 194  2
     }
 195  
 
 196  
     protected void encodeHtmlPanelGridEnd(FacesContext context,
 197  
             HtmlPanelGrid htmlPanelGrid) throws IOException {
 198  2
         ResponseWriter writer = context.getResponseWriter();
 199  2
         writer.endElement(JsfConstants.TABLE_ELEM);
 200  2
     }
 201  
 
 202  
     public boolean getRendersChildren() {
 203  4
         return true;
 204  
     }
 205  
 
 206  
     private int getColumns(HtmlPanelGrid htmlPanelGrid) {
 207  12
         int columns = htmlPanelGrid.getColumns();
 208  12
         if (columns <= 0) {
 209  6
             columns = 1;
 210  
         }
 211  12
         return columns;
 212  
     }
 213  
 
 214  
 }