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.custom.ppr;
20  
21  import org.apache.myfaces.component.html.ext.UIComponentPerspective;
22  import org.apache.myfaces.shared_tomahawk.component.ExecuteOnCallback;
23  
24  import javax.faces.component.UIComponent;
25  import javax.faces.component.UIViewRoot;
26  import javax.faces.context.FacesContext;
27  import javax.faces.el.ValueBinding;
28  import javax.faces.event.AbortProcessingException;
29  import javax.faces.event.FacesEvent;
30  import java.io.IOException;
31  import java.util.Iterator;
32  import java.util.List;
33  import java.util.Locale;
34  import java.util.Map;
35  
36  /**
37   * UIViewRoot wrapper which will process only those components configured using {@link PPRSubmit}.
38   * If this configuration is missing the default process will take place.
39   */
40  public class PPRViewRootWrapper extends UIViewRoot
41  {
42      private final UIViewRoot delegateViewRoot;
43  
44      public PPRViewRootWrapper(UIViewRoot delegateViewRoot)
45      {
46          this.delegateViewRoot = delegateViewRoot;
47      }
48  
49      public boolean getRendersChildren()
50      {
51          return true;
52      }
53  
54      public List getChildren()
55      {
56          return delegateViewRoot.getChildren();
57      }
58  
59      public int getChildCount()
60      {
61          return delegateViewRoot.getChildCount();
62      }
63  
64  
65      public String getViewId()
66      {
67          return delegateViewRoot.getViewId();
68      }
69  
70      public void setViewId(String viewId)
71      {
72          delegateViewRoot.setViewId(viewId);
73      }
74  
75      public void queueEvent(FacesEvent event)
76      {
77          delegateViewRoot.queueEvent(event);
78      }
79  
80      public void processDecodes(FacesContext context)
81      {
82          delegateViewRoot.processDecodes(context);
83      }
84  
85      public void processValidators(FacesContext context)
86      {
87          Map requestMap = context.getExternalContext().getRequestMap();
88          List allProcessComponents = (List) requestMap.get(PPRSupport.PROCESS_COMPONENTS);
89          if (allProcessComponents != null)
90          {
91              invokeOnComponents(context, allProcessComponents, new ContextCallback()
92              {
93                  public void invokeContextCallback(FacesContext context, UIComponent target)
94                  {
95                      target.processValidators(context);
96                  }
97              });
98          }
99          else
100         {
101             delegateViewRoot.processValidators(context);
102         }
103     }
104 
105     private void invokeOnComponents(FacesContext context, List componentClientIds, final ContextCallback contextCallback)
106     {
107         Iterator iterComponents = componentClientIds.iterator();
108         while (iterComponents.hasNext())
109         {
110             String componentId = (String) iterComponents.next();
111 
112             UIComponent component = context.getViewRoot().findComponent(componentId);
113             if (component instanceof UIComponentPerspective)
114             {
115                 UIComponentPerspective uiComponentPerspective = (UIComponentPerspective) component;
116                 ExecuteOnCallback getComponentCallback = new ExecuteOnCallback()
117                 {
118                     public Object execute(FacesContext context, UIComponent component)
119                     {
120                         contextCallback.invokeContextCallback(context, component);
121                         return null;
122                     }
123                 };
124                 uiComponentPerspective.executeOn(context, getComponentCallback);
125             }
126             else
127             {
128                 contextCallback.invokeContextCallback(context, component);
129             }
130         }
131     }
132 
133     public void processUpdates(FacesContext context)
134     {
135         Map requestMap = context.getExternalContext().getRequestMap();
136         List allProcessComponents = (List) requestMap.get(PPRSupport.PROCESS_COMPONENTS);
137         if (allProcessComponents != null)
138         {
139             invokeOnComponents(context, allProcessComponents, new ContextCallback()
140             {
141                 public void invokeContextCallback(FacesContext context, UIComponent target)
142                 {
143                     target.processUpdates(context);
144                 }
145             });
146         }
147         else
148         {
149             delegateViewRoot.processUpdates(context);
150         }
151     }
152 
153     public void processApplication(FacesContext context)
154     {
155         delegateViewRoot.processApplication(context);
156     }
157 
158     public void encodeBegin(FacesContext context)
159         throws java.io.IOException
160     {
161         delegateViewRoot.encodeBegin(context);
162     }
163 
164     /* Provides a unique id for this component instance.
165     */
166     public String createUniqueId()
167     {
168         return delegateViewRoot.createUniqueId();
169     }
170 
171     public Locale getLocale()
172     {
173         return delegateViewRoot.getLocale();
174     }
175 
176 
177     public void setLocale(Locale locale)
178     {
179         delegateViewRoot.setLocale(locale);
180     }
181 
182     public static final String COMPONENT_TYPE = "javax.faces.ViewRoot";
183     public static final String COMPONENT_FAMILY = "javax.faces.ViewRoot";
184 
185     public String getFamily()
186     {
187         return COMPONENT_FAMILY;
188     }
189 
190 
191     public void setRenderKitId(String renderKitId)
192     {
193         delegateViewRoot.setRenderKitId(renderKitId);
194     }
195 
196     public String getRenderKitId()
197     {
198         return delegateViewRoot.getRenderKitId();
199     }
200 
201 
202     public Object saveState(FacesContext context)
203     {
204         return delegateViewRoot.saveState(context);
205     }
206 
207     public void restoreState(FacesContext context, Object state)
208     {
209         delegateViewRoot.restoreState(context, state);
210     }
211 
212     public Map getAttributes()
213     {
214         return delegateViewRoot.getAttributes();
215     }
216 
217     public ValueBinding getValueBinding(String name)
218     {
219         return delegateViewRoot.getValueBinding(name);
220     }
221 
222     public void setValueBinding(String name, ValueBinding binding)
223     {
224         delegateViewRoot.setValueBinding(name, binding);
225     }
226 
227     public String getClientId(FacesContext context)
228     {
229         return delegateViewRoot.getClientId(context);
230     }
231 
232     public String getId()
233     {
234         return delegateViewRoot.getId();
235     }
236 
237     public void setId(String id)
238     {
239         delegateViewRoot.setId(id);
240     }
241 
242     public UIComponent getParent()
243     {
244         return delegateViewRoot.getParent();
245     }
246 
247     public void setParent(UIComponent parent)
248     {
249         delegateViewRoot.setParent(parent);
250     }
251 
252     public UIComponent findComponent(String expr)
253     {
254         return delegateViewRoot.findComponent(expr);
255     }
256 
257     public Map getFacets()
258     {
259         return delegateViewRoot.getFacets();
260     }
261 
262     public UIComponent getFacet(String name)
263     {
264         return delegateViewRoot.getFacet(name);
265     }
266 
267     public Iterator getFacetsAndChildren()
268     {
269         return delegateViewRoot.getFacetsAndChildren();
270     }
271 
272     public void broadcast(FacesEvent event)
273         throws AbortProcessingException
274     {
275         delegateViewRoot.broadcast(event);
276     }
277 
278     public void decode(FacesContext context)
279     {
280         delegateViewRoot.decode(context);
281     }
282 
283     public void encodeChildren(FacesContext context)
284         throws IOException
285     {
286         delegateViewRoot.encodeChildren(context);
287     }
288 
289     public void encodeEnd(FacesContext context)
290         throws IOException
291     {
292         delegateViewRoot.encodeEnd(context);
293     }
294 
295     public Object processSaveState(FacesContext context)
296     {
297         return delegateViewRoot.processSaveState(context);
298     }
299 
300     public void processRestoreState(FacesContext context, Object state)
301     {
302         delegateViewRoot.processRestoreState(context, state);
303     }
304 
305     public boolean isTransient()
306     {
307         return delegateViewRoot.isTransient();
308     }
309 
310     public void setTransient(boolean transientFlag)
311     {
312         delegateViewRoot.setTransient(transientFlag);
313     }
314 
315     public void setRendered(boolean rendered)
316     {
317         delegateViewRoot.setRendered(rendered);
318     }
319 
320     public boolean isRendered()
321     {
322         return delegateViewRoot.isRendered();
323     }
324 
325     public void setRendererType(String rendererType)
326     {
327         if (delegateViewRoot == null)
328         {
329             // the MyFaces 1.2 implmemenation of UIViewRoot calls setRendererType(null) from within the
330             // constructor. This breaks the decorator pattern as delegateViewRoot has not been called yet.
331             // Since every instance of UIViewRoot will issue this call it doesn't matter if we just
332             // discard this request.
333             return;
334         }
335 
336         delegateViewRoot.setRendererType(rendererType);
337     }
338 
339     public String getRendererType()
340     {
341         return delegateViewRoot.getRendererType();
342     }
343 
344 }