View Javadoc

1   /*
2    * $Id: AbstractRequestContext.java,v 1.18 2011/06/16 09:29:41 jbmeslin Exp $
3    * 
4    */
5   package org.rcfaces.core.internal.renderkit;
6   
7   import java.util.HashMap;
8   import java.util.Map;
9   
10  import javax.faces.component.UIComponent;
11  import javax.faces.component.UIViewRoot;
12  import javax.faces.context.FacesContext;
13  import javax.faces.render.Renderer;
14  
15  import org.rcfaces.core.internal.AbstractReleasable;
16  import org.rcfaces.core.internal.Constants;
17  
18  /**
19   * @author Olivier Oeuillot (latest modification by $Author: jbmeslin $)
20   * @version $Revision: 1.18 $ $Date: 2011/06/16 09:29:41 $
21   */
22  public abstract class AbstractRequestContext extends AbstractReleasable
23          implements IRequestContext {
24      private static final String REVISION = "$Revision: 1.18 $";
25  
26      protected static final String EMPTY_PROPERTIES[] = new String[0];
27  
28      private static final String LOCKED_CLIENT_ATTRIBUTES_PROPERTY = "org.rcfaces.core.LOCKED_CLIENT_ATTRIBUTES";
29  
30      private static final String LOCKED_CLIENT_ATTRIBUTES_ENABLED = LOCKED_CLIENT_ATTRIBUTES_PROPERTY;
31  
32      private final Map componentDatas = new HashMap(32);
33  
34      private IComponentData emptyComponentData;
35  
36      private FacesContext facesContext;
37  
38      private boolean lockedClientAttributes = Constants.LOCKED_CLIENT_ATTRIBUTES_DEFAULT_VALUE;
39  
40      public void setFacesContext(FacesContext facesContext) {
41          this.facesContext = facesContext;
42          this.componentDatas.clear(); // On ne sait jamais ....
43  
44          boolean configFound = false;
45  
46          UIViewRoot viewRoot = facesContext.getViewRoot();
47          if (viewRoot != null) {
48              Boolean enabled = (Boolean) viewRoot.getAttributes().get(
49                      LOCKED_CLIENT_ATTRIBUTES_ENABLED);
50              if (enabled != null) {
51                  lockedClientAttributes = enabled.booleanValue();
52                  configFound = true;
53              }
54          }
55  
56          if (configFound == false) {
57              String enable = facesContext.getExternalContext().getInitParameter(
58                      LOCKED_CLIENT_ATTRIBUTES_PROPERTY);
59              if (enable != null) {
60                  lockedClientAttributes = Boolean.valueOf(enable).booleanValue();
61                  configFound = true;
62              }
63          }
64      }
65  
66      /*
67       * (non-Javadoc)
68       * 
69       * @see
70       * org.rcfaces.core.internal.renderkit.IRequestContext#getFacesContext()
71       */
72      public final FacesContext getFacesContext() {
73          return facesContext;
74      }
75  
76      protected final void putComponentData(String key, Object data) {
77          componentDatas.put(key, data);
78      }
79  
80      public boolean isLockedClientAttributes() {
81          return lockedClientAttributes;
82      }
83  
84      public static void setLockedAttributes(FacesContext facesContext,
85              boolean lock) {
86  
87          facesContext.getViewRoot().getAttributes().put(
88                  LOCKED_CLIENT_ATTRIBUTES_ENABLED, Boolean.valueOf(lock));
89      }
90  
91      /*
92       * (non-Javadoc)
93       * 
94       * @see
95       * org.rcfaces.core.internal.renderkit.IRequestContext#getComponentData(
96       * javax.faces.component.UIComponent)
97       */
98      public final IComponentData getComponentData(UIComponent component,
99              String componentId, Renderer renderer) {
100         Object data = componentDatas.get(componentId);
101 
102         if (data == null) {
103             return emptyComponentData();
104         }
105 
106         if (data instanceof IComponentData) {
107             return (IComponentData) data;
108         }
109 
110         data = getComponentData(component, componentId, data, renderer);
111         if (data == null) {
112             return emptyComponentData();
113         }
114 
115         componentDatas.put(componentId, data);
116 
117         return (IComponentData) data;
118     }
119 
120     protected IComponentData getComponentData(UIComponent component,
121             String key, Object data, Renderer renderer) {
122         return emptyComponentData();
123     }
124 
125     protected IComponentData emptyComponentData() {
126         if (emptyComponentData != null) {
127             return emptyComponentData;
128         }
129 
130         emptyComponentData = createEmptyComponentData();
131 
132         return emptyComponentData;
133     }
134 
135     protected abstract IComponentData createEmptyComponentData();
136 
137     protected final String getKey(UIComponent component) {
138         return component.getClientId(facesContext);
139     }
140 
141     /**
142      * 
143      * @author Olivier Oeuillot (latest modification by $Author: jbmeslin $)
144      * @version $Revision: 1.18 $ $Date: 2011/06/16 09:29:41 $
145      */
146     protected static abstract class AbstractComponentData extends
147             AbstractProperties implements IComponentData {
148         private static final String REVISION = "$Revision: 1.18 $";
149     }
150 
151     public String getComponentId(UIComponent component) {
152         return component.getClientId(getFacesContext());
153     }
154 
155     public void release() {
156         componentDatas.clear();
157 
158         super.release();
159     }
160 
161     /*
162      * public void pushScopeVar(String var, ValueExpression valueBinding) {
163      * Object value = valueBinding.getValue(getFacesContext());
164      * 
165      * facesContext.getExternalContext().getRequestMap().put(var, value); }
166      */
167 
168 }