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