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  
20  package org.apache.myfaces.tobago.util;
21  
22  
23  import org.apache.myfaces.tobago.component.Attributes;
24  import org.apache.myfaces.tobago.component.Facets;
25  import org.apache.myfaces.tobago.internal.layout.LayoutContext;
26  import org.apache.myfaces.tobago.layout.LayoutContainer;
27  import org.apache.myfaces.tobago.layout.Measure;
28  import org.apache.myfaces.tobago.renderkit.RendererBase;
29  
30  import javax.faces.FacesException;
31  import javax.faces.component.UIComponent;
32  import javax.faces.context.FacesContext;
33  import javax.faces.event.PhaseId;
34  import java.io.IOException;
35  import java.util.Iterator;
36  
37  public class EncodeAjaxCallback implements TobagoCallback {
38  
39    public void invokeContextCallback(FacesContext facesContext, UIComponent component) {
40      try {
41         UIComponent reload = component.getFacet(Facets.RELOAD);
42         if (reload != null && reload.isRendered()) {
43           Boolean immediate = (Boolean) reload.getAttributes().get(Attributes.IMMEDIATE);
44           if (immediate != null && !immediate) {
45             Boolean update = (Boolean) reload.getAttributes().get(Attributes.UPDATE);
46             if (update != null && !update) {
47               return;
48             }
49           }
50        }
51        prepareRendererAll(facesContext, component);
52        if (component instanceof LayoutContainer) {
53          LayoutContainer layoutContainer = (LayoutContainer) component;
54          Measure width = layoutContainer.getCurrentWidth();
55          Measure height = layoutContainer.getCurrentHeight();
56          Measure oldWidth = layoutContainer.getWidth();
57          Measure oldHeight = layoutContainer.getHeight();
58          layoutContainer.setWidth(width);
59          layoutContainer.setHeight(height);
60          new LayoutContext(layoutContainer).layout();
61          layoutContainer.setWidth(oldWidth);
62          layoutContainer.setHeight(oldHeight);
63        }
64        encodeAll(facesContext, component);
65      } catch (IOException e) {
66        throw new FacesException(e);
67      }
68    }
69    
70    public PhaseId getPhaseId() {
71        return PhaseId.RENDER_RESPONSE;
72    }
73    
74  
75    // TODO replace with component.encodeAll after removing jsf 1.1 support
76    public static void encodeAll(FacesContext facesContext, UIComponent component) throws IOException {
77       if (component.isRendered()) {
78        component.encodeBegin(facesContext);
79        if (component.getRendersChildren()) {
80          component.encodeChildren(facesContext);
81        } else {
82          for (UIComponent child : component.getChildren()) {
83            encodeAll(facesContext, child);
84          }
85        }
86        component.encodeEnd(facesContext);
87      }
88    }
89  
90    // TODO merge with RenderUtils.prepareRendererAll
91    public static void prepareRendererAll(FacesContext facesContext, UIComponent component) throws IOException {
92      if (!component.isRendered()) {
93        return;
94      }
95      RendererBase renderer = ComponentUtils.getRenderer(facesContext,  component);
96      boolean prepareRendersChildren = false;
97      if (renderer != null) {
98        renderer.prepareRender(facesContext, component);
99        prepareRendersChildren = renderer.getPrepareRendersChildren();
100     }
101     if (prepareRendersChildren) {
102       renderer.prepareRendersChildren(facesContext, component);
103     } else {
104       Iterator it = component.getFacetsAndChildren();
105       while (it.hasNext()) {
106         UIComponent child = (UIComponent) it.next();
107         prepareRendererAll(facesContext, child);
108       }
109     }
110   }
111 }