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 org.apache.myfaces.view.facelets.tag.composite;
20  
21  import java.io.IOException;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Map;
28  
29  import javax.el.ValueExpression;
30  import javax.faces.FacesException;
31  import javax.faces.FacesWrapper;
32  import javax.faces.component.ContextCallback;
33  import javax.faces.component.UIComponent;
34  import javax.faces.component.behavior.ClientBehavior;
35  import javax.faces.component.behavior.ClientBehaviorHolder;
36  import javax.faces.component.visit.VisitCallback;
37  import javax.faces.component.visit.VisitContext;
38  import javax.faces.context.FacesContext;
39  import javax.faces.el.ValueBinding;
40  import javax.faces.event.AbortProcessingException;
41  import javax.faces.event.ComponentSystemEvent;
42  import javax.faces.event.ComponentSystemEventListener;
43  import javax.faces.event.FacesEvent;
44  import javax.faces.event.FacesListener;
45  import javax.faces.event.SystemEvent;
46  import javax.faces.event.SystemEventListener;
47  import javax.faces.render.Renderer;
48  
49  /**
50   * This class has two usages:
51   * 
52   * 1. On ClientBehaviorAttachedObjectTargetImpl to redirect the incoming sourceEvent
53   * to the final targetEvent.   
54   * 2. On FaceletsViewDeclarationLanguage.retargetAttachedObjects to redirect too, but
55   * this time is to allow chain events for nested composite components.
56   * 
57   * This class also implements FacesWrapper interface, to make possible to retrieve the
58   * real component if necessary.
59   * 
60   * @author Leonardo Uribe (latest modification by $Author: bommel $)
61   * @version $Revision: 1187700 $ $Date: 2011-10-22 07:19:37 -0500 (Sat, 22 Oct 2011) $
62   */
63  public class ClientBehaviorRedirectEventComponentWrapper extends UIComponent 
64      implements FacesWrapper<UIComponent>, ClientBehaviorHolder
65  {
66  
67      private final UIComponent _delegate;
68      private final String _sourceEvent; //cc:clientBehavior "name"
69      private final String _targetEvent; //cc:clientBehavior "event"
70  
71      public ClientBehaviorRedirectEventComponentWrapper(UIComponent delegate,
72              String sourceEvent, String targetEvent)
73      {
74          super();
75          _delegate = delegate;
76          _sourceEvent = sourceEvent;
77          _targetEvent = targetEvent;
78      }
79  
80      public UIComponent getWrapped()
81      {
82          return _delegate;
83      }
84  
85      public void addClientBehavior(String eventName, ClientBehavior behavior)
86      {
87          if (_sourceEvent.equals(eventName))
88          {
89              ((ClientBehaviorHolder)_delegate).addClientBehavior(_targetEvent == null ? ((ClientBehaviorHolder)_delegate).getDefaultEventName(): _targetEvent , behavior);
90          }
91      }
92  
93      public Map<String, List<ClientBehavior>> getClientBehaviors()
94      {
95          Map<String, List<ClientBehavior>> clientBehaviors = new HashMap<String, List<ClientBehavior>>(1);
96          clientBehaviors.put(_sourceEvent, ((ClientBehaviorHolder)_delegate).getClientBehaviors().get(_targetEvent));
97          return Collections.unmodifiableMap(clientBehaviors);
98      }
99  
100     public String getDefaultEventName()
101     {
102         if (_targetEvent == null )
103         {
104             // There is no targetEvent assigned, so we need to check if there is 
105             // a default event name on the delegate, if so return sourceEvent, otherwise
106             // there is no default event and we can't resolve the redirection, so
107             // return null. Note this usually could cause another exception later
108             // (see AjaxHandler code 
109             if (((ClientBehaviorHolder)_delegate).getDefaultEventName() != null)
110             {
111                 return _sourceEvent;
112             }
113             else
114             {
115                 return null;
116             }
117         }
118         else
119         {
120             // We have a target event, so in this case we have to return the sourceEvent,
121             // because it is expected the client behavior to be attached has this event name.
122             return _sourceEvent;
123         }
124     }
125 
126     public Collection<String> getEventNames()
127     {
128         return Collections.singletonList(_sourceEvent);
129     }
130     
131     // UIComponent wrapping. Just delegate.    
132     public void broadcast(FacesEvent event) throws AbortProcessingException
133     {
134         _delegate.broadcast(event);
135     }
136 
137     public void clearInitialState()
138     {
139         _delegate.clearInitialState();
140     }
141 
142     public void decode(FacesContext context)
143     {
144         _delegate.decode(context);
145     }
146 
147     public void encodeAll(FacesContext context) throws IOException
148     {
149         _delegate.encodeAll(context);
150     }
151 
152     public void encodeBegin(FacesContext context) throws IOException
153     {
154         _delegate.encodeBegin(context);
155     }
156 
157     public void encodeChildren(FacesContext context) throws IOException
158     {
159         _delegate.encodeChildren(context);
160     }
161 
162     public void encodeEnd(FacesContext context) throws IOException
163     {
164         _delegate.encodeEnd(context);
165     }
166 
167     public UIComponent findComponent(String expr)
168     {
169         return _delegate.findComponent(expr);
170     }
171 
172     public Map<String, Object> getAttributes()
173     {
174         return _delegate.getAttributes();
175     }
176 
177     public int getChildCount()
178     {
179         return _delegate.getChildCount();
180     }
181 
182     public List<UIComponent> getChildren()
183     {
184         return _delegate.getChildren();
185     }
186 
187     public String getClientId()
188     {
189         return _delegate.getClientId();
190     }
191 
192     public String getClientId(FacesContext context)
193     {
194         return _delegate.getClientId(context);
195     }
196 
197     public String getContainerClientId(FacesContext ctx)
198     {
199         return _delegate.getContainerClientId(ctx);
200     }
201 
202     public UIComponent getFacet(String name)
203     {
204         return _delegate.getFacet(name);
205     }
206 
207     public int getFacetCount()
208     {
209         return _delegate.getFacetCount();
210     }
211 
212     public Map<String, UIComponent> getFacets()
213     {
214         return _delegate.getFacets();
215     }
216 
217     public Iterator<UIComponent> getFacetsAndChildren()
218     {
219         return _delegate.getFacetsAndChildren();
220     }
221 
222     public String getFamily()
223     {
224         return _delegate.getFamily();
225     }
226 
227     public String getId()
228     {
229         return _delegate.getId();
230     }
231 
232     public List<SystemEventListener> getListenersForEventClass(
233             Class<? extends SystemEvent> eventClass)
234     {
235         return _delegate.getListenersForEventClass(eventClass);
236     }
237 
238     public UIComponent getNamingContainer()
239     {
240         return _delegate.getNamingContainer();
241     }
242 
243     public UIComponent getParent()
244     {
245         return _delegate.getParent();
246     }
247 
248     public String getRendererType()
249     {
250         return _delegate.getRendererType();
251     }
252 
253     public boolean getRendersChildren()
254     {
255         return _delegate.getRendersChildren();
256     }
257 
258     public Map<String, String> getResourceBundleMap()
259     {
260         return _delegate.getResourceBundleMap();
261     }
262 
263     public ValueBinding getValueBinding(String name)
264     {
265         return _delegate.getValueBinding(name);
266     }
267 
268     public ValueExpression getValueExpression(String name)
269     {
270         return _delegate.getValueExpression(name);
271     }
272 
273     public boolean initialStateMarked()
274     {
275         return _delegate.initialStateMarked();
276     }
277 
278     public boolean invokeOnComponent(FacesContext context, String clientId,
279             ContextCallback callback) throws FacesException
280     {
281         return _delegate.invokeOnComponent(context, clientId, callback);
282     }
283 
284     public boolean isInView()
285     {
286         return _delegate.isInView();
287     }
288 
289     public boolean isRendered()
290     {
291         return _delegate.isRendered();
292     }
293 
294     public boolean isTransient()
295     {
296         return _delegate.isTransient();
297     }
298 
299     public void markInitialState()
300     {
301         _delegate.markInitialState();
302     }
303 
304     public void processDecodes(FacesContext context)
305     {
306         _delegate.processDecodes(context);
307     }
308 
309     public void processEvent(ComponentSystemEvent event)
310             throws AbortProcessingException
311     {
312         _delegate.processEvent(event);
313     }
314 
315     public void processRestoreState(FacesContext context, Object state)
316     {
317         _delegate.processRestoreState(context, state);
318     }
319 
320     public Object processSaveState(FacesContext context)
321     {
322         return _delegate.processSaveState(context);
323     }
324 
325     public void processUpdates(FacesContext context)
326     {
327         _delegate.processUpdates(context);
328     }
329 
330     public void processValidators(FacesContext context)
331     {
332         _delegate.processValidators(context);
333     }
334 
335     public void queueEvent(FacesEvent event)
336     {
337         _delegate.queueEvent(event);
338     }
339 
340     public void restoreState(FacesContext context, Object state)
341     {
342         _delegate.restoreState(context, state);
343     }
344 
345     public Object saveState(FacesContext context)
346     {
347         return _delegate.saveState(context);
348     }
349 
350     public void setId(String id)
351     {
352         _delegate.setId(id);
353     }
354 
355     public void setInView(boolean isInView)
356     {
357         _delegate.setInView(isInView);
358     }
359 
360     public void setParent(UIComponent parent)
361     {
362         _delegate.setParent(parent);
363     }
364 
365     public void setRendered(boolean rendered)
366     {
367         _delegate.setRendered(rendered);
368     }
369 
370     public void setRendererType(String rendererType)
371     {
372         _delegate.setRendererType(rendererType);
373     }
374 
375     public void setTransient(boolean newTransientValue)
376     {
377         _delegate.setTransient(newTransientValue);
378     }
379 
380     public void setValueBinding(String name, ValueBinding binding)
381     {
382         _delegate.setValueBinding(name, binding);
383     }
384 
385     public void setValueExpression(String name, ValueExpression expression)
386     {
387         _delegate.setValueExpression(name, expression);
388     }
389 
390     public void subscribeToEvent(Class<? extends SystemEvent> eventClass,
391             ComponentSystemEventListener componentListener)
392     {
393         _delegate.subscribeToEvent(eventClass, componentListener);
394     }
395 
396     public void unsubscribeFromEvent(Class<? extends SystemEvent> eventClass,
397             ComponentSystemEventListener componentListener)
398     {
399         _delegate.unsubscribeFromEvent(eventClass, componentListener);
400     }
401 
402     public boolean visitTree(VisitContext context, VisitCallback callback)
403     {
404         return _delegate.visitTree(context, callback);
405     }
406     
407     // Some methods of UIComponent are protected, but for the scope of this
408     // wrapper are never used, so it is safe to just do nothing or return null.
409 
410     @Override
411     protected FacesContext getFacesContext()
412     {
413         return FacesContext.getCurrentInstance();
414     }
415 
416     @Override
417     protected void addFacesListener(FacesListener listener)
418     {
419     }
420 
421     @Override
422     protected FacesListener[] getFacesListeners(Class clazz)
423     {
424         return null;
425     }
426 
427     @Override
428     protected Renderer getRenderer(FacesContext context)
429     {
430         return null;
431     }
432 
433     @Override
434     protected void removeFacesListener(FacesListener listener)
435     {
436     }
437 }