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  
20  package org.apache.myfaces.tobago.renderkit.html.scarborough.standard.tag;
21  
22  import org.apache.myfaces.tobago.component.Attributes;
23  import org.apache.myfaces.tobago.component.UITextarea;
24  import org.apache.myfaces.tobago.renderkit.HtmlUtils;
25  import org.apache.myfaces.tobago.renderkit.InputRendererBase;
26  import org.apache.myfaces.tobago.renderkit.css.Classes;
27  import org.apache.myfaces.tobago.renderkit.css.Style;
28  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
29  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
30  import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
31  import org.apache.myfaces.tobago.renderkit.util.RenderUtils;
32  import org.apache.myfaces.tobago.util.ComponentUtils;
33  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  import javax.faces.component.UIComponent;
38  import javax.faces.context.FacesContext;
39  import javax.faces.validator.LengthValidator;
40  import javax.faces.validator.Validator;
41  import java.io.IOException;
42  
43  public class TextareaRenderer extends InputRendererBase {
44  
45    private static final Logger LOG = LoggerFactory.getLogger(TextareaRenderer.class);
46  
47    @Override
48    public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
49      if (!(component instanceof UITextarea)) {
50        LOG.error("Wrong type: Need " + UITextarea.class.getName() + ", but was " + component.getClass().getName());
51        return;
52      }
53  
54      UITextarea input = (UITextarea) component;
55      String title = HtmlRendererUtils.getTitleFromTipAndMessages(facesContext, component);
56  
57      String clientId = input.getClientId(facesContext);
58      String onchange = HtmlUtils.generateOnchange(input, facesContext);
59  
60      TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
61      writer.startElement(HtmlElements.TEXTAREA, input);
62      writer.writeNameAttribute(clientId);
63      writer.writeIdAttribute(clientId);
64      HtmlRendererUtils.writeDataAttributes(facesContext, writer, input);
65      writer.writeAttribute(HtmlAttributes.ROWS, null, Attributes.ROWS);
66      if (title != null) {
67        writer.writeAttribute(HtmlAttributes.TITLE, title, true);
68      }
69      writer.writeAttribute(HtmlAttributes.READONLY, input.isReadonly());
70      writer.writeAttribute(HtmlAttributes.DISABLED, input.isDisabled());
71      writer.writeAttribute(HtmlAttributes.REQUIRED, input.isRequired());
72      Integer tabIndex = input.getTabIndex();
73      if (tabIndex != null) {
74        writer.writeAttribute(HtmlAttributes.TABINDEX, tabIndex);
75      }
76      HtmlRendererUtils.renderDojoDndItem(component, writer, true);
77  
78      writer.writeClassAttribute(Classes.create(input));
79      Style style = new Style(facesContext, input);
80      writer.writeStyleAttribute(style);
81      if (onchange != null) {
82        writer.writeAttribute(HtmlAttributes.ONCHANGE, onchange, null);
83      }
84      int maxLength = -1;
85      String pattern = null;
86      for (Validator validator : input.getValidators()) {
87        if (validator instanceof LengthValidator) {
88          LengthValidator lengthValidator = (LengthValidator) validator;
89          maxLength = lengthValidator.getMaximum();
90        }
91        /*if (validator instanceof RegexValidator) {
92          RegexValidator regexValidator = (RegexValidator) validator;
93          pattern = regexValidator.getPattern();
94        }*/
95      }
96      if (maxLength > 0) {
97        writer.writeAttribute(HtmlAttributes.MAXLENGTH, maxLength);
98      }
99      if (pattern != null) {
100       writer.writeAttribute(HtmlAttributes.PATTERN, pattern, false);
101     }
102     HtmlRendererUtils.renderCommandFacet(input, facesContext, writer);
103     HtmlRendererUtils.renderFocus(clientId, input.isFocus(), ComponentUtils.isError(input), facesContext, writer);
104 
105     /*String placeholder = input.getPlaceholder();
106     if (placeholder != null) {
107       writer.writeAttribute(HtmlAttributes.PLACEHOLDER, placeholder, true);
108     }*/
109     String currentValue = RenderUtils.currentValue(input);
110     if (currentValue != null) {
111       // this is because browsers eat the first CR+LF of <textarea>
112       if (currentValue.startsWith("\r\n")) {
113         currentValue = "\r\n" + currentValue;
114       } else if (currentValue.startsWith("\n")) {
115         currentValue = "\n" + currentValue;
116       } else if (currentValue.startsWith("\r")) {
117         currentValue = "\r" + currentValue;
118       }
119       writer.writeText(currentValue);
120     }
121     writer.endElement(HtmlElements.TEXTAREA);
122     /*if (placeholder != null && !VariableResolverUtils.resolveClientProperties(facesContext)
123         .getUserAgent().hasCapability(Capability.PLACEHOLDER)) {
124       HtmlRendererUtils.createPlaceholderDiv(input, currentValue, placeholder, style, writer);
125     }*/
126 
127   }
128 }