001    package org.gwtbootstrap3.client.ui.gwt;
002    
003    /*
004     * #%L
005     * GwtBootstrap3
006     * %%
007     * Copyright (C) 2013 - 2015 GwtBootstrap3
008     * %%
009     * Licensed under the Apache License, Version 2.0 (the "License");
010     * you may not use this file except in compliance with the License.
011     * You may obtain a copy of the License at
012     * 
013     *      http://www.apache.org/licenses/LICENSE-2.0
014     * 
015     * Unless required by applicable law or agreed to in writing, software
016     * distributed under the License is distributed on an "AS IS" BASIS,
017     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018     * See the License for the specific language governing permissions and
019     * limitations under the License.
020     * #L%
021     */
022    
023    import org.gwtbootstrap3.client.ui.constants.ButtonSize;
024    import org.gwtbootstrap3.client.ui.constants.ButtonType;
025    import org.gwtbootstrap3.client.ui.constants.IconType;
026    import org.gwtbootstrap3.client.ui.constants.Styles;
027    
028    import com.google.gwt.safehtml.shared.SafeHtml;
029    import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
030    import com.google.gwt.text.shared.SimpleSafeHtmlRenderer;
031    
032    public class ButtonCell extends com.google.gwt.cell.client.ButtonCell {
033    
034        private IconType icon;
035    
036        private ButtonType type = ButtonType.DEFAULT;
037    
038        private ButtonSize size = ButtonSize.DEFAULT;
039    
040        public ButtonCell() {
041            super(SimpleSafeHtmlRenderer.getInstance());
042        }
043    
044        public ButtonCell(ButtonType type) {
045            this();
046            this.type = type;
047        }
048    
049        public ButtonCell(IconType icon) {
050            this();
051            this.icon = icon;
052        }
053    
054        public ButtonCell(ButtonSize size) {
055            this();
056            this.size = size;
057        }
058    
059        public ButtonCell(ButtonType type, IconType icon) {
060            this();
061            this.type = type;
062            this.icon = icon;
063        }
064    
065        public ButtonCell(ButtonType type, ButtonSize size) {
066            this();
067            this.type = type;
068            this.size = size;
069        }
070    
071        public ButtonCell(IconType icon, ButtonSize size) {
072            this();
073            this.icon = icon;
074            this.size = size;
075        }
076    
077        public ButtonCell(IconType icon, ButtonType type, ButtonSize size) {
078            this();
079            this.icon = icon;
080            this.type = type;
081            this.size = size;
082        }
083    
084        @Override
085        public void render(com.google.gwt.cell.client.Cell.Context context, SafeHtml data, SafeHtmlBuilder sb) {
086            String cssClasses = new StringBuilder("btn") //
087                    .append(" ") //
088                    .append(type.getCssName()) //
089                    .append(" ") //
090                    .append(size.getCssName()) //
091                    .toString();
092    
093            sb.appendHtmlConstant("<button type=\"button\" class=\"" + cssClasses + "\" tabindex=\"-1\">");
094            if (icon != null) {
095                String iconHtml = new StringBuilder("<i class=\"") //
096                        .append(Styles.FONT_AWESOME_BASE) //
097                        .append(" ") //
098                        .append(icon.getCssName()) //
099                        .append("\"></i> ") //
100                        .toString();
101                sb.appendHtmlConstant(iconHtml);
102            }
103            if (data != null) {
104                sb.append(data);
105            }
106            sb.appendHtmlConstant("</button>");
107        }
108    
109    }