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: 1634162 $ $Date: 2014-10-24 23:41:47 +0000 (Fri, 24 Oct 2014) $
44   */
45  public class HtmlTextareaRendererBase
46          extends HtmlRenderer
47  {
48      private static final String ADD_NEW_LINE_AT_START_ATTR = "org.apache.myfaces.addNewLineAtStart";
49      
50      public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
51              throws IOException
52      {
53          RendererUtils.checkParamValidity(facesContext, uiComponent, UIInput.class);
54  
55          Map<String, List<ClientBehavior>> behaviors = null;
56          if (uiComponent instanceof ClientBehaviorHolder)
57          {
58              behaviors = ((ClientBehaviorHolder) uiComponent).getClientBehaviors();
59              if (!behaviors.isEmpty())
60              {
61                  ResourceUtils.renderDefaultJsfJsInlineIfNecessary(facesContext, 
62                          facesContext.getResponseWriter());
63              }
64          }
65          
66          encodeTextArea(facesContext, uiComponent);
67  
68      }
69  
70      protected void encodeTextArea(FacesContext facesContext, UIComponent uiComponent) 
71          throws IOException
72      {
73         //allow subclasses to render custom attributes by separating rendering begin and end
74          renderTextAreaBegin(facesContext, uiComponent);
75          renderTextAreaValue(facesContext, uiComponent);
76          renderTextAreaEnd(facesContext, uiComponent);
77          
78      }
79  
80      //Subclasses can set the value of an attribute before, or can render a custom attribute after calling this method
81      protected void renderTextAreaBegin(FacesContext facesContext,
82              UIComponent uiComponent) throws IOException
83      {
84          ResponseWriter writer = facesContext.getResponseWriter();
85          writer.startElement(HTML.TEXTAREA_ELEM, uiComponent);
86  
87          Map<String, List<ClientBehavior>> behaviors = null;
88          if (uiComponent instanceof ClientBehaviorHolder && JavascriptUtils.isJavascriptAllowed(
89                  facesContext.getExternalContext()))
90          {
91              behaviors = ((ClientBehaviorHolder) uiComponent).getClientBehaviors();
92              if (!behaviors.isEmpty())
93              {
94                  HtmlRendererUtils.writeIdAndName(writer, uiComponent, facesContext);
95              }
96              else
97              {
98                  HtmlRendererUtils.writeIdIfNecessary(writer, uiComponent, facesContext);
99                  writer.writeAttribute(HTML.NAME_ATTR, uiComponent.getClientId(facesContext), null);
100             }
101             long commonPropertiesMarked = 0L;
102             if (isCommonPropertiesOptimizationEnabled(facesContext))
103             {
104                 commonPropertiesMarked = CommonPropertyUtils.getCommonPropertiesMarked(uiComponent);
105             }
106             if (behaviors.isEmpty() && isCommonPropertiesOptimizationEnabled(facesContext))
107             {
108                 CommonPropertyUtils.renderChangeEventProperty(writer, 
109                         commonPropertiesMarked, uiComponent);
110                 CommonPropertyUtils.renderEventProperties(writer, 
111                         commonPropertiesMarked, uiComponent);
112                 CommonPropertyUtils.renderFieldEventPropertiesWithoutOnchange(writer, 
113                         commonPropertiesMarked, uiComponent);
114             }
115             else
116             {
117                 HtmlRendererUtils.renderBehaviorizedOnchangeEventHandler(facesContext, writer, uiComponent, behaviors);
118                 if (isCommonEventsOptimizationEnabled(facesContext))
119                 {
120                     Long commonEventsMarked = CommonEventUtils.getCommonEventsMarked(uiComponent);
121                     CommonEventUtils.renderBehaviorizedEventHandlers(facesContext, writer, 
122                             commonPropertiesMarked, commonEventsMarked, uiComponent, behaviors);
123                     CommonEventUtils.renderBehaviorizedFieldEventHandlersWithoutOnchange(
124                         facesContext, writer, commonPropertiesMarked, commonEventsMarked, uiComponent, behaviors);
125                 }
126                 else
127                 {
128                     HtmlRendererUtils.renderBehaviorizedEventHandlers(facesContext, writer, uiComponent, behaviors);
129                     HtmlRendererUtils.renderBehaviorizedFieldEventHandlersWithoutOnchange(
130                             facesContext, writer, uiComponent, behaviors);
131                 }
132             }
133             if (isCommonPropertiesOptimizationEnabled(facesContext))
134             {
135                 CommonPropertyUtils.renderCommonFieldPassthroughPropertiesWithoutDisabledAndEvents(writer, 
136                         CommonPropertyUtils.getCommonPropertiesMarked(uiComponent), uiComponent);
137                 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.TEXTAREA_ATTRIBUTES);
138             }
139             else
140             {
141                 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, 
142                         HTML.TEXTAREA_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED_AND_EVENTS);
143             }
144         }
145         else
146         {
147             HtmlRendererUtils.writeIdIfNecessary(writer, uiComponent, facesContext);
148             writer.writeAttribute(HTML.NAME_ATTR, uiComponent.getClientId(facesContext), null);
149             if (isCommonPropertiesOptimizationEnabled(facesContext))
150             {
151                 CommonPropertyUtils.renderCommonFieldPassthroughPropertiesWithoutDisabled(writer, 
152                         CommonPropertyUtils.getCommonPropertiesMarked(uiComponent), uiComponent);
153                 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.TEXTAREA_ATTRIBUTES);
154             }
155             else
156             {
157                 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, 
158                         HTML.TEXTAREA_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED);
159             }
160         }
161 
162         if (isDisabled(facesContext, uiComponent))
163         {
164             writer.writeAttribute(org.apache.myfaces.shared.renderkit.html.HTML.DISABLED_ATTR, Boolean.TRUE, null);
165         }
166     }
167 
168     //Subclasses can override the writing of the "text" value of the textarea
169     protected void renderTextAreaValue(FacesContext facesContext, UIComponent uiComponent) throws IOException
170     {
171         ResponseWriter writer = facesContext.getResponseWriter();
172         
173         Object addNewLineAtStart = uiComponent.getAttributes().get(ADD_NEW_LINE_AT_START_ATTR);
174         if (addNewLineAtStart != null)
175         {
176             boolean addNewLineAtStartBoolean = false;
177             if (addNewLineAtStart instanceof String)
178             {
179                 addNewLineAtStartBoolean = Boolean.valueOf((String)addNewLineAtStart);
180             }
181             else if (addNewLineAtStart instanceof Boolean)
182             {
183                 addNewLineAtStartBoolean = (Boolean) addNewLineAtStart;
184             }
185             if (addNewLineAtStartBoolean)
186             {
187                 writer.writeText("\n", null);
188             }
189         }
190         
191         String strValue = org.apache.myfaces.shared.renderkit.RendererUtils.getStringValue(facesContext, uiComponent);
192         if (strValue != null)
193         {
194             writer.writeText(strValue, org.apache.myfaces.shared.renderkit.JSFAttr.VALUE_ATTR);
195         }
196     }
197     
198     protected void renderTextAreaEnd(FacesContext facesContext,
199             UIComponent uiComponent) throws IOException
200     {
201         facesContext.getResponseWriter().endElement(HTML.TEXTAREA_ELEM);
202     }
203     
204     protected boolean isDisabled(FacesContext facesContext, UIComponent uiComponent)
205     {
206         //TODO: overwrite in extended HtmlTextareaRenderer and check for enabledOnUserRole
207         if (uiComponent instanceof HtmlInputTextarea)
208         {
209             return ((HtmlInputTextarea)uiComponent).isDisabled();
210         }
211 
212         return org.apache.myfaces.shared.renderkit.RendererUtils.getBooleanAttribute(
213                 uiComponent, HTML.DISABLED_ATTR, false);
214         
215     }
216 
217     public void decode(FacesContext facesContext, UIComponent component)
218     {
219         RendererUtils.checkParamValidity(facesContext, component, UIInput.class);
220         HtmlRendererUtils.decodeUIInput(facesContext, component);
221         if (component instanceof ClientBehaviorHolder &&
222             !HtmlRendererUtils.isDisabled(component))
223         {
224             HtmlRendererUtils.decodeClientBehaviors(facesContext, component);
225         }
226     }
227 
228     public Object getConvertedValue(FacesContext facesContext, UIComponent uiComponent, Object submittedValue)
229         throws ConverterException
230     {
231         RendererUtils.checkParamValidity(facesContext, uiComponent, UIOutput.class);
232         return org.apache.myfaces.shared.renderkit.RendererUtils.getConvertedUIOutputValue(facesContext,
233                                                        (UIOutput)uiComponent,
234                                                        submittedValue);
235     }
236 
237 }