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