001: // WARNING: This file was automatically generated. Do not edit it directly,
002: // or you will lose your changes.
003:
004: /*
005: * Licensed to the Apache Software Foundation (ASF) under one
006: * or more contributor license agreements. See the NOTICE file
007: * distributed with this work for additional information
008: * regarding copyright ownership. The ASF licenses this file
009: * to you under the Apache License, Version 2.0 (the
010: * "License"); you may not use this file except in compliance
011: * with the License. You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing,
016: * software distributed under the License is distributed on an
017: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018: * KIND, either express or implied. See the License for the
019: * specific language governing permissions and limitations
020: * under the License.
021: */
022: package javax.faces.component;
023:
024: import java.util.ArrayList;
025: import java.util.List;
026: import javax.el.ELException;
027: import javax.el.ValueExpression;
028: import javax.faces.application.FacesMessage;
029: import javax.faces.context.FacesContext;
030: import javax.faces.convert.Converter;
031: import javax.faces.convert.ConverterException;
032: import javax.faces.el.EvaluationException;
033: import javax.faces.el.MethodBinding;
034: import javax.faces.el.ValueBinding;
035: import javax.faces.event.AbortProcessingException;
036: import javax.faces.event.FacesEvent;
037: import javax.faces.event.ValueChangeEvent;
038: import javax.faces.event.ValueChangeListener;
039: import javax.faces.render.Renderer;
040: import javax.faces.validator.Validator;
041:
042: /**
043: *
044: * <h4>Events:</h4>
045: * <table border="1" width="100%" cellpadding="3" summary="">
046: * <tr bgcolor="#CCCCFF" class="TableHeadingColor">
047: * <th align="left">Type</th>
048: * <th align="left">Phases</th>
049: * <th align="left">Description</th>
050: * </tr>
051: * <tr class="TableRowColor">
052: * <td valign="top"><code>javax.faces.event.ValueChangeEvent</code></td>
053: * <td valign="top" nowrap></td>
054: * <td valign="top">The valueChange event is delivered when the value
055: attribute is changed.</td>
056: * </tr>
057: * </table>
058: */
059: public class UIInput extends UIOutput implements EditableValueHolder {
060:
061: static public final String COMPONENT_FAMILY = "javax.faces.Input";
062: static public final String COMPONENT_TYPE = "javax.faces.Input";
063:
064: /**
065: * Construct an instance of the UIInput.
066: */
067: public UIInput() {
068: setRendererType("javax.faces.Text");
069: }
070:
071: public static final String CONVERSION_MESSAGE_ID = "javax.faces.component.UIInput.CONVERSION";
072: public static final String REQUIRED_MESSAGE_ID = "javax.faces.component.UIInput.REQUIRED";
073: public static final String UPDATE_MESSAGE_ID = "javax.faces.component.UIInput.UPDATE";
074:
075: /**
076: * Store the specified object as the "local value" of this component.
077: * The value-binding named "value" (if any) is ignored; the object is
078: * only stored locally on this component. During the "update model"
079: * phase, if there is a value-binding named "value" then this local
080: * value will be stored via that value-binding and the "local value"
081: * reset to null.
082: */
083: public void setValue(Object value) {
084: setLocalValueSet(true);
085: super .setValue(value);
086: }
087:
088: /**
089: * Set the "submitted value" of this component from the relevant data
090: * in the current servet request object.
091: * <p>
092: * If this component is not rendered, then do nothing; no output would
093: * have been sent to the client so no input is expected.
094: * <p>
095: * Invoke the inherited functionality, which typically invokes the
096: * renderer associated with this component to extract and set this
097: * component's "submitted value".
098: * <p>
099: * If this component is marked "immediate", then immediately apply
100: * validation to the submitted value found. On error, call context
101: * method "renderResponse" which will force processing to leap to
102: * the "render response" phase as soon as the "decode" step has
103: * completed for all other components.
104: */
105: public void processDecodes(FacesContext context) {
106: if (context == null)
107: throw new NullPointerException("context");
108: if (!isRendered())
109: return;
110: super .processDecodes(context);
111: if (isImmediate()) {
112: try {
113: validate(context);
114: } catch (RuntimeException e) {
115: context.renderResponse();
116: throw e;
117: }
118: if (!isValid()) {
119: context.renderResponse();
120: }
121: }
122: }
123:
124: public void processValidators(FacesContext context) {
125: if (context == null)
126: throw new NullPointerException("context");
127: if (!isRendered())
128: return;
129:
130: super .processValidators(context);
131:
132: if (!isImmediate()) {
133: try {
134: validate(context);
135: } catch (RuntimeException e) {
136: context.renderResponse();
137: throw e;
138: }
139: if (!isValid()) {
140: context.renderResponse();
141: }
142: }
143: }
144:
145: public void processUpdates(FacesContext context) {
146: if (context == null)
147: throw new NullPointerException("context");
148: if (!isRendered())
149: return;
150:
151: super .processUpdates(context);
152:
153: try {
154: updateModel(context);
155: } catch (RuntimeException e) {
156: context.renderResponse();
157: throw e;
158: }
159: if (!isValid()) {
160: context.renderResponse();
161: }
162: }
163:
164: public void decode(FacesContext context) {
165: //We (re)set to valid, so that component automatically gets (re)validated
166: setValid(true);
167: super .decode(context);
168: }
169:
170: public void broadcast(FacesEvent event)
171: throws AbortProcessingException {
172: if (!(event instanceof ValueChangeEvent)) {
173: throw new IllegalArgumentException("FacesEvent of class "
174: + event.getClass().getName()
175: + " not supported by UIInput");
176: }
177:
178: // invoke standard listeners attached to this component first
179: super .broadcast(event);
180:
181: // invoke the single listener defined directly on the component
182: MethodBinding valueChangeListenerBinding = getValueChangeListener();
183: if (valueChangeListenerBinding != null) {
184: try {
185: valueChangeListenerBinding.invoke(getFacesContext(),
186: new Object[] { event });
187: } catch (EvaluationException e) {
188: Throwable cause = e.getCause();
189: if (cause != null
190: && cause instanceof AbortProcessingException) {
191: throw (AbortProcessingException) cause;
192: } else {
193: throw e;
194: }
195: }
196: }
197: }
198:
199: public void updateModel(FacesContext context) {
200: if (!isValid())
201: return;
202: if (!isLocalValueSet())
203: return;
204: ValueExpression expression = getValueExpression("value");
205: if (expression == null)
206: return;
207: try {
208: expression
209: .setValue(context.getELContext(), getLocalValue());
210: setValue(null);
211: setLocalValueSet(false);
212: } catch (ELException elException) {
213: String exceptionMessage = elException.getMessage();
214: if (exceptionMessage == null) {
215: _MessageUtils.addErrorMessage(context, this ,
216: UPDATE_MESSAGE_ID, new Object[] { _MessageUtils
217: .getLabel(context, this ) });
218: } else {
219: _MessageUtils.addErrorMessage(context, this ,
220: elException);
221: }
222: setValid(false);
223: } catch (Exception e) {
224: context.getExternalContext().log(e.getMessage(), e);
225: _MessageUtils.addErrorMessage(context, this ,
226: UPDATE_MESSAGE_ID, new Object[] { _MessageUtils
227: .getLabel(context, this ) });
228: setValid(false);
229: }
230: }
231:
232: protected void validateValue(FacesContext context,
233: Object convertedValue) {
234: boolean empty = convertedValue == null
235: || (convertedValue instanceof String && ((String) convertedValue)
236: .length() == 0);
237:
238: if (isRequired() && empty) {
239: if (getRequiredMessage() != null) {
240: String requiredMessage = getRequiredMessage();
241: context.addMessage(this .getClientId(context),
242: new FacesMessage(FacesMessage.SEVERITY_ERROR,
243: requiredMessage, requiredMessage));
244: } else {
245: _MessageUtils.addErrorMessage(context, this ,
246: REQUIRED_MESSAGE_ID,
247: new Object[] { _MessageUtils.getLabel(context,
248: this ) });
249: }
250: setValid(false);
251: return;
252: }
253:
254: if (!empty) {
255: _ComponentUtils.callValidators(context, this ,
256: convertedValue);
257: }
258:
259: }
260:
261: /**
262: * Determine whether the new value is valid, and queue a ValueChangeEvent
263: * if necessary.
264: * <p>
265: * The "submitted value" is converted to the necessary type; conversion
266: * failure is reported as an error and validation processing terminates
267: * for this component. See documentation for method getConvertedValue
268: * for details on the conversion process.
269: * <p>
270: * Any validators attached to this component are then run, passing
271: * the converted value.
272: * <p>
273: * The old value of this component is then fetched (possibly involving
274: * the evaluation of a value-binding expression, ie invoking a method
275: * on a user object). The old value is compared to the new validated
276: * value, and if they are different then a ValueChangeEvent is queued
277: * for later processing.
278: * <p>
279: * On successful completion of this method:
280: * <ul>
281: * <li> isValid() is true
282: * <li> isLocalValueSet() is true
283: * <li> submittedValue is reset to null
284: * <li> a ValueChangeEvent is queued if the new value != old value
285: * </ul>
286: */
287: public void validate(FacesContext context) {
288: if (context == null)
289: throw new NullPointerException("context");
290: Object submittedValue = getSubmittedValue();
291: if (submittedValue == null)
292: return;
293:
294: Object convertedValue = getConvertedValue(context,
295: submittedValue);
296:
297: if (!isValid())
298: return;
299:
300: validateValue(context, convertedValue);
301:
302: if (!isValid())
303: return;
304:
305: Object previousValue = getValue();
306: setValue(convertedValue);
307: setSubmittedValue(null);
308: if (compareValues(previousValue, convertedValue)) {
309: queueEvent(new ValueChangeEvent(this , previousValue,
310: convertedValue));
311: }
312: }
313:
314: /**
315: * Convert the provided object to the desired value.
316: * <p>
317: * If there is a renderer for this component, then call the renderer's
318: * getConvertedValue method. While this can of course be implemented in
319: * any way the renderer desires, it typically performs exactly the same
320: * processing that this method would have done anyway (ie that described
321: * below for the no-renderer case).
322: * <p>
323: * Otherwise:
324: * <ul>
325: * <li>If the submittedValue is not a String then just return the
326: * submittedValue unconverted.
327: * <li>If there is no "value" value-binding then just return the
328: * submittedValue unconverted.
329: * <li>Use introspection to determine the type of the target
330: * property specified by the value-binding, and then use
331: * Application.createConverter to find a converter that can
332: * map from String to the required type. Apply the converter
333: * to the submittedValue and return the result.
334: * </ul>
335: */
336: protected Object getConvertedValue(FacesContext context,
337: Object submittedValue) {
338: try {
339: Renderer renderer = getRenderer(context);
340: if (renderer != null) {
341: return renderer.getConvertedValue(context, this ,
342: submittedValue);
343: } else if (submittedValue instanceof String) {
344: Converter converter = _SharedRendererUtils
345: .findUIOutputConverter(context, this );
346: if (converter != null) {
347: return converter.getAsObject(context, this ,
348: (String) submittedValue);
349: }
350: }
351: } catch (ConverterException e) {
352: String converterMessage = getConverterMessage();
353: if (converterMessage != null) {
354: context.addMessage(getClientId(context),
355: new FacesMessage(FacesMessage.SEVERITY_ERROR,
356: converterMessage, converterMessage));
357: } else {
358: FacesMessage facesMessage = e.getFacesMessage();
359: if (facesMessage != null) {
360: context.addMessage(getClientId(context),
361: facesMessage);
362: } else {
363: _MessageUtils.addErrorMessage(context, this ,
364: CONVERSION_MESSAGE_ID,
365: new Object[] { _MessageUtils.getLabel(
366: context, this ) });
367: }
368: }
369: setValid(false);
370: }
371: return submittedValue;
372: }
373:
374: protected boolean compareValues(Object previous, Object value) {
375: return previous == null ? (value != null) : (!previous
376: .equals(value));
377: }
378:
379: /**
380: * @since 1.2
381: */
382: public void resetValue() {
383: setValue(null);
384: setSubmittedValue(null);
385: setLocalValueSet(false);
386: setValid(true);
387: }
388:
389: // Property: immediate
390: private boolean _immediate;
391: private boolean _immediateSet;
392:
393: /**
394: * Gets A boolean value that identifies the phase during which action events
395: * should fire. During normal event processing, action methods and
396: * action listener methods are fired during the "invoke application"
397: * phase of request processing. If this attribute is set to "true",
398: * these methods are fired instead at the end of the "apply request
399: * values" phase.
400: *
401: * @return the new immediate value
402: */
403: public boolean isImmediate() {
404: if (_immediateSet) {
405: return _immediate;
406: }
407: ValueExpression expression = getValueExpression("immediate");
408: if (expression != null) {
409: return (Boolean) expression.getValue(getFacesContext()
410: .getELContext());
411: }
412: return false;
413: }
414:
415: /**
416: * Sets A boolean value that identifies the phase during which action events
417: * should fire. During normal event processing, action methods and
418: * action listener methods are fired during the "invoke application"
419: * phase of request processing. If this attribute is set to "true",
420: * these methods are fired instead at the end of the "apply request
421: * values" phase.
422: *
423: * @param immediate the new immediate value
424: */
425: public void setImmediate(boolean immediate) {
426: this ._immediate = immediate;
427: this ._immediateSet = true;
428: }
429:
430: // Property: required
431: private boolean _required = false;
432: private boolean _requiredSet;
433:
434: /**
435: * Gets A boolean value that indicates whether a value is required. Default value: false.
436: *
437: * @return the new required value
438: */
439: public boolean isRequired() {
440: if (_requiredSet) {
441: return _required;
442: }
443: ValueExpression expression = getValueExpression("required");
444: if (expression != null) {
445: return (Boolean) expression.getValue(getFacesContext()
446: .getELContext());
447: }
448: return false;
449: }
450:
451: /**
452: * Sets A boolean value that indicates whether a value is required. Default value: false.
453: *
454: * @param required the new required value
455: */
456: public void setRequired(boolean required) {
457: this ._required = required;
458: this ._requiredSet = true;
459: }
460:
461: // Property: converterMessage
462: private String _converterMessage;
463:
464: /**
465: * Gets Text of the converter message.
466: *
467: * @return the new converterMessage value
468: */
469: public String getConverterMessage() {
470: if (_converterMessage != null) {
471: return _converterMessage;
472: }
473: ValueExpression expression = getValueExpression("converterMessage");
474: if (expression != null) {
475: return (String) expression.getValue(getFacesContext()
476: .getELContext());
477: }
478: return null;
479: }
480:
481: /**
482: * Sets Text of the converter message.
483: *
484: * @param converterMessage the new converterMessage value
485: */
486: public void setConverterMessage(String converterMessage) {
487: this ._converterMessage = converterMessage;
488: }
489:
490: // Property: requiredMessage
491: private String _requiredMessage;
492:
493: /**
494: * Gets Text which will be shown if a required value is not submitted.
495: *
496: * @return the new requiredMessage value
497: */
498: public String getRequiredMessage() {
499: if (_requiredMessage != null) {
500: return _requiredMessage;
501: }
502: ValueExpression expression = getValueExpression("requiredMessage");
503: if (expression != null) {
504: return (String) expression.getValue(getFacesContext()
505: .getELContext());
506: }
507: return null;
508: }
509:
510: /**
511: * Sets Text which will be shown if a required value is not submitted.
512: *
513: * @param requiredMessage the new requiredMessage value
514: */
515: public void setRequiredMessage(String requiredMessage) {
516: this ._requiredMessage = requiredMessage;
517: }
518:
519: // Property: validator
520: private MethodBinding _validator;
521:
522: /**
523: * Gets Validator Method, which should be invoked for validation.
524: *
525: * @return the new validator value
526: * @deprecated
527: */
528: public MethodBinding getValidator() {
529: if (_validator != null) {
530: return _validator;
531: }
532: ValueExpression expression = getValueExpression("validator");
533: if (expression != null) {
534: return (MethodBinding) expression
535: .getValue(getFacesContext().getELContext());
536: }
537: return null;
538: }
539:
540: /**
541: * Sets Validator Method, which should be invoked for validation.
542: *
543: * @param validator the new validator value
544: * @deprecated
545: */
546: public void setValidator(MethodBinding validator) {
547: this ._validator = validator;
548: }
549:
550: // Property: validator
551: private List<Validator> _validatorList;
552:
553: /**
554: * Adds a Validator Method, which should be invoked for validation.
555: */
556: public void addValidator(Validator validator) {
557: if (validator == null)
558: throw new NullPointerException("validator");
559: if (_validatorList == null)
560: _validatorList = new ArrayList<Validator>();
561:
562: _validatorList.add(validator);
563: }
564:
565: /**
566: * Removes a Validator Method, which should be invoked for validation.
567: */
568: public void removeValidator(Validator validator) {
569: if (validator == null || _validatorList == null)
570: return;
571:
572: _validatorList.remove(validator);
573: }
574:
575: private static final Validator[] EMPTY_VALIDATOR_ARRAY = new Validator[0];
576:
577: /**
578: * Gets all Validator Method, which should be invoked for validation.
579: */
580: public Validator[] getValidators() {
581: return _validatorList == null ? EMPTY_VALIDATOR_ARRAY
582: : _validatorList.toArray(new Validator[_validatorList
583: .size()]);
584: }
585:
586: // Property: validatorMessage
587: private String _validatorMessage;
588:
589: /**
590: * Gets Text which will be shown, if validation fails.
591: *
592: * @return the new validatorMessage value
593: */
594: public String getValidatorMessage() {
595: if (_validatorMessage != null) {
596: return _validatorMessage;
597: }
598: ValueExpression expression = getValueExpression("validatorMessage");
599: if (expression != null) {
600: return (String) expression.getValue(getFacesContext()
601: .getELContext());
602: }
603: return null;
604: }
605:
606: /**
607: * Sets Text which will be shown, if validation fails.
608: *
609: * @param validatorMessage the new validatorMessage value
610: */
611: public void setValidatorMessage(String validatorMessage) {
612: this ._validatorMessage = validatorMessage;
613: }
614:
615: // Property: valueChangeListener
616: private MethodBinding _valueChangeListener;
617:
618: /**
619: * Gets A Method which is called when the value changed.
620: *
621: * @return the new valueChangeListener value
622: * @deprecated
623: */
624: public MethodBinding getValueChangeListener() {
625: if (_valueChangeListener != null) {
626: return _valueChangeListener;
627: }
628: ValueExpression expression = getValueExpression("valueChangeListener");
629: if (expression != null) {
630: return (MethodBinding) expression
631: .getValue(getFacesContext().getELContext());
632: }
633: return null;
634: }
635:
636: /**
637: * Sets A Method which is called when the value changed.
638: *
639: * @param valueChangeListener the new valueChangeListener value
640: * @deprecated
641: */
642: public void setValueChangeListener(MethodBinding valueChangeListener) {
643: this ._valueChangeListener = valueChangeListener;
644: }
645:
646: // Property: valid
647: private boolean _valid = true;
648:
649: /**
650: * Gets whether the component's value is currently valid
651: *
652: * @return the new valid value
653: */
654: public boolean isValid() {
655: return _valid;
656: }
657:
658: /**
659: * Sets whether the component's value is currently valid
660: *
661: * @param valid the new valid value
662: */
663: public void setValid(boolean valid) {
664: this ._valid = valid;
665: }
666:
667: // Property: localValueSet
668: private boolean _localValueSet = false;
669:
670: /**
671: * Gets whether a local value is currently set. If false, values are being retrieved from any attached ValueBinding
672: *
673: * @return the new localValueSet value
674: */
675: public boolean isLocalValueSet() {
676: return _localValueSet;
677: }
678:
679: /**
680: * Sets whether a local value is currently set. If false, values are being retrieved from any attached ValueBinding
681: *
682: * @param localValueSet the new localValueSet value
683: */
684: public void setLocalValueSet(boolean localValueSet) {
685: this ._localValueSet = localValueSet;
686: }
687:
688: // Property: submittedValue
689: private Object _submittedValue;
690:
691: /**
692: * Gets the current submitted value. This value,
693: * if non-null, is set by the Renderer to store a possibly invalid value
694: * for later conversion or redisplay, and has not yet been converted
695: * into the proper type for this component instance. This method
696: * should only be used by the decode() and validate() method
697: * of this component, or its corresponding Renderer; however, user code
698: * may manually set it to null to erase any submitted value.
699: *
700: * @return the new submittedValue value
701: */
702: public Object getSubmittedValue() {
703: return _submittedValue;
704: }
705:
706: /**
707: * Sets the current submitted value. This value,
708: * if non-null, is set by the Renderer to store a possibly invalid value
709: * for later conversion or redisplay, and has not yet been converted
710: * into the proper type for this component instance. This method
711: * should only be used by the decode() and validate() method
712: * of this component, or its corresponding Renderer; however, user code
713: * may manually set it to null to erase any submitted value.
714: *
715: * @param submittedValue the new submittedValue value
716: */
717: public void setSubmittedValue(Object submittedValue) {
718: this ._submittedValue = submittedValue;
719: }
720:
721: /**
722: * Adds a valueChange listener.
723: *
724: * @param listener the valueChange listener to add
725: */
726: public void addValueChangeListener(ValueChangeListener listener) {
727: addFacesListener(listener);
728: }
729:
730: /**
731: * Removes a valueChange listener.
732: *
733: * @param listener the valueChange listener to remove
734: */
735: public void removeValueChangeListener(ValueChangeListener listener) {
736: removeFacesListener(listener);
737: }
738:
739: /**
740: * Returns an array of attached valueChange listeners.
741: *
742: * @return an array of attached valueChange listeners.
743: */
744: public ValueChangeListener[] getValueChangeListeners() {
745: return (ValueChangeListener[]) getFacesListeners(ValueChangeListener.class);
746: }
747:
748: @Override
749: public Object saveState(FacesContext facesContext) {
750: Object[] values = new Object[15];
751: values[0] = super .saveState(facesContext);
752: values[1] = _immediate;
753: values[2] = _immediateSet;
754: values[3] = _required;
755: values[4] = _requiredSet;
756: values[5] = _converterMessage;
757: values[6] = _requiredMessage;
758: values[8] = saveAttachedState(facesContext, _validatorList);
759: values[9] = _validatorMessage;
760: values[10] = saveAttachedState(facesContext,
761: _valueChangeListener);
762: values[11] = _valid;
763: values[12] = _localValueSet;
764: values[13] = _submittedValue;
765:
766: return values;
767: }
768:
769: @Override
770: public void restoreState(FacesContext facesContext, Object state) {
771: Object[] values = (Object[]) state;
772: super .restoreState(facesContext, values[0]);
773: _immediate = (Boolean) values[1];
774: _immediateSet = (Boolean) values[2];
775: _required = (Boolean) values[3];
776: _requiredSet = (Boolean) values[4];
777: _converterMessage = (String) values[5];
778: _requiredMessage = (String) values[6];
779: _validatorList = (List<Validator>) restoreAttachedState(
780: facesContext, values[8]);
781: _validatorMessage = (String) values[9];
782: _valueChangeListener = (MethodBinding) restoreAttachedState(
783: facesContext, values[10]);
784: _valid = (Boolean) values[11];
785: _localValueSet = (Boolean) values[12];
786: _submittedValue = values[13];
787: }
788:
789: @Override
790: public String getFamily() {
791: return COMPONENT_FAMILY;
792: }
793: }
|