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.UIComponent; 22 import javax.faces.context.FacesContext; 23 import javax.faces.context.ResponseWriter; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 import org.apache.myfaces.wap.component.PanelGrid; 28 import org.apache.myfaces.wap.renderkit.Attributes; 29 import org.apache.myfaces.wap.renderkit.RendererUtils; 30 import org.apache.myfaces.wap.renderkit.WmlRenderer; 31 32 33 41 public class GridRenderer extends WmlRenderer { 42 private static Log log = LogFactory.getLog(GridRenderer.class); 43 44 45 public GridRenderer() { 46 super(); 47 log.debug("created object " + this.getClass().getName()); 48 } 49 50 public boolean getRendersChildren(){ 51 return(true); 52 } 53 54 public void encodeBegin(FacesContext context, UIComponent component) throws java.io.IOException { 55 log.debug("encodeBegin(" + component.getId() + ")"); 56 57 if (context == null || component == null) { 58 throw new NullPointerException (); 59 } 60 if (!component.isRendered()) return; 61 } 63 64 public void encodeChildren(FacesContext context, UIComponent component) throws java.io.IOException { 65 log.debug("encodeChildren(" + component.getId() + ")"); 66 if (context == null || component == null) { 67 throw new NullPointerException (); 68 } 69 if (!component.isRendered()) return; 70 } 72 73 public void encodeEnd(FacesContext context, UIComponent component) throws java.io.IOException { 74 log.debug("encodeEnd(" + component.getId() + ")"); 75 if (context == null || component == null) { 76 throw new NullPointerException (); 77 } 78 if (!component.isRendered()) return; 79 80 ResponseWriter writer = context.getResponseWriter(); 81 PanelGrid comp = (PanelGrid)component; 82 int columns = getColumnsNumber(comp.getColumns()); 83 84 if (!hasTableAnyContext(context, component)) return; 86 87 writer.startElement(Attributes.TABLE, component); 88 RendererUtils.writeAttribute(Attributes.ID, comp.getClientId(context), writer); 89 RendererUtils.writeAttribute(Attributes.CLASS, comp.getStyleClass(), writer); 90 RendererUtils.writeAttribute(Attributes.XML_LANG, comp.getXmllang(), writer); 91 92 RendererUtils.writeAttribute(Attributes.ALIGN, comp.getAlign(), writer); 93 RendererUtils.writeAttribute(Attributes.COLUMNS, comp.getColumns(), writer); 94 RendererUtils.writeAttribute(Attributes.TITLE, comp.getTitle(), writer); 95 96 renderHeaderOrFooter(context, component, Attributes.HEADER, columns); 97 98 renderChildern(context, component, columns); 99 100 renderHeaderOrFooter(context, component, Attributes.FOOTER, columns); 101 102 writer.endElement(Attributes.TABLE); 103 } 104 105 public void decode(FacesContext context, UIComponent component) { 106 if (component == null ) throw new NullPointerException (); 107 } 108 109 private void renderChildern(FacesContext context, UIComponent component, int maxColumn) throws IOException { 110 ResponseWriter writer = context.getResponseWriter(); 111 int column = 0, row = 0; 112 boolean rowStarted = false; 113 114 Iterator childn = component.getChildren().iterator(); 115 while (childn.hasNext()){ 116 if (!rowStarted) { 117 writer.startElement(Attributes.TR, component); 118 rowStarted = true; column = 0; row = 1; 119 } 120 121 writer.startElement(Attributes.TD, component); 123 RendererUtils.renderChild(context, (UIComponent)childn.next()); 124 writer.endElement(Attributes.TD); 125 column++; 126 127 if (column >= maxColumn) { 129 writer.endElement(Attributes.TR); 130 rowStarted = false; 131 } 132 } 133 134 if (rowStarted) writeRowEnd(component, writer, column, maxColumn); 135 } 136 137 145 private void renderHeaderOrFooter(FacesContext context, UIComponent component, String str, int maxColumn) throws IOException { 146 ResponseWriter writer = context.getResponseWriter(); 147 UIComponent facet = component.getFacet(str); 148 149 int column = 0; 150 boolean rowStarted = false; 151 152 if (facet == null) return; 153 154 log.debug("Facet " + str + " is not null. Child numb:" + facet.getChildCount()); 155 Iterator childn = facet.getChildren().iterator(); 156 157 if (childn.hasNext()){ 159 writer.startElement(Attributes.TR, component); 160 rowStarted = true; 161 } 162 163 while (childn.hasNext()){ 165 writer.startElement(Attributes.TD, component); 166 RendererUtils.renderChild(context, (UIComponent)childn.next()); 168 writer.endElement(Attributes.TD); 169 column++; 170 } 171 172 if (rowStarted) writeRowEnd(component, writer, column, maxColumn); 173 } 174 175 183 private void writeRowEnd(UIComponent component, ResponseWriter writer, int column, int maxColumn) throws IOException { 184 while (column < maxColumn) { 185 writer.startElement(Attributes.TD, component); 186 writer.endElement(Attributes.TD); 187 column++; 188 } 189 writer.endElement(Attributes.TR); 190 } 191 192 193 private boolean hasTableAnyContext(FacesContext context, UIComponent component){ 194 boolean hasHeader = component.getFacet(Attributes.HEADER) != null; 195 boolean hasBody = component.getChildCount() > 0; 196 boolean hasFooter = component.getFacet(Attributes.FOOTER) != null; 197 198 return (hasHeader || hasBody || hasFooter); 199 } 200 201 202 private int getColumnsNumber(String columns){ 203 int number; 204 try { 205 number = Integer.parseInt(columns); 206 } catch (NumberFormatException ex){ 207 return (0); 208 } 209 return (number); 210 } 211 } 212 213 | Popular Tags |