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