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.ui;
021
022import com.google.gwt.dom.client.Document;
023import com.google.gwt.safehtml.shared.SafeHtmlUtils;
024import gwt.material.design.client.constants.Color;
025import gwt.material.design.client.constants.CssName;
026import gwt.material.design.client.ui.html.Span;
027
028//@formatter:off
029
030/**
031 * Badges can notify you that there are new or unread messages or notifications.
032 * Add the new class to the badge to give it the background.
033 * <h3>UiBinder Usage:</h3>
034 * <pre>
035 * {@code
036 * <m:MaterialBadge text="1 new" color="BLUE"/>}
037 * </pre>
038 *
039 * @author kevzlou7979
040 * @see <a href="http://gwtmaterialdesign.github.io/gwt-material-demo/#badges">Material Badge</a>
041 */
042//@formatter:on
043public class MaterialBadge extends Span {
044
045    /**
046     * Creates a badge component that can be added to Link,
047     * Collection, DropDown, SideNav and any other Material components.
048     */
049    public MaterialBadge() {
050        super(Document.get().createSpanElement(), CssName.BADGE, CssName.SIDEBAR_BADGE);
051    }
052
053    /**
054     * @param text text of the badge
055     */
056    public MaterialBadge(String text) {
057        this();
058        setText(text);
059    }
060
061    /**
062     * @param text     text of the badge
063     * @param isCircle is a circle badge
064     */
065    public MaterialBadge(String text, boolean isCircle) {
066        this();
067        setText(text);
068        setCircle(isCircle);
069    }
070
071    /**
072     * Badge with text and color
073     *
074     * @param text      text of the badge
075     * @param textColor text color of the badge
076     * @param bgColor   background color of the badge
077     */
078    public MaterialBadge(String text, Color textColor, Color bgColor) {
079        this();
080        setText(text);
081        setTextColor(textColor);
082        setBackgroundColor(bgColor);
083    }
084
085    @Override
086    public String getText() {
087        return getElement().getInnerHTML();
088    }
089
090    @Override
091    public void setText(String text) {
092        getElement().setInnerSafeHtml(SafeHtmlUtils.fromString(text));
093    }
094}