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.standard.standard.tag;
21  
22  import org.apache.myfaces.tobago.component.Facets;
23  import org.apache.myfaces.tobago.component.UILabel;
24  import org.apache.myfaces.tobago.component.UISeparator;
25  import org.apache.myfaces.tobago.config.Configurable;
26  import org.apache.myfaces.tobago.internal.util.Deprecation;
27  import org.apache.myfaces.tobago.internal.util.StringUtils;
28  import org.apache.myfaces.tobago.layout.Measure;
29  import org.apache.myfaces.tobago.renderkit.HtmlUtils;
30  import org.apache.myfaces.tobago.renderkit.LayoutComponentRendererBase;
31  import org.apache.myfaces.tobago.renderkit.css.Classes;
32  import org.apache.myfaces.tobago.renderkit.css.Style;
33  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
34  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
35  import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
36  import org.apache.myfaces.tobago.util.VariableResolverUtils;
37  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
38  
39  import javax.faces.component.UIComponent;
40  import javax.faces.context.FacesContext;
41  import java.io.IOException;
42  
43  public class SeparatorRenderer extends LayoutComponentRendererBase {
44  
45    public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
46  
47      UISeparator separator = (UISeparator) component;
48      TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
49  
50      String label = getLabel(separator);
51  
52      if (label != null) {
53        if (VariableResolverUtils.resolveClientProperties(facesContext).getUserAgent().isMsie()) {
54          label = StringUtils.replace(label, " ", HtmlUtils.CHAR_NON_BEAKING_SPACE);
55        }
56  
57        writer.startElement(HtmlElements.TABLE, component);
58        writer.writeIdAttribute(separator.getClientId(facesContext));
59        writer.writeClassAttribute(Classes.create(component));
60        HtmlRendererUtils.writeDataAttributes(facesContext, writer, component);
61        Style style = new Style(facesContext, separator);
62        writer.writeStyleAttribute(style);
63  
64        writer.writeAttribute(HtmlAttributes.CELLPADDING, "0", false);
65        writer.writeAttribute(HtmlAttributes.CELLSPACING, "0", false);
66        writer.startElement(HtmlElements.TR, component);
67  
68        writer.startElement(HtmlElements.TD, component);
69        writer.writeClassAttribute(Classes.create(component, "start"));
70        writer.startElement(HtmlElements.HR , component);
71        writer.endElement(HtmlElements.HR);
72        writer.endElement(HtmlElements.TD);
73  
74        writer.startElement(HtmlElements.TD, component);
75        writer.writeClassAttribute(Classes.create(component, "label"));
76        writer.writeText(label);
77        writer.endElement(HtmlElements.TD);
78  
79        writer.startElement(HtmlElements.TD, component);
80        writer.writeClassAttribute(Classes.create(component, "end"));
81        writer.startElement(HtmlElements.HR , component);
82        writer.endElement(HtmlElements.HR);
83        writer.endElement(HtmlElements.TD);
84  
85        writer.endElement(HtmlElements.TR);
86        writer.endElement(HtmlElements.TABLE);
87      } else {
88        writer.startElement(HtmlElements.HR , component);
89        writer.writeIdAttribute(separator.getClientId(facesContext));
90        writer.writeClassAttribute(Classes.create(component));
91        Style style = new Style(facesContext, separator);
92        style.setHeight(Measure.ZERO); // not nice
93        writer.writeStyleAttribute(style);
94        writer.endElement(HtmlElements.HR);
95      }
96    }
97  
98    private String getLabel(UISeparator separator) {
99      String label = separator.getLabel();
100     if (label == null && separator.getFacet(Facets.LABEL) != null) {
101       // deprecated
102       Deprecation.LOG.warn("label facet in tc:separator is deprecated, use label attribute instead, please.");
103       label = String.valueOf(((UILabel) separator.getFacet(Facets.LABEL)).getValue());
104     }
105     return label;
106   }
107 
108   @Override
109   public Measure getHeight(FacesContext facesContext, Configurable component) {
110     String label = getLabel((UISeparator) component);
111     if (label == null) {
112       return getResourceManager().getThemeMeasure(facesContext, component, "withoutLabelHeight");
113     } else {
114       return super.getHeight(facesContext, component);
115     }
116   }
117 }