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.component.Attributes;
23  import org.apache.myfaces.tobago.context.Markup;
24  import org.apache.myfaces.tobago.internal.component.AbstractUIBadge;
25  import org.apache.myfaces.tobago.internal.component.AbstractUIButton;
26  import org.apache.myfaces.tobago.renderkit.css.BootstrapClass;
27  import org.apache.myfaces.tobago.renderkit.css.CssItem;
28  import org.apache.myfaces.tobago.renderkit.css.TobagoClass;
29  import org.apache.myfaces.tobago.util.ComponentUtils;
30  
31  import javax.faces.component.UIComponent;
32  import javax.faces.context.FacesContext;
33  import java.io.IOException;
34  
35  public class ButtonRenderer<T extends AbstractUIButton> extends CommandRendererBase<T> {
36  
37    @Override
38    protected TobagoClass getRendererCssClass() {
39      return TobagoClass.BUTTON;
40    }
41  
42    @Override
43    protected CssItem[] getCssItems(final FacesContext facesContext, final T command) {
44      final boolean defaultCommand = ComponentUtils.getBooleanAttribute(command, Attributes.defaultCommand);
45      final Markup markup = command.getMarkup() != null ? command.getMarkup() : Markup.NULL;
46  
47      return new CssItem[]{
48          BootstrapClass.BTN,
49          getButtonColor(command.getMarkup(), defaultCommand),
50          markup.contains(Markup.BADGE) ? BootstrapClass.BADGE : null,
51          markup.contains(Markup.BADGE) && markup.contains(Markup.PILL) ? BootstrapClass.ROUNDED_PILL : null,
52      };
53    }
54  
55    private BootstrapClass getButtonColor(final Markup markup, final boolean defaultCommand) {
56      if (markup != null) {
57        if (markup.contains(Markup.NONE)) {
58          return null;
59        } else if (markup.contains(Markup.PRIMARY)) {
60          return BootstrapClass.BTN_PRIMARY;
61        } else if (markup.contains(Markup.SECONDARY)) {
62          return BootstrapClass.BTN_SECONDARY;
63        } else if (markup.contains(Markup.SUCCESS)) {
64          return BootstrapClass.BTN_SUCCESS;
65        } else if (markup.contains(Markup.DANGER)) {
66          return BootstrapClass.BTN_DANGER;
67        } else if (markup.contains(Markup.WARNING)) {
68          return BootstrapClass.BTN_WARNING;
69        } else if (markup.contains(Markup.INFO)) {
70          return BootstrapClass.BTN_INFO;
71        } else if (markup.contains(Markup.LIGHT)) {
72          return BootstrapClass.BTN_LIGHT;
73        } else if (markup.contains(Markup.DARK)) {
74          return BootstrapClass.BTN_DARK;
75        } else if (markup.contains(Markup.OUTLINE_PRIMARY)) {
76          return BootstrapClass.BTN_OUTLINE_PRIMARY;
77        } else if (markup.contains(Markup.OUTLINE_SECONDARY)) {
78          return BootstrapClass.BTN_OUTLINE_SECONDARY;
79        } else if (markup.contains(Markup.OUTLINE_SUCCESS)) {
80          return BootstrapClass.BTN_OUTLINE_SUCCESS;
81        } else if (markup.contains(Markup.OUTLINE_DANGER)) {
82          return BootstrapClass.BTN_OUTLINE_DANGER;
83        } else if (markup.contains(Markup.OUTLINE_WARNING)) {
84          return BootstrapClass.BTN_OUTLINE_WARNING;
85        } else if (markup.contains(Markup.OUTLINE_INFO)) {
86          return BootstrapClass.BTN_OUTLINE_INFO;
87        } else if (markup.contains(Markup.OUTLINE_LIGHT)) {
88          return BootstrapClass.BTN_OUTLINE_LIGHT;
89        } else if (markup.contains(Markup.OUTLINE_DARK)) {
90          return BootstrapClass.BTN_OUTLINE_DARK;
91        }
92      }
93      return defaultCommand ? BootstrapClass.BTN_PRIMARY : BootstrapClass.BTN_SECONDARY;
94    }
95  
96    @Override
97    protected void encodeBadge(FacesContext facesContext, T component) throws IOException {
98      for (final UIComponent child : component.getChildren()) {
99        if (child instanceof AbstractUIBadge) {
100         child.encodeAll(facesContext);
101       }
102     }
103   }
104 }