View Javadoc

1   /*
2    * $Id: AbstractCameliaRenderer0.java,v 1.5 2010/06/15 08:02:10 oeuillot Exp $
3    */
4   package org.rcfaces.core.internal.renderkit;
5   
6   import java.io.IOException;
7   import java.util.HashSet;
8   import java.util.Iterator;
9   import java.util.Set;
10  
11  import javax.faces.component.UIComponent;
12  import javax.faces.component.ValueHolder;
13  import javax.faces.context.FacesContext;
14  import javax.faces.convert.ConverterException;
15  import javax.faces.render.RenderKit;
16  import javax.faces.render.Renderer;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  import org.rcfaces.core.component.capability.IPartialRenderingCapability;
21  import org.rcfaces.core.internal.RcfacesContext;
22  import org.rcfaces.core.internal.tools.AsyncModeTools;
23  import org.rcfaces.core.internal.tools.ValuesTools;
24  
25  /**
26   * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
27   * @version $Revision: 1.5 $ $Date: 2010/06/15 08:02:10 $
28   */
29  public abstract class AbstractCameliaRenderer0 extends Renderer implements
30          IDefaultUnlockedPropertiesRenderer {
31      private static final String REVISION = "$Revision: 1.5 $";
32  
33      private static final Log LOG = LogFactory
34              .getLog(AbstractCameliaRenderer0.class);
35  
36      private static final String HIDE_CHILDREN_PROPERTY = "camelia.ASYNC_TREE_MODE";
37  
38      private static final String COMPONENT_HIDDEN = "camelia.COMPONENT_HIDDEN";
39  
40      private static final String[] EMPTY_STRING_ARRAY = new String[0];
41  
42      private final String defaultUnlockedProperties[];
43  
44      protected AbstractCameliaRenderer0() {
45          Set unlockedProperties = new HashSet();
46          addUnlockProperties(unlockedProperties);
47  
48          if (unlockedProperties.isEmpty() == false) {
49              defaultUnlockedProperties = (String[]) unlockedProperties
50                      .toArray(new String[unlockedProperties.size()]);
51  
52          } else {
53              defaultUnlockedProperties = EMPTY_STRING_ARRAY;
54          }
55      }
56  
57      protected void addUnlockProperties(Set unlockedProperties) {
58      }
59  
60      public String[] getDefaultUnlockedProperties(FacesContext facesContext,
61              UIComponent component) {
62          return defaultUnlockedProperties;
63      }
64  
65      public final void encodeBegin(FacesContext context, UIComponent component)
66              throws IOException {
67  
68          super.encodeBegin(context, component);
69  
70          IRenderContext renderContext = getRenderContext(context);
71  
72          String clientId = renderContext.getComponentClientId(component);
73  
74          renderContext.pushComponent(component, clientId);
75  
76          IComponentWriter writer = renderContext.getComponentWriter();
77  
78          /*
79           * if (component instanceof IVisibilityCapability) {
80           * IComponentRenderContext componentRenderContext = writer
81           * .getComponentRenderContext();
82           * 
83           * if (componentRenderContext.containsAttribute(COMPONENT_HIDDEN) ==
84           * false) { if (Boolean.FALSE.equals(((IVisibilityCapability) component)
85           * .getVisibleState())) {
86           * 
87           * // Visibilité PHANTOM
88           * 
89           * componentRenderContext.setAttribute(COMPONENT_HIDDEN, component); } }
90           * }
91           */
92  
93          try {
94              encodeBegin(writer);
95  
96          } catch (RuntimeException e) {
97              LOG.error("Encode begin of component '" + clientId
98                      + "' throws an exception.", e);
99  
100             throw new WriterException(null, e, component);
101         }
102 
103         if (writer instanceof ISgmlWriter) {
104             ((ISgmlWriter) writer).endComponent();
105         }
106     }
107 
108     protected abstract void encodeBegin(IComponentWriter writer)
109             throws WriterException;
110 
111     protected abstract IRenderContext getRenderContext(FacesContext context);
112 
113     protected void hideChildren(IComponentRenderContext componentRenderContext) {
114         componentRenderContext.setAttribute(HIDE_CHILDREN_PROPERTY,
115                 Boolean.TRUE);
116     }
117 
118     public void encodeChildren(FacesContext facesContext, UIComponent component)
119             throws IOException {
120         if ((this instanceof IAsyncRenderer) == false) {
121             super.encodeChildren(facesContext, component);
122             return;
123         }
124 
125         IRenderContext renderContext = getRenderContext(facesContext);
126 
127         IComponentWriter componentWriter = renderContext.getComponentWriter();
128 
129         IComponentRenderContext componentRenderContext = componentWriter
130                 .getComponentRenderContext();
131 
132         if (componentRenderContext.containsAttribute(HIDE_CHILDREN_PROPERTY)) {
133             return;
134         }
135 
136         super.encodeChildren(facesContext, component);
137     }
138 
139     public boolean getRendersChildren() {
140         if ((this instanceof IAsyncRenderer) == false) {
141             return false;
142         }
143 
144         if (RcfacesContext.isJSF1_2()) {
145             // En jsf 1.2 nous sommes forcement en parcours d'arbre
146             return true;
147         }
148 
149         // Jsf 1.1 : on doit distinguer d'un parcours par TAG ou par
150         // programmation
151 
152         if (AsyncModeTools.isTagProcessor(null)) {
153             // Nous sommes en mode TAG, c'est le tag qui détourne le flux.
154             // NON => pas forcement, pas en mode tree !
155             return false;
156         }
157 
158         // Nous sommes en mode de rendu par parcours d'arbre ...
159         return true;
160     }
161 
162     public void encodeEnd(FacesContext context, UIComponent component)
163             throws IOException {
164 
165         IRenderContext renderContext = getRenderContext(context);
166 
167         IComponentWriter writer = renderContext.getComponentWriter();
168 
169         if (writer instanceof ISgmlWriter) {
170             ((ISgmlWriter) writer).endComponent();
171         }
172 
173         renderContext.encodeEnd(writer);
174 
175         try {
176             encodeEnd(writer);
177 
178         } catch (RuntimeException e) {
179 
180             String clientId = renderContext.getComponentClientId(component);
181 
182             LOG.error("Encode end of component '" + clientId
183                     + "' throws an exception.", e);
184 
185             throw new WriterException(null, e, component);
186         }
187 
188         super.encodeEnd(context, component);
189 
190         if (writer instanceof ISgmlWriter) {
191             ((ISgmlWriter) writer).endComponent();
192         }
193 
194         /*
195          * if (component instanceof IVisibilityCapability) {
196          * IComponentRenderContext componentRenderContext = writer
197          * .getComponentRenderContext();
198          * 
199          * if (componentRenderContext.getAttribute(COMPONENT_HIDDEN) ==
200          * component) {
201          * componentRenderContext.removeAttribute(COMPONENT_HIDDEN); } }
202          */
203 
204         if (component instanceof IPartialRenderingCapability) {
205             IPartialRenderingCapability partialRenderingCapability = (IPartialRenderingCapability) component;
206 
207             if (partialRenderingCapability.isPartialRendering()) {
208                 
209             }
210         }
211 
212         renderContext.popComponent(component);
213     }
214 
215     protected void encodeEnd(IComponentWriter writer) throws WriterException {
216     }
217 
218     protected abstract IRequestContext getRequestContext(FacesContext context);
219 
220     public final void decode(FacesContext context, UIComponent component) {
221 
222         IRequestContext requestContext = getRequestContext(context);
223 
224         String requestComponentId = getRequestComponentId(requestContext,
225                 component);
226 
227         IComponentData componentData = requestContext.getComponentData(
228                 component, requestComponentId, this);
229 
230         decode(requestContext, component, componentData);
231     }
232 
233     protected String getRequestComponentId(IRequestContext requestContext,
234             UIComponent component) {
235         return requestContext.getComponentId(component);
236     }
237 
238     protected void decode(IRequestContext context, UIComponent component,
239             IComponentData componentData) {
240     }
241 
242     protected void decodeEvent(IRequestContext context, UIComponent component,
243             IEventData eventData) {
244     }
245 
246     public void decodeChildren(FacesContext context, UIComponent component) {
247         for (Iterator children = component.getFacetsAndChildren(); children
248                 .hasNext();) {
249             UIComponent child = (UIComponent) children.next();
250 
251             decodeChild(context, component, child);
252         }
253     }
254 
255     public void decodeChild(FacesContext context, UIComponent parent,
256             UIComponent child) {
257         child.processDecodes(context);
258     }
259 
260     public void decodeEnd(FacesContext context, UIComponent component) {
261     }
262 
263     public boolean getDecodesChildren() {
264         return false;
265     }
266 
267     protected String convertValue(FacesContext facesContext,
268             ValueHolder valueHolder) {
269         return ValuesTools.valueToString(valueHolder, facesContext);
270     }
271 
272     protected String convertValue(FacesContext facesContext,
273             UIComponent component, Object value) {
274         return ValuesTools.valueToString(value, component, facesContext);
275     }
276 
277     public Object getConvertedValue(FacesContext context,
278             UIComponent component, Object submittedValue)
279             throws ConverterException {
280 
281         return ValuesTools.convertStringToValue(context, component,
282                 submittedValue, true);
283     }
284 
285     protected static final Renderer getRenderer(FacesContext facesContext,
286             UIComponent component) {
287         String rendererType = component.getRendererType();
288         if (rendererType == null) {
289             LOG.error("Invalid renderType for component id="
290                     + component.getId() + " component=" + component);
291             return null;
292         }
293 
294         RenderKit renderKit = facesContext.getRenderKit();
295 
296         if (renderKit == null) {
297             LOG.error("No renderKit associated to renderKitId='"
298                     + facesContext.getViewRoot().getRenderKitId() + "'.");
299 
300             return null;
301         }
302 
303         Renderer renderer = renderKit.getRenderer(component.getFamily(),
304                 rendererType);
305 
306         if (LOG.isDebugEnabled()) {
307             LOG.debug("getRenderer(id='" + component.getId() + " family='"
308                     + component.getFamily() + "' rendererType='" + rendererType
309                     + "' class='" + component.getClass().getName()
310                     + "') for renderKitId='"
311                     + facesContext.getViewRoot().getRenderKitId() + "' => "
312                     + renderer);
313         }
314 
315         return renderer;
316     }
317 
318     protected Object getValue(UIComponent component) {
319         return ValuesTools.getValue(component);
320     }
321 
322     protected boolean isComponentVisible(
323             IComponentRenderContext componentRenderContext) {
324         return componentRenderContext.containsAttribute(COMPONENT_HIDDEN) == false;
325     }
326 }