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.Style.Unit;
023import com.google.gwt.user.client.DOM;
024import com.google.gwt.user.client.ui.RootPanel;
025import com.google.gwt.user.client.ui.Widget;
026import gwt.material.design.client.js.JsMaterialElement;
027import gwt.material.design.jquery.client.api.Functions;
028
029import static gwt.material.design.jquery.client.api.JQuery.$;
030
031//@formatter:off
032
033/**
034 * GWT Material provides an easy way for you to send unobtrusive alerts to your users through toasts.
035 * These toasts are also placed and sized responsively, try it out by clicking the button below on
036 * different device sizes.
037 *
038 * <p>
039 * <h3>Java Usage:</h3>
040 * <pre>
041 * {@code
042 * MaterialToast.fireToast("I love Material Design");
043 * }
044 * </pre>
045 * </p>
046 *
047 * @author kevzlou7979
048 * @author Ben Dol
049 *
050 * @see <a href="http://gwtmaterialdesign.github.io/gwt-material-demo/#dialogs">Material Toast</a>
051 * @see <a href="https://material.io/guidelines/components/snackbars-toasts.html">Material Design Specification</a>
052 */
053//@formatter:on
054public class MaterialToast {
055
056    public static final int DURATION = 4000;
057
058    private Functions.Func callback;
059    private Widget[] widgets;
060
061    public MaterialToast(Widget... widgets) {
062        this.widgets = widgets;
063    }
064
065    public MaterialToast(Functions.Func callback, Widget... widgets) {
066        this.callback = callback;
067        this.widgets = widgets;
068    }
069
070    /**
071     * Quick fire your toast.
072     *
073     * @param msg Message text for your toast.
074     */
075    public static void fireToast(String msg) {
076        fireToast(msg, DURATION, null);
077    }
078
079    /**
080     * Quick fire your toast.
081     *
082     * @param msg        Message text for your toast.
083     * @param lifeMillis how long it should present itself before being removed.
084     */
085    public static void fireToast(String msg, int lifeMillis) {
086        fireToast(msg, lifeMillis, null);
087    }
088
089    /**
090     * Quick fire your toast.
091     *
092     * @param msg        Message text for your toast.
093     * @param lifeMillis how long it should present itself before being removed.
094     * @param className  class name to custom style your toast.
095     */
096    public static void fireToast(String msg, int lifeMillis, String className) {
097        new MaterialToast().toast(msg, lifeMillis, className);
098    }
099
100    /**
101     * Quick fire your toast.
102     *
103     * @param msg       Message text for your toast.
104     * @param className class name to custom style your toast.
105     */
106    public static void fireToast(String msg, String className) {
107        new MaterialToast().toast(msg, DURATION, className);
108    }
109
110    /**
111     * @param msg Message text for your toast.
112     */
113    public void toast(String msg) {
114        toast(msg, DURATION, null);
115    }
116
117    /**
118     * @param msg        Message text for your toast.
119     * @param lifeMillis how long it should present itself before being removed.
120     */
121    public void toast(String msg, int lifeMillis) {
122        toast(msg, lifeMillis, null);
123    }
124
125    /**
126     * @param msg       Message text for your toast.
127     * @param className class name to custom style your toast.
128     */
129    public void toast(String msg, String className) {
130        toast(msg, DURATION, className);
131    }
132
133    /**
134     * @param msg        Message text for your toast.
135     * @param lifeMillis how long it should present itself before being removed.
136     * @param className  class name to custom style your toast.
137     */
138    public void toast(String msg, int lifeMillis, String className) {
139        String genId = DOM.createUniqueId();
140        if (className == null) {
141            className = genId;
142        } else {
143            className += ' ' + genId;
144        }
145        toast(msg, lifeMillis, genId, className, callback);
146
147        if (widgets != null) {
148            for (Widget widget : widgets) {
149                widget.getElement().getStyle().setPaddingLeft(30, Unit.PX);
150                RootPanel.get(genId).add(widget);
151            }
152        }
153    }
154
155    protected void toast(String msg, int lifeMillis, String id, String className, Functions.Func callback) {
156        JsMaterialElement.toast(msg, lifeMillis, className, () -> {
157            if (callback != null) {
158                callback.call();
159            }
160        });
161        $(".toast." + id).attr("id", id);
162    }
163}