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