View Javadoc

1   // WARNING: This file was automatically generated. Do not edit it directly,
2   //          or you will lose your changes.
3   
4   /*
5    * Licensed to the Apache Software Foundation (ASF) under one
6    * or more contributor license agreements.  See the NOTICE file
7    * distributed with this work for additional information
8    * regarding copyright ownership.  The ASF licenses this file
9    * to you under the Apache License, Version 2.0 (the
10   * "License"); you may not use this file except in compliance
11   * with the License.  You may obtain a copy of the License at
12   *
13   *   http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing,
16   * software distributed under the License is distributed on an
17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   * KIND, either express or implied.  See the License for the
19   * specific language governing permissions and limitations
20   * under the License.
21  */
22  package org.apache.myfaces.trinidad.component;
23  
24  import java.io.IOException;
25  import java.util.AbstractMap;
26  import java.util.Collections;
27  import java.util.Map;
28  import java.util.Set;
29  import javax.faces.FacesException;
30  import javax.faces.component.ContextCallback;
31  import javax.faces.component.NamingContainer;
32  import javax.faces.component.UIComponent;
33  import javax.faces.context.FacesContext;
34  import javax.faces.event.AbortProcessingException;
35  import javax.faces.event.FacesEvent;
36  import org.apache.myfaces.trinidad.bean.FacesBean;
37  import org.apache.myfaces.trinidad.bean.PropertyKey;
38  import org.apache.myfaces.trinidad.logging.TrinidadLogger;
39  import org.apache.myfaces.trinidad.util.ComponentUtils;
40  
41  /**
42   *
43   * This component inserts a component (defined by a separate page
44   *           that starts with <tr:componentDef>)
45   *           into the current page. The "id" attribute is required on this
46   *           component, which implements NamingContainer.  This component may have
47   *           a value, custom attributes, or both.  The value will be
48   *           made available to within component definition using the "binding"
49   *           EL attribute.  Custom attributes, defined with
50   *           <f:attribute> or <tr:attribute>, will also
51   *           be made available within the component definition - see the
52   *           <tr:componentDef> tag documentation for more information.
53   * 
54   *           This component may also have facets. Each facet may be referenced
55   *           exactly once from within a <tr:componentDef> tag by
56   *           using a <tr:facetRef> - see the
57   *           <tr:componentRef> tag documentation for more information.
58   *
59   * <h4>Events:</h4>
60   * <table border="1" width="100%" cellpadding="3" summary="">
61   * <tr bgcolor="#CCCCFF" class="TableHeadingColor">
62   * <th align="left">Type</th>
63   * <th align="left">Phases</th>
64   * <th align="left">Description</th>
65   * </tr>
66   * <tr class="TableRowColor">
67   * <td valign="top"><code>org.apache.myfaces.trinidad.event.AttributeChangeEvent</code></td>
68   * <td valign="top" nowrap>Invoke<br>Application<br>Apply<br>Request<br>Values</td>
69   * <td valign="top">Event delivered to describe an attribute change.  Attribute change events are not delivered for any programmatic change to a property.  They are only delivered when a renderer changes a property without the application's specific request.  An example of an attribute change event might include the width of a column that supported client-side resizing.</td>
70   * </tr>
71   * </table>
72   */
73  public class UIXComponentRef extends UIXComponentBase
74                               implements NamingContainer
75  {
76    static public final FacesBean.Type TYPE = new FacesBean.Type(
77      UIXComponentBase.TYPE);
78    static public final PropertyKey VALUE_KEY =
79      TYPE.registerKey("value");
80    static public final PropertyKey COMPONENT_TYPE_KEY =
81      TYPE.registerKey("componentType", String.class);
82    static public final PropertyKey VAR_KEY =
83      TYPE.registerKey("var", String.class);
84  
85    static public final String COMPONENT_FAMILY =
86      "org.apache.myfaces.trinidad.ComponentRef";
87    static public final String COMPONENT_TYPE =
88      "org.apache.myfaces.trinidad.ComponentRef";
89  
90    /**
91     * Construct an instance of the UIXComponentRef.
92     */
93    public UIXComponentRef()
94    {
95      super(null);
96    }
97    
98  
99  
100   @Override
101   public boolean invokeOnComponent(FacesContext context,
102                                    String clientId,
103                                    ContextCallback callback)
104     throws FacesException
105   {
106     // optimize case where clientId isn't in NamingContainer
107     return invokeOnNamingContainerComponent(context, clientId, callback);
108   }
109 
110   @Override
111   public void queueEvent(FacesEvent event)
112   {
113     // we want to wrap up
114     // the event so we can execute it in the correct context (with the correct
115     // pageDefinition):
116     event = new WrapperEvent(this, event);
117     super.queueEvent(event);
118   }
119 
120   @Override
121   public void broadcast(FacesEvent event)
122     throws AbortProcessingException
123   {
124     if (event instanceof WrapperEvent)
125     {
126       final WrapperEvent wrapper = (WrapperEvent) event;
127       final FacesContext context = FacesContext.getCurrentInstance();
128       Runnable runner = new Runnable()
129       {
130         public void run()
131         {
132           wrapper.broadcastWrappedEvent(context);
133         }
134       };
135       _processPhase(context, runner);
136     }
137     else
138     {
139       super.broadcast(event);
140     }
141   }
142 
143   @Override
144   public void processDecodes(final FacesContext context)
145   {
146     Runnable runner = new Runnable()
147     {
148       public void run()
149       {
150         UIXComponentRef.super.processDecodes(context);
151       }
152     };
153     _processPhase(context, runner);
154   }
155 
156   @Override
157   public void processValidators(final FacesContext context)
158   {
159     Runnable runner = new Runnable()
160     {
161       public void run()
162       {
163         UIXComponentRef.super.processValidators(context);
164       }
165     };
166     _processPhase(context, runner);
167   }
168 
169   @Override
170   public void processUpdates(final FacesContext context)
171   {
172     Runnable runner = new Runnable()
173     {
174       public void run()
175       {
176         UIXComponentRef.super.processUpdates(context);
177       }
178     };
179     _processPhase(context, runner);
180   }
181 
182   @Override
183   public void encodeBegin(FacesContext context) throws IOException
184   {
185     _setupEL(context);
186     super.encodeBegin(context);
187   }
188 
189   @Override
190   public void encodeEnd(final FacesContext context) throws IOException
191   {
192     super.encodeEnd(context);
193     _resetEL(context);
194   }
195 
196   private void _setupEL(FacesContext context)
197   {
198     UIXComponentRef region = _getParent();
199     // in the JSP VE we try to display a regiondef page. so we can't
200     // blow up if there isn't a parent region component.
201     if (region != null)
202     {
203       Object newValue = region.getValue();
204       _oldValue =
205         TableUtils.setupELVariable(context, _BINDINGS_VAR, newValue);
206 
207       String var = _getVar();
208       _oldVar =
209         TableUtils.setupELVariable(context, var, new AttrMap());
210     }
211   }
212 
213   private void _resetEL(FacesContext context)
214   {
215     TableUtils.setupELVariable(context, _BINDINGS_VAR, _oldValue);
216     String var = _getVar();
217     TableUtils.setupELVariable(context, var, _oldVar);
218 
219     _oldValue = Boolean.FALSE; // GC
220     _oldVar = null; // GC
221   }
222 
223   private void _processPhase(FacesContext context, Runnable runner)
224   {
225     assert context != null : "FacesContext is null";
226 
227     if (!isRendered())
228       return;
229 
230     _setupEL(context);
231 
232     try
233     {
234       runner.run();
235     }
236     finally
237     {
238       _resetEL(context);
239     }
240   }
241 
242   private UIXComponentRef _getParent()
243   {
244     for(UIComponent test = this; test != null; test = test.getParent())
245     {
246       if (test instanceof UIXComponentRef)
247         return (UIXComponentRef) test;
248     }
249 
250     _LOG.severe("NO_PARENT_COMPONENTREF_FOUND");
251     return null;
252   }
253 
254   private String _getVar()
255   {
256     if (_var == null)
257     {
258       _var = getVar();
259       if (_var == null)
260       {
261         _LOG.fine("var not set");
262         _var = "null"; // initialize so that we don't keep checking.
263       }
264     }
265     return _var;
266   }
267 
268   private final class AttrMap extends AbstractMap<Object, Object>
269   {
270     @Override
271     public Object get(final Object key)
272     {
273       FacesContext context = FacesContext.getCurrentInstance();
274 
275       assert _oldValue != Boolean.FALSE : "EL variables have not been setup";
276 
277       // when evaluating an attribute value on the parent, we need to reset the
278       // EL variables, evaluate the attribute value and then restore the EL variables:
279       Object currentValue = TableUtils.setupELVariable(context, _BINDINGS_VAR, _oldValue);
280       String var = _getVar();
281       Object currentVar = TableUtils.setupELVariable(context, var, _oldVar);
282 
283       UIXComponentRef region =_getParent();
284       Object result = region.getAttributes().get(key);
285 
286       TableUtils.setupELVariable(context, _BINDINGS_VAR, currentValue);
287       TableUtils.setupELVariable(context, var, currentVar);
288       return result;
289     }
290 
291     @Override
292     public Set<Map.Entry<Object, Object>> entrySet()
293     {
294       return Collections.emptySet();
295     }
296   }
297 
298   // Local backing for the actual Var
299   private transient String _var = null;
300   private transient Object _oldValue = Boolean.FALSE;
301   private transient Object _oldVar = null;
302   private static final String _BINDINGS_VAR = "bindings";
303   private static final TrinidadLogger _LOG = TrinidadLogger.createTrinidadLogger(UIXComponentRef.class);
304 
305   /**
306    * Gets the value of the component.  This value be evaluated during
307    *               all lifecycle processing and
308    *               made available to the componentDef as an EL variable named "bindings".
309    *
310    * @return  the new value value
311    */
312   final public Object getValue()
313   {
314     return getProperty(VALUE_KEY);
315   }
316 
317   /**
318    * Sets the value of the component.  This value be evaluated during
319    *               all lifecycle processing and
320    *               made available to the componentDef as an EL variable named "bindings".
321    * 
322    * @param value  the new value value
323    */
324   final public void setValue(Object value)
325   {
326     setProperty(VALUE_KEY, (value));
327   }
328 
329   /**
330    * Gets <html> which componnet to use. This should match a
331    * &lt;component-type&gt; string in a region metadata file.
332    * An example might look like "org.apache.myfaces.trinidaddemo.region.stock".
333    * This attribute can be bound to an EL expression or manually
334    * set, which will change the region definition used the next
335    * time the page renders.</html>
336    *
337    * @return  the new componentType value
338    */
339   final public String getComponentType()
340   {
341     return ComponentUtils.resolveString(getProperty(COMPONENT_TYPE_KEY));
342   }
343 
344   /**
345    * Sets <html> which componnet to use. This should match a
346    * &lt;component-type&gt; string in a region metadata file.
347    * An example might look like "org.apache.myfaces.trinidaddemo.region.stock".
348    * This attribute can be bound to an EL expression or manually
349    * set, which will change the region definition used the next
350    * time the page renders.</html>
351    * 
352    * @param componentType  the new componentType value
353    */
354   final public void setComponentType(String componentType)
355   {
356     setProperty(COMPONENT_TYPE_KEY, (componentType));
357   }
358 
359   /**
360    * Gets <html> The name of the EL variable that will be set when encoding the component.
361    *         This attribute is set by the tr:componentDef on behalf of the UIXComponentRef, and not the tr:componentRef.
362    *         For example,
363    *               let's say this "var" attribute is set to the value "attrs";
364    *               if the &lt;tr:componentRef&gt; tag has an attribute called "bar",
365    *               The value of "bar" can be referenced from a child within
366    *               &lt;tr:componentDef&gt; using the expression
367    *               <code>#{attrs.bar}</code>.</html>
368    *
369    * @return  the new var value
370    */
371   final public String getVar()
372   {
373     return ComponentUtils.resolveString(getProperty(VAR_KEY));
374   }
375 
376   /**
377    * Sets <html> The name of the EL variable that will be set when encoding the component.
378    *         This attribute is set by the tr:componentDef on behalf of the UIXComponentRef, and not the tr:componentRef.
379    *         For example,
380    *               let's say this "var" attribute is set to the value "attrs";
381    *               if the &lt;tr:componentRef&gt; tag has an attribute called "bar",
382    *               The value of "bar" can be referenced from a child within
383    *               &lt;tr:componentDef&gt; using the expression
384    *               <code>#{attrs.bar}</code>.</html>
385    * 
386    * @param var  the new var value
387    */
388   final public void setVar(String var)
389   {
390     setProperty(VAR_KEY, (var));
391   }
392 
393   @Override
394   public String getFamily()
395   {
396     return COMPONENT_FAMILY;
397   }
398 
399   @Override
400   protected FacesBean.Type getBeanType()
401   {
402     return TYPE;
403   }
404 
405   /**
406    * Construct an instance of the UIXComponentRef.
407    */
408   protected UIXComponentRef(
409     String rendererType
410     )
411   {
412     super(rendererType);
413   }
414 
415   static
416   {
417     TYPE.lock();
418   }
419 }