1 16 package org.apache.myfaces.wap.renderkit.wml; 17 18 import java.io.IOException ; 19 import java.util.Iterator ; 20 21 import javax.faces.component.UIColumn; 22 import javax.faces.component.UIComponent; 23 import javax.faces.component.UIData; 24 import javax.faces.context.FacesContext; 25 import javax.faces.context.ResponseWriter; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.apache.myfaces.wap.component.DataTable; 30 import org.apache.myfaces.wap.renderkit.Attributes; 31 import org.apache.myfaces.wap.renderkit.RendererUtils; 32 import org.apache.myfaces.wap.renderkit.WmlRenderer; 33 34 35 43 public class TableRenderer extends WmlRenderer { 44 private static Log log = LogFactory.getLog(TableRenderer.class); 45 46 47 public TableRenderer() { 48 super(); 49 log.debug("created object " + this.getClass().getName()); 50 } 51 52 public boolean getRendersChildren(){ 53 return(true); 54 } 55 56 public void encodeBegin(FacesContext context, UIComponent component) throws java.io.IOException { 57 log.debug("encodeBegin(" + component.getId() + ")"); 58 59 if (context == null || component == null) { 60 throw new NullPointerException (); 61 } 62 if (!component.isRendered()) return; 63 } 65 66 public void encodeChildren(FacesContext context, UIComponent component) throws java.io.IOException { 67 log.debug("encodeChildren(" + component.getId() + ")"); 68 if (context == null || component == null) { 69 throw new NullPointerException (); 70 } 71 if (!component.isRendered()) return; 72 73 } 75 76 public void encodeEnd(FacesContext context, UIComponent component) throws java.io.IOException { 77 log.debug("encodeEnd(" + component.getId() + ")"); 78 if (context == null || component == null) { 79 throw new NullPointerException (); 80 } 81 if (!component.isRendered()) return; 82 83 ResponseWriter writer = context.getResponseWriter(); 84 DataTable comp = (DataTable)component; 85 86 int columns = component.getChildCount(); 88 89 if (columns == 0) return; 91 92 writer.startElement(Attributes.TABLE, component); 93 RendererUtils.writeAttribute(Attributes.ID, comp.getClientId(context), writer); 94 RendererUtils.writeAttribute(Attributes.CLASS, comp.getStyleClass(), writer); 95 RendererUtils.writeAttribute(Attributes.XML_LANG, comp.getXmllang(), writer); 96 97 RendererUtils.writeAttribute(Attributes.ALIGN, comp.getAlign(), writer); 98 99 RendererUtils.writeAttribute(Attributes.COLUMNS, new Integer (columns), writer); 100 RendererUtils.writeAttribute(Attributes.TITLE, comp.getTitle(), writer); 101 102 if (hasHeaderOrFooter(context, component, true)) 103 renderHeaderOrFooter(context, component, true); 104 105 renderChildren(context, component); 106 107 if (hasHeaderOrFooter(context, component, false)) 108 renderHeaderOrFooter(context, component, false); 109 110 writer.endElement(Attributes.TABLE); 111 } 112 113 public void decode(FacesContext context, UIComponent component) { 114 if (component == null ) throw new NullPointerException (); 115 } 116 117 private void renderChildren(FacesContext context, UIComponent component) throws IOException { 118 ResponseWriter writer = context.getResponseWriter(); 119 120 121 log.debug("Childern: " + component.getChildCount() + " of Component " + component.getFamily()); 122 123 UIData comp = (UIData)component; 124 125 int first = comp.getFirst(); 126 int rows = comp.getRows(); 127 int rowCount = comp.getRowCount(); 128 if (rows <= 0) { 129 rows = rowCount - first; 130 } 131 int last = first + rows; 132 if (last > rowCount) last = rowCount; 133 134 for (int i = first; i < last; i++){ 135 writer.startElement(Attributes.TR, component); 136 comp.setRowIndex(i); 137 138 if (!comp.isRowAvailable()){ 139 log.error("Row: " + i + " is not available."); 140 continue; 141 } 142 143 Iterator columns = component.getChildren().iterator(); 144 while (columns.hasNext()){ 145 UIComponent column = (UIComponent)columns.next(); 146 if (!(column instanceof UIColumn)) { 147 log.error("Only column elements can be nested in dataTable."); 148 break; 149 } 150 151 writer.startElement(Attributes.TD, component); 152 RendererUtils.renderChild(context, column); 153 writer.endElement(Attributes.TD); 154 } 155 writer.endElement(Attributes.TR); 156 } 157 } 158 159 160 private void renderHeaderOrFooter(FacesContext context, UIComponent component, boolean header) throws IOException { 161 ResponseWriter writer = context.getResponseWriter(); 162 163 writer.startElement(Attributes.TR, component); 164 165 Iterator columns = component.getChildren().iterator(); 166 while (columns.hasNext()){ 167 UIComponent column = (UIComponent)columns.next(); 168 if (!(column instanceof UIColumn)) { 169 log.error("Only column elements can be nested in dataTable."); 170 break; 171 } 172 173 UIComponent facet = header ? ((UIColumn)column).getHeader() : ((UIColumn)column).getFooter(); 174 175 writer.startElement(Attributes.TD, component); 176 RendererUtils.renderChild(context, facet); 177 writer.endElement(Attributes.TD); 178 } 179 writer.endElement(Attributes.TR); 180 } 181 182 183 private boolean hasHeaderOrFooter(FacesContext context, UIComponent component, boolean header){ 184 Iterator childn = component.getChildren().iterator(); 185 while (childn.hasNext()){ 186 UIComponent child = (UIComponent)childn.next(); 187 if (child instanceof UIColumn && child.isRendered()){ 188 if (header) { 189 if (((UIColumn)child).getHeader() != null) return(true); 190 } 191 else{ 192 if (((UIColumn)child).getFooter() != null) return(true); 193 } 194 } 195 } 196 return (false); 197 } 198 } 199 200 | Popular Tags |