1 16 package javax.faces.component; 17 18 import javax.faces.application.FacesMessage; 19 import javax.faces.context.FacesContext; 20 import javax.faces.convert.Converter; 21 import javax.faces.convert.ConverterException; 22 import javax.faces.el.EvaluationException; 23 import javax.faces.el.MethodBinding; 24 import javax.faces.el.ValueBinding; 25 import javax.faces.event.AbortProcessingException; 26 import javax.faces.event.FacesEvent; 27 import javax.faces.event.ValueChangeEvent; 28 import javax.faces.event.ValueChangeListener; 29 import javax.faces.render.Renderer; 30 import javax.faces.validator.Validator; 31 import java.util.ArrayList ; 32 import java.util.List ; 33 34 66 public class UIInput 67 extends UIOutput 68 implements EditableValueHolder 69 { 70 public static final String CONVERSION_MESSAGE_ID = "javax.faces.component.UIInput.CONVERSION"; 71 public static final String REQUIRED_MESSAGE_ID = "javax.faces.component.UIInput.REQUIRED"; 72 73 private static final Validator[] EMPTY_VALIDATOR_ARRAY = new Validator[0]; 74 75 private Object _submittedValue = null; 76 private boolean _localValueSet = false; 77 private boolean _valid = true; 78 private MethodBinding _validator = null; 79 private MethodBinding _valueChangeListener = null; 80 private List _validatorList = null; 81 82 public Object getSubmittedValue() 83 { 84 return _submittedValue; 85 } 86 87 public void setSubmittedValue(Object submittedValue) 88 { 89 _submittedValue = submittedValue; 90 } 91 92 public void setValue(Object value) 93 { 94 setLocalValueSet(true); 95 super.setValue(value); 96 } 97 98 public boolean isLocalValueSet() 99 { 100 return _localValueSet; 101 } 102 103 public void setLocalValueSet(boolean localValueSet) 104 { 105 _localValueSet = localValueSet; 106 } 107 108 public boolean isValid() 109 { 110 return _valid; 111 } 112 113 public void setValid(boolean valid) 114 { 115 _valid = valid; 116 } 117 118 public MethodBinding getValidator() 119 { 120 return _validator; 121 } 122 123 public void setValidator(MethodBinding validator) 124 { 125 _validator = validator; 126 } 127 128 public MethodBinding getValueChangeListener() 129 { 130 return _valueChangeListener; 131 } 132 133 public void setValueChangeListener(MethodBinding valueChangeListener) 134 { 135 _valueChangeListener = valueChangeListener; 136 } 137 138 public void processDecodes(FacesContext context) 139 { 140 if (context == null) throw new NullPointerException ("context"); 141 if (!isRendered()) return; 142 super.processDecodes(context); 143 if (isImmediate()) 144 { 145 try 146 { 147 validate(context); 148 } 149 catch (RuntimeException e) 150 { 151 context.renderResponse(); 152 throw e; 153 } 154 if (!isValid()) 155 { 156 context.renderResponse(); 157 } 158 } 159 } 160 161 public void processValidators(FacesContext context) 162 { 163 if (context == null) throw new NullPointerException ("context"); 164 if (!isRendered()) return; 165 super.processValidators(context); 166 if (!isImmediate()) 167 { 168 try 169 { 170 validate(context); 171 } 172 catch (RuntimeException e) 173 { 174 context.renderResponse(); 175 throw e; 176 } 177 if (!isValid()) 178 { 179 context.renderResponse(); 180 } 181 } 182 } 183 184 public void processUpdates(FacesContext context) 185 { 186 if (context == null) throw new NullPointerException ("context"); 187 if (!isRendered()) return; 188 super.processUpdates(context); 189 try 190 { 191 updateModel(context); 192 } 193 catch (RuntimeException e) 194 { 195 context.renderResponse(); 196 throw e; 197 } 198 if (!isValid()) 199 { 200 context.renderResponse(); 201 } 202 } 203 204 public void decode(FacesContext context) 205 { 206 setValid(true); 208 super.decode(context); 209 } 210 211 public void broadcast(FacesEvent event) 212 throws AbortProcessingException 213 { 214 if (!(event instanceof ValueChangeEvent)) 215 { 216 throw new IllegalArgumentException ("FacesEvent of class " + event.getClass().getName() + " not supported by UIInput"); 217 } 218 219 super.broadcast(event); 220 221 MethodBinding valueChangeListenerBinding = getValueChangeListener(); 222 if (valueChangeListenerBinding != null) 223 { 224 try 225 { 226 valueChangeListenerBinding.invoke(getFacesContext(), 227 new Object []{event}); 228 } 229 catch (EvaluationException e) 230 { 231 Throwable cause = e.getCause(); 232 if (cause != null && cause instanceof AbortProcessingException) 233 { 234 throw (AbortProcessingException)cause; 235 } 236 else 237 { 238 throw e; 239 } 240 } 241 } 242 } 243 244 public void updateModel(FacesContext context) 245 { 246 if (!isValid()) return; 247 if (!isLocalValueSet()) return; 248 ValueBinding vb = getValueBinding("value"); 249 if (vb == null) return; 250 try 251 { 252 vb.setValue(context, getLocalValue()); 253 setValue(null); 254 setLocalValueSet(false); 255 } 256 catch (RuntimeException e) 257 { 258 context.getExternalContext().log(e.getMessage(), e); 260 _MessageUtils.addErrorMessage(context, this,CONVERSION_MESSAGE_ID,new Object []{getId()}); 261 setValid(false); 262 } 263 } 264 265 protected void validateValue(FacesContext context,Object convertedValue) 266 { 267 boolean empty = convertedValue == null || 268 (convertedValue instanceof String 269 && ((String )convertedValue).length() == 0); 270 271 if (isRequired() && empty) 272 { 273 _MessageUtils.addErrorMessage(context, this, REQUIRED_MESSAGE_ID,new Object []{getId()}); 274 setValid(false); 275 return; 276 } 277 278 if (!empty) 279 { 280 _ComponentUtils.callValidators(context, this, convertedValue); 281 } 282 283 } 284 285 public void validate(FacesContext context) 286 { 287 if (context == null) throw new NullPointerException ("context"); 288 Object submittedValue = getSubmittedValue(); 289 if (submittedValue == null) return; 290 291 if(submittedValue instanceof String && ((String ) submittedValue).length()==0) 292 { 293 submittedValue = null; 294 } 295 296 Object convertedValue = getConvertedValue(context, submittedValue); 297 298 if (!isValid()) return; 299 300 validateValue(context, convertedValue); 301 302 if (!isValid()) return; 303 304 Object previousValue = getValue(); 305 setValue(convertedValue); 306 setSubmittedValue(null); 307 if (compareValues(previousValue, convertedValue)) 308 { 309 queueEvent(new ValueChangeEvent(this, previousValue, convertedValue)); 310 } 311 } 312 313 protected Object getConvertedValue(FacesContext context, Object submittedValue) 314 { 315 try 316 { 317 Renderer renderer = getRenderer(context); 318 if (renderer != null) 319 { 320 return renderer.getConvertedValue(context, this, submittedValue); 321 } 322 else if (submittedValue instanceof String ) 323 { 324 Converter converter = _SharedRendererUtils.findUIOutputConverter(context, this); 325 if (converter != null) 326 { 327 return converter.getAsObject(context, this, (String )submittedValue); 328 } 329 } 330 } 331 catch (ConverterException e) 332 { 333 FacesMessage facesMessage = e.getFacesMessage(); 334 if (facesMessage != null) 335 { 336 context.addMessage(getClientId(context), facesMessage); 337 } 338 else 339 { 340 _MessageUtils.addErrorMessage(context, this, CONVERSION_MESSAGE_ID,new Object []{getId()}); 341 } 342 setValid(false); 343 } 344 return submittedValue; 345 } 346 347 348 349 protected boolean compareValues(Object previous, 350 Object value) 351 { 352 return !((previous == null && value == null) || 353 (previous != null && value != null && previous.equals(value))); 354 } 355 356 public void addValidator(Validator validator) 357 { 358 if (validator == null) throw new NullPointerException ("validator"); 359 if (_validatorList == null) 360 { 361 _validatorList = new ArrayList (); 362 } 363 _validatorList.add(validator); 364 } 365 366 public Validator[] getValidators() 367 { 368 return _validatorList != null ? 369 (Validator[])_validatorList.toArray(new Validator[_validatorList.size()]) : 370 EMPTY_VALIDATOR_ARRAY; 371 } 372 373 public void removeValidator(Validator validator) 374 { 375 if (validator == null) throw new NullPointerException ("validator"); 376 if (_validatorList != null) 377 { 378 _validatorList.remove(validator); 379 } 380 } 381 382 public void addValueChangeListener(ValueChangeListener listener) 383 { 384 addFacesListener(listener); 385 } 386 387 public ValueChangeListener[] getValueChangeListeners() 388 { 389 return (ValueChangeListener[])getFacesListeners(ValueChangeListener.class); 390 } 391 392 public void removeValueChangeListener(ValueChangeListener listener) 393 { 394 removeFacesListener(listener); 395 } 396 397 public Object saveState(FacesContext context) 398 { 399 Object values[] = new Object [9]; 400 values[0] = super.saveState(context); 401 values[1] = _immediate; 402 values[2] = Boolean.valueOf(_localValueSet); 403 values[3] = _required; 404 values[4] = _submittedValue; 405 values[5] = Boolean.valueOf(_valid); 406 values[6] = saveAttachedState(context, _validator); 407 values[7] = saveAttachedState(context, _valueChangeListener); 408 values[8] = saveAttachedState(context, _validatorList); 409 return ((Object )(values)); 410 } 411 412 public void restoreState(FacesContext context, Object state) 413 { 414 Object values[] = (Object [])state; 415 super.restoreState(context, values[0]); 416 _immediate = (Boolean )values[1]; 417 _localValueSet = ((Boolean )values[2]).booleanValue(); 418 _required = (Boolean )values[3]; 419 _submittedValue = (Object )values[4]; 420 _valid = ((Boolean )values[5]).booleanValue(); 421 _validator = (MethodBinding)restoreAttachedState(context, values[6]); 422 _valueChangeListener = (MethodBinding)restoreAttachedState(context, values[7]); 423 _validatorList = (List )restoreAttachedState(context, values[8]); 424 } 425 426 427 429 public static final String COMPONENT_TYPE = "javax.faces.Input"; 430 public static final String COMPONENT_FAMILY = "javax.faces.Input"; 431 private static final String DEFAULT_RENDERER_TYPE = "javax.faces.Text"; 432 private static final boolean DEFAULT_IMMEDIATE = false; 433 private static final boolean DEFAULT_REQUIRED = false; 434 435 private Boolean _immediate = null; 436 private Boolean _required = null; 437 438 public UIInput() 439 { 440 setRendererType(DEFAULT_RENDERER_TYPE); 441 } 442 443 public String getFamily() 444 { 445 return COMPONENT_FAMILY; 446 } 447 448 public void setImmediate(boolean immediate) 449 { 450 _immediate = Boolean.valueOf(immediate); 451 } 452 453 public boolean isImmediate() 454 { 455 if (_immediate != null) return _immediate.booleanValue(); 456 ValueBinding vb = getValueBinding("immediate"); 457 Boolean v = vb != null ? (Boolean )vb.getValue(getFacesContext()) : null; 458 return v != null ? v.booleanValue() : DEFAULT_IMMEDIATE; 459 } 460 461 public void setRequired(boolean required) 462 { 463 _required = Boolean.valueOf(required); 464 } 465 466 public boolean isRequired() 467 { 468 if (_required != null) return _required.booleanValue(); 469 ValueBinding vb = getValueBinding("required"); 470 Boolean v = vb != null ? (Boolean )vb.getValue(getFacesContext()) : null; 471 return v != null ? v.booleanValue() : DEFAULT_REQUIRED; 472 } 473 474 475 } 477 | Popular Tags |