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