comparison dwt/widgets/MessageBox.d @ 0:380af2bdd8e5

Upload of whole dwt tree
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 09 Aug 2008 17:00:02 +0200
parents
children 649b8e223d5a
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
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 module dwt.widgets.MessageBox;
12
13 import dwt.dwthelper.utils;
14
15
16 import dwt.DWT;
17 import dwt.DWTException;
18 import dwt.internal.cocoa.NSAlert;
19 import dwt.internal.cocoa.NSString;
20 import dwt.internal.cocoa.OS;
21
22 /**
23 * Instances of this class are used to inform or warn the user.
24 * <dl>
25 * <dt><b>Styles:</b></dt>
26 * <dd>ICON_ERROR, ICON_INFORMATION, ICON_QUESTION, ICON_WARNING, ICON_WORKING</dd>
27 * <dd>OK, OK | CANCEL</dd>
28 * <dd>YES | NO, YES | NO | CANCEL</dd>
29 * <dd>RETRY | CANCEL</dd>
30 * <dd>ABORT | RETRY | IGNORE</dd>
31 * <dt><b>Events:</b></dt>
32 * <dd>(none)</dd>
33 * </dl>
34 * <p>
35 * Note: Only one of the styles ICON_ERROR, ICON_INFORMATION, ICON_QUESTION,
36 * ICON_WARNING and ICON_WORKING may be specified.
37 * </p><p>
38 * IMPORTANT: This class is intended to be subclassed <em>only</em>
39 * within the DWT implementation.
40 * </p>
41 */
42 public class MessageBox extends Dialog {
43 String message = "";
44
45
46 /**
47 * Constructs a new instance of this class given only its parent.
48 *
49 * @param parent a shell which will be the parent of the new instance
50 *
51 * @exception IllegalArgumentException <ul>
52 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
53 * </ul>
54 * @exception DWTException <ul>
55 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
56 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
57 * </ul>
58 */
59 public MessageBox (Shell parent) {
60 this (parent, DWT.OK | DWT.ICON_INFORMATION | DWT.APPLICATION_MODAL);
61 }
62
63 /**
64 * Constructs a new instance of this class given its parent
65 * and a style value describing its behavior and appearance.
66 * <p>
67 * The style value is either one of the style constants defined in
68 * class <code>DWT</code> which is applicable to instances of this
69 * class, or must be built by <em>bitwise OR</em>'ing together
70 * (that is, using the <code>int</code> "|" operator) two or more
71 * of those <code>DWT</code> style constants. The class description
72 * lists the style constants that are applicable to the class.
73 * Style bits are also inherited from superclasses.
74 *
75 * @param parent a shell which will be the parent of the new instance
76 * @param style the style of dialog to construct
77 *
78 * @exception IllegalArgumentException <ul>
79 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
80 * </ul>
81 * @exception DWTException <ul>
82 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
83 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
84 * </ul>
85 */
86 public MessageBox (Shell parent, int style) {
87 super (parent, checkStyle (style));
88 checkSubclass ();
89 }
90
91 static int checkStyle (int style) {
92 if ((style & (DWT.PRIMARY_MODAL | DWT.APPLICATION_MODAL | DWT.SYSTEM_MODAL)) is 0) style |= DWT.APPLICATION_MODAL;
93 int mask = (DWT.YES | DWT.NO | DWT.OK | DWT.CANCEL | DWT.ABORT | DWT.RETRY | DWT.IGNORE);
94 int bits = style & mask;
95 if (bits is DWT.OK || bits is DWT.CANCEL || bits is (DWT.OK | DWT.CANCEL)) return style;
96 if (bits is DWT.YES || bits is DWT.NO || bits is (DWT.YES | DWT.NO) || bits is (DWT.YES | DWT.NO | DWT.CANCEL)) return style;
97 if (bits is (DWT.RETRY | DWT.CANCEL) || bits is (DWT.ABORT | DWT.RETRY | DWT.IGNORE)) return style;
98 style = (style & ~mask) | DWT.OK;
99 return style;
100 }
101
102 /**
103 * Returns the dialog's message, or an empty string if it does not have one.
104 * The message is a description of the purpose for which the dialog was opened.
105 * This message will be visible in the dialog while it is open.
106 *
107 * @return the message
108 */
109 public String getMessage () {
110 return message;
111 }
112
113 /**
114 * Makes the dialog visible and brings it to the front
115 * of the display.
116 *
117 * @return the ID of the button that was selected to dismiss the
118 * message box (e.g. DWT.OK, DWT.CANCEL, etc.)
119 *
120 * @exception DWTException <ul>
121 * <li>ERROR_WIDGET_DISPOSED - if the dialog has been disposed</li>
122 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the dialog</li>
123 * </ul>
124 */
125 public int open () {
126 int alertType = OS.NSInformationalAlertStyle;
127 if ((style & DWT.ICON_ERROR) !is 0) alertType = OS.NSCriticalAlertStyle;
128 if ((style & DWT.ICON_INFORMATION) !is 0) alertType = OS.NSInformationalAlertStyle;
129 if ((style & DWT.ICON_QUESTION) !is 0) alertType = OS.NSInformationalAlertStyle;
130 if ((style & DWT.ICON_WARNING) !is 0) alertType = OS.NSWarningAlertStyle;
131 if ((style & DWT.ICON_WORKING) !is 0) alertType = OS.NSInformationalAlertStyle;
132 NSString defaultButton = null, alternateButton = null, otherButton = null;
133 int mask = (DWT.YES | DWT.NO | DWT.OK | DWT.CANCEL | DWT.ABORT | DWT.RETRY | DWT.IGNORE);
134 int bits = style & mask;
135 switch (bits) {
136 case DWT.OK:
137 break;
138 case DWT.CANCEL:
139 defaultButton = NSString.stringWith(DWT.getMessage("SWT_Cancel"));
140 break;
141 case DWT.OK | DWT.CANCEL:
142 alternateButton = NSString.stringWith(DWT.getMessage("SWT_Cancel"));
143 break;
144 case DWT.YES:
145 defaultButton = NSString.stringWith(DWT.getMessage("SWT_Yes"));
146 break;
147 case DWT.NO:
148 defaultButton = NSString.stringWith(DWT.getMessage("SWT_No"));
149 break;
150 case DWT.YES | DWT.NO:
151 defaultButton = NSString.stringWith(DWT.getMessage("SWT_Yes"));
152 alternateButton = NSString.stringWith(DWT.getMessage("SWT_No"));
153 break;
154 case DWT.YES | DWT.NO | DWT.CANCEL:
155 defaultButton = NSString.stringWith(DWT.getMessage("SWT_Yes"));
156 alternateButton = NSString.stringWith(DWT.getMessage("SWT_No"));
157 otherButton = NSString.stringWith(DWT.getMessage("SWT_Cancel"));
158 break;
159 case DWT.RETRY | DWT.CANCEL:
160 defaultButton = NSString.stringWith(DWT.getMessage("SWT_Retry"));
161 alternateButton = NSString.stringWith(DWT.getMessage("SWT_Cancel"));
162 break;
163 case DWT.ABORT | DWT.RETRY | DWT.IGNORE:
164 defaultButton = NSString.stringWith(DWT.getMessage("SWT_Abort"));
165 alternateButton = NSString.stringWith(DWT.getMessage("SWT_Retry"));
166 otherButton = NSString.stringWith(DWT.getMessage("SWT_Ignore"));
167 break;
168 }
169 NSString title = NSString.stringWith(this.title !is null ? this.title : "");
170 NSString message = NSString.stringWith(this.message !is null ? this.message : "");
171 NSAlert alert = NSAlert.alertWithMessageText(title, defaultButton, alternateButton, otherButton, message);
172 alert.setAlertStyle(alertType);
173 int response = alert.runModal();
174 switch (bits) {
175 case DWT.OK:
176 switch (response) {
177 case OS.NSAlertDefaultReturn:
178 return DWT.OK;
179 }
180 break;
181 case DWT.CANCEL:
182 switch (response) {
183 case OS.NSAlertDefaultReturn:
184 return DWT.CANCEL;
185 }
186 break;
187 case DWT.OK | DWT.CANCEL:
188 switch (response) {
189 case OS.NSAlertDefaultReturn:
190 return DWT.OK;
191 case OS.NSAlertAlternateReturn:
192 return DWT.CANCEL;
193 }
194 break;
195 case DWT.YES:
196 switch (response) {
197 case OS.NSAlertDefaultReturn:
198 return DWT.YES;
199 }
200 break;
201 case DWT.NO:
202 switch (response) {
203 case OS.NSAlertDefaultReturn:
204 return DWT.NO;
205 }
206 break;
207 case DWT.YES | DWT.NO:
208 switch (response) {
209 case OS.NSAlertDefaultReturn:
210 return DWT.YES;
211 case OS.NSAlertAlternateReturn:
212 return DWT.NO;
213 }
214 break;
215 case DWT.YES | DWT.NO | DWT.CANCEL:
216 switch (response) {
217 case OS.NSAlertDefaultReturn:
218 return DWT.YES;
219 case OS.NSAlertAlternateReturn:
220 return DWT.NO;
221 case OS.NSAlertOtherReturn:
222 return DWT.CANCEL;
223 }
224 break;
225 case DWT.RETRY | DWT.CANCEL:
226 switch (response) {
227 case OS.NSAlertDefaultReturn:
228 return DWT.RETRY;
229 case OS.NSAlertAlternateReturn:
230 return DWT.CANCEL;
231 }
232 break;
233 case DWT.ABORT | DWT.RETRY | DWT.IGNORE:
234 switch (response) {
235 case OS.NSAlertDefaultReturn:
236 return DWT.ABORT;
237 case OS.NSAlertAlternateReturn:
238 return DWT.RETRY;
239 case OS.NSAlertOtherReturn:
240 return DWT.IGNORE;
241 }
242 break;
243 }
244 return DWT.CANCEL;
245 }
246
247 /**
248 * Sets the dialog's message, which is a description of
249 * the purpose for which it was opened. This message will be
250 * visible on the dialog while it is open.
251 *
252 * @param string the message
253 *
254 * @exception IllegalArgumentException <ul>
255 * <li>ERROR_NULL_ARGUMENT - if the string is null</li>
256 * </ul>
257 */
258 public void setMessage (String string) {
259 if (string is null) error (DWT.ERROR_NULL_ARGUMENT);
260 message = string;
261 }
262
263 }