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.context.Markup;
23  import org.apache.myfaces.tobago.internal.component.AbstractUIBadge;
24  import org.apache.myfaces.tobago.internal.util.RenderUtils;
25  import org.apache.myfaces.tobago.renderkit.RendererBase;
26  import org.apache.myfaces.tobago.renderkit.css.BootstrapClass;
27  import org.apache.myfaces.tobago.renderkit.css.TobagoClass;
28  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
29  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
30  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
31  
32  import javax.faces.context.FacesContext;
33  import java.io.IOException;
34  
35  public class BadgeRenderer<T extends AbstractUIBadge> extends RendererBase<T> {
36  
37    @Override
38    public void encodeBeginInternal(final FacesContext facesContext, final T component) throws IOException {
39      final TobagoResponseWriter writer = getResponseWriter(facesContext);
40      final Markup markup = component.getMarkup() != null ? component.getMarkup() : Markup.NULL;
41      final String tip = component.getTip();
42      final String value = RenderUtils.currentValue(component);
43  
44      writer.startElement(HtmlElements.TOBAGO_BADGE);
45      writer.writeIdAttribute(component.getClientId(facesContext));
46      writer.writeClassAttribute(
47          null,
48          TobagoClass.BADGE.createMarkup(markup),
49          BootstrapClass.BADGE,
50          getBadgeColor(markup),
51          markup.contains(Markup.PILL) ? BootstrapClass.ROUNDED_PILL : null,
52          isInside(facesContext, HtmlElements.TOBAGO_BUTTONS) ? BootstrapClass.BTN : null,
53          component.getCustomClass());
54  
55      if (tip != null) {
56        writer.writeAttribute(HtmlAttributes.TITLE, tip, true);
57      }
58      if (value != null) {
59        writer.writeText(value);
60      }
61    }
62  
63    @Override
64    public void encodeEndInternal(FacesContext facesContext, T component) throws IOException {
65      final TobagoResponseWriter writer = getResponseWriter(facesContext);
66      writer.endElement(HtmlElements.TOBAGO_BADGE);
67    }
68  
69    private BootstrapClass getBadgeColor(final Markup markup) {
70      if (markup.contains(Markup.NONE)) {
71        return null;
72      } else if (markup.contains(Markup.PRIMARY)) {
73        return BootstrapClass.BG_PRIMARY;
74      } else if (markup.contains(Markup.SECONDARY)) {
75        return BootstrapClass.BG_SECONDARY;
76      } else if (markup.contains(Markup.SUCCESS)) {
77        return BootstrapClass.BG_SUCCESS;
78      } else if (markup.contains(Markup.DANGER)) {
79        return BootstrapClass.BG_DANGER;
80      } else if (markup.contains(Markup.WARNING)) {
81        return BootstrapClass.BG_WARNING;
82      } else if (markup.contains(Markup.INFO)) {
83        return BootstrapClass.BG_INFO;
84      } else if (markup.contains(Markup.LIGHT)) {
85        return BootstrapClass.BG_LIGHT;
86      } else if (markup.contains(Markup.DARK)) {
87        return BootstrapClass.BG_DARK;
88      } else {
89        return BootstrapClass.BG_SECONDARY;
90      }
91    }
92  }