KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ajaxanywhere > jsf > ZoneUIComponent


1 package org.ajaxanywhere.jsf;
2
3 import org.ajaxanywhere.AAUtils;
4
5 import javax.faces.component.UIComponent;
6 import javax.faces.component.UIComponentBase;
7 import javax.faces.component.UIViewRoot;
8 import javax.faces.context.FacesContext;
9 import javax.faces.context.ResponseWriter;
10 import javax.faces.el.ValueBinding;
11 import java.io.IOException JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Map JavaDoc;
14
15
16 public class ZoneUIComponent extends UIComponentBase {
17     private static final String JavaDoc COMPONENT_FAMILY = "AjaxAnywhereFamily";
18
19     public ZoneUIComponent() {
20         setTransient(false);
21     }
22
23     public String JavaDoc getFamily() {
24         return COMPONENT_FAMILY;
25     }
26     
27     public void encodeBegin(FacesContext context)
28             throws IOException JavaDoc {
29         if (context == null) throw new IllegalArgumentException JavaDoc("context is null");
30         if (!isRendered()) return;
31         if (getId() == null)
32             throw new IllegalArgumentException JavaDoc("name attribute is null");
33
34         if (canSkipRendering(this, context))
35             return;
36
37         ResponseWriter writer = context.getResponseWriter();
38         writer.write(AAUtils.getZoneStartDelimiter(getCompositeId()));
39
40     }
41
42     private String JavaDoc getCompositeId() {
43         ValueBinding vb = getValueBinding("idSuffix");
44         String JavaDoc compId = vb == null ? getId() : getId()+vb.getValue(getFacesContext());
45         return compId;
46     }
47
48     public boolean getRendersChildren() {
49         return true;
50     }
51
52     public void encodeChildren(FacesContext context)
53             throws IOException JavaDoc {
54         if (context == null) throw new IllegalArgumentException JavaDoc("context is null");
55         if (canSkipRendering(this, context))
56             return;
57
58          renderChildren(context, this);
59     }
60
61     public void encodeEnd(FacesContext context)
62             throws IOException JavaDoc {
63         if (context == null) throw new IllegalArgumentException JavaDoc("context is null");
64         if (!isRendered()) return;
65         if (canSkipRendering(this, context))
66             return;
67
68         ResponseWriter writer = context.getResponseWriter();
69         writer.write(AAUtils.getZoneEndDelimiter(getCompositeId()));
70     }
71
72
73     private static void renderChildren(FacesContext facesContext, UIComponent component)
74             throws IOException JavaDoc {
75         if (component.getChildCount() > 0) {
76             for (Iterator JavaDoc it = component.getChildren().iterator(); it.hasNext();) {
77                 UIComponent child = (UIComponent) it.next();
78                 renderComponent(facesContext, child);
79             }
80         }
81     }
82
83
84     private static void renderComponent(FacesContext facesContext, UIComponent child)
85             throws IOException JavaDoc {
86
87         if (!child.isRendered()) {
88             return;
89         }
90
91         child.encodeBegin(facesContext);
92         if (child.getRendersChildren()) {
93             child.encodeChildren(facesContext);
94         } else {
95             renderChildren(facesContext, child);
96         }
97         child.encodeEnd(facesContext);
98     }
99
100     private static boolean canSkipRendering(UIComponent component, FacesContext context) {
101         return noIncludedSubZones(component, context) && notInsideIncludedZone(component, context);
102     }
103     /**
104      * checks if this zones is surrently inside another included zone
105      */

106     private static boolean notInsideIncludedZone(UIComponent component, FacesContext context) {
107         Map JavaDoc requestMap = context.getExternalContext().getRequestMap();
108         UIComponent parent = component.getParent();
109         while (parent != null && ! (parent instanceof UIViewRoot)) {
110             if (isRefreshZone(parent, requestMap))
111                 return false;
112             parent = parent.getParent();
113         }
114         return true;
115     }
116
117     /**
118      * checks if there are any sun-zones to include
119      */

120     private static boolean noIncludedSubZones(UIComponent component, FacesContext context) {
121         Map JavaDoc requestMap = context.getExternalContext().getRequestMap();
122         Map JavaDoc requestParameterMap = context.getExternalContext().getRequestParameterMap();
123         if (! AAUtils.isAjaxRequest(requestParameterMap) )
124             return false;
125         if (isRefreshZone(component, requestMap))
126             return false;
127         else {
128             for (Iterator JavaDoc iterator = component.getChildren().iterator(); iterator.hasNext();) {
129                 UIComponent child = (UIComponent) iterator.next();
130                 if (!canSkipRendering(child, context))
131                     return false;
132             }
133             return true;
134         }
135     }
136
137     private static boolean isRefreshZone(UIComponent component, Map JavaDoc requestMap)
138     {
139         if (component instanceof ZoneUIComponent==false)
140             return false;
141         
142         ZoneUIComponent c = (ZoneUIComponent)component;
143         for (Iterator JavaDoc it = AAUtils.getZonesToRefresh(requestMap).iterator();it.hasNext();)
144         {
145             String JavaDoc zoneName = (String JavaDoc)it.next();
146             if (c.getValueBinding("idSuffix")!=null)
147             {
148                 if (zoneName.startsWith(c.getId()))
149                     return true;
150             }
151             else
152             {
153                 if (zoneName.equals(c.getId()))
154                     return true;
155             }
156         }
157         return false;
158     }
159 }
160
Popular Tags