001    package org.gwtbootstrap3.extras.growl.client.ui;
002    
003    /*
004     * #%L
005     * GwtBootstrap3
006     * %%
007     * Copyright (C) 2013 - 2014 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    
023    import org.gwtbootstrap3.client.ui.constants.IconType;
024    import org.gwtbootstrap3.client.ui.constants.Styles;
025    
026    import com.google.gwt.core.client.JavaScriptObject;
027    
028    /**
029     * This class represent instance of displayed Growl.
030     * <p/>
031     * You can display new Growl using static methods, e.g.:
032     * {@see #growl(String)},
033     * {@see #growl(String, GrowlType)},
034     * {@see #growl(String, GrowlOptions)} and others
035     * <p/>
036     * To further configure Growl before displaying see:
037     * {@see org.gwtbootstrap3.extras.growl.client.ui.GrowlOptions}
038     * <p/>
039     * You can update displayed Growl by:
040     * {@see #updateTitle(String)},
041     * {@see #updateMessage(String)},
042     * {@see #updateIcon(String)},
043     * {@see #updateType(GrowlType)},
044     * <p/>
045     * You can hide displayed Growl:
046     * {@see #hide()},
047     * {@see #hideAll()},
048     * {@see #hideAll(GrowlPosition)}
049     *
050     * @author jeffisenhart
051     * @author Sven Jacobs
052     * @author Joshua Godi
053     * @author Pavel Zlámal
054     */
055    public class Growl extends JavaScriptObject {
056    
057        protected Growl() {
058        }
059    
060        /**
061         * Display Growl with custom message and default settings.
062         *
063         * @param message Message to set
064         * @return Displayed Growl for update or hiding.
065         */
066        public static final native Growl growl(final String message) /*-{
067            return $wnd.jQuery.growl({ message: message }, null);
068        }-*/;
069    
070        /**
071         * Display Growl with custom title, message and default settings.
072         *
073         * @param title   Title to set
074         * @param message Message to set
075         * @return Displayed Growl for update or hiding.
076         */
077        public static final native Growl growl(final String title, final String message) /*-{
078            return $wnd.jQuery.growl({ title: title, message: message }, null);
079        }-*/;
080    
081        /**
082         * Display Growl with custom title, message, icon and default settings.
083         *
084         * @param title   Title to set
085         * @param message Message to set
086         * @param icon    Icon to set
087         * @return Displayed Growl for update or hiding.
088         */
089        public static final native Growl growl(final String title, final String message, final String icon) /*-{
090            return $wnd.jQuery.growl({ title: title, message: message, icon: icon }, null);
091        }-*/;
092    
093        /**
094         * Display Growl with custom title, message, icon and default settings.
095         *
096         * @param title    Title to set
097         * @param message  Message to set
098         * @param iconType IconType to set
099         * @return Displayed Growl for update or hiding.
100         */
101        public static final Growl growl(final String title, final String message, final IconType iconType) {
102            return Growl.growl(title, message, Styles.FONT_AWESOME_BASE + " " + iconType.getCssName());
103        }
104    
105        /**
106         * Display Growl with custom title, message, icon and default settings.
107         *
108         * @param title   Title to set
109         * @param message Message to set
110         * @param icon    IconType to set
111         * @param url     Url to set
112         * @return Displayed Growl for update or hiding.
113         */
114        public static final native Growl growl(final String title, final String message, final String icon, final String url) /*-{
115            return $wnd.jQuery.growl({ title: title, message: message, icon: icon, url: url }, null);
116        }-*/;
117    
118        /**
119         * Display Growl with custom title, message, icon, url and default settings.
120         *
121         * @param title    Title to set
122         * @param message  Message to set
123         * @param iconType IconType to set
124         * @param url      Url to set
125         * @return Displayed Growl for update or hiding.
126         */
127        public static final Growl growl(final String title, final String message, final IconType iconType, final String url) {
128            return Growl.growl(title, message, Styles.FONT_AWESOME_BASE + " " + iconType.getCssName(), url);
129        }
130    
131        /**
132         * Display Growl with custom message, type and default settings.
133         *
134         * @param message Message to set
135         * @param type    GrowlType
136         * @return Displayed Growl for update or hiding.
137         * @see org.gwtbootstrap3.extras.growl.client.ui.GrowlType
138         */
139        public static final native Growl growl(final String message, final GrowlType type) /*-{
140            return $wnd.jQuery.growl({ message: message }, { type: type.@org.gwtbootstrap3.extras.growl.client.ui.GrowlType::getCssName()() });
141        }-*/;
142    
143        /**
144         * Display Growl with custom title, message, type and default settings.
145         *
146         * @param title   Title to set
147         * @param message Message to set
148         * @param type    GrowlType
149         * @return Displayed Growl for update or hiding.
150         * @see org.gwtbootstrap3.extras.growl.client.ui.GrowlType
151         */
152        public static final native Growl growl(final String title, final String message, final GrowlType type) /*-{
153            return $wnd.jQuery.growl({ title: title, message: message }, { type: type.@org.gwtbootstrap3.extras.growl.client.ui.GrowlType::getCssName()() });
154        }-*/;
155    
156        /**
157         * Display Growl with custom title, message, icon, type and default settings.
158         *
159         * @param title   Title to set
160         * @param message Message to set
161         * @param icon    Icon to set
162         * @param type    GrowlType
163         * @return Displayed Growl for update or hiding.
164         * @see org.gwtbootstrap3.extras.growl.client.ui.GrowlType
165         */
166        public static final native Growl growl(final String title, final String message, final String icon, final GrowlType type) /*-{
167            return $wnd.jQuery.growl({ title: title, message: message, icon: icon }, { type: type.@org.gwtbootstrap3.extras.growl.client.ui.GrowlType::getCssName()() });
168        }-*/;
169    
170        /**
171         * Display Growl with custom title, message, icon, type and default settings.
172         *
173         * @param title    Title to set
174         * @param message  Message to set
175         * @param iconType IconType to set (css name of icon form FONT AWESOME)
176         * @param type     GrowlType
177         * @return Displayed Growl for update or hiding.
178         * @see org.gwtbootstrap3.extras.growl.client.ui.GrowlType
179         */
180        public static final Growl growl(final String title, final String message, final IconType iconType, final GrowlType type) {
181            return Growl.growl(title, message, Styles.FONT_AWESOME_BASE + " " + iconType.getCssName(), type);
182        }
183    
184        /**
185         * Display Growl with custom title, message, icon, url, type and default settings.
186         *
187         * @param title   Title to set
188         * @param message Message to set
189         * @param icon    Icon to set
190         * @param url     Url to set
191         * @param type    GrowlType
192         * @return Displayed Growl for update or hiding.
193         * @see org.gwtbootstrap3.extras.growl.client.ui.GrowlType
194         */
195        public static final native Growl growl(final String title, final String message, final String icon, final String url, final GrowlType type) /*-{
196            return $wnd.jQuery.growl({ title: title, message: message, icon: icon, url: url }, { type: type.@org.gwtbootstrap3.extras.growl.client.ui.GrowlType::getCssName()() });
197        }-*/;
198    
199        /**
200         * Display Growl with custom title, message, icon, url, type and default settings.
201         *
202         * @param title    Title to set
203         * @param message  Message to set
204         * @param iconType IconType to set (css name of icon form FONT AWESOME)
205         * @param url      Url to set
206         * @param type     GrowlType
207         * @return Displayed Growl for update or hiding.
208         * @see org.gwtbootstrap3.extras.growl.client.ui.GrowlType
209         */
210        public static final Growl growl(final String title, final String message, final IconType iconType, final String url, final GrowlType type) {
211            return Growl.growl(title, message, Styles.FONT_AWESOME_BASE + " " + iconType.getCssName(), url, type);
212        }
213    
214        /**
215         * Display Growl with custom message and custom settings.
216         *
217         * @param message Message to set
218         * @param options custom options
219         * @return Displayed Growl for update or hiding.
220         * @see org.gwtbootstrap3.extras.growl.client.ui.GrowlOptions
221         */
222        public static final native Growl growl(final String message, final GrowlOptions options) /*-{
223            return $wnd.jQuery.growl({ message: message }, options);
224        }-*/;
225    
226        /**
227         * Display Growl with custom title, message and custom settings.
228         *
229         * @param title   Title to set
230         * @param message Message to set
231         * @param options custom options
232         * @return Displayed Growl for update or hiding.
233         * @see org.gwtbootstrap3.extras.growl.client.ui.GrowlOptions
234         */
235        public static final native Growl growl(final String title, final String message, final GrowlOptions options) /*-{
236            return $wnd.jQuery.growl({ title: title, message: message }, options);
237        }-*/;
238    
239        /**
240         * Display Growl with custom title, message, icon and custom settings.
241         *
242         * @param title   Title to set
243         * @param message Message to set
244         * @param icon    Icon to set
245         * @param options custom options
246         * @return Displayed Growl for update or hiding.
247         * @see org.gwtbootstrap3.extras.growl.client.ui.GrowlOptions
248         */
249        public static final native Growl growl(final String title, final String message, final String icon, final GrowlOptions options) /*-{
250            return $wnd.jQuery.growl({ title: title, message: message, icon: icon }, options);
251        }-*/;
252    
253        /**
254         * Display Growl with custom title, message, icon and custom settings.
255         *
256         * @param title    Title to set
257         * @param message  Message to set
258         * @param iconType IconType to set (css name of icon form FONT AWESOME)
259         * @param options  custom options
260         * @return Displayed Growl for update or hiding.
261         * @see org.gwtbootstrap3.extras.growl.client.ui.GrowlOptions
262         */
263        public static final Growl growl(final String title, final String message, final IconType iconType, final GrowlOptions options) {
264            return Growl.growl(title, message, Styles.FONT_AWESOME_BASE + " " + iconType.getCssName(), options);
265        }
266    
267        /**
268         * Display Growl with custom title, message, icon, URL and custom settings.
269         *
270         * @param title   Title to set
271         * @param message Message to set
272         * @param icon    Icon to set
273         * @param url     Url to set
274         * @param options custom options
275         * @return Displayed Growl for update or hiding.
276         * @see org.gwtbootstrap3.extras.growl.client.ui.GrowlOptions
277         */
278        public static final native Growl growl(final String title, final String message, final String icon, final String url, final GrowlOptions options) /*-{
279            return $wnd.jQuery.growl({ title: title, message: message, icon: icon, url: url}, options);
280        }-*/;
281    
282        /**
283         * Display Growl with custom title, message, icon, URL and custom settings.
284         *
285         * @param title    Title to set
286         * @param message  Message to set
287         * @param iconType IconType to set
288         * @param url      Url to set
289         * @param options  custom options
290         * @return Displayed Growl for update or hiding.
291         * @see org.gwtbootstrap3.extras.growl.client.ui.GrowlOptions
292         */
293        public static final Growl growl(final String title, final String message, final IconType iconType, final String url, final GrowlOptions options) {
294            return Growl.growl(title, message, Styles.FONT_AWESOME_BASE + " " + iconType.getCssName(), url, options);
295        }
296    
297        /**
298         * Hide all displayed Growls.
299         */
300        public static final void hideAll() {
301            hideAllGrowls(null);
302        }
303    
304        /**
305         * Hide all displayed Growls on specific screen location.
306         *
307         * @param position Growls position on screen.
308         * @see org.gwtbootstrap3.extras.growl.client.ui.GrowlPosition
309         */
310        public static final void hideAll(GrowlPosition position) {
311            if (position != null) {
312                hideAllGrowls(position.getPosition());
313            }
314        }
315    
316        /**
317         * Hide all displayed Growls, optionally only on specified position.
318         *
319         * @param position
320         */
321        private static final native void hideAllGrowls(String position) /*-{
322    
323            if (position !== null) {
324                $wnd.jQuery.growl(false, {
325                    command: 'closeAll',
326                    position: position
327                });
328            } else {
329                $wnd.jQuery.growl(false, {
330                    command: 'closeAll'
331                });
332            }
333    
334        }-*/;
335    
336        /**
337         * Updates title parameter of once displayed Growl.
338         *
339         * @param title Title to set
340         */
341        public final native void updateTitle(String title) /*-{
342            this.update('title', title);
343        }-*/;
344    
345        /**
346         * Updates message parameter of once displayed Growl.
347         *
348         * @param message Message to set
349         */
350        public final native void updateMessage(String message) /*-{
351            this.update('message', message);
352        }-*/;
353    
354        /**
355         * Updates Icon parameter of once displayed Growl.
356         *
357         * @param icon Icon to set
358         */
359        public final native void updateIcon(String icon) /*-{
360            this.update('icon', icon);
361        }-*/;
362    
363        /**
364         * Updates Icon parameter of once displayed Growl.
365         * This method is shortcut when using FONT AWESOME iconic font.
366         *
367         * @param type IconType to get CSS class name to set
368         */
369        public final void updateIcon(final IconType type) {
370            if (type != null) updateIcon(Styles.FONT_AWESOME_BASE + " " + type.getCssName());
371        }
372    
373        /**
374         * Update type of once displayed Growl (CSS style class name).
375         *
376         * @param type one of INFO, WARNING, DANGER, SUCCESS
377         * @see org.gwtbootstrap3.extras.growl.client.ui.GrowlType
378         */
379        public final void updateType(final GrowlType type) {
380            if (type != null) {
381                updateType(type.getCssName());
382            }
383        }
384    
385        /**
386         * Update type of once displayed Growl (CSS style class name).
387         * Resulting class name to use is "alert-[type]".
388         *
389         * @param type CSS class name to set
390         */
391        private final native void updateType(String type) /*-{
392            this.update('type', type);
393        }-*/;
394    
395        /**
396         * Hide this Growl.
397         */
398        public final native void hide() /*-{
399            this.close();
400        }-*/;
401    
402    }