1 package org.appfuse.webapp.jsf; 2 3 import java.io.IOException ; 4 import java.util.Iterator ; 5 6 import javax.faces.component.UIComponent; 7 import javax.faces.component.UIPanel; 8 import javax.faces.component.html.HtmlPanelGrid; 9 import javax.faces.context.FacesContext; 10 import javax.faces.context.ResponseWriter; 11 12 import org.apache.commons.logging.Log; 13 import org.apache.commons.logging.LogFactory; 14 import org.apache.myfaces.shared_tomahawk.renderkit.JSFAttr; 15 import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils; 16 import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML; 17 import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRenderer; 18 import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils; 19 import org.apache.myfaces.shared_tomahawk.util.StringUtils; 20 21 27 public class PanelGridRenderer extends HtmlRenderer { 28 private static final Log log = LogFactory.getLog(PanelGridRenderer.class); 29 30 public boolean getRendersChildren() { 31 return true; 32 } 33 34 public void encodeBegin(FacesContext facesContext, UIComponent component) 35 throws IOException { 36 } 38 39 public void encodeChildren(FacesContext context, UIComponent component) 40 throws IOException { 41 } 43 44 public void encodeEnd(FacesContext facesContext, UIComponent component) 45 throws IOException { 46 RendererUtils.checkParamValidity(facesContext, component, UIPanel.class); 47 48 int columns; 49 if (component instanceof HtmlPanelGrid) { 50 columns = ((HtmlPanelGrid) component).getColumns(); 51 } else { 52 Integer i = (Integer ) component.getAttributes().get(org.apache.myfaces.shared_tomahawk.renderkit.JSFAttr.COLUMNS_ATTR); 53 columns = i != null ? i.intValue() : 0; 54 } 55 56 if (columns <= 0) { 57 if (log.isErrorEnabled()) { 58 log.error("Wrong columns attribute for PanelGrid " + component.getClientId(facesContext) + ": " + columns); 59 } 60 columns = 1; 61 } 62 63 ResponseWriter writer = facesContext.getResponseWriter(); 64 writer.startElement(HTML.UL_ELEM, component); 65 HtmlRendererUtils.writeIdIfNecessary(writer, component, facesContext); 66 HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.UL_PASSTHROUGH_ATTRIBUTES); 67 68 writer.flush(); 69 70 renderChildren(facesContext, writer, component, columns); 71 72 writer.endElement(HTML.UL_ELEM); 73 } 74 75 protected void renderChildren(FacesContext context, 76 ResponseWriter writer, 77 UIComponent component, 78 int columns) 79 throws IOException { 80 81 String rowClasses; 82 if (component instanceof HtmlPanelGrid) { 83 rowClasses = ((HtmlPanelGrid) component).getRowClasses(); 84 } else { 85 rowClasses = (String ) component.getAttributes().get(JSFAttr.ROW_CLASSES_ATTR); 86 } 87 88 String [] rowClassesArray = (rowClasses == null) 89 ? org.apache.myfaces.shared_tomahawk.util.ArrayUtils.EMPTY_STRING_ARRAY 90 : StringUtils.trim(StringUtils.splitShortString(rowClasses, ',')); 91 int rowClassesCount = rowClassesArray.length; 92 93 int childCount = getChildCount(component); 94 if (childCount > 0) { 95 int columnIndex = 0; 96 int rowClassIndex = 0; 97 boolean rowStarted = false; 98 for (Iterator it = getChildren(component).iterator(); it.hasNext();) { 99 UIComponent child = (UIComponent) it.next(); 100 if (child.isRendered()) { 101 if (columnIndex == 0) { 102 if (rowStarted) { 104 writer.endElement(HTML.LI_ELEM); 106 HtmlRendererUtils.writePrettyLineSeparator(context); 107 } 108 writer.startElement(HTML.LI_ELEM, component); 109 if (rowClassIndex < rowClassesCount) { 110 writer.writeAttribute(HTML.CLASS_ATTR, rowClassesArray[rowClassIndex], null); 111 } 112 rowStarted = true; 113 rowClassIndex++; 114 if (rowClassIndex == rowClassesCount) { 115 rowClassIndex = 0; 116 } 117 } 118 119 RendererUtils.renderChild(context, child); 120 121 columnIndex++; 122 if (columnIndex >= columns) { 123 columnIndex = 0; 124 } 125 } 126 } 127 128 if (rowStarted) { 129 writer.endElement(HTML.LI_ELEM); 130 HtmlRendererUtils.writePrettyLineSeparator(context); 131 } 132 } 133 } 134 } 135 | Popular Tags |