001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.swt.widgets;
011:
012: import org.eclipse.swt.*;
013: import org.eclipse.swt.internal.carbon.*;
014:
015: /**
016: * Instances of this class are used to inform or warn the user.
017: * <dl>
018: * <dt><b>Styles:</b></dt>
019: * <dd>ICON_ERROR, ICON_INFORMATION, ICON_QUESTION, ICON_WARNING, ICON_WORKING</dd>
020: * <dd>OK, OK | CANCEL</dd>
021: * <dd>YES | NO, YES | NO | CANCEL</dd>
022: * <dd>RETRY | CANCEL</dd>
023: * <dd>ABORT | RETRY | IGNORE</dd>
024: * <dt><b>Events:</b></dt>
025: * <dd>(none)</dd>
026: * </dl>
027: * <p>
028: * Note: Only one of the styles ICON_ERROR, ICON_INFORMATION, ICON_QUESTION,
029: * ICON_WARNING and ICON_WORKING may be specified.
030: * </p><p>
031: * IMPORTANT: This class is intended to be subclassed <em>only</em>
032: * within the SWT implementation.
033: * </p>
034: */
035: public class MessageBox extends Dialog {
036: String message = "";
037:
038: /**
039: * Constructs a new instance of this class given only its parent.
040: *
041: * @param parent a shell which will be the parent of the new instance
042: *
043: * @exception IllegalArgumentException <ul>
044: * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
045: * </ul>
046: * @exception SWTException <ul>
047: * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
048: * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
049: * </ul>
050: */
051: public MessageBox(Shell parent) {
052: this (parent, SWT.OK | SWT.ICON_INFORMATION
053: | SWT.APPLICATION_MODAL);
054: }
055:
056: /**
057: * Constructs a new instance of this class given its parent
058: * and a style value describing its behavior and appearance.
059: * <p>
060: * The style value is either one of the style constants defined in
061: * class <code>SWT</code> which is applicable to instances of this
062: * class, or must be built by <em>bitwise OR</em>'ing together
063: * (that is, using the <code>int</code> "|" operator) two or more
064: * of those <code>SWT</code> style constants. The class description
065: * lists the style constants that are applicable to the class.
066: * Style bits are also inherited from superclasses.
067: *
068: * @param parent a shell which will be the parent of the new instance
069: * @param style the style of dialog to construct
070: *
071: * @exception IllegalArgumentException <ul>
072: * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
073: * </ul>
074: * @exception SWTException <ul>
075: * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
076: * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
077: * </ul>
078: */
079: public MessageBox(Shell parent, int style) {
080: super (parent, checkStyle(style));
081: checkSubclass();
082: }
083:
084: static int checkStyle(int style) {
085: if ((style & (SWT.PRIMARY_MODAL | SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) == 0)
086: style |= SWT.APPLICATION_MODAL;
087: int mask = (SWT.YES | SWT.NO | SWT.OK | SWT.CANCEL | SWT.ABORT
088: | SWT.RETRY | SWT.IGNORE);
089: int bits = style & mask;
090: if (bits == SWT.OK || bits == SWT.CANCEL
091: || bits == (SWT.OK | SWT.CANCEL))
092: return style;
093: if (bits == SWT.YES || bits == SWT.NO
094: || bits == (SWT.YES | SWT.NO)
095: || bits == (SWT.YES | SWT.NO | SWT.CANCEL))
096: return style;
097: if (bits == (SWT.RETRY | SWT.CANCEL)
098: || bits == (SWT.ABORT | SWT.RETRY | SWT.IGNORE))
099: return style;
100: style = (style & ~mask) | SWT.OK;
101: return style;
102: }
103:
104: int createCFString(String id) {
105: String string = SWT.getMessage(id);
106: char[] buffer = new char[string.length()];
107: string.getChars(0, buffer.length, buffer, 0);
108: return OS.CFStringCreateWithCharacters(OS.kCFAllocatorDefault,
109: buffer, buffer.length);
110: }
111:
112: /**
113: * Returns the dialog's message, or an empty string if it does not have one.
114: * The message is a description of the purpose for which the dialog was opened.
115: * This message will be visible in the dialog while it is open.
116: *
117: * @return the message
118: */
119: public String getMessage() {
120: return message;
121: }
122:
123: /**
124: * Makes the dialog visible and brings it to the front
125: * of the display.
126: *
127: * @return the ID of the button that was selected to dismiss the
128: * message box (e.g. SWT.OK, SWT.CANCEL, etc.)
129: *
130: * @exception SWTException <ul>
131: * <li>ERROR_WIDGET_DISPOSED - if the dialog has been disposed</li>
132: * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the dialog</li>
133: * </ul>
134: */
135: public int open() {
136: int alertType = OS.kAlertPlainAlert;
137: if ((style & SWT.ICON_ERROR) != 0)
138: alertType = OS.kAlertStopAlert;
139: if ((style & SWT.ICON_INFORMATION) != 0)
140: alertType = OS.kAlertNoteAlert;
141: if ((style & SWT.ICON_QUESTION) != 0)
142: alertType = OS.kAlertNoteAlert;
143: if ((style & SWT.ICON_WARNING) != 0)
144: alertType = OS.kAlertCautionAlert;
145: if ((style & SWT.ICON_WORKING) != 0)
146: alertType = OS.kAlertNoteAlert;
147:
148: int error = 0;
149: int explanation = 0;
150: String errorString = title;
151: String explanationString = message;
152: if (errorString != null) {
153: char[] buffer = new char[errorString.length()];
154: errorString.getChars(0, buffer.length, buffer, 0);
155: error = OS.CFStringCreateWithCharacters(
156: OS.kCFAllocatorDefault, buffer, buffer.length);
157: }
158: if (explanationString != null) {
159: char[] buffer = new char[explanationString.length()];
160: explanationString.getChars(0, buffer.length, buffer, 0);
161: explanation = OS.CFStringCreateWithCharacters(
162: OS.kCFAllocatorDefault, buffer, buffer.length);
163: }
164:
165: AlertStdCFStringAlertParamRec param = new AlertStdCFStringAlertParamRec();
166: param.version = OS.kStdCFStringAlertVersionOne;
167: param.position = (short) OS.kWindowAlertPositionParentWindowScreen;
168: int defaultStr = 0, cancelStr = 0, otherStr = 0;
169: int mask = (SWT.YES | SWT.NO | SWT.OK | SWT.CANCEL | SWT.ABORT
170: | SWT.RETRY | SWT.IGNORE);
171: int bits = style & mask;
172: switch (bits) {
173: case SWT.OK:
174: param.defaultButton = (short) OS.kAlertStdAlertOKButton;
175: param.defaultText = OS.kAlertDefaultOKText;
176: break;
177: case SWT.CANCEL:
178: param.defaultButton = (short) OS.kAlertStdAlertOKButton;
179: param.defaultText = defaultStr = createCFString("SWT_Cancel");
180: break;
181: case SWT.OK | SWT.CANCEL:
182: param.defaultButton = (short) OS.kAlertStdAlertOKButton;
183: param.defaultText = OS.kAlertDefaultOKText;
184: param.cancelButton = (short) OS.kAlertStdAlertCancelButton;
185: param.cancelText = OS.kAlertDefaultCancelText;
186: break;
187: case SWT.YES:
188: param.defaultButton = (short) OS.kAlertStdAlertOKButton;
189: param.defaultText = defaultStr = createCFString("SWT_Yes");
190: break;
191: case SWT.NO:
192: param.cancelButton = (short) OS.kAlertStdAlertOKButton;
193: param.cancelText = defaultStr = createCFString("SWT_No");
194: break;
195: case SWT.YES | SWT.NO:
196: param.defaultButton = (short) OS.kAlertStdAlertOKButton;
197: param.defaultText = defaultStr = createCFString("SWT_Yes");
198: param.cancelButton = (short) OS.kAlertStdAlertCancelButton;
199: param.cancelText = cancelStr = createCFString("SWT_No");
200: break;
201: case SWT.YES | SWT.NO | SWT.CANCEL:
202: param.defaultButton = (short) OS.kAlertStdAlertOKButton;
203: param.defaultText = defaultStr = createCFString("SWT_Yes");
204: param.otherText = cancelStr = createCFString("SWT_No");
205: param.cancelButton = (short) OS.kAlertStdAlertCancelButton;
206: param.cancelText = OS.kAlertDefaultCancelText;
207: break;
208: case SWT.RETRY | SWT.CANCEL:
209: param.defaultButton = (short) OS.kAlertStdAlertOKButton;
210: param.defaultText = defaultStr = createCFString("SWT_Retry");
211: param.cancelButton = (short) OS.kAlertStdAlertCancelButton;
212: param.cancelText = OS.kAlertDefaultCancelText;
213: break;
214: case SWT.ABORT | SWT.RETRY | SWT.IGNORE:
215: param.defaultButton = (short) OS.kAlertStdAlertOKButton;
216: param.defaultText = defaultStr = createCFString("SWT_Abort");
217: param.otherText = cancelStr = createCFString("SWT_Retry");
218: param.cancelButton = (short) OS.kAlertStdAlertCancelButton;
219: param.cancelText = otherStr = createCFString("SWT_Ignore");
220: break;
221: }
222:
223: int[] dialogRef = new int[1];
224: OS.CreateStandardAlert((short) alertType, error, explanation,
225: param, dialogRef);
226: if (error != 0)
227: OS.CFRelease(error);
228: if (explanation != 0)
229: OS.CFRelease(explanation);
230: if (defaultStr != 0)
231: OS.CFRelease(defaultStr);
232: if (cancelStr != 0)
233: OS.CFRelease(cancelStr);
234: if (otherStr != 0)
235: OS.CFRelease(otherStr);
236:
237: if (dialogRef[0] != 0) {
238: /* Force a system modal message box to the front */
239: if ((style & SWT.SYSTEM_MODAL) != 0) {
240: OS.SetFrontProcessWithOptions(new int[] { 0,
241: OS.kCurrentProcess },
242: OS.kSetFrontProcessFrontWindowOnly);
243: }
244: short[] outItemHit = new short[1];
245: OS.RunStandardAlert(dialogRef[0], 0, outItemHit);
246: if (outItemHit[0] != 0) {
247: switch (bits) {
248: case SWT.OK:
249: return SWT.OK;
250: case SWT.CANCEL:
251: return SWT.CANCEL;
252: case SWT.OK | SWT.CANCEL:
253: if (outItemHit[0] == OS.kAlertStdAlertOKButton)
254: return SWT.OK;
255: return SWT.CANCEL;
256: case SWT.YES:
257: return SWT.YES;
258: case SWT.NO:
259: return SWT.NO;
260: case SWT.YES | SWT.NO:
261: if (outItemHit[0] == OS.kAlertStdAlertOKButton)
262: return SWT.YES;
263: return SWT.NO;
264: case SWT.YES | SWT.NO | SWT.CANCEL:
265: if (outItemHit[0] == OS.kAlertStdAlertOKButton)
266: return SWT.YES;
267: if (outItemHit[0] == OS.kAlertStdAlertOtherButton)
268: return SWT.NO;
269: return SWT.CANCEL;
270: case SWT.RETRY | SWT.CANCEL:
271: if (outItemHit[0] == OS.kAlertStdAlertOKButton)
272: return SWT.RETRY;
273: return SWT.CANCEL;
274: case SWT.ABORT | SWT.RETRY | SWT.IGNORE:
275: if (outItemHit[0] == OS.kAlertStdAlertOKButton)
276: return SWT.ABORT;
277: if (outItemHit[0] == OS.kAlertStdAlertOtherButton)
278: return SWT.RETRY;
279: return SWT.IGNORE;
280: }
281: }
282: }
283: return SWT.CANCEL;
284: }
285:
286: /**
287: * Sets the dialog's message, which is a description of
288: * the purpose for which it was opened. This message will be
289: * visible on the dialog while it is open.
290: *
291: * @param string the message
292: *
293: * @exception IllegalArgumentException <ul>
294: * <li>ERROR_NULL_ARGUMENT - if the string is null</li>
295: * </ul>
296: */
297: public void setMessage(String string) {
298: if (string == null)
299: error(SWT.ERROR_NULL_ARGUMENT);
300: message = string;
301: }
302:
303: }
|