Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
 */
 package javax.faces.component;
 
 import java.util.List;

see Javadoc of JSF Specification

Author(s):
Manfred Geiler (latest modification by $Author: matzew $)
Version:
$Revision: 499699 $ $Date: 2007-01-25 09:41:01 +0100 (Do, 25 Jän 2007) $
 
 public class UIInput
         extends UIOutput
         implements EditableValueHolder
 {
     public static final String CONVERSION_MESSAGE_ID = "javax.faces.component.UIInput.CONVERSION";
     public static final String REQUIRED_MESSAGE_ID = "javax.faces.component.UIInput.REQUIRED";
 
     private static final Validator[] EMPTY_VALIDATOR_ARRAY = new Validator[0];
 
     private Object _submittedValue = null;
     private boolean _localValueSet = false;
     private boolean _valid = true;
     private MethodBinding _validator = null;
     private MethodBinding _valueChangeListener = null;
     private List _validatorList = null;
 
     // use javadoc inherited from EditableValueHolder
     public Object getSubmittedValue()
     {
         return ;
     }
 
     // use javadoc inherited from EditableValueHolder
     public void setSubmittedValue(Object submittedValue)
     {
          = submittedValue;
     }

    
Store the specified object as the "local value" of this component. The value-binding named "value" (if any) is ignored; the object is only stored locally on this component. During the "update model" phase, if there is a value-binding named "value" then this local value will be stored via that value-binding and the "local value" reset to null.
 
     public void setValue(Object value)
     {
         setLocalValueSet(true);
         super.setValue(value);
     }
 
     // use javadoc inherited from EditableValueHolder
     public boolean isLocalValueSet()
     {
         return ;
     }
 
     // use javadoc inherited from EditableValueHolder
     public void setLocalValueSet(boolean localValueSet)
     {
          = localValueSet;
     }
 
     // use javadoc inherited from EditableValueHolder
     public boolean isValid()
     {
        return ;
    }
    // use javadoc inherited from EditableValueHolder
    public void setValid(boolean valid)
    {
         = valid;
    }
    // use javadoc inherited from EditableValueHolder
    public MethodBinding getValidator()
    {
        return ;
    }
    // use javadoc inherited from EditableValueHolder
    public void setValidator(MethodBinding validator)
    {
         = validator;
    }
    // use javadoc inherited from EditableValueHolder
    {
        return ;
    }
    // use javadoc inherited from EditableValueHolder
    public void setValueChangeListener(MethodBinding valueChangeListener)
    {
         = valueChangeListener;
    }

    
Set the "submitted value" of this component from the relevant data in the current servlet request object.

If this component is not rendered, then do nothing; no output would have been sent to the client so no input is expected.

Invoke the inherited functionality, which typically invokes the renderer associated with this component to extract and set this component's "submitted value".

If this component is marked "immediate", then immediately apply validation to the submitted value found. On error, call context method "renderResponse" which will force processing to leap to the "render response" phase as soon as the "decode" step has completed for all other components.

    public void processDecodes(FacesContext context)
    {
        if (context == nullthrow new NullPointerException("context");
        if (!isRendered()) return;
        super.processDecodes(context);
        if (isImmediate())
        {
            try
            {
                validate(context);
            }
            catch (RuntimeException e)
            {
                context.renderResponse();
                throw e;
            }
            if (!isValid())
            {
                context.renderResponse();
            }
        }
    }
    public void processValidators(FacesContext context)
    {
        if (context == nullthrow new NullPointerException("context");
        if (!isRendered()) return;
        super.processValidators(context);
        if (!isImmediate())
        {
            try
            {
                validate(context);
            }
            catch (RuntimeException e)
            {
                context.renderResponse();
                throw e;
            }
            if (!isValid())
            {
                context.renderResponse();
            }
        }
    }
    public void processUpdates(FacesContext context)
    {
        if (context == nullthrow new NullPointerException("context");
        if (!isRendered()) return;
        super.processUpdates(context);
        try
        {
            updateModel(context);
        }
        catch (RuntimeException e)
        {
            context.renderResponse();
            throw e;
        }
        if (!isValid())
        {
            context.renderResponse();
        }
    }
    public void decode(FacesContext context)
    {
        //We (re)set to valid, so that component automatically gets (re)validated
        setValid(true);
        super.decode(context);
    }
    public void broadcast(FacesEvent event)
            throws AbortProcessingException
    {
        if (!(event instanceof ValueChangeEvent))
        {
            throw new IllegalArgumentException("FacesEvent of class " + event.getClass().getName() + " not supported by UIInput");
        }
        // invoke standard listeners attached to this component first
        super.broadcast(event);
        // invoke the single listener defined directly on the component
        MethodBinding valueChangeListenerBinding = getValueChangeListener();
        if (valueChangeListenerBinding != null)
        {
            try
            {
                valueChangeListenerBinding.invoke(getFacesContext(),
                                                  new Object[]{event});
            }
            catch (EvaluationException e)
            {
                Throwable cause = e.getCause();
                if (cause != null && cause instanceof AbortProcessingException)
                {
                    throw (AbortProcessingException)cause;
                }
                else
                {
                    throw e;
                }
            }
        }
    }
    public void updateModel(FacesContext context)
    {
        if (!isValid()) return;
        if (!isLocalValueSet()) return;
        ValueBinding vb = getValueBinding("value");
        if (vb == nullreturn;
        try
        {
            vb.setValue(contextgetLocalValue());
            setValue(null);
            setLocalValueSet(false);
        }
        catch (EvaluationException ee)
        {
            String exceptionMessage = ee.getMessage();
            if (exceptionMessage == null)
            {
                _MessageUtils.addErrorMessage(contextthis,
                        new Object[] { getId() });
            }
            else
            {
                _MessageUtils.addErrorMessage(contextthisee);
            }
            
            setValid(false);
        }
        catch (RuntimeException e)
        {
        	//Object[] args = {getId()};
            context.getExternalContext().log(e.getMessage(), e);
            _MessageUtils.addErrorMessage(contextthis,,new Object[]{getId()});
            setValid(false);
        }
    }
    protected void validateValue(FacesContext context,Object convertedValue)
    {
        boolean empty = convertedValue == null ||
                        (convertedValue instanceof String
                         && ((String)convertedValue).length() == 0);
        if (isRequired() && empty)
        {
            _MessageUtils.addErrorMessage(contextthis,new Object[]{getId()});
            setValid(false);
            return;
        }
        if (!empty)
        {
            _ComponentUtils.callValidators(contextthisconvertedValue);
        }
    }

    
Determine whether the new value is valid, and queue a ValueChangeEvent if necessary.

The "submitted value" is converted to the necessary type; conversion failure is reported as an error and validation processing terminates for this component. See documentation for method getConvertedValue for details on the conversion process.

Any validators attached to this component are then run, passing the converted value.

The old value of this component is then fetched (possibly involving the evaluation of a value-binding expression, ie invoking a method on a user object). The old value is compared to the new validated value, and if they are different then a ValueChangeEvent is queued for later processing.

On successful completion of this method:

  • isValid() is true
  • isLocalValueSet() is true
  • submittedValue is reset to null
  • a ValueChangeEvent is queued if the new value != old value
    public void validate(FacesContext context)
    {
        if (context == nullthrow new NullPointerException("context");
        Object submittedValue = getSubmittedValue();
        if (submittedValue == nullreturn;
        Object convertedValue = getConvertedValue(contextsubmittedValue);
        if (!isValid()) return;
        validateValue(contextconvertedValue);
        if (!isValid()) return;
        Object previousValue = getValue();
        setValue(convertedValue);
        setSubmittedValue(null);
        if (compareValues(previousValueconvertedValue))
        {
            queueEvent(new ValueChangeEvent(thispreviousValueconvertedValue));
        }
    }

    
Convert the provided object to the desired value.

If there is a renderer for this component, then call the renderer's getConvertedValue method. While this can of course be implemented in any way the renderer desires, it typically performs exactly the same processing that this method would have done anyway (ie that described below for the no-renderer case).

Otherwise:

  • If the submittedValue is not a String then just return the submittedValue unconverted.
  • If there is no "value" value-binding then just return the submittedValue unconverted.
  • Use introspection to determine the type of the target property specified by the value-binding, and then use Application.createConverter to find a converter that can map from String to the required type. Apply the converter to the submittedValue and return the result.
    protected Object getConvertedValue(FacesContext contextObject submittedValue)
    {
        try
        {
            Renderer renderer = getRenderer(context);
            if (renderer != null)
            {
                return renderer.getConvertedValue(contextthissubmittedValue);
            }
            else if (submittedValue instanceof String)
            {
                Converter converter = _SharedRendererUtils.findUIOutputConverter(contextthis);
                if (converter != null)
                {
                    return converter.getAsObject(contextthis, (String)submittedValue);
                }
            }
        }
        catch (ConverterException e)
        {
            FacesMessage facesMessage = e.getFacesMessage();
            if (facesMessage != null)
            {
                context.addMessage(getClientId(context), facesMessage);
            }
            else
            {
                _MessageUtils.addErrorMessage(contextthis,new Object[]{getId()});
            }
            setValid(false);
        }
        return submittedValue;
    }
    protected boolean compareValues(Object previous,
                                      Object value)
    {
        return previous==null?(value!=null):(!previous.equals(value));
    }
    public void addValidator(Validator validator)
    {
        if (validator == nullthrow new NullPointerException("validator");
        if ( == null)
        {
             = new ArrayList();
        }
        .add(validator);
    }
    public Validator[] getValidators()
    {
        return  != null ?
               (Validator[]).toArray(new Validator[.size()]) :
               ;
    }
    public void removeValidator(Validator validator)
    {
        if (validator == nullthrow new NullPointerException("validator");
        if ( != null)
        {
            .remove(validator);
        }
    }
    public void addValueChangeListener(ValueChangeListener listener)
    {
        addFacesListener(listener);
    }
    {
    }
    public void removeValueChangeListener(ValueChangeListener listener)
    {
        removeFacesListener(listener);
    }
    public Object saveState(FacesContext context)
    {
        Object values[] = new Object[9];
        values[0] = super.saveState(context);
        values[1] = ;
        values[2] = Boolean.valueOf();
        values[3] = ;
        values[4] = ;
        values[5] = Boolean.valueOf();
        values[6] = saveAttachedState(context);
        values[7] = saveAttachedState(context);
        values[8] = saveAttachedState(context);
        return values;
    }
    public void restoreState(FacesContext contextObject state)
    {
        Object values[] = (Object[])state;
        super.restoreState(context, values[0]);
         = (Boolean)values[1];
         = ((Boolean)values[2]).booleanValue();
         = (Boolean)values[3];
         = values[4];
         = ((Boolean)values[5]).booleanValue();
         = (MethodBinding)restoreAttachedState(contextvalues[6]);
         = (MethodBinding)restoreAttachedState(contextvalues[7]);
         = (List)restoreAttachedState(contextvalues[8]);
    }
    //------------------ GENERATED CODE BEGIN (do not modify!) --------------------
    public static final String COMPONENT_TYPE = "javax.faces.Input";
    public static final String COMPONENT_FAMILY = "javax.faces.Input";
    private static final String DEFAULT_RENDERER_TYPE = "javax.faces.Text";
    private static final boolean DEFAULT_IMMEDIATE = false;
    private static final boolean DEFAULT_REQUIRED = false;
    private Boolean _immediate = null;
    private Boolean _required = null;
    public UIInput()
    {
    }
    public String getFamily()
    {
        return ;
    }
    public void setImmediate(boolean immediate)
    {
         = Boolean.valueOf(immediate);
    }
    public boolean isImmediate()
    {
        if ( != nullreturn .booleanValue();
        ValueBinding vb = getValueBinding("immediate");
        Boolean v = vb != null ? (Boolean)vb.getValue(getFacesContext()) : null;
        return v != null ? v.booleanValue() : ;
    }
    public void setRequired(boolean required)
    {
         = Boolean.valueOf(required);
    }
    public boolean isRequired()
    {
        if ( != nullreturn .booleanValue();
        ValueBinding vb = getValueBinding("required");
        Boolean v = vb != null ? (Boolean)vb.getValue(getFacesContext()) : null;
        return v != null ? v.booleanValue() : ;
    }
    //------------------ GENERATED CODE END ---------------------------------------
New to GrepCode? Check out our FAQ X