1 16 package org.apache.myfaces.renderkit.html.ext; 17 18 import org.apache.myfaces.component.UserRoleUtils; 19 import org.apache.myfaces.custom.radio.HtmlRadio; 20 import org.apache.myfaces.renderkit.RendererUtils; 21 import org.apache.myfaces.renderkit.html.HTML; 22 import org.apache.myfaces.renderkit.html.HtmlRadioRendererBase; 23 24 import javax.faces.FacesException; 25 import javax.faces.component.UIComponent; 26 import javax.faces.component.UISelectOne; 27 import javax.faces.context.FacesContext; 28 import javax.faces.context.ResponseWriter; 29 import javax.faces.convert.Converter; 30 import javax.faces.model.SelectItem; 31 import java.io.IOException ; 32 import java.util.List ; 33 34 35 68 public class HtmlRadioRenderer 69 extends HtmlRadioRendererBase 70 { 71 73 private static final String LAYOUT_SPREAD = "spread"; 74 75 public void encodeEnd(FacesContext context, UIComponent component) throws IOException 76 { 77 if (context == null) throw new NullPointerException ("context"); 78 if (component == null) throw new NullPointerException ("component"); 79 80 if (component instanceof HtmlRadio) 81 { 82 renderRadio(context, (HtmlRadio)component); 83 } 84 else if (component instanceof UISelectOne) 85 { 86 String layout = getLayout(component); 87 if (layout != null && layout.equals(LAYOUT_SPREAD)) 88 { 89 return; } 91 else 92 { 93 super.encodeEnd(context, component); 94 } 95 } 96 else 97 { 98 throw new IllegalArgumentException ("Unsupported component class " + component.getClass().getName()); 99 } 100 } 101 102 103 private void renderRadio(FacesContext facesContext, HtmlRadio radio) throws IOException 104 { 105 String forAttr = radio.getFor(); 106 if (forAttr == null) 107 { 108 throw new IllegalStateException ("mandatory attribute 'for'"); 109 } 110 int index = radio.getIndex(); 111 if (index < 0) 112 { 113 throw new IllegalStateException ("positive index must be given"); 114 } 115 116 UIComponent uiComponent = radio.findComponent(forAttr); 117 if (uiComponent == null) 118 { 119 throw new IllegalStateException ("Could not find component '" + forAttr + "' (calling findComponent on component '" + radio.getClientId(facesContext) + "')"); 120 } 121 if (!(uiComponent instanceof UISelectOne)) 122 { 123 throw new IllegalStateException ("UISelectOne expected"); 124 } 125 126 UISelectOne uiSelectOne = (UISelectOne)uiComponent; 127 Converter converter; 128 List selectItemList = RendererUtils.getSelectItemList(uiSelectOne); 129 if (index >= selectItemList.size()) 130 { 131 throw new IndexOutOfBoundsException ("index " + index + " >= " + selectItemList.size()); 132 } 133 134 try 135 { 136 converter = RendererUtils.findUIOutputConverter(facesContext, uiSelectOne); 137 } 138 catch (FacesException e) 139 { 140 converter = null; 141 } 142 143 Object currentValue = uiSelectOne.getValue(); 144 SelectItem selectItem = (SelectItem)selectItemList.get(index); 145 Object itemValue = selectItem.getValue(); 146 String itemStrValue; 147 if (converter == null) 148 { 149 itemStrValue = itemValue.toString(); 150 } 151 else 152 { 153 itemStrValue = converter.getAsString(facesContext, uiSelectOne, itemValue); 154 } 155 ResponseWriter writer = facesContext.getResponseWriter(); 156 157 writer.startElement(HTML.LABEL_ELEM, uiSelectOne); 158 159 renderRadio(facesContext, 160 uiSelectOne, 161 itemStrValue, 162 selectItem.getLabel(), 163 currentValue == null && itemValue == null || 164 currentValue != null && currentValue.equals(itemValue), false); 165 writer.endElement(HTML.LABEL_ELEM); 166 } 167 168 169 protected boolean isDisabled(FacesContext facesContext, UIComponent uiComponent) 170 { 171 if (!UserRoleUtils.isEnabledOnUserRole(uiComponent)) 172 { 173 return false; 174 } 175 else 176 { 177 return super.isDisabled(facesContext, uiComponent); 178 } 179 } 180 181 182 public void decode(FacesContext facesContext, UIComponent uiComponent) 183 { 184 if (uiComponent instanceof HtmlRadio) 185 { 186 } 188 else 189 { 190 super.decode(facesContext, uiComponent); 191 } 192 } 193 194 } 195 | Popular Tags |