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: 1634163 $ $Date: 2014-10-24 23:42:05 +0000 (Fri, 24 Oct 2014) $
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         if (strValue != null)
172         {
173             writer.writeText(strValue, org.apache.myfaces.shared.renderkit.JSFAttr.VALUE_ATTR);
174         }
175     }
176     
177     protected void renderTextAreaEnd(FacesContext facesContext,
178             UIComponent uiComponent) throws IOException
179     {
180         facesContext.getResponseWriter().endElement(HTML.TEXTAREA_ELEM);
181     }
182     
183     protected boolean isDisabled(FacesContext facesContext, UIComponent uiComponent)
184     {
185         //TODO: overwrite in extended HtmlTextareaRenderer and check for enabledOnUserRole
186         if (uiComponent instanceof HtmlInputTextarea)
187         {
188             return ((HtmlInputTextarea)uiComponent).isDisabled();
189         }
190 
191         return org.apache.myfaces.shared.renderkit.RendererUtils.getBooleanAttribute(
192                 uiComponent, HTML.DISABLED_ATTR, false);
193         
194     }
195 
196     public void decode(FacesContext facesContext, UIComponent component)
197     {
198         RendererUtils.checkParamValidity(facesContext, component, UIInput.class);
199         HtmlRendererUtils.decodeUIInput(facesContext, component);
200         if (component instanceof ClientBehaviorHolder &&
201             !HtmlRendererUtils.isDisabled(component))
202         {
203             HtmlRendererUtils.decodeClientBehaviors(facesContext, component);
204         }
205     }
206 
207     public Object getConvertedValue(FacesContext facesContext, UIComponent uiComponent, Object submittedValue)
208         throws ConverterException
209     {
210         RendererUtils.checkParamValidity(facesContext, uiComponent, UIOutput.class);
211         return org.apache.myfaces.shared.renderkit.RendererUtils.getConvertedUIOutputValue(facesContext,
212                                                        (UIOutput)uiComponent,
213                                                        submittedValue);
214     }
215 
216 }