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.internal.renderkit.renderer;
21  
22  import org.apache.myfaces.tobago.component.SupportFieldId;
23  import org.apache.myfaces.tobago.context.Markup;
24  import org.apache.myfaces.tobago.internal.component.AbstractUILabel;
25  import org.apache.myfaces.tobago.internal.util.AccessKeyLogger;
26  import org.apache.myfaces.tobago.internal.util.HtmlRendererUtils;
27  import org.apache.myfaces.tobago.renderkit.LabelWithAccessKey;
28  import org.apache.myfaces.tobago.renderkit.RendererBase;
29  import org.apache.myfaces.tobago.renderkit.css.BootstrapClass;
30  import org.apache.myfaces.tobago.renderkit.css.TobagoClass;
31  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
32  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
33  import org.apache.myfaces.tobago.util.ComponentUtils;
34  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
35  
36  import javax.faces.component.UIComponent;
37  import javax.faces.component.UIInput;
38  import javax.faces.context.FacesContext;
39  import javax.faces.event.ComponentSystemEvent;
40  import javax.faces.event.ComponentSystemEventListener;
41  import javax.faces.event.ListenerFor;
42  import javax.faces.event.PostAddToViewEvent;
43  import java.io.IOException;
44  
45  @ListenerFor(systemEventClass = PostAddToViewEvent.class)
46  public class LabelRenderer<T extends AbstractUILabel> extends RendererBase<T> implements ComponentSystemEventListener {
47  
48    @Override
49    public void processEvent(final ComponentSystemEvent event) {
50      ComponentUtils.evaluateAutoFor(event.getComponent(), UIInput.class);
51    }
52  
53    @Override
54    public void encodeEndInternal(final FacesContext facesContext, final T component) throws IOException {
55  
56      final TobagoResponseWriter writer = getResponseWriter(facesContext);
57      final UIComponent corresponding = ComponentUtils.findFor(component);
58      final String forId;
59      if (corresponding instanceof SupportFieldId) {
60        forId = ((SupportFieldId) corresponding).getFieldId(facesContext);
61      } else {
62        forId = corresponding != null ? corresponding.getClientId(facesContext) : null;
63      }
64      final String clientId = component.getClientId(facesContext);
65      final Markup markup = component.getMarkup();
66      final boolean required;
67      if (corresponding instanceof UIInput) {
68        required = ((UIInput) corresponding).isRequired();
69      } else {
70        required = false;
71      }
72  
73      writer.startElement(HtmlElements.LABEL);
74      HtmlRendererUtils.writeDataAttributes(facesContext, writer, component);
75      writer.writeClassAttribute(
76          BootstrapClass.COL_FORM_LABEL,
77          TobagoClass.LABEL.createMarkup(markup),
78          required ? TobagoClass.REQUIRED : null,
79          component.getCustomClass());
80      writer.writeIdAttribute(clientId);
81      if (forId != null) {
82        writer.writeAttribute(HtmlAttributes.FOR, forId, false);
83      }
84      final String title = HtmlRendererUtils.getTitleFromTipAndMessages(facesContext, component);
85      if (title != null) {
86        writer.writeAttribute(HtmlAttributes.TITLE, title, true);
87      }
88  
89      encodeTextContent(facesContext, writer, component);
90  
91      writer.endElement(HtmlElements.LABEL);
92    }
93  
94    /**
95     * Encodes the text inside of the label.
96     * Can be overwritten in other themes.
97     */
98    protected void encodeTextContent(
99        final FacesContext facesContext, final TobagoResponseWriter writer, final AbstractUILabel component)
100       throws IOException {
101 
102     final LabelWithAccessKey label = new LabelWithAccessKey(component);
103 
104     if (label.getAccessKey() != null) {
105       writer.writeAttribute(HtmlAttributes.ACCESSKEY, Character.toString(label.getAccessKey()), false);
106       AccessKeyLogger.addAccessKey(facesContext, label.getAccessKey(), component.getClientId());
107     }
108     HtmlRendererUtils.writeLabelWithAccessKey(writer, label);
109   }
110 }