1 16 package org.apache.myfaces.custom.datalist; 17 18 import org.apache.myfaces.renderkit.JSFAttr; 19 import org.apache.myfaces.renderkit.RendererUtils; 20 import org.apache.myfaces.renderkit.html.HTML; 21 import org.apache.myfaces.renderkit.html.HtmlRenderer; 22 import org.apache.myfaces.renderkit.html.HtmlRendererUtils; 23 24 import javax.faces.component.UIComponent; 25 import javax.faces.component.UIData; 26 import javax.faces.component.UIViewRoot; 27 import javax.faces.context.FacesContext; 28 import javax.faces.context.ResponseWriter; 29 import java.io.IOException ; 30 31 51 public class HtmlListRenderer 52 extends HtmlRenderer 53 { 54 56 public static final String LAYOUT_SIMPLE = "simple"; 57 public static final String LAYOUT_UL = "unorderedList"; 58 public static final String LAYOUT_OL = "orderedList"; 59 60 public boolean getRendersChildren() 61 { 62 return true; 63 } 64 65 public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) throws IOException 66 { 67 RendererUtils.checkParamValidity(facesContext, uiComponent, UIData.class); 68 ResponseWriter writer = facesContext.getResponseWriter(); 69 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 70 String layout = getLayout(uiComponent); 71 if (layout != null) 72 { 73 if (layout.equals(LAYOUT_UL)) 74 { 75 writer.startElement(HTML.UL_ELEM, uiComponent); 76 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, 77 HTML.COMMON_PASSTROUGH_ATTRIBUTES); 78 } 79 else if (layout.equals(LAYOUT_OL)) 80 { 81 writer.startElement(HTML.OL_ELEM, uiComponent); 82 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, 83 HTML.COMMON_PASSTROUGH_ATTRIBUTES); 84 } 85 else 86 { 87 if(uiComponent.getId()!=null && !uiComponent.getId().startsWith(UIViewRoot.UNIQUE_ID_PREFIX)) 88 { 89 writer.startElement(HTML.SPAN_ELEM, uiComponent); 90 91 writer.writeAttribute(HTML.ID_ATTR, uiComponent.getClientId(facesContext),null); 92 93 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.COMMON_PASSTROUGH_ATTRIBUTES); 94 95 } 96 else 97 { 98 HtmlRendererUtils.renderHTMLAttributesWithOptionalStartElement(writer,uiComponent, 99 HTML.SPAN_ELEM,HTML.COMMON_PASSTROUGH_ATTRIBUTES); 100 } 101 } 102 } 103 } 104 105 public void encodeChildren(FacesContext facesContext, UIComponent component) throws IOException 106 { 107 RendererUtils.checkParamValidity(facesContext, component, UIData.class); 108 109 UIData uiData = (UIData)component; 110 String layout = getLayout(component); 111 113 ResponseWriter writer = facesContext.getResponseWriter(); 114 115 int first = uiData.getFirst(); 116 int rows = uiData.getRows(); 117 int rowCount = uiData.getRowCount(); 118 if (rows <= 0) 119 { 120 rows = rowCount - first; 121 } 122 int last = first + rows; 123 if (last > rowCount) last = rowCount; 124 125 134 135 for (int i = first; i < last; i++) 136 { 137 uiData.setRowIndex(i); 138 if (uiData.isRowAvailable()) 139 { 140 146 147 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 148 if (layout != null && (layout.equals(LAYOUT_UL) || (layout.equals(LAYOUT_OL)))) 149 { 150 writer.startElement(HTML.LI_ELEM, component); 151 } 152 153 RendererUtils.renderChildren(facesContext, component); 154 155 if (layout != null && (layout.equals(LAYOUT_UL) || (layout.equals(LAYOUT_OL)))) 156 { 157 writer.endElement(HTML.LI_ELEM); 158 } 159 160 166 } 167 } 168 169 175 } 176 177 178 public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) throws IOException 179 { 180 RendererUtils.checkParamValidity(facesContext, uiComponent, UIData.class); 181 ResponseWriter writer = facesContext.getResponseWriter(); 182 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 183 String layout = getLayout(uiComponent); 184 if (layout != null) 185 { 186 if (layout.equals(LAYOUT_UL)) 187 { 188 writer.endElement(HTML.UL_ELEM); 189 } 190 else if (layout.equals(LAYOUT_OL)) 191 { 192 writer.endElement(HTML.OL_ELEM); 193 } 194 else 195 { 196 if(uiComponent.getId()!=null && !uiComponent.getId().startsWith(UIViewRoot.UNIQUE_ID_PREFIX)) 197 { 198 writer.endElement(HTML.SPAN_ELEM); 199 } 200 else 201 { 202 HtmlRendererUtils.renderOptionalEndElement(writer, 203 uiComponent, 204 HTML.SPAN_ELEM, 205 HTML.COMMON_PASSTROUGH_ATTRIBUTES); 206 } 207 } 208 } 209 } 210 211 212 private String getLayout(UIComponent component) 213 { 214 if (component instanceof HtmlDataList) 215 { 216 return ((HtmlDataList)component).getLayout(); 217 } 218 else 219 { 220 return (String )component.getAttributes().get(JSFAttr.LAYOUT_ATTR); 221 } 222 } 223 224 249 250 } 251 | Popular Tags |