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