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.UITreeLabel;
23  import org.apache.myfaces.tobago.internal.component.AbstractUIData;
24  import org.apache.myfaces.tobago.internal.component.AbstractUITreeListbox;
25  import org.apache.myfaces.tobago.internal.util.StringUtils;
26  import org.apache.myfaces.tobago.renderkit.LayoutComponentRendererBase;
27  import org.apache.myfaces.tobago.renderkit.css.Classes;
28  import org.apache.myfaces.tobago.renderkit.css.Style;
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.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 java.io.IOException;
40  
41  public class TreeLabelRenderer extends LayoutComponentRendererBase {
42  
43    private static final Logger LOG = LoggerFactory.getLogger(TreeLabelRenderer.class);
44  
45    public void encodeBegin(FacesContext facesContext, UIComponent component) throws IOException {
46  
47      final AbstractUIData data = ComponentUtils.findAncestor(component, AbstractUIData.class);
48      final boolean listbox = data instanceof AbstractUITreeListbox;
49  
50      final UITreeLabel label = (UITreeLabel) component;
51      final TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
52      final String text = StringUtils.defaultString((String) label.getValue());
53  
54      if (listbox) {
55        writer.writeText(text);
56      } else {
57        writer.startElement(HtmlElements.LABEL, label);
58        writer.writeClassAttribute(Classes.create(label));
59        HtmlRendererUtils.writeDataAttributes(facesContext, writer, label);
60        writer.writeStyleAttribute(createStyle(facesContext, label));
61        String title = HtmlRendererUtils.getTitleFromTipAndMessages(facesContext, label);
62        if (title != null) {
63          writer.writeAttribute(HtmlAttributes.TITLE, title, true);
64        }
65  
66        writer.writeText(text);
67  
68        writer.endElement(HtmlElements.LABEL);
69      }
70    }
71  
72    protected Style createStyle(FacesContext facesContext, UITreeLabel link) {
73      return new Style(facesContext, link);
74    }
75  }