001: /* Messagebox.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Mon Jul 18 19:07:13 2005, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2004 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.zul;
020:
021: import java.util.Map;
022: import java.util.HashMap;
023:
024: import org.zkoss.mesg.Messages;
025:
026: import org.zkoss.zk.ui.WebApp;
027: import org.zkoss.zk.ui.Executions;
028: import org.zkoss.zk.ui.UiException;
029:
030: import org.zkoss.zul.impl.MessageboxDlg;
031:
032: /**
033: * Represents the message box.
034: *
035: * <p>You don't create {@link Messagebox} directly. Rather, use {@link #show}.
036: *
037: * <p>A non-XUL extension.
038: *
039: * @author tomyeh
040: */
041: public class Messagebox {
042: private static String _templ = "~./zul/html/messagebox.zul";
043:
044: /** A symbol consisting of a question mark in a circle. */
045: public static final String QUESTION = "~./zul/img/question.gif";
046: /** A symbol consisting of an exclamation point in a triangle with
047: * a yellow background.
048: */
049: public static final String EXCLAMATION = "~./zul/img/exclamation.gif";
050: /** A symbol of a lowercase letter i in a circle.
051: */
052: public static final String INFORMATION = "~./zul/img/information.gif";
053: /** A symbol consisting of a white X in a circle with a red background. */
054: public static final String ERROR = "~./zul/img/error.gif";
055: /** Contains no symbols. */
056: public static final String NONE = null;
057:
058: /** A OK button. */
059: public static final int OK = 0x0001;
060: /** A Cancel button. */
061: public static final int CANCEL = 0x0002;
062: /** A Yes button. */
063: public static final int YES = 0x0010;
064: /** A No button. */
065: public static final int NO = 0x0020;
066: /** A Abort button. */
067: public static final int ABORT = 0x0100;
068: /** A Retry button. */
069: public static final int RETRY = 0x0200;
070: /** A IGNORE button. */
071: public static final int IGNORE = 0x0400;
072:
073: /** Shows a message box and returns what button is pressed.
074: *
075: * @param title the title. If null, {@link WebApp#getAppName} is used.
076: * @param buttons a combination of {@link #OK}, {@link #CANCEL},
077: * {@link #YES}, {@link #NO}, {@link #ABORT}, {@link #RETRY},
078: * and {@link #IGNORE}. If zero, {@link #OK} is assumed
079: * @param icon one of predefined images: {@link #QUESTION},
080: * {@link #EXCLAMATION}, {@link #ERROR}, {@link #NONE}, or any URI of
081: * an image.
082: * @return the button being pressed (one of {@link #OK}, {@link #CANCEL},
083: * {@link #YES}, {@link #NO}, {@link #ABORT}, {@link #RETRY},
084: * and {@link #IGNORE}).
085: */
086: public static final int show(String message, String title,
087: int buttons, String icon) throws InterruptedException {
088: return show(message, title, buttons, icon, 0);
089: }
090:
091: /** Shows a message box and returns what button is pressed.
092: *
093: * @param title the title. If null, {@link WebApp#getAppName} is used.
094: * @param buttons a combination of {@link #OK}, {@link #CANCEL},
095: * {@link #YES}, {@link #NO}, {@link #ABORT}, {@link #RETRY},
096: * and {@link #IGNORE}. If zero, {@link #OK} is assumed
097: * @param icon one of predefined images: {@link #QUESTION},
098: * {@link #EXCLAMATION}, {@link #ERROR}, {@link #NONE}, or any URI of
099: * an image.
100: * @param focus one of button to have to focus. If 0, the first button
101: * will gain the focus. One of {@link #OK}, {@link #CANCEL},
102: * {@link #YES}, {@link #NO}, {@link #ABORT}, {@link #RETRY},
103: * and {@link #IGNORE}.
104: * @return the button being pressed (one of {@link #OK}, {@link #CANCEL},
105: * {@link #YES}, {@link #NO}, {@link #ABORT}, {@link #RETRY},
106: * and {@link #IGNORE}).
107: * @since 3.0.0
108: */
109: public static final int show(String message, String title,
110: int buttons, String icon, int focus)
111: throws InterruptedException {
112: final Map params = new HashMap();
113: params.put("message", message);
114: params.put("title", title != null ? title : Executions
115: .getCurrent().getDesktop().getWebApp().getAppName());
116: params.put("icon", icon);
117: params.put("buttons", new Integer((buttons & (OK | CANCEL | YES
118: | NO | ABORT | RETRY | IGNORE)) != 0 ? buttons : OK));
119: if ((buttons & OK) != 0)
120: params.put("OK", new Integer(OK));
121: if ((buttons & CANCEL) != 0)
122: params.put("CANCEL", new Integer(CANCEL));
123: if ((buttons & YES) != 0)
124: params.put("YES", new Integer(YES));
125: if ((buttons & NO) != 0)
126: params.put("NO", new Integer(NO));
127: if ((buttons & RETRY) != 0)
128: params.put("RETRY", new Integer(RETRY));
129: if ((buttons & ABORT) != 0)
130: params.put("ABORT", new Integer(ABORT));
131: if ((buttons & IGNORE) != 0)
132: params.put("IGNORE", new Integer(IGNORE));
133:
134: final MessageboxDlg dlg = (MessageboxDlg) Executions
135: .createComponents(_templ, null, params);
136: dlg.setButtons(buttons);
137: if (focus > 0)
138: dlg.setFocus(focus);
139:
140: if (dlg.getDesktop().getWebApp().getConfiguration()
141: .isEventThreadEnabled()) {
142: try {
143: dlg.doModal();
144: } catch (Throwable ex) {
145: dlg.detach();
146: if (ex instanceof InterruptedException)
147: throw (InterruptedException) ex;
148: throw UiException.Aide.wrap(ex);
149: }
150: return dlg.getResult();
151: } else {
152: dlg.doHighlighted();
153: return OK;
154: }
155: }
156:
157: /** Shows a message box and returns what button is pressed.
158: * A shortcut to show(message, null, OK, INFORMATION).
159: */
160: public static final int show(String message)
161: throws InterruptedException {
162: return show(message, null, OK, INFORMATION, 0);
163: }
164:
165: /** Shows a message box by specifying a message code, and returns what
166: * button is pressed.
167: *
168: * @param titleCode the message code for the title. If non-positive,
169: * the default title is used.
170: */
171: public static final int show(int messageCode, Object[] args,
172: int titleCode, int buttons, String icon)
173: throws InterruptedException {
174: return show(messageCode, args, titleCode, buttons, icon, 0);
175: }
176:
177: /** Shows a message box by specifying a message code, and returns what
178: * button is pressed.
179: *
180: * @param titleCode the message code for the title. If non-positive,
181: * the default title is used.
182: * @param focus one of button to have to focus. If 0, the first button
183: * will gain the focus. One of {@link #OK}, {@link #CANCEL},
184: * {@link #YES}, {@link #NO}, {@link #ABORT}, {@link #RETRY},
185: * and {@link #IGNORE}.
186: * @since 3.0.0
187: */
188: public static final int show(int messageCode, Object[] args,
189: int titleCode, int buttons, String icon, int focus)
190: throws InterruptedException {
191: return show(Messages.get(messageCode, args),
192: titleCode > 0 ? Messages.get(titleCode) : null,
193: buttons, icon, focus);
194: }
195:
196: /** Shows a message box by specifying a message code, and returns what
197: * button is pressed.
198: *
199: * @param titleCode the message code for the title. If non-positive,
200: * the default title is used.
201: */
202: public static final int show(int messageCode, Object arg,
203: int titleCode, int buttons, String icon)
204: throws InterruptedException {
205: return show(messageCode, arg, titleCode, buttons, icon, 0);
206: }
207:
208: /** Shows a message box by specifying a message code, and returns what
209: * button is pressed.
210: *
211: * @param titleCode the message code for the title. If non-positive,
212: * the default title is used.
213: * @param focus one of button to have to focus. If 0, the first button
214: * will gain the focus. One of {@link #OK}, {@link #CANCEL},
215: * {@link #YES}, {@link #NO}, {@link #ABORT}, {@link #RETRY},
216: * and {@link #IGNORE}.
217: * @since 3.0.0
218: */
219: public static final int show(int messageCode, Object arg,
220: int titleCode, int buttons, String icon, int focus)
221: throws InterruptedException {
222: return show(Messages.get(messageCode, arg),
223: titleCode > 0 ? Messages.get(titleCode) : null,
224: buttons, icon, focus);
225: }
226:
227: /** Shows a message box by specifying a message code, and returns what
228: * button is pressed.
229: *
230: * @param titleCode the message code for the title. If non-positive,
231: * the default title is used.
232: */
233: public static final int show(int messageCode, int titleCode,
234: int buttons, String icon) throws InterruptedException {
235: return show(messageCode, titleCode, buttons, icon, 0);
236: }
237:
238: /** Shows a message box by specifying a message code, and returns what
239: * button is pressed.
240: *
241: * @param titleCode the message code for the title. If non-positive,
242: * the default title is used.
243: * @param focus one of button to have to focus. If 0, the first button
244: * will gain the focus. One of {@link #OK}, {@link #CANCEL},
245: * {@link #YES}, {@link #NO}, {@link #ABORT}, {@link #RETRY},
246: * and {@link #IGNORE}.
247: * @since 3.0.0
248: */
249: public static final int show(int messageCode, int titleCode,
250: int buttons, String icon, int focus)
251: throws InterruptedException {
252: return show(Messages.get(messageCode), titleCode > 0 ? Messages
253: .get(titleCode) : null, buttons, icon, focus);
254: }
255:
256: /** Sets the template used to create the message dialog.
257: *
258: * <p>The template must follow the default template:
259: * ~./zul/html/messagebox.zul
260: *
261: * <p>In other words, just adjust the label and layout and don't
262: * change the component's ID.
263: */
264: public static void setTemplate(String uri) {
265: if (uri == null || uri.length() == 0)
266: throw new IllegalArgumentException("empty");
267: _templ = uri;
268: }
269:
270: /** Returns the template used to create the message dialog.
271: */
272: public static String getTemplate() {
273: return _templ;
274: }
275: }
|