001package 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
023import org.gwtbootstrap3.client.ui.constants.ButtonSize;
024import org.gwtbootstrap3.client.ui.constants.ButtonType;
025import org.gwtbootstrap3.client.ui.constants.IconType;
026import org.gwtbootstrap3.client.ui.constants.Styles;
027
028import com.google.gwt.safehtml.shared.SafeHtml;
029import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
030import com.google.gwt.text.shared.SimpleSafeHtmlRenderer;
031import com.google.gwt.user.client.ui.HasEnabled;
032
033public class ButtonCell extends com.google.gwt.cell.client.ButtonCell implements HasEnabled {
034
035    private IconType icon;
036
037    private ButtonType type = ButtonType.DEFAULT;
038
039    private ButtonSize size = ButtonSize.DEFAULT;
040
041    private boolean enabled = true;
042
043    public ButtonCell() {
044        super(SimpleSafeHtmlRenderer.getInstance());
045    }
046
047    public ButtonCell(ButtonType type) {
048        this();
049        this.type = type;
050    }
051
052    public ButtonCell(IconType icon) {
053        this();
054        this.icon = icon;
055    }
056
057    public ButtonCell(ButtonSize size) {
058        this();
059        this.size = size;
060    }
061
062    public ButtonCell(ButtonType type, IconType icon) {
063        this();
064        this.type = type;
065        this.icon = icon;
066    }
067
068    public ButtonCell(ButtonType type, ButtonSize size) {
069        this();
070        this.type = type;
071        this.size = size;
072    }
073
074    public ButtonCell(IconType icon, ButtonSize size) {
075        this();
076        this.icon = icon;
077        this.size = size;
078    }
079
080    public ButtonCell(IconType icon, ButtonType type, ButtonSize size) {
081        this();
082        this.icon = icon;
083        this.type = type;
084        this.size = size;
085    }
086
087    @Override
088    public boolean isEnabled() {
089        return enabled;
090    }
091
092    @Override
093    public void setEnabled(boolean enabled) {
094        this.enabled = enabled;
095    }
096
097    @Override
098    public void render(com.google.gwt.cell.client.Cell.Context context, SafeHtml data, SafeHtmlBuilder sb) {
099        String cssClasses = new StringBuilder("btn") //
100                .append(" ") //
101                .append(type.getCssName()) //
102                .append(" ") //
103                .append(size.getCssName()) //
104                .toString();
105
106        String disabled = "";
107        if (!enabled) {
108            disabled = " disabled=\"disabled\"";
109        }
110
111        sb.appendHtmlConstant("<button type=\"button\" class=\"" + cssClasses + "\" tabindex=\"-1\"" + disabled + ">");
112        if (icon != null) {
113            String iconHtml = new StringBuilder("<i class=\"") //
114                    .append(Styles.FONT_AWESOME_BASE) //
115                    .append(" ") //
116                    .append(icon.getCssName()) //
117                    .append("\"></i> ") //
118                    .toString();
119            sb.appendHtmlConstant(iconHtml);
120        }
121        if (data != null) {
122            sb.append(data);
123        }
124        sb.appendHtmlConstant("</button>");
125    }
126
127}