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       WrapperEvent wrapper = (WrapperEvent) event;
127       final FacesEvent wrapped = wrapper.getEvent();
128       Runnable runner = new Runnable()
129       {
130         public void run()
131         {
132           wrapped.getComponent().broadcast(wrapped);
133         }
134       };
135       FacesContext context = FacesContext.getCurrentInstance();
136       _processPhase(context, runner);
137     }
138     else
139     {
140       super.broadcast(event);
141     }
142   }
143 
144   @Override
145   public void processDecodes(final FacesContext context)
146   {
147     Runnable runner = new Runnable()
148     {
149       public void run()
150       {
151         UIXComponentRef.super.processDecodes(context);
152       }
153     };
154     _processPhase(context, runner);
155   }
156 
157   @Override
158   public void processValidators(final FacesContext context)
159   {
160     Runnable runner = new Runnable()
161     {
162       public void run()
163       {
164         UIXComponentRef.super.processValidators(context);
165       }
166     };
167     _processPhase(context, runner);
168   }
169 
170   @Override
171   public void processUpdates(final FacesContext context)
172   {
173     Runnable runner = new Runnable()
174     {
175       public void run()
176       {
177         UIXComponentRef.super.processUpdates(context);
178       }
179     };
180     _processPhase(context, runner);
181   }
182 
183   @Override
184   public void encodeBegin(FacesContext context) throws IOException
185   {
186     _setupEL(context);
187     super.encodeBegin(context);
188   }
189 
190   @Override
191   public void encodeEnd(final FacesContext context) throws IOException
192   {
193     super.encodeEnd(context);
194     _resetEL(context);
195   }
196 
197   private void _setupEL(FacesContext context)
198   {
199     UIXComponentRef region = _getParent();
200     // in the JSP VE we try to display a regiondef page. so we can't
201     // blow up if there isn't a parent region component.
202     if (region != null)
203     {
204       Object newValue = region.getValue();
205       _oldValue =
206         TableUtils.setupELVariable(context, _BINDINGS_VAR, newValue);
207 
208       String var = _getVar();
209       _oldVar =
210         TableUtils.setupELVariable(context, var, new AttrMap());
211     }
212   }
213 
214   private void _resetEL(FacesContext context)
215   {
216     TableUtils.setupELVariable(context, _BINDINGS_VAR, _oldValue);
217     String var = _getVar();
218     TableUtils.setupELVariable(context, var, _oldVar);
219 
220     _oldValue = Boolean.FALSE; // GC
221     _oldVar = null; // GC
222   }
223 
224   private void _processPhase(FacesContext context, Runnable runner)
225   {
226     assert context != null : "FacesContext is null";
227 
228     if (!isRendered())
229       return;
230 
231     _setupEL(context);
232 
233     try
234     {
235       runner.run();
236     }
237     finally
238     {
239       _resetEL(context);
240     }
241   }
242 
243   private UIXComponentRef _getParent()
244   {
245     for(UIComponent test = this; test != null; test = test.getParent())
246     {
247       if (test instanceof UIXComponentRef)
248         return (UIXComponentRef) test;
249     }
250 
251     _LOG.severe("NO_PARENT_COMPONENTREF_FOUND");
252     return null;
253   }
254 
255   private String _getVar()
256   {
257     if (_var == null)
258     {
259       _var = getVar();
260       if (_var == null)
261       {
262         _LOG.fine("var not set");
263         _var = "null"; // initialize so that we don't keep checking.
264       }
265     }
266     return _var;
267   }
268 
269   private final class AttrMap extends AbstractMap<Object, Object>
270   {
271     @Override
272     public Object get(final Object key)
273     {
274       FacesContext context = FacesContext.getCurrentInstance();
275 
276       assert _oldValue != Boolean.FALSE : "EL variables have not been setup";
277 
278       // when evaluating an attribute value on the parent, we need to reset the
279       // EL variables, evaluate the attribute value and then restore the EL variables:
280       Object currentValue = TableUtils.setupELVariable(context, _BINDINGS_VAR, _oldValue);
281       String var = _getVar();
282       Object currentVar = TableUtils.setupELVariable(context, var, _oldVar);
283 
284       UIXComponentRef region =_getParent();
285       Object result = region.getAttributes().get(key);
286 
287       TableUtils.setupELVariable(context, _BINDINGS_VAR, currentValue);
288       TableUtils.setupELVariable(context, var, currentVar);
289       return result;
290     }
291 
292     @Override
293     public Set<Map.Entry<Object, Object>> entrySet()
294     {
295       return Collections.emptySet();
296     }
297   }
298 
299   // Local backing for the actual Var
300   private transient String _var = null;
301   private transient Object _oldValue = Boolean.FALSE;
302   private transient Object _oldVar = null;
303   private static final String _BINDINGS_VAR = "bindings";
304   private static final TrinidadLogger _LOG = TrinidadLogger.createTrinidadLogger(UIXComponentRef.class);
305 
306   /**
307    * Gets the value of the component.  This value be evaluated during
308    *               all lifecycle processing and
309    *               made available to the componentDef as an EL variable named "bindings".
310    *
311    * @return  the new value value
312    */
313   final public Object getValue()
314   {
315     return getProperty(VALUE_KEY);
316   }
317 
318   /**
319    * Sets the value of the component.  This value be evaluated during
320    *               all lifecycle processing and
321    *               made available to the componentDef as an EL variable named "bindings".
322    * 
323    * @param value  the new value value
324    */
325   final public void setValue(Object value)
326   {
327     setProperty(VALUE_KEY, (value));
328   }
329 
330   /**
331    * Gets <html> which componnet to use. This should match a
332    * &lt;component-type&gt; string in a region metadata file.
333    * An example might look like "org.apache.myfaces.trinidaddemo.region.stock".
334    * This attribute can be bound to an EL expression or manually
335    * set, which will change the region definition used the next
336    * time the page renders.</html>
337    *
338    * @return  the new componentType value
339    */
340   final public String getComponentType()
341   {
342     return ComponentUtils.resolveString(getProperty(COMPONENT_TYPE_KEY));
343   }
344 
345   /**
346    * Sets <html> which componnet to use. This should match a
347    * &lt;component-type&gt; string in a region metadata file.
348    * An example might look like "org.apache.myfaces.trinidaddemo.region.stock".
349    * This attribute can be bound to an EL expression or manually
350    * set, which will change the region definition used the next
351    * time the page renders.</html>
352    * 
353    * @param componentType  the new componentType value
354    */
355   final public void setComponentType(String componentType)
356   {
357     setProperty(COMPONENT_TYPE_KEY, (componentType));
358   }
359 
360   /**
361    * Gets <html> The name of the EL variable that will be set when encoding the component.
362    *         This attribute is set by the tr:componentDef on behalf of the UIXComponentRef, and not the tr:componentRef.
363    *         For example,
364    *               let's say this "var" attribute is set to the value "attrs";
365    *               if the &lt;tr:componentRef&gt; tag has an attribute called "bar",
366    *               The value of "bar" can be referenced from a child within
367    *               &lt;tr:componentDef&gt; using the expression
368    *               <code>#{attrs.bar}</code>.</html>
369    *
370    * @return  the new var value
371    */
372   final public String getVar()
373   {
374     return ComponentUtils.resolveString(getProperty(VAR_KEY));
375   }
376 
377   /**
378    * Sets <html> The name of the EL variable that will be set when encoding the component.
379    *         This attribute is set by the tr:componentDef on behalf of the UIXComponentRef, and not the tr:componentRef.
380    *         For example,
381    *               let's say this "var" attribute is set to the value "attrs";
382    *               if the &lt;tr:componentRef&gt; tag has an attribute called "bar",
383    *               The value of "bar" can be referenced from a child within
384    *               &lt;tr:componentDef&gt; using the expression
385    *               <code>#{attrs.bar}</code>.</html>
386    * 
387    * @param var  the new var value
388    */
389   final public void setVar(String var)
390   {
391     setProperty(VAR_KEY, (var));
392   }
393 
394   @Override
395   public String getFamily()
396   {
397     return COMPONENT_FAMILY;
398   }
399 
400   @Override
401   protected FacesBean.Type getBeanType()
402   {
403     return TYPE;
404   }
405 
406   /**
407    * Construct an instance of the UIXComponentRef.
408    */
409   protected UIXComponentRef(
410     String rendererType
411     )
412   {
413     super(rendererType);
414   }
415 
416   static
417   {
418     TYPE.lock();
419   }
420 }