View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package javax.faces.component;
20  
21  import javax.el.ValueExpression;
22  import javax.faces.context.FacesContext;
23  import javax.faces.convert.Converter;
24  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFComponent;
25  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty;
26  
27  /**
28   * Displays a value to the user.
29   */
30  @JSFComponent(defaultRendererType = "javax.faces.Text")
31  public class UIOutput extends UIComponentBase implements ValueHolder
32  {
33      public static final String COMPONENT_TYPE = "javax.faces.Output";
34      public static final String COMPONENT_FAMILY = "javax.faces.Output";
35  
36      private Object _value;
37      private Converter _converter;
38  
39      /**
40       * Construct an instance of the UIOutput.
41       */
42      public UIOutput()
43      {
44          setRendererType("javax.faces.Text");
45      }
46  
47      @Override
48      public String getFamily()
49      {
50          return COMPONENT_FAMILY;
51      }
52  
53      public Object getLocalValue()
54      {
55          return _value;
56      }
57  
58      /**
59       * Gets The initial value of this component.
60       * 
61       * @return the new value value
62       */
63      @JSFProperty
64      public Object getValue()
65      {
66          if (_value != null)
67          {
68              return _value;
69          }
70          ValueExpression expression = getValueExpression("value");
71          if (expression != null)
72          {
73              return expression.getValue(getFacesContext().getELContext());
74          }
75          return null;
76      }
77  
78      /**
79       * The initial value of this component.
80       */
81      public void setValue(Object value)
82      {
83          this._value = value;
84      }
85  
86      /**
87       * An expression that specifies the Converter for this component.
88       * <p>
89       * The value can either be a static value (ID) or an EL expression. When a static id is
90       * specified, an instance of the converter type registered with that id is used. When this
91       * is an EL expression, the result of evaluating the expression must be an object that
92       * implements the Converter interface.
93       * </p>
94       */
95      @JSFProperty(stateHolder=true)
96      public Converter getConverter()
97      {
98          if (_converter != null)
99          {
100             return _converter;
101         }
102         ValueExpression expression = getValueExpression("converter");
103         if (expression != null)
104         {
105             return (Converter) expression.getValue(getFacesContext().getELContext());
106         }
107         return null;
108     }
109 
110     public void setConverter(Converter converter)
111     {
112         this._converter = converter;
113     }
114 
115     @Override
116     public Object saveState(FacesContext facesContext)
117     {
118         Object[] values = new Object[3];
119         values[0] = super.saveState(facesContext);
120         values[1] = _value;
121         values[2] = saveAttachedState(facesContext, _converter);
122 
123         return values;
124     }
125 
126     @Override
127     public void restoreState(FacesContext facesContext, Object state)
128     {
129         Object[] values = (Object[]) state;
130         super.restoreState(facesContext, values[0]);
131         _value = values[1];
132         _converter = (Converter) restoreAttachedState(facesContext, values[2]);
133     }
134 }