| 1 33 34 package com.icesoft.faces.renderkit.dom_html_basic; 35 36 import com.icesoft.faces.context.DOMContext; 37 import org.w3c.dom.Element ; 38 39 import javax.faces.component.UIComponent; 40 import javax.faces.component.html.HtmlPanelGrid; 41 import javax.faces.context.FacesContext; 42 import java.io.IOException ; 43 import java.util.Iterator ; 44 45 public class GridRenderer extends DomBasicRenderer { 46 47 public boolean getRendersChildren() { 48 return true; 49 } 50 51 public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) 52 throws IOException { 53 validateParameters(facesContext, uiComponent, null); 54 DOMContext domContext = 55 DOMContext.attachDOMContext(facesContext, uiComponent); 56 if (!domContext.isInitialized()) { 57 Element root = domContext.createElement("table"); 58 domContext.setRootNode(root); 59 setRootElementId(facesContext, root, uiComponent); 60 PassThruAttributeRenderer.renderAttributes( 61 facesContext, uiComponent, null); 62 } 63 Element root = (Element ) domContext.getRootNode(); 64 String styleClass = ((HtmlPanelGrid) uiComponent).getStyleClass(); 65 if (styleClass != null) { 66 root.setAttribute("class", styleClass); 67 } 68 } 69 70 private void renderHeaderFacet(FacesContext facesContext, 71 UIComponent uiComponent, 72 DOMContext domContext) throws IOException { 73 Element root = (Element ) domContext.getRootNode(); 74 DOMContext.removeChildrenByTagName(root, "thead"); 75 UIComponent headerFacet = getFacetByName(uiComponent, "header"); 76 if (headerFacet != null && headerFacet.isRendered()) { 77 Element thead = domContext.createElement("thead"); 78 Element tr = domContext.createElement("tr"); 79 Element th = domContext.createElement("th"); 80 root.appendChild(thead); 81 thead.appendChild(tr); 82 tr.appendChild(th); 83 String headerClassAttribute = 84 ((HtmlPanelGrid) uiComponent).getHeaderClass(); 85 if (headerClassAttribute != null) { 86 th.setAttribute("class", headerClassAttribute); 87 } 88 th.setAttribute("scope", "colgroup"); 89 th.setAttribute("colspan", 90 String.valueOf( 91 getConvertedColumnAttribute(uiComponent))); 92 domContext.setCursorParent(th); 93 domContext.streamWrite(facesContext, uiComponent, 94 domContext.getRootNode(), th); 95 encodeParentAndChildren(facesContext, headerFacet); 96 } 97 } 98 99 private void renderFooterFacet(FacesContext facesContext, 100 UIComponent uiComponent, 101 DOMContext domContext) throws IOException { 102 Element root = (Element ) domContext.getRootNode(); 103 DOMContext.removeChildrenByTagName(root, "tfoot"); 104 UIComponent footerFacet = getFacetByName(uiComponent, "footer"); 105 if (footerFacet != null && footerFacet.isRendered()) { 106 Element tfoot = domContext.createElement("tfoot"); 107 Element tr = domContext.createElement("tr"); 108 Element td = domContext.createElement("td"); 109 root.appendChild(tfoot); 110 tfoot.appendChild(tr); 111 tr.appendChild(td); 112 String footerClassAttribute = 113 ((HtmlPanelGrid) uiComponent).getFooterClass(); 114 if (footerClassAttribute != null) { 115 td.setAttribute("class", footerClassAttribute); 116 } 117 td.setAttribute("colspan", 118 String.valueOf( 119 getConvertedColumnAttribute(uiComponent))); 120 domContext.setCursorParent(td); 121 domContext.streamWrite(facesContext, uiComponent, 122 domContext.getRootNode(), td); 123 encodeParentAndChildren(facesContext, footerFacet); 124 } 125 } 126 127 public void encodeChildren(FacesContext facesContext, 128 UIComponent uiComponent) 129 throws IOException { 130 validateParameters(facesContext, uiComponent, null); 131 DOMContext domContext = 132 DOMContext.getDOMContext(facesContext, uiComponent); 133 renderHeaderFacet(facesContext, uiComponent, domContext); 134 Element root = (Element ) domContext.getRootNode(); 136 DOMContext.removeChildrenByTagName(root, "tbody"); 137 Element tbody = domContext.createElement("tbody"); 138 root.appendChild(tbody); 139 Element tr = null; 140 141 Iterator children = uiComponent.getChildren().iterator(); 147 if (children != null) { 148 int numberOfColumns = getConvertedColumnAttribute(uiComponent); 149 int rowIndex = 150 -1; int columnIndex = 152 numberOfColumns; String columnStyleClasses[] = getColumnStyleClasses(uiComponent); 154 String rowStyleClasses[] = getRowStyles(uiComponent); 155 int columnStyleIndex = 0; 156 int rowStyleIndex = 0; 157 int numberOfColumnStyles = columnStyleClasses.length - 1; 158 int numberOfRowStyles = rowStyleClasses.length; 159 UIComponent facet = null; 160 while (children.hasNext()) { 161 UIComponent nextChild = (UIComponent) children.next(); 162 if (!nextChild.isRendered()) { 163 continue; 164 } 165 if (columnIndex >= numberOfColumns) { 167 tr = domContext.createElement("tr"); 168 tbody.appendChild(tr); 169 if (numberOfRowStyles > 0) { 170 tr.setAttribute("class",rowStyleClasses[rowStyleIndex++]); 171 if (rowStyleIndex >= numberOfRowStyles) { 172 rowStyleIndex = 0; 173 } 174 } 175 columnStyleIndex = 0; 176 columnIndex = 0; 177 rowIndex++; 178 } 179 Element td = domContext.createElement("td"); 181 tr.appendChild(td); 182 td.setAttribute("id", getIndexedClientId(facesContext, 183 uiComponent, 184 columnIndex, 185 rowIndex)); 186 187 writeColStyles(columnStyleClasses, numberOfColumnStyles, 188 columnStyleIndex, td, columnIndex + 1, uiComponent); 189 if (++columnStyleIndex > numberOfColumnStyles) { 190 columnStyleIndex = 0; 191 } 192 193 domContext.setCursorParent(td); 194 domContext.streamWrite(facesContext, uiComponent, 195 domContext.getRootNode(), td); 196 encodeParentAndChildren(facesContext, nextChild); 197 columnIndex++; 198 } 199 } 200 renderFooterFacet(facesContext, uiComponent, domContext); 201 domContext.stepOver(); 202 domContext.streamWrite(facesContext, uiComponent); 203 } 204 205 public String [] getRowStyles(UIComponent uiComponent) { 207 return getRowStyleClasses(uiComponent); 208 } 209 210 private String getIndexedClientId(FacesContext facesContext, 211 UIComponent uiComponent, 212 int columnIndex, int rowIndex) { 213 return uiComponent.getClientId(facesContext) 214 + "-" 215 + rowIndex 216 + "-" 217 + columnIndex; 218 } 219 220 private int getConvertedColumnAttribute(UIComponent uiComponent) { 221 int convertedColumnAttribute = 1; Object columnAttribute = uiComponent.getAttributes().get("columns"); 223 int value; 224 if (columnAttribute != null 225 && columnAttribute instanceof Integer  226 && (value = ((Integer ) columnAttribute).intValue()) > 0) { 227 convertedColumnAttribute = value; 228 } 229 return (convertedColumnAttribute); 230 } 231 232 233 public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) 234 throws IOException { 235 validateParameters(facesContext, uiComponent, null); 236 } 237 238 public void writeColStyles(String [] columnStyles, int columnStylesMaxIndex, 240 int columnStyleIndex, Element td, 241 int colNumber, UIComponent uiComponent) { 242 if (columnStyles.length > 0) { 243 if (columnStylesMaxIndex >= 0) { 244 td.setAttribute("class", 245 columnStyles[columnStyleIndex]); 246 if (++columnStyleIndex > columnStylesMaxIndex) { 247 columnStyleIndex = 0; 248 } 249 } 250 } 251 } 252 253 protected String getRowStyle(UIComponent uiComponent, String style) { 254 return style; 255 } 256 257 258 } 259 | Popular Tags |