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.internal.component.AbstractUIData;
23  import org.apache.myfaces.tobago.internal.component.AbstractUITreeLabel;
24  import org.apache.myfaces.tobago.internal.component.AbstractUITreeListbox;
25  import org.apache.myfaces.tobago.internal.util.HtmlRendererUtils;
26  import org.apache.myfaces.tobago.internal.util.StringUtils;
27  import org.apache.myfaces.tobago.renderkit.RendererBase;
28  import org.apache.myfaces.tobago.renderkit.css.TobagoClass;
29  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
30  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
31  import org.apache.myfaces.tobago.util.ComponentUtils;
32  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
33  
34  import javax.faces.context.FacesContext;
35  import java.io.IOException;
36  
37  public class TreeLabelRenderer<T extends AbstractUITreeLabel> extends RendererBase<T> {
38  
39    @Override
40    public void encodeBeginInternal(final FacesContext facesContext, final T component) throws IOException {
41  
42      final AbstractUIData data = ComponentUtils.findAncestor(component, AbstractUIData.class);
43      final boolean listbox = data instanceof AbstractUITreeListbox;
44  
45      final TobagoResponseWriter writer = getResponseWriter(facesContext);
46      final String text = StringUtils.defaultString((String) component.getValue());
47  
48      if (listbox) {
49        writer.writeText(text);
50      } else {
51        writer.startElement(HtmlElements.LABEL);
52        writer.writeClassAttribute(
53            TobagoClass.TREE_LABEL,
54            TobagoClass.TREE_LABEL.createMarkup(component.getMarkup()),
55            component.getCustomClass());
56        HtmlRendererUtils.writeDataAttributes(facesContext, writer, component);
57        final String title = HtmlRendererUtils.getTitleFromTipAndMessages(facesContext, component);
58        if (title != null) {
59          writer.writeAttribute(HtmlAttributes.TITLE, title, true);
60        }
61  
62        writer.writeText(text);
63  
64        writer.endElement(HtmlElements.LABEL);
65      }
66    }
67  }