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