1 16 package javax.faces.component; 17 18 import javax.faces.application.FacesMessage; 19 import javax.faces.context.FacesContext; 20 import javax.faces.convert.ConverterException; 21 import javax.faces.el.ValueBinding; 22 import javax.faces.event.ValueChangeEvent; 23 import javax.faces.render.Renderer; 24 import java.lang.reflect.Array ; 25 import java.util.List ; 26 27 61 public class UISelectMany 62 extends UIInput 63 { 64 public static final String INVALID_MESSAGE_ID = "javax.faces.component.UISelectMany.INVALID"; 65 66 public Object [] getSelectedValues() 67 { 68 return (Object [])getValue(); 69 } 70 71 public void setSelectedValues(Object [] selectedValues) 72 { 73 setValue(selectedValues); 74 } 75 76 public ValueBinding getValueBinding(String name) 77 { 78 if (name == null) throw new NullPointerException ("name"); 79 if (name.equals("selectedValues")) 80 { 81 return super.getValueBinding("value"); 82 } 83 else 84 { 85 return super.getValueBinding(name); 86 } 87 } 88 89 public void setValueBinding(String name, 90 ValueBinding binding) 91 { 92 if (name == null) throw new NullPointerException ("name"); 93 if (name.equals("selectedValues")) 94 { 95 super.setValueBinding("value", binding); 96 } 97 else 98 { 99 super.setValueBinding(name, binding); 100 } 101 } 102 103 106 protected boolean compareValues(Object previous, 107 Object value) 108 { 109 if (previous == null) 110 { 111 return value != null; 113 } 114 else if (value == null) 115 { 116 return previous != null; 118 } 119 else 120 { 121 if (previous instanceof Object [] && 122 value instanceof Object []) 123 { 124 return compareObjectArrays((Object [])previous, (Object [])value); 125 } 126 else if (previous instanceof List && 127 value instanceof List ) 128 { 129 return compareLists((List )previous, (List )value); 130 } 131 else if (previous.getClass().isArray() && 132 value.getClass().isArray()) 133 { 134 return comparePrimitiveArrays(previous, value); 135 } 136 else 137 { 138 return true; 140 } 141 } 142 } 143 144 private boolean compareObjectArrays(Object [] previous, 145 Object [] value) 146 { 147 int length = ((Object [])value).length; 148 if (((Object [])previous).length != length) 149 { 150 return true; 152 } 153 154 boolean[] scoreBoard = new boolean[length]; 155 for (int i = 0; i < length; i++) 156 { 157 Object p = previous[i]; 158 boolean found = false; 159 for (int j = 0; j < length; j++) 160 { 161 if (scoreBoard[j] == false) 162 { 163 Object v = value[j]; 164 if ((p == null && v == null) || 165 (p != null && v != null && p.equals(v))) 166 { 167 scoreBoard[j] = true; 168 found = true; 169 break; 170 } 171 } 172 } 173 if (!found) 174 { 175 return true; } 177 } 178 179 return false; } 181 182 private boolean compareLists(List previous, List value) 183 { 184 int length = value.size(); 185 if (previous.size() != length) 186 { 187 return true; 189 } 190 191 boolean[] scoreBoard = new boolean[length]; 192 for (int i = 0; i < length; i++) 193 { 194 Object p = previous.get(i); 195 boolean found = false; 196 for (int j = 0; j < length; j++) 197 { 198 if (scoreBoard[j] == false) 199 { 200 Object v = value.get(j); 201 if ((p == null && v == null) || 202 (p != null && v != null && p.equals(v))) 203 { 204 scoreBoard[j] = true; 205 found = true; 206 break; 207 } 208 } 209 } 210 if (!found) 211 { 212 return true; } 214 } 215 216 return false; } 218 219 private boolean comparePrimitiveArrays(Object previous, Object value) 220 { 221 int length = Array.getLength(value); 222 if (Array.getLength(previous) != length) 223 { 224 return true; 226 } 227 228 boolean[] scoreBoard = new boolean[length]; 229 for (int i = 0; i < length; i++) 230 { 231 Object p = Array.get(previous, i); 232 boolean found = false; 233 for (int j = 0; j < length; j++) 234 { 235 if (scoreBoard[j] == false) 236 { 237 Object v = Array.get(value, j); 238 if ((p == null && v == null) || 239 (p != null && v != null && p.equals(v))) 240 { 241 scoreBoard[j] = true; 242 found = true; 243 break; 244 } 245 } 246 } 247 if (!found) 248 { 249 return true; } 251 } 252 253 return false; } 255 256 protected void validateValue(FacesContext context, Object convertedValue) 257 { 258 boolean empty = 259 convertedValue == null 260 || ((convertedValue instanceof Object []) && (((Object []) convertedValue).length == 0)) 261 || ((convertedValue instanceof List ) && ((List ) convertedValue).isEmpty()); 262 if (isRequired() && empty) 263 { 264 _MessageUtils.addErrorMessage(context, this, REQUIRED_MESSAGE_ID,new Object []{getId()}); 265 setValid(false); 266 return; 267 } 268 269 if (!empty) 270 { 271 _ComponentUtils.callValidators(context, this, convertedValue); 272 } 273 274 } 277 278 283 public void validate(FacesContext context) 284 { 285 super.validate(context); 288 } 289 290 291 protected Object getConvertedValue(FacesContext context, Object submittedValue) 292 { 293 try 294 { 295 Renderer renderer = getRenderer(context); 296 if (renderer != null) 297 { 298 return renderer.getConvertedValue(context, this, submittedValue); 299 } 300 else if (submittedValue == null) 301 { 302 return null; 303 } 304 else if (submittedValue instanceof String []) 305 { 306 return _SharedRendererUtils.getConvertedUISelectManyValue(context, this, 307 (String [])submittedValue); 308 } 309 } 310 catch (ConverterException e) 311 { 312 FacesMessage facesMessage = e.getFacesMessage(); 313 if (facesMessage != null) 314 { 315 context.addMessage(getClientId(context), facesMessage); 316 } 317 else 318 { 319 _MessageUtils.addErrorMessage(context, this, CONVERSION_MESSAGE_ID,new Object []{getId()}); 320 } 321 setValid(false); 322 } 323 return submittedValue; 324 } 325 326 327 328 329 331 public static final String COMPONENT_TYPE = "javax.faces.SelectMany"; 332 public static final String COMPONENT_FAMILY = "javax.faces.SelectMany"; 333 private static final String DEFAULT_RENDERER_TYPE = "javax.faces.Listbox"; 334 335 336 public UISelectMany() 337 { 338 setRendererType(DEFAULT_RENDERER_TYPE); 339 } 340 341 public String getFamily() 342 { 343 return COMPONENT_FAMILY; 344 } 345 346 347 } 349 | Popular Tags |