001    package org.gwtbootstrap3.extras.bootbox.client;
002    
003    /*
004     * #%L
005     * GwtBootstrap3
006     * %%
007     * Copyright (C) 2013 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 com.google.gwt.core.client.JavaScriptObject;
024    import org.gwtbootstrap3.extras.bootbox.client.callback.AlertCallback;
025    import org.gwtbootstrap3.extras.bootbox.client.callback.ConfirmCallback;
026    import org.gwtbootstrap3.extras.bootbox.client.callback.PromptCallback;
027    import org.gwtbootstrap3.extras.bootbox.client.constants.BootboxSize;
028    
029    /**
030     * Created by kyle on 2013/12/11.
031     */
032    public class Bootbox {
033        /**
034         * Displays a message in a modal dialog box.
035         *
036         * @param msg the message to be displayed.
037         */
038        public static native void alert(String msg) /*-{
039            $wnd.bootbox.alert(msg);
040        }-*/;
041    
042        /**
043         * Displays a message in a modal dialog box.
044         * With callback handler.
045         *
046         * @param msg      the message to be displayed.
047         * @param callback the callback handler.
048         */
049        public static native void alert(String msg, AlertCallback callback) /*-{
050            $wnd.bootbox.alert(msg, function () {
051                callback.@org.gwtbootstrap3.extras.bootbox.client.callback.AlertCallback::callback()();
052            });
053        }-*/;
054    
055        /**
056         * Displays a message in a modal dialog box, along with the standard 'OK' and
057         * 'Cancel' buttons.
058         *
059         * @param msg      the message to be displayed.
060         * @param callback the callback handler.
061         */
062        public static native void confirm(String msg, ConfirmCallback callback) /*-{
063            $wnd.bootbox.confirm(msg, function (result) {
064                callback.@org.gwtbootstrap3.extras.bootbox.client.callback.ConfirmCallback::callback(Z)(result);
065            });
066        }-*/;
067    
068        /**
069         * Displays a request for information in a modal dialog box, along with the
070         * standard 'OK' and 'Cancel' buttons.
071         *
072         * @param msg      the message to be displayed.
073         * @param callback the callback handler.
074         */
075        public static native void prompt(String msg, PromptCallback callback) /*-{
076            $wnd.bootbox.prompt(msg, function (result) {
077                callback.@org.gwtbootstrap3.extras.bootbox.client.callback.PromptCallback::callback(Ljava/lang/String;)(result);
078            });
079        }-*/;
080    
081        /**
082         * Displays a completely customisable dialog in a modal dialog box.
083         *
084         * @param dialog      the dialog configuration.
085         */
086        public static native void dialog(Dialog dialog) /*-{
087            $wnd.bootbox.dialog(dialog);
088        }-*/;
089    
090        /**
091         * Hide all currently active bootbox dialogs. 
092         * <p>Individual dialogs can be closed as per normal Bootstrap dialogs: dialog.modal('hide').
093         */
094        public static native void hideAll() /*-{
095            $wnd.bootbox.hideAll();
096        }-*/;
097    
098        /**
099         * Creates a Defaults object.
100         */
101        public static Defaults createDefaults() {
102            return Defaults.create();
103        }
104    
105        /**
106         * Used to provide defaults configurations to Bootbox.
107         *
108         * @author Tercio Gaudencio Filho (terciofilho [at] gmail.com)
109         */
110        public static class Defaults extends JavaScriptObject {
111    
112            protected Defaults() {
113            }
114    
115            public static final Defaults create() {
116                return JavaScriptObject.createObject().cast();
117            }
118    
119            public final native Defaults setLocale(final String locale) /*-{
120                this.locale = locale;
121                return this;
122            }-*/;
123    
124            public final native Defaults setShow(final boolean show) /*-{
125                this.show = show;
126                return this;
127            }-*/;
128    
129            public final native Defaults setBackdrop(final boolean backdrop) /*-{
130                this.backdrop = backdrop;
131                return this;
132            }-*/;
133    
134            public final native Defaults setCloseButton(final boolean closeButton) /*-{
135                this.closeButton = closeButton;
136                return this;
137            }-*/;
138    
139            public final native Defaults setAnimate(final boolean animate) /*-{
140                this.animate = animate;
141                return this;
142            }-*/;
143    
144            public final native Defaults setClassName(final String className) /*-{
145                this.className = className;
146                return this;
147            }-*/;
148            
149            /**
150             * Define Bootbox defaults. Call this method to set the defaults in Bootbox. 
151             */
152            public final native void setDefaults() /*-{
153                $wnd.bootbox.setDefaults(this);
154            }-*/;
155    
156        }
157        
158        /**
159         * Used to provide a Dialog configuration.
160         * 
161         * @author Tercio Gaudencio Filho (terciofilho [at] gmail.com)
162         */
163        public static class Dialog extends JavaScriptObject {
164    
165            protected Dialog() {
166            }
167    
168            public static final Dialog create() {
169                return JavaScriptObject.createObject().cast();
170            }
171    
172            public final native Dialog setMessage(final String message) /*-{
173                this.message = message;
174                return this;
175            }-*/;
176    
177            public final native Dialog setTitle(final String title) /*-{
178                this.title = title;
179                return this;
180            }-*/;
181    
182            public final native Dialog setOnEscape(final AlertCallback callback) /*-{
183                this.onEscape = function() {
184                    callback.@org.gwtbootstrap3.extras.bootbox.client.callback.AlertCallback::callback()();
185                };
186                return this;
187            }-*/;
188    
189            public final native Dialog setBackdrop(final boolean backdrop) /*-{
190                this.backdrop = backdrop;
191                return this;
192            }-*/;
193    
194            public final native Dialog setCloseButton(final boolean closeButton) /*-{
195                this.closeButton = closeButton;
196                return this;
197            }-*/;
198    
199            public final native Dialog setAnimate(final boolean animate) /*-{
200                this.animate = animate;
201                return this;
202            }-*/;
203    
204            public final native Dialog setClassName(final String className) /*-{
205                this.className = className;
206                return this;
207            }-*/;
208    
209            public final native Dialog setSize(final BootboxSize size) /*-{
210                this.size = size.@org.gwtbootstrap3.extras.bootbox.client.constants.BootboxSize::getSize()();
211                return this;
212            }-*/;
213    
214            public final native Dialog addButton(String label , String className, AlertCallback callback) /*-{
215                this.buttons = this.buttons || {};
216                this.buttons[label] = {
217                    className: className,
218                    callback: function() {
219                        callback.@org.gwtbootstrap3.extras.bootbox.client.callback.AlertCallback::callback()();
220                    }
221                };
222                return this;
223            }-*/;
224    
225            public final native Dialog addButton(String label , String className) /*-{
226                this.buttons = this.buttons || {};
227                this.buttons[label] = {
228                    className: className
229                };
230                return this;
231            }-*/;
232    
233            public final native Dialog addButton(String label , AlertCallback callback) /*-{
234                this.buttons = this.buttons || {};
235                this.buttons[label] = {
236                    callback: function() {
237                        callback.@org.gwtbootstrap3.extras.bootbox.client.callback.AlertCallback::callback()();
238                    }
239                };
240                return this;
241            }-*/;
242    
243            public final native Dialog addButton(String label) /*-{
244                this.buttons = this.buttons || {};
245                this.buttons[label] = {};
246                return this;
247            }-*/;
248    
249            public final void show() {
250                Bootbox.dialog(this);
251            }
252    
253        }
254    
255    }