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.internal.component.AbstractUIIn;
24  import org.apache.myfaces.tobago.internal.component.AbstractUIInput;
25  import org.apache.myfaces.tobago.internal.util.FacesContextUtils;
26  import org.apache.myfaces.tobago.internal.util.StringUtils;
27  import org.apache.myfaces.tobago.renderkit.InputRendererBase;
28  import org.apache.myfaces.tobago.renderkit.css.Classes;
29  import org.apache.myfaces.tobago.renderkit.css.Style;
30  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
31  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
32  import org.apache.myfaces.tobago.renderkit.html.HtmlInputTypes;
33  import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
34  import org.apache.myfaces.tobago.util.ComponentUtils;
35  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  import javax.faces.component.UIComponent;
40  import javax.faces.context.FacesContext;
41  import javax.faces.validator.LengthValidator;
42  import javax.faces.validator.Validator;
43  import java.io.IOException;
44  
45  public class InRenderer extends InputRendererBase {
46  
47    private static final Logger LOG = LoggerFactory.getLogger(InRenderer.class);
48  
49    @Override
50    public void decode(FacesContext facesContext, UIComponent component) {
51      super.decode(facesContext, component);
52      String clientId = component.getClientId(facesContext);
53      if (clientId.equals(FacesContextUtils.getActionId(facesContext))) {
54        // this is a inputSuggest request -> render response
55        facesContext.renderResponse();
56      }
57    }
58  
59    @Override
60    public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
61  
62      final AbstractUIInput input = (AbstractUIInput) component;
63      final String title = HtmlRendererUtils.getTitleFromTipAndMessages(facesContext, input);
64      final String currentValue = getCurrentValue(facesContext, input);
65      final boolean password = ComponentUtils.getBooleanAttribute(input, Attributes.PASSWORD);
66      if (LOG.isDebugEnabled()) {
67        LOG.debug("currentValue = '" + (password ? StringUtils.repeat("*", currentValue.length()) : currentValue) +  "'");
68      }
69      final String type = password ? HtmlInputTypes.PASSWORD : HtmlInputTypes.TEXT;
70      final String id = input.getClientId(facesContext);
71      final boolean readonly = input.isReadonly();
72      final boolean disabled = input.isDisabled();
73  
74      final TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
75  
76      writer.startElement(HtmlElements.INPUT, input);
77      writer.writeAttribute(HtmlAttributes.TYPE, type, false);
78      writer.writeNameAttribute(id);
79      writer.writeIdAttribute(id);
80      HtmlRendererUtils.writeDataAttributes(facesContext, writer, input);
81      if (currentValue != null) {
82        writer.writeAttribute(HtmlAttributes.VALUE, currentValue, true);
83      }
84      if (title != null) {
85        writer.writeAttribute(HtmlAttributes.TITLE, title, true);
86      }
87      int maxLength = 0;
88      String pattern = null;
89      for (Validator validator : input.getValidators()) {
90        if (validator instanceof LengthValidator) {
91          LengthValidator lengthValidator = (LengthValidator) validator;
92          maxLength = lengthValidator.getMaximum();
93        }
94          /*if (validator instanceof RegexValidator) {
95            RegexValidator regexValidator = (RegexValidator) validator;
96            pattern = regexValidator.getPattern();
97          }*/
98      }
99      if (maxLength > 0) {
100       writer.writeAttribute(HtmlAttributes.MAXLENGTH, maxLength);
101     }
102     if (pattern != null) {
103       writer.writeAttribute(HtmlAttributes.PATTERN, pattern, false);
104     }
105     writer.writeAttribute(HtmlAttributes.READONLY, readonly);
106     writer.writeAttribute(HtmlAttributes.DISABLED, disabled);
107     Integer tabIndex = input.getTabIndex();
108     if (tabIndex != null) {
109       writer.writeAttribute(HtmlAttributes.TABINDEX, tabIndex);
110     }
111     Style style = new Style(facesContext, input);
112     writer.writeStyleAttribute(style);
113 
114     final String placeholder = input.getPlaceholder();
115     if (!disabled && !readonly && StringUtils.isNotBlank(placeholder)) {
116       writer.writeAttribute(HtmlAttributes.PLACEHOLDER, placeholder, true);
117     }
118 
119     if (input instanceof AbstractUIIn && ((AbstractUIIn) input).getSuggest() != null) {
120       writer.writeAttribute(HtmlAttributes.AUTOCOMPLETE, "off", false);
121     }
122 
123     HtmlRendererUtils.renderDojoDndItem(component, writer, true);
124     writer.writeClassAttribute(Classes.create(input));
125       /*if (component instanceof AbstractUIInput) {
126        String onchange = HtmlUtils.generateOnchange((AbstractUIInput) component, facesContext);
127        if (onchange != null) {
128          // TODO: create and use utility method to write attributes without quoting
129      //      writer.writeAttribute(HtmlAttributes.ONCHANGE, onchange, null);
130        }
131      } */
132     boolean required = ComponentUtils.getBooleanAttribute(input, Attributes.REQUIRED);
133     writer.writeAttribute(HtmlAttributes.REQUIRED, required);
134     HtmlRendererUtils.renderFocus(id, input.isFocus(), ComponentUtils.isError(input), facesContext, writer);
135     writeAdditionalAttributes(facesContext, writer, input);
136     HtmlRendererUtils.renderCommandFacet(input, facesContext, writer);
137     writer.endElement(HtmlElements.INPUT);
138   }
139 
140   protected void writeAdditionalAttributes(
141       FacesContext facesContext, TobagoResponseWriter writer, AbstractUIInput input) throws IOException {
142   }
143 }