1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
92
93
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
106
107
108
109 String currentValue = RenderUtils.currentValue(input);
110 if (currentValue != null) {
111
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
123
124
125
126
127 }
128 }