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 java.util.Map;
22  
23  import javax.el.ValueExpression;
24  import javax.faces.application.Application;
25  import javax.faces.application.ResourceDependencies;
26  import javax.faces.application.ResourceDependency;
27  import javax.faces.context.FacesContext;
28  import javax.faces.convert.Converter;
29  
30  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFComponent;
31  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty;
32  
33  /**
34   * Displays a value to the user.
35   */
36  @JSFComponent(defaultRendererType = "javax.faces.Text")
37  public class UIOutput extends UIComponentBase implements ValueHolder
38  {
39      public static final String COMPONENT_TYPE = "javax.faces.Output";
40      public static final String COMPONENT_FAMILY = "javax.faces.Output";
41  
42      private Converter _converter;
43  
44      /**
45       * Construct an instance of the UIOutput.
46       */
47      public UIOutput()
48      {
49          setRendererType("javax.faces.Text");
50      }
51  
52      @Override
53      public String getFamily()
54      {
55          return COMPONENT_FAMILY;
56      }
57  
58      public Object getLocalValue()
59      {
60          return  getStateHelper().get(PropertyKeys.value);
61      }
62  
63      /**
64       * Gets The initial value of this component.
65       * 
66       * @return the new value value
67       */
68      @JSFProperty
69      public Object getValue()
70      {
71          return  getStateHelper().eval(PropertyKeys.value);
72      }
73  
74      /**
75       * The initial value of this component.
76       */
77      public void setValue(Object value)
78      {
79          getStateHelper().put(PropertyKeys.value, value );
80      }
81  
82      /**
83       * An expression that specifies the Converter for this component.
84       * <p>
85       * The value can either be a static value (ID) or an EL expression. When a static id is
86       * specified, an instance of the converter type registered with that id is used. When this
87       * is an EL expression, the result of evaluating the expression must be an object that
88       * implements the Converter interface.
89       * </p>
90       */
91      @JSFProperty(partialStateHolder=true)
92      public Converter getConverter()
93      {
94          if (_converter != null)
95          {
96              return _converter;
97          }
98          ValueExpression expression = getValueExpression("converter");
99          if (expression != null)
100         {
101             return (Converter) expression.getValue(getFacesContext().getELContext());
102         }
103         return null;
104     }
105 
106     public void setConverter(Converter converter)
107     {
108         this._converter = converter;
109         if (initialStateMarked())
110         {
111             getStateHelper().put(PropertyKeys.converterSet,Boolean.TRUE);
112         }
113         // The argument converter must be inspected for the presence of the ResourceDependency annotation.
114         //_handleAnnotations(FacesContext.getCurrentInstance(), converter);
115     }
116     
117     private boolean _isSetConverter()
118     {
119         Boolean value = (Boolean) getStateHelper().get(PropertyKeys.converterSet);
120         return value == null ? false : value;
121     }
122     
123     public void markInitialState()
124     {
125         super.markInitialState();
126         if (_converter != null && 
127             _converter instanceof PartialStateHolder)
128         {
129             ((PartialStateHolder)_converter).markInitialState();
130         }
131     }
132     
133     public void clearInitialState()
134     {
135         if (initialStateMarked())
136         {
137             super.clearInitialState();
138             if (_converter != null && 
139                 _converter instanceof PartialStateHolder)
140             {
141                 ((PartialStateHolder)_converter).clearInitialState();
142             }
143         }
144     }
145     
146     enum PropertyKeys
147     {
148          value
149         , converterSet
150     }
151 
152     @Override
153     public Object saveState(FacesContext facesContext)
154     {
155         if (initialStateMarked())
156         {
157             Object parentSaved = super.saveState(facesContext);
158             Object converterSaved = null;
159             boolean nullDelta = true;
160             if (!_isSetConverter() &&
161                 _converter != null && 
162                 _converter instanceof PartialStateHolder)
163             {
164                 //Delta
165                 StateHolder holder = (StateHolder) _converter;
166                 if (!holder.isTransient())
167                 {
168                     Object attachedState = holder.saveState(facesContext);
169                     if (attachedState != null)
170                     {
171                         nullDelta = false;
172                     }
173                     converterSaved = new _AttachedDeltaWrapper(_converter.getClass(),
174                         attachedState);
175                 }
176                 else
177                 {
178                     converterSaved = null;
179                 }
180             }
181             else if (_isSetConverter() || _converter != null)
182             {
183                 //Full
184                 converterSaved = saveAttachedState(facesContext,_converter);
185                 nullDelta = false;
186             }
187             
188             if (parentSaved == null && nullDelta)
189             {
190                 //No values
191                 return null;
192             }   
193             return new Object[]{parentSaved, converterSaved};
194         }
195         else
196         {
197             Object[] values = new Object[2];
198             values[0] = super.saveState(facesContext);
199             values[1] = saveAttachedState(facesContext,_converter);
200             return values;
201         } 
202     }
203 
204     @Override
205     public void restoreState(FacesContext facesContext, Object state)
206     {
207         if (state == null)
208         {
209             return;
210         }
211         
212         Object[] values = (Object[])state;
213         super.restoreState(facesContext,values[0]);
214         if (values[1] instanceof _AttachedDeltaWrapper)
215         {
216             //Delta
217             ((StateHolder)_converter).restoreState(facesContext, ((_AttachedDeltaWrapper) values[1]).getWrappedStateObject());
218         }
219         else
220         {
221             //Full
222             _converter = (javax.faces.convert.Converter) restoreAttachedState(facesContext,values[1]);
223         }         
224     }
225     
226     /*
227     void _handleAnnotations(FacesContext context, Object inspected)
228     {
229         if (inspected == null) {
230             return;
231         }
232         
233         ResourceDependency annotation = inspected.getClass().getAnnotation(ResourceDependency.class);
234         
235         if (annotation == null)
236         {
237             // If the ResourceDependency annotation is not present, the argument must be inspected for the presence 
238             // of the ResourceDependencies annotation. 
239             ResourceDependencies dependencies = inspected.getClass().getAnnotation(ResourceDependencies.class);
240             if (dependencies != null)
241             {
242                 // If the ResourceDependencies annotation is present, the action described in ResourceDependencies 
243                 // must be taken.
244                 for (ResourceDependency dependency : dependencies.value())
245                 {
246                     _handleResourceDependency(context, dependency);
247                 }
248             }
249         }
250         else
251         {
252             // If the ResourceDependency annotation is present, the action described in ResourceDependency must be 
253             // taken. 
254             _handleResourceDependency(context, annotation);
255         }
256     }
257     
258     private void _handleResourceDependency(FacesContext context, ResourceDependency annotation)
259     {
260         // If this annotation is not present on the class in question, no action must be taken. 
261         if (annotation != null)
262         {
263             Application application = context.getApplication();
264             
265             // Create a UIOutput instance by passing javax.faces.Output. to 
266             // Application.createComponent(java.lang.String).
267             UIOutput output = (UIOutput) application.createComponent(COMPONENT_TYPE);
268             
269             // Get the annotation instance from the class and obtain the values of the name, library, and 
270             // target attributes.
271             String name = annotation.name();
272             
273             // Obtain the renderer-type for the resource name by passing name to 
274             // ResourceHandler.getRendererTypeForResourceName(java.lang.String).
275             String rendererType = application.getResourceHandler().getRendererTypeForResourceName(name);
276             
277             // Call setRendererType on the UIOutput instance, passing the renderer-type.
278             output.setRendererType(rendererType);
279             
280             // Obtain the Map of attributes from the UIOutput component by calling UIComponent.getAttributes().
281             Map<String, Object> attributes = output.getAttributes();
282             
283             // Store the name into the attributes Map under the key "name".
284             attributes.put("name", name);
285             
286             // If library is the empty string, let library be null.
287             String library = annotation.library();
288             if (library != null && library.length() > 0)
289             {
290                 // If library is non-null, store it under the key "library".
291                 attributes.put("library", library);
292             }
293             
294             // If target is the empty string, let target be null.
295             String target = annotation.target();
296             if (target != null && target.length() > 0)
297             {
298                 // If target is non-null, store it under the key "target".
299                 attributes.put("target", target);
300             }
301             else
302             {
303                 // Otherwise, if target is null, call UIViewRoot.addComponentResource(javax.faces.context.FacesContext, 
304                 // javax.faces.component.UIComponent), passing the UIOutput instance as the second argument.
305                 context.getViewRoot().addComponentResource(context, output);
306             }
307         }
308     }
309     */
310 }