KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > widgets > MessageBox


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.widgets;
12
13
14 import org.eclipse.swt.internal.win32.*;
15 import org.eclipse.swt.*;
16
17 /**
18  * Instances of this class are used to inform or warn the user.
19  * <dl>
20  * <dt><b>Styles:</b></dt>
21  * <dd>ICON_ERROR, ICON_INFORMATION, ICON_QUESTION, ICON_WARNING, ICON_WORKING</dd>
22  * <dd>OK, OK | CANCEL</dd>
23  * <dd>YES | NO, YES | NO | CANCEL</dd>
24  * <dd>RETRY | CANCEL</dd>
25  * <dd>ABORT | RETRY | IGNORE</dd>
26  * <dt><b>Events:</b></dt>
27  * <dd>(none)</dd>
28  * </dl>
29  * <p>
30  * Note: Only one of the styles ICON_ERROR, ICON_INFORMATION, ICON_QUESTION,
31  * ICON_WARNING and ICON_WORKING may be specified.
32  * </p><p>
33  * IMPORTANT: This class is intended to be subclassed <em>only</em>
34  * within the SWT implementation.
35  * </p>
36  */

37 public class MessageBox extends Dialog {
38     String JavaDoc message = "";
39     
40 /**
41  * Constructs a new instance of this class given only its parent.
42  *
43  * @param parent a shell which will be the parent of the new instance
44  *
45  * @exception IllegalArgumentException <ul>
46  * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
47  * </ul>
48  * @exception SWTException <ul>
49  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
50  * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
51  * </ul>
52  */

53 public MessageBox (Shell parent) {
54     this (parent, SWT.OK | SWT.ICON_INFORMATION | SWT.APPLICATION_MODAL);
55 }
56
57 /**
58  * Constructs a new instance of this class given its parent
59  * and a style value describing its behavior and appearance.
60  * <p>
61  * The style value is either one of the style constants defined in
62  * class <code>SWT</code> which is applicable to instances of this
63  * class, or must be built by <em>bitwise OR</em>'ing together
64  * (that is, using the <code>int</code> "|" operator) two or more
65  * of those <code>SWT</code> style constants. The class description
66  * lists the style constants that are applicable to the class.
67  * Style bits are also inherited from superclasses.
68  *
69  * @param parent a shell which will be the parent of the new instance
70  * @param style the style of dialog to construct
71  *
72  * @exception IllegalArgumentException <ul>
73  * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
74  * </ul>
75  * @exception SWTException <ul>
76  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
77  * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
78  * </ul>
79  */

80 public MessageBox (Shell parent, int style) {
81     super (parent, checkStyle (style));
82     checkSubclass ();
83 }
84
85 static int checkStyle (int style) {
86     if ((style & (SWT.PRIMARY_MODAL | SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) == 0) style |= SWT.APPLICATION_MODAL;
87     int mask = (SWT.YES | SWT.NO | SWT.OK | SWT.CANCEL | SWT.ABORT | SWT.RETRY | SWT.IGNORE);
88     int bits = style & mask;
89     if (bits == SWT.OK || bits == SWT.CANCEL || bits == (SWT.OK | SWT.CANCEL)) return style;
90     if (bits == SWT.YES || bits == SWT.NO || bits == (SWT.YES | SWT.NO) || bits == (SWT.YES | SWT.NO | SWT.CANCEL)) return style;
91     if (bits == (SWT.RETRY | SWT.CANCEL) || bits == (SWT.ABORT | SWT.RETRY | SWT.IGNORE)) return style;
92     style = (style & ~mask) | SWT.OK;
93     return style;
94 }
95
96 /**
97  * Returns the dialog's message, or an empty string if it does not have one.
98  * The message is a description of the purpose for which the dialog was opened.
99  * This message will be visible in the dialog while it is open.
100  *
101  * @return the message
102  */

103 public String JavaDoc getMessage () {
104     return message;
105 }
106
107 /**
108  * Makes the dialog visible and brings it to the front
109  * of the display.
110  *
111  * @return the ID of the button that was selected to dismiss the
112  * message box (e.g. SWT.OK, SWT.CANCEL, etc.)
113  *
114  * @exception SWTException <ul>
115  * <li>ERROR_WIDGET_DISPOSED - if the dialog has been disposed</li>
116  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the dialog</li>
117  * </ul>
118  */

119 public int open () {
120
121     /* Compute the MessageBox style */
122     int buttonBits = 0;
123     if ((style & SWT.OK) == SWT.OK) buttonBits = OS.MB_OK;
124     if ((style & (SWT.OK | SWT.CANCEL)) == (SWT.OK | SWT.CANCEL)) buttonBits = OS.MB_OKCANCEL;
125     if ((style & (SWT.YES | SWT.NO)) == (SWT.YES | SWT.NO)) buttonBits = OS.MB_YESNO;
126     if ((style & (SWT.YES | SWT.NO | SWT.CANCEL)) == (SWT.YES | SWT.NO | SWT.CANCEL)) buttonBits = OS.MB_YESNOCANCEL;
127     if ((style & (SWT.RETRY | SWT.CANCEL)) == (SWT.RETRY | SWT.CANCEL)) buttonBits = OS.MB_RETRYCANCEL;
128     if ((style & (SWT.ABORT | SWT.RETRY | SWT.IGNORE)) == (SWT.ABORT | SWT.RETRY | SWT.IGNORE)) buttonBits = OS.MB_ABORTRETRYIGNORE;
129     if (buttonBits == 0) buttonBits = OS.MB_OK;
130
131     int iconBits = 0;
132     if ((style & SWT.ICON_ERROR) != 0) iconBits = OS.MB_ICONERROR;
133     if ((style & SWT.ICON_INFORMATION) != 0) iconBits = OS.MB_ICONINFORMATION;
134     if ((style & SWT.ICON_QUESTION) != 0) iconBits = OS.MB_ICONQUESTION;
135     if ((style & SWT.ICON_WARNING) != 0) iconBits = OS.MB_ICONWARNING;
136     if ((style & SWT.ICON_WORKING) != 0) iconBits = OS.MB_ICONINFORMATION;
137
138     /* Only MB_APPLMODAL is supported on WinCE */
139     int modalBits = 0;
140     if (OS.IsWinCE) {
141         if ((style & (SWT.PRIMARY_MODAL | SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) {
142             modalBits = OS.MB_APPLMODAL;
143         }
144     } else {
145         if ((style & SWT.PRIMARY_MODAL) != 0) modalBits = OS.MB_APPLMODAL;
146         if ((style & SWT.APPLICATION_MODAL) != 0) modalBits = OS.MB_TASKMODAL;
147         if ((style & SWT.SYSTEM_MODAL) != 0) modalBits = OS.MB_SYSTEMMODAL;
148     }
149
150     int bits = buttonBits | iconBits | modalBits;
151     if ((style & SWT.RIGHT_TO_LEFT) != 0) bits |= OS.MB_RTLREADING;
152     if ((style & (SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT)) == 0) {
153         if (parent != null && (parent.style & SWT.MIRRORED) != 0) {
154             bits |= OS.MB_RTLREADING;
155         }
156     }
157     
158     /*
159     * Feature in Windows. System modal is not supported
160     * on Windows 95 and NT. The fix is to convert system
161     * modal to task modal.
162     */

163     if ((bits & OS.MB_SYSTEMMODAL) != 0) {
164         bits |= OS.MB_TASKMODAL;
165         bits &= ~OS.MB_SYSTEMMODAL;
166         /* Force a system modal message box to the front */
167         bits |= OS.MB_TOPMOST;
168     }
169
170     /*
171     * Feature in Windows. In order for MB_TASKMODAL to work,
172     * the parent HWND of the MessageBox () call must be NULL.
173     * If the parent is not NULL, MB_TASKMODAL behaves the
174     * same as MB_APPLMODAL. The fix to set the parent HWND
175     * anyway and not rely on MB_MODAL to work by making the
176     * parent be temporarily modal.
177     */

178     int hwndOwner = parent != null ? parent.handle : 0;
179     Shell oldModal = null;
180     Display display = null;
181     if ((bits & OS.MB_TASKMODAL) != 0) {
182         display = parent.getDisplay ();
183         oldModal = display.getModalDialogShell ();
184         display.setModalDialogShell (parent);
185     }
186
187     /* Open the message box */
188     /* Use the character encoding for the default locale */
189     TCHAR buffer1 = new TCHAR (0, message, true);
190     TCHAR buffer2 = new TCHAR (0, title, true);
191     int code = OS.MessageBox (hwndOwner, buffer1, buffer2, bits);
192     
193     /* Clear the temporarily dialog modal parent */
194     if ((bits & OS.MB_TASKMODAL) != 0) {
195         display.setModalDialogShell (oldModal);
196     }
197     
198     /*
199     * This code is intentionally commented. On some
200     * platforms, the owner window is repainted right
201     * away when a dialog window exits. This behavior
202     * is currently unspecified.
203     */

204 // if (hwndOwner != 0) OS.UpdateWindow (hwndOwner);
205

206     /* Compute and return the result */
207     if (code != 0) {
208         int type = bits & 0x0F;
209         if (type == OS.MB_OK) return SWT.OK;
210         if (type == OS.MB_OKCANCEL) {
211             return (code == OS.IDOK) ? SWT.OK : SWT.CANCEL;
212         }
213         if (type == OS.MB_YESNO) {
214             return (code == OS.IDYES) ? SWT.YES : SWT.NO;
215         }
216         if (type == OS.MB_YESNOCANCEL) {
217             if (code == OS.IDYES) return SWT.YES;
218             if (code == OS.IDNO) return SWT.NO;
219             return SWT.CANCEL;
220         }
221         if (type == OS.MB_RETRYCANCEL) {
222             return (code == OS.IDRETRY) ? SWT.RETRY : SWT.CANCEL;
223         }
224         if (type == OS.MB_ABORTRETRYIGNORE) {
225             if (code == OS.IDRETRY) return SWT.RETRY;
226             if (code == OS.IDABORT) return SWT.ABORT;
227             return SWT.IGNORE;
228         }
229     }
230     return SWT.CANCEL;
231 }
232
233 /**
234  * Sets the dialog's message, which is a description of
235  * the purpose for which it was opened. This message will be
236  * visible on the dialog while it is open.
237  *
238  * @param string the message
239  *
240  * @exception IllegalArgumentException <ul>
241  * <li>ERROR_NULL_ARGUMENT - if the string is null</li>
242  * </ul>
243  */

244 public void setMessage (String JavaDoc string) {
245     if (string == null) error (SWT.ERROR_NULL_ARGUMENT);
246     message = string;
247 }
248
249 }
250
Popular Tags