1 16 package org.apache.myfaces.custom.newspaper; 17 18 import javax.faces.component.UIComponent; 19 import javax.faces.context.FacesContext; 20 import javax.faces.component.html.HtmlDataTable; 21 import javax.faces.context.ResponseWriter; 22 import javax.faces.component.UIColumn; 23 import javax.faces.component.UIData; 24 import org.apache.myfaces.renderkit.JSFAttr; 25 import org.apache.myfaces.renderkit.RendererUtils; 26 import org.apache.myfaces.renderkit.html.HtmlRendererUtils; 27 import org.apache.myfaces.renderkit.html.HTML; 28 import org.apache.myfaces.renderkit.html.HtmlTableRendererBase; 29 import org.apache.myfaces.util.ArrayUtils; 30 import org.apache.myfaces.util.StringUtils; 31 import java.io.IOException ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 37 42 public class HtmlNewspaperTableRenderer 43 extends HtmlTableRendererBase 44 { 45 private static final Log log = LogFactory.getLog(HtmlNewspaperTableRenderer.class); 46 47 public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) throws IOException { 48 RendererUtils.checkParamValidity(facesContext, uiComponent, UIData.class); 49 ResponseWriter writer = facesContext.getResponseWriter(); 50 HtmlNewspaperTable newspaperTable = (HtmlNewspaperTable)uiComponent; 51 52 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 54 writer.startElement(HTML.TABLE_ELEM, newspaperTable); 55 writer.writeAttribute(HTML.ID_ATTR, newspaperTable.getClientId(facesContext), null); 56 HtmlRendererUtils.renderHTMLAttributes(writer, newspaperTable, HTML.TABLE_PASSTHROUGH_ATTRIBUTES); 57 58 renderFacet(facesContext, writer, newspaperTable, true); 60 } 61 62 public void encodeChildren(FacesContext facesContext, UIComponent uiComponent) throws IOException { 63 RendererUtils.checkParamValidity(facesContext, uiComponent, UIData.class); 64 ResponseWriter writer = facesContext.getResponseWriter(); 65 HtmlNewspaperTable newspaperTable = (HtmlNewspaperTable)uiComponent; 66 67 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 69 writer.startElement(HTML.TBODY_ELEM, newspaperTable); 70 71 Styles styles = getStyles(newspaperTable); 73 74 int first = newspaperTable.getFirst(); 76 int rows = newspaperTable.getRows(); 77 int rowCount = newspaperTable.getRowCount(); 78 if(rows <= 0) { 79 rows = rowCount - first; 80 } 81 int last = first + rows; 82 if(last > rowCount) last = rowCount; 83 84 int newspaperColumns = newspaperTable.getNewspaperColumns(); 86 int newspaperRows; 87 if((last - first) % newspaperColumns == 0) newspaperRows = (last - first) / newspaperColumns; 88 else newspaperRows = ((last - first) / newspaperColumns) + 1; 89 90 for(int nr = 0; nr < newspaperRows; nr++) { 92 93 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 95 writer.startElement(HTML.TR_ELEM, newspaperTable); 96 if(styles.hasRowStyle()) { 97 String rowStyle = styles.getRowStyle(nr); 98 writer.writeAttribute(HTML.CLASS_ATTR, rowStyle, null); 99 } 100 101 for(int nc = 0; nc < newspaperColumns; nc++) { 103 104 int currentRow = nc * newspaperRows + nr + first; 106 107 if(currentRow >= last) continue; 109 110 newspaperTable.setRowIndex(currentRow); 112 if(!newspaperTable.isRowAvailable()) { 113 log.error("Row is not available. Rowindex = " + currentRow); 114 return; 115 } 116 117 List children = newspaperTable.getChildren(); 119 for(int j = 0; j < newspaperTable.getChildCount(); j++) { 120 UIComponent child = (UIComponent)children.get(j); 122 if(!(child instanceof UIColumn)) continue; 123 UIColumn column = (UIColumn)child; 124 if(!child.isRendered()) continue; 125 writer.startElement(HTML.TD_ELEM, newspaperTable); 127 if(styles.hasColumnStyle()) writer.writeAttribute(HTML.CLASS_ATTR, styles.getColumnStyle(j), null); 128 RendererUtils.renderChild(facesContext, child); 129 writer.endElement(HTML.TD_ELEM); 130 } 131 132 if(nc < newspaperColumns - 1) renderSpacerCell(facesContext, writer, newspaperTable); 134 } 135 writer.endElement(HTML.TR_ELEM); 137 } 138 139 writer.endElement(HTML.TBODY_ELEM); 141 } 142 143 146 private int getNewspaperColumns() { 147 return 3; 148 } 149 150 public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) throws IOException { 151 RendererUtils.checkParamValidity(facesContext, uiComponent, UIData.class); 152 ResponseWriter writer = facesContext.getResponseWriter(); 153 HtmlNewspaperTable newspaperTable = (HtmlNewspaperTable)uiComponent; 154 155 renderFacet(facesContext, writer, newspaperTable, false); 157 158 writer.endElement(HTML.TABLE_ELEM); 160 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 161 } 162 163 164 167 private int countColumns(HtmlNewspaperTable newspaperTable) { 168 int columnCount = 0; 169 for(Iterator it = newspaperTable.getChildren().iterator(); it.hasNext(); ) { 170 UIComponent uiComponent = (UIComponent)it.next(); 171 if (uiComponent instanceof UIColumn && ((UIColumn)uiComponent).isRendered()) { 172 columnCount++; 173 } 174 } 175 return columnCount; 176 } 177 178 181 private boolean hasFacet(HtmlNewspaperTable newspaperTable, boolean header) { 182 for(Iterator it = newspaperTable.getChildren().iterator(); it.hasNext(); ) { 183 UIComponent uiComponent = (UIComponent)it.next(); 185 if(!(uiComponent instanceof UIColumn)) continue; 186 UIColumn column = (UIColumn)uiComponent; 187 if(!column.isRendered()) continue; 188 189 if(header && ((UIColumn)uiComponent).getHeader() != null) return true; 191 if(!header && ((UIColumn)uiComponent).getFooter() != null) return true; 192 } 193 return false; 194 } 195 196 199 private void renderFacet(FacesContext facesContext, ResponseWriter writer, HtmlNewspaperTable newspaperTable, boolean header) throws IOException { 200 int columnCount = countColumns(newspaperTable); 201 boolean hasColumnFacet = hasFacet(newspaperTable, header); 202 UIComponent facet = header ? newspaperTable.getHeader() : newspaperTable.getFooter(); 203 204 if(facet == null && !hasColumnFacet) return; 206 207 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 209 String elemName = header ? HTML.THEAD_ELEM : HTML.TFOOT_ELEM; 210 writer.startElement(elemName, newspaperTable); 211 212 String styleClass; 214 if(header) styleClass = getHeaderClass(newspaperTable); 215 else styleClass = getFooterClass(newspaperTable); 216 217 if(header) { 219 if (facet != null) renderTableHeaderOrFooterRow(facesContext, writer, newspaperTable, facet, styleClass, HTML.TD_ELEM, columnCount); 220 if (hasColumnFacet) renderColumnHeaderOrFooterRow(facesContext, writer, newspaperTable, styleClass, header); 221 } else { 223 if (hasColumnFacet) renderColumnHeaderOrFooterRow(facesContext, writer, newspaperTable, styleClass, header); 224 if (facet != null) renderTableHeaderOrFooterRow(facesContext, writer, newspaperTable, facet, styleClass, HTML.TD_ELEM, columnCount); 225 } 226 227 writer.endElement(elemName); 229 } 230 231 235 private void renderTableHeaderOrFooterRow(FacesContext facesContext, ResponseWriter writer, 236 HtmlNewspaperTable newspaperTable, UIComponent facet, String styleClass, String colElementName, int tableColumns) 237 throws IOException { 238 239 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 241 writer.startElement(HTML.TR_ELEM, newspaperTable); 242 writer.startElement(colElementName, newspaperTable); 243 if(styleClass != null) writer.writeAttribute(HTML.CLASS_ATTR, styleClass, null); 244 if(colElementName.equals(HTML.TH_ELEM)) writer.writeAttribute(HTML.SCOPE_ATTR, HTML.SCOPE_COLGROUP_VALUE, null); 245 246 int totalColumns = newspaperTable.getNewspaperColumns() * tableColumns; 248 if(newspaperTable.getSpacer() != null) totalColumns = totalColumns + getNewspaperColumns() - 1; 249 writer.writeAttribute(HTML.COLSPAN_ATTR, new Integer (totalColumns), null); 250 251 if(facet != null) RendererUtils.renderChild(facesContext, facet); 253 254 writer.endElement(colElementName); 256 writer.endElement(HTML.TR_ELEM); 257 } 258 259 260 263 private void renderColumnHeaderOrFooterRow(FacesContext facesContext, 264 ResponseWriter writer, HtmlNewspaperTable newspaperTable, String styleClass, boolean header) 265 throws IOException { 266 267 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 268 writer.startElement(HTML.TR_ELEM, newspaperTable); 269 int newspaperColumns = newspaperTable.getNewspaperColumns(); 270 for(int nc = 0; nc < newspaperColumns; nc++) { 271 for(Iterator it = newspaperTable.getChildren().iterator(); it.hasNext(); ) { 272 UIComponent uiComponent = (UIComponent)it.next(); 273 if(!(uiComponent instanceof UIColumn)) continue; 274 UIColumn column = (UIColumn)uiComponent; 275 if(!column.isRendered()) continue; 276 UIComponent facet = null; 278 if(header) facet = column.getHeader(); 279 else facet = column.getFooter(); 280 renderColumnHeaderOrFooterCell(facesContext, writer, column, styleClass, facet); 282 } 283 284 if(nc < newspaperColumns - 1) renderSpacerCell(facesContext, writer, newspaperTable); 286 } 287 writer.endElement(HTML.TR_ELEM); 288 } 289 290 293 private void renderColumnHeaderOrFooterCell(FacesContext facesContext, ResponseWriter writer, 294 UIColumn uiColumn, String styleClass, UIComponent facet) throws IOException { 295 296 writer.startElement(HTML.TH_ELEM, uiColumn); 297 if(styleClass != null) writer.writeAttribute(HTML.CLASS_ATTR, styleClass, null); 298 if(facet != null) RendererUtils.renderChild(facesContext, facet); 299 writer.endElement(HTML.TH_ELEM); 300 } 301 302 305 private void renderSpacerCell(FacesContext facesContext, ResponseWriter writer, 306 HtmlNewspaperTable newspaperTable) throws IOException { 307 if(newspaperTable.getSpacer() == null) return; 308 309 writer.startElement(HTML.TD_ELEM, newspaperTable); 310 RendererUtils.renderChild(facesContext, newspaperTable.getSpacer()); 311 writer.endElement(HTML.TD_ELEM); 312 } 313 314 317 private static String getHeaderClass(HtmlNewspaperTable newspaperTable) { 318 if(newspaperTable instanceof HtmlDataTable) { 319 return ((HtmlDataTable)newspaperTable).getHeaderClass(); 320 } else { 321 return (String )newspaperTable.getAttributes().get(JSFAttr.HEADER_CLASS_ATTR); 322 } 323 } 324 327 private static String getFooterClass(HtmlNewspaperTable newspaperTable) { 328 if(newspaperTable instanceof HtmlDataTable) { 329 return ((HtmlDataTable)newspaperTable).getFooterClass(); 330 } else { 331 return (String )newspaperTable.getAttributes().get(JSFAttr.FOOTER_CLASS_ATTR); 332 } 333 } 334 335 338 public static Styles getStyles(HtmlNewspaperTable newspaperTable) { 339 String rowClasses; 340 String columnClasses; 341 if(newspaperTable instanceof HtmlDataTable) { 342 rowClasses = ((HtmlDataTable)newspaperTable).getRowClasses(); 343 columnClasses = ((HtmlDataTable)newspaperTable).getColumnClasses(); 344 } else { 345 rowClasses = (String )newspaperTable.getAttributes().get(JSFAttr.ROW_CLASSES_ATTR); 346 columnClasses = (String )newspaperTable.getAttributes().get(JSFAttr.COLUMN_CLASSES_ATTR); 347 } 348 return new Styles(rowClasses, columnClasses); 349 } 350 351 354 private static class Styles { 355 356 private String [] _columnStyle; 357 private String [] _rowStyle; 358 359 Styles(String rowStyles, String columnStyles) { 360 _rowStyle = (rowStyles == null) 361 ? ArrayUtils.EMPTY_STRING_ARRAY 362 : StringUtils.trim( 363 StringUtils.splitShortString(rowStyles, ',')); 364 _columnStyle = (columnStyles == null) 365 ? ArrayUtils.EMPTY_STRING_ARRAY 366 : StringUtils.trim( 367 StringUtils.splitShortString(columnStyles, ',')); 368 } 369 370 public String getRowStyle(int idx) { 371 if(!hasRowStyle()) { 372 return null; 373 } 374 return _rowStyle[idx % _rowStyle.length]; 375 } 376 377 public String getColumnStyle(int idx) { 378 if(!hasColumnStyle()) { 379 return null; 380 } 381 return _columnStyle[idx % _columnStyle.length]; 382 } 383 384 public boolean hasRowStyle() { 385 return _rowStyle.length > 0; 386 } 387 388 public boolean hasColumnStyle() { 389 return _columnStyle.length > 0; 390 } 391 } 392 } | Popular Tags |