001/*
002 * #%L
003 * GwtMaterial
004 * %%
005 * Copyright (C) 2015 - 2017 GwtMaterialDesign
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 * 
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 * 
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package gwt.material.design.client.base;
021
022import com.google.gwt.dom.client.Style;
023import gwt.material.design.client.constants.*;
024import gwt.material.design.client.ui.MaterialIcon;
025
026/**
027 * @author Ben Dol
028 */
029public abstract class AbstractIconButton extends AbstractButton implements HasIcon {
030
031    private MaterialIcon icon = new MaterialIcon();
032
033    public AbstractIconButton(ButtonType type, String text, MaterialIcon icon) {
034        super(type, text);
035
036        this.icon = icon;
037        ensureIconAttached();
038    }
039
040    public AbstractIconButton(ButtonType type, String text) {
041        super(type, text);
042        setIconPosition(IconPosition.LEFT);
043    }
044
045    public AbstractIconButton(IconType iconType) {
046        super();
047        setIconType(iconType);
048        setIconPosition(IconPosition.LEFT);
049    }
050
051    public AbstractIconButton(ButtonType type) {
052        super(type);
053        setIconPosition(IconPosition.LEFT);
054    }
055
056    public AbstractIconButton() {
057        super();
058        setIconPosition(IconPosition.LEFT);
059    }
060
061    public AbstractIconButton(String... initialClass) {
062        super();
063        setInitialClasses(initialClass);
064    }
065
066    @Override
067    public MaterialIcon getIcon() {
068        return icon;
069    }
070
071    @Override
072    public void setIconType(IconType iconType) {
073        icon.setIconType(iconType);
074        ensureIconAttached();
075    }
076
077    @Override
078    public void setIconPosition(IconPosition position) {
079        icon.setIconPosition(position);
080    }
081
082    @Override
083    public void setIconSize(IconSize size) {
084        icon.setIconSize(size);
085    }
086
087    @Override
088    public void setIconFontSize(double size, Style.Unit unit) {
089        icon.setIconFontSize(size, unit);
090    }
091
092    @Override
093    public void setIconColor(Color iconColor) {
094        icon.setIconColor(iconColor);
095    }
096
097    @Override
098    public Color getIconColor() {
099        return icon.getIconColor();
100    }
101
102    @Override
103    public void setIconPrefix(boolean prefix) {
104        icon.setIconPrefix(prefix);
105    }
106
107    @Override
108    public boolean isIconPrefix() {
109        return icon.isIconPrefix();
110    }
111
112    /**
113     * Ensure the icon is attached in slot 0.
114     */
115    protected void ensureIconAttached() {
116        if (icon != null && !icon.isAttached()) {
117            insert(icon, 0);
118        }
119    }
120}