1 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 ; 32 import java.util.Iterator ; 33 34 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 87 { 88 } 90 91 public void encodeChildren(FacesContext context, UIComponent component) 92 throws IOException 93 { 94 } 96 97 public void encodeEnd(FacesContext facesContext, UIComponent component) 98 throws IOException 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 i = (Integer )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); 131 renderChildren(facesContext, writer, component, columns); 132 133 renderHeaderOrFooter(facesContext, writer, component, columns, false); 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 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 styleClass = (component instanceof HtmlPanelGrid) 155 ? (header ? 156 ((HtmlPanelGrid)component).getHeaderClass() : 157 ((HtmlPanelGrid)component).getFooterClass()) 158 : (header ? 159 (String )component.getAttributes().get(JSFAttr.HEADER_CLASS_ATTR) : 160 (String )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 189 { 190 try 191 { 192 writer.startElement(HTML.TBODY_ELEM, component); 193 194 String columnClasses; 195 String rowClasses; 196 if (component instanceof HtmlPanelGrid) 197 { 198 columnClasses = ((HtmlPanelGrid)component).getColumnClasses(); 199 rowClasses = ((HtmlPanelGrid)component).getRowClasses(); 200 } 201 else 202 { 203 columnClasses = (String )component.getAttributes().get(JSFAttr.COLUMN_CLASSES_ATTR); 204 rowClasses = (String )component.getAttributes().get(JSFAttr.ROW_CLASSES_ATTR); 205 } 206 207 String [] columnClassesArray = (columnClasses == null) 208 ? ArrayUtils.EMPTY_STRING_ARRAY 209 : StringUtils.trim(StringUtils.splitShortString(columnClasses, ',')); 210 int columnClassesCount = columnClassesArray.length; 211 212 String [] 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 it = component.getChildren().iterator(); it.hasNext(); ) 224 { 225 UIComponent child = (UIComponent)it.next(); 226 if (child.isRendered()) 227 { 228 if (columnIndex == 0) 229 { 230 if (rowStarted) 232 { 233 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 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 ex) 287 { 288 throw ex; 289 } 290 catch(RuntimeException ex2) 291 { 292 log.error("Exception while rendering children of panel-grid.",ex2); 293 throw ex2; 294 } 295 } 296 297 } 298 | Popular Tags |