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  package org.apache.myfaces.shared.renderkit.html;
20  
21  import java.io.IOException;
22  import java.util.List;
23  import java.util.Map;
24  
25  import javax.faces.component.UIComponent;
26  import javax.faces.component.UIInput;
27  import javax.faces.component.UIOutput;
28  import javax.faces.component.behavior.ClientBehavior;
29  import javax.faces.component.behavior.ClientBehaviorHolder;
30  import javax.faces.component.html.HtmlInputTextarea;
31  import javax.faces.context.FacesContext;
32  import javax.faces.context.ResponseWriter;
33  import javax.faces.convert.ConverterException;
34  
35  import org.apache.myfaces.shared.renderkit.RendererUtils;
36  import org.apache.myfaces.shared.renderkit.html.util.JavascriptUtils;
37  import org.apache.myfaces.shared.renderkit.html.util.ResourceUtils;
38  
39  
40  /**
41   * @author Manfred Geiler (latest modification by $Author: lu4242 $)
42   * @author Anton Koinov
43   * @version $Revision: 1230371 $ $Date: 2012-01-11 21:09:45 -0500 (Wed, 11 Jan 2012) $
44   */
45  public class HtmlTextareaRendererBase
46          extends HtmlRenderer
47  {
48      public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
49              throws IOException
50      {
51          RendererUtils.checkParamValidity(facesContext, uiComponent, UIInput.class);
52  
53          Map<String, List<ClientBehavior>> behaviors = null;
54          if (uiComponent instanceof ClientBehaviorHolder)
55          {
56              behaviors = ((ClientBehaviorHolder) uiComponent).getClientBehaviors();
57              if (!behaviors.isEmpty())
58              {
59                  ResourceUtils.renderDefaultJsfJsInlineIfNecessary(facesContext, 
60                          facesContext.getResponseWriter());
61              }
62          }
63          
64          encodeTextArea(facesContext, uiComponent);
65  
66      }
67  
68      protected void encodeTextArea(FacesContext facesContext, UIComponent uiComponent) 
69          throws IOException
70      {
71         //allow subclasses to render custom attributes by separating rendering begin and end
72          renderTextAreaBegin(facesContext, uiComponent);
73          renderTextAreaValue(facesContext, uiComponent);
74          renderTextAreaEnd(facesContext, uiComponent);
75          
76      }
77  
78      //Subclasses can set the value of an attribute before, or can render a custom attribute after calling this method
79      protected void renderTextAreaBegin(FacesContext facesContext,
80              UIComponent uiComponent) throws IOException
81      {
82          ResponseWriter writer = facesContext.getResponseWriter();
83          writer.startElement(HTML.TEXTAREA_ELEM, uiComponent);
84  
85          Map<String, List<ClientBehavior>> behaviors = null;
86          if (uiComponent instanceof ClientBehaviorHolder && JavascriptUtils.isJavascriptAllowed(
87                  facesContext.getExternalContext()))
88          {
89              behaviors = ((ClientBehaviorHolder) uiComponent).getClientBehaviors();
90              if (!behaviors.isEmpty())
91              {
92                  HtmlRendererUtils.writeIdAndName(writer, uiComponent, facesContext);
93              }
94              else
95              {
96                  HtmlRendererUtils.writeIdIfNecessary(writer, uiComponent, facesContext);
97                  writer.writeAttribute(HTML.NAME_ATTR, uiComponent.getClientId(facesContext), null);
98              }
99              long commonPropertiesMarked = 0L;
100             if (isCommonPropertiesOptimizationEnabled(facesContext))
101             {
102                 commonPropertiesMarked = CommonPropertyUtils.getCommonPropertiesMarked(uiComponent);
103             }
104             if (behaviors.isEmpty() && isCommonPropertiesOptimizationEnabled(facesContext))
105             {
106                 CommonPropertyUtils.renderChangeEventProperty(writer, 
107                         commonPropertiesMarked, uiComponent);
108                 CommonPropertyUtils.renderEventProperties(writer, 
109                         commonPropertiesMarked, uiComponent);
110                 CommonPropertyUtils.renderFieldEventPropertiesWithoutOnchange(writer, 
111                         commonPropertiesMarked, uiComponent);
112             }
113             else
114             {
115                 HtmlRendererUtils.renderBehaviorizedOnchangeEventHandler(facesContext, writer, uiComponent, behaviors);
116                 if (isCommonEventsOptimizationEnabled(facesContext))
117                 {
118                     Long commonEventsMarked = CommonEventUtils.getCommonEventsMarked(uiComponent);
119                     CommonEventUtils.renderBehaviorizedEventHandlers(facesContext, writer, 
120                             commonPropertiesMarked, commonEventsMarked, uiComponent, behaviors);
121                     CommonEventUtils.renderBehaviorizedFieldEventHandlersWithoutOnchange(
122                         facesContext, writer, commonPropertiesMarked, commonEventsMarked, uiComponent, behaviors);
123                 }
124                 else
125                 {
126                     HtmlRendererUtils.renderBehaviorizedEventHandlers(facesContext, writer, uiComponent, behaviors);
127                     HtmlRendererUtils.renderBehaviorizedFieldEventHandlersWithoutOnchange(
128                             facesContext, writer, uiComponent, behaviors);
129                 }
130             }
131             if (isCommonPropertiesOptimizationEnabled(facesContext))
132             {
133                 CommonPropertyUtils.renderCommonFieldPassthroughPropertiesWithoutDisabledAndEvents(writer, 
134                         CommonPropertyUtils.getCommonPropertiesMarked(uiComponent), uiComponent);
135                 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.TEXTAREA_ATTRIBUTES);
136             }
137             else
138             {
139                 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, 
140                         HTML.TEXTAREA_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED_AND_EVENTS);
141             }
142         }
143         else
144         {
145             HtmlRendererUtils.writeIdIfNecessary(writer, uiComponent, facesContext);
146             writer.writeAttribute(HTML.NAME_ATTR, uiComponent.getClientId(facesContext), null);
147             if (isCommonPropertiesOptimizationEnabled(facesContext))
148             {
149                 CommonPropertyUtils.renderCommonFieldPassthroughPropertiesWithoutDisabled(writer, 
150                         CommonPropertyUtils.getCommonPropertiesMarked(uiComponent), uiComponent);
151                 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.TEXTAREA_ATTRIBUTES);
152             }
153             else
154             {
155                 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, 
156                         HTML.TEXTAREA_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED);
157             }
158         }
159 
160         if (isDisabled(facesContext, uiComponent))
161         {
162             writer.writeAttribute(org.apache.myfaces.shared.renderkit.html.HTML.DISABLED_ATTR, Boolean.TRUE, null);
163         }
164     }
165 
166     //Subclasses can override the writing of the "text" value of the textarea
167     protected void renderTextAreaValue(FacesContext facesContext, UIComponent uiComponent) throws IOException
168     {
169         ResponseWriter writer = facesContext.getResponseWriter();
170         String strValue = org.apache.myfaces.shared.renderkit.RendererUtils.getStringValue(facesContext, uiComponent);
171         writer.writeText(strValue, org.apache.myfaces.shared.renderkit.JSFAttr.VALUE_ATTR);
172     }
173     
174     protected void renderTextAreaEnd(FacesContext facesContext,
175             UIComponent uiComponent) throws IOException
176     {
177         facesContext.getResponseWriter().endElement(HTML.TEXTAREA_ELEM);
178     }
179     
180     protected boolean isDisabled(FacesContext facesContext, UIComponent uiComponent)
181     {
182         //TODO: overwrite in extended HtmlTextareaRenderer and check for enabledOnUserRole
183         if (uiComponent instanceof HtmlInputTextarea)
184         {
185             return ((HtmlInputTextarea)uiComponent).isDisabled();
186         }
187 
188         return org.apache.myfaces.shared.renderkit.RendererUtils.getBooleanAttribute(
189                 uiComponent, HTML.DISABLED_ATTR, false);
190         
191     }
192 
193     public void decode(FacesContext facesContext, UIComponent component)
194     {
195         RendererUtils.checkParamValidity(facesContext, component, UIInput.class);
196         HtmlRendererUtils.decodeUIInput(facesContext, component);
197         if (component instanceof ClientBehaviorHolder &&
198             !HtmlRendererUtils.isDisabled(component))
199         {
200             HtmlRendererUtils.decodeClientBehaviors(facesContext, component);
201         }
202     }
203 
204     public Object getConvertedValue(FacesContext facesContext, UIComponent uiComponent, Object submittedValue)
205         throws ConverterException
206     {
207         RendererUtils.checkParamValidity(facesContext, uiComponent, UIOutput.class);
208         return org.apache.myfaces.shared.renderkit.RendererUtils.getConvertedUIOutputValue(facesContext,
209                                                        (UIOutput)uiComponent,
210                                                        submittedValue);
211     }
212 
213 }