1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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 }