1 16 package org.apache.myfaces.renderkit.html; 17 18 import java.io.IOException ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 22 import javax.faces.FacesException; 23 import javax.faces.component.UIComponent; 24 import javax.faces.component.UIInput; 25 import javax.faces.component.UIOutput; 26 import javax.faces.component.UISelectOne; 27 import javax.faces.component.html.HtmlSelectOneRadio; 28 import javax.faces.context.FacesContext; 29 import javax.faces.context.ResponseWriter; 30 import javax.faces.convert.Converter; 31 import javax.faces.convert.ConverterException; 32 import javax.faces.model.SelectItem; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 import org.apache.myfaces.renderkit.JSFAttr; 37 import org.apache.myfaces.renderkit.RendererUtils; 38 39 92 public class HtmlRadioRendererBase 93 extends HtmlRenderer 94 { 95 private static final Log log = LogFactory.getLog(HtmlRadioRendererBase.class); 96 97 private static final String PAGE_DIRECTION = "pageDirection"; 98 private static final String LINE_DIRECTION = "lineDirection"; 99 100 public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) throws IOException 101 { 102 RendererUtils.checkParamValidity(facesContext, uiComponent, UISelectOne.class); 103 104 UISelectOne selectOne = (UISelectOne)uiComponent; 105 106 String layout = getLayout(selectOne); 107 108 boolean pageDirectionLayout = false; if (layout != null) 110 { 111 if (layout.equals(PAGE_DIRECTION)) 112 { 113 pageDirectionLayout = true; 114 } 115 else if (layout.equals(LINE_DIRECTION)) 116 { 117 pageDirectionLayout = false; 118 } 119 else 120 { 121 log.error("Wrong layout attribute for component " + selectOne.getClientId(facesContext) + ": " + layout); 122 } 123 } 124 125 ResponseWriter writer = facesContext.getResponseWriter(); 126 127 writer.startElement(HTML.TABLE_ELEM, selectOne); 128 HtmlRendererUtils.renderHTMLAttributes(writer, selectOne, 129 HTML.SELECT_TABLE_PASSTHROUGH_ATTRIBUTES); 130 HtmlRendererUtils.writeIdIfNecessary(writer, uiComponent, facesContext); 131 132 if (!pageDirectionLayout) writer.startElement(HTML.TR_ELEM, selectOne); 133 134 Converter converter; 135 List selectItemList = RendererUtils.getSelectItemList(selectOne); 136 try 137 { 138 converter = RendererUtils.findUIOutputConverter(facesContext, selectOne); 139 } 140 catch (FacesException e) 141 { 142 log.error("Error finding Converter for component with id " + uiComponent.getClientId(facesContext)); 143 converter = null; 144 } 145 146 String currentValueStr = RendererUtils.getStringValue(facesContext, selectOne); 147 148 for (Iterator it = selectItemList.iterator(); it.hasNext(); ) 149 { 150 SelectItem selectItem = (SelectItem)it.next(); 151 String itemStrValue = RendererUtils.getConvertedStringValue(facesContext, selectOne, converter, selectItem.getValue()); 152 153 writer.write("\t\t"); 154 if (pageDirectionLayout) writer.startElement(HTML.TR_ELEM, selectOne); 155 writer.startElement(HTML.TD_ELEM, selectOne); 156 writer.startElement(HTML.LABEL_ELEM, selectOne); 157 renderRadio(facesContext, 158 selectOne, 159 itemStrValue, 160 selectItem.getLabel(), 161 currentValueStr.equals(itemStrValue), 162 false); 163 writer.endElement(HTML.LABEL_ELEM); 164 writer.endElement(HTML.TD_ELEM); 165 if (pageDirectionLayout) writer.endElement(HTML.TR_ELEM); 166 } 167 168 if (!pageDirectionLayout) writer.endElement(HTML.TR_ELEM); 169 writer.endElement(HTML.TABLE_ELEM); 170 } 171 172 173 protected String getLayout(UIComponent selectOne) 174 { 175 if (selectOne instanceof HtmlSelectOneRadio) 176 { 177 return ((HtmlSelectOneRadio)selectOne).getLayout(); 178 } 179 180 return (String )selectOne.getAttributes().get(JSFAttr.LAYOUT_ATTR); 181 } 182 183 184 protected String getStyleClass(UISelectOne selectOne) 185 { 186 if (selectOne instanceof HtmlSelectOneRadio) 187 { 188 return ((HtmlSelectOneRadio)selectOne).getStyleClass(); 189 } 190 191 return (String )selectOne.getAttributes().get(JSFAttr.STYLE_CLASS_ATTR); 192 } 193 194 195 protected void renderRadio(FacesContext facesContext, 196 UIInput uiComponent, 197 String value, 198 String label, 199 boolean checked, boolean renderId) 200 throws IOException 201 { 202 String clientId = uiComponent.getClientId(facesContext); 203 204 ResponseWriter writer = facesContext.getResponseWriter(); 205 206 writer.startElement(HTML.INPUT_ELEM, uiComponent); 207 writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_RADIO, null); 208 writer.writeAttribute(HTML.NAME_ATTR, clientId, null); 209 210 if (renderId) { 211 writer.writeAttribute(HTML.ID_ATTR, clientId, null); 212 } 213 214 if (checked) 215 { 216 writer.writeAttribute(HTML.CHECKED_ATTR, HTML.CHECKED_ATTR, null); 217 } 218 219 if ((value != null) && (value.length() > 0)) 220 { 221 writer.writeAttribute(HTML.VALUE_ATTR, value, null); 222 } 223 224 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.INPUT_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED); 225 if (isDisabled(facesContext, uiComponent)) 226 { 227 writer.writeAttribute(HTML.DISABLED_ATTR, Boolean.TRUE, null); 228 } 229 230 writer.endElement(HTML.INPUT_ELEM); 231 232 if ((label != null) && (label.length() > 0)) 233 { 234 writer.write(HTML.NBSP_ENTITY); 235 writer.writeText(label, null); 236 } 237 } 238 239 240 protected boolean isDisabled(FacesContext facesContext, UIComponent uiComponent) 241 { 242 if (uiComponent instanceof HtmlSelectOneRadio) 244 { 245 return ((HtmlSelectOneRadio)uiComponent).isDisabled(); 246 } 247 248 return RendererUtils.getBooleanAttribute(uiComponent, HTML.DISABLED_ATTR, false); 249 } 250 251 252 public void decode(FacesContext facesContext, UIComponent uiComponent) 253 { 254 RendererUtils.checkParamValidity(facesContext, uiComponent, null); 255 if (uiComponent instanceof UIInput) 256 { 257 HtmlRendererUtils.decodeUIInput(facesContext, uiComponent); 258 } 259 } 260 261 262 public Object getConvertedValue(FacesContext facesContext, UIComponent uiComponent, Object submittedValue) throws ConverterException 263 { 264 RendererUtils.checkParamValidity(facesContext, uiComponent, UIOutput.class); 265 return RendererUtils.getConvertedUIOutputValue(facesContext, 266 (UIOutput)uiComponent, 267 submittedValue); 268 } 269 270 } 271 | Popular Tags |