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.util.EncodeUtils;
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  
36  public class EncodeAjaxCallback implements TobagoCallback {
37  
38    public void invokeContextCallback(final FacesContext facesContext, final UIComponent component) {
39      try {
40         final UIComponent reload = component.getFacet(Facets.RELOAD);
41         if (reload != null && reload.isRendered()) {
42           final Boolean immediate = (Boolean) reload.getAttributes().get(Attributes.IMMEDIATE);
43           if (immediate != null && !immediate) {
44             final Boolean update = (Boolean) reload.getAttributes().get(Attributes.UPDATE);
45             if (update != null && !update) {
46               return;
47             }
48           }
49        }
50        EncodeUtils.prepareRendererAll(facesContext, component);
51        if (component instanceof LayoutContainer) {
52          final LayoutContainer layoutContainer = (LayoutContainer) component;
53          final Measure width = layoutContainer.getCurrentWidth();
54          final Measure height = layoutContainer.getCurrentHeight();
55          final Measure oldWidth = layoutContainer.getWidth();
56          final Measure oldHeight = layoutContainer.getHeight();
57          layoutContainer.setWidth(width);
58          layoutContainer.setHeight(height);
59          new LayoutContext(layoutContainer).layout();
60          layoutContainer.setWidth(oldWidth);
61          layoutContainer.setHeight(oldHeight);
62        }
63        component.encodeAll(facesContext);
64      } catch (final IOException e) {
65        throw new FacesException(e);
66      }
67    }
68    
69    public PhaseId getPhaseId() {
70        return PhaseId.RENDER_RESPONSE;
71    }
72    
73    /**
74     * @deprecated since 2.0.0, please use component.encodeAll()
75     */
76    @Deprecated
77    public static void encodeAll(final FacesContext facesContext, final UIComponent component) throws IOException {
78      component.encodeAll(facesContext);
79    }
80  
81    /**
82     * @deprecated since 2.0.0, please use EncodeUtils.prepareRendererAll()
83     */
84    @Deprecated
85    public static void prepareRendererAll(final FacesContext facesContext, final UIComponent component)
86        throws IOException {
87      EncodeUtils.prepareRendererAll(facesContext, component);
88    }
89  }