21
|
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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.dialogs.MessageDialog;
|
|
14
|
|
15 import dwtx.jface.dialogs.IconAndMessageDialog;
|
|
16 import dwtx.jface.dialogs.IDialogConstants;
|
|
17
|
|
18 import dwt.DWT;
|
|
19 import dwt.custom.CLabel;
|
|
20 import dwt.graphics.Image;
|
|
21 import dwt.layout.GridData;
|
|
22 import dwt.layout.GridLayout;
|
|
23 import dwt.widgets.Button;
|
|
24 import dwt.widgets.Composite;
|
|
25 import dwt.widgets.Control;
|
|
26 import dwt.widgets.Label;
|
|
27 import dwt.widgets.Shell;
|
|
28
|
|
29 import dwt.dwthelper.utils;
|
|
30
|
|
31 /**
|
|
32 * A dialog for showing messages to the user.
|
|
33 * <p>
|
|
34 * This concrete dialog class can be instantiated as is, or further subclassed
|
|
35 * as required.
|
|
36 * </p>
|
|
37 */
|
|
38 public class MessageDialog : IconAndMessageDialog {
|
|
39 /**
|
|
40 * Constant for a dialog with no image (value 0).
|
|
41 */
|
|
42 public const static int NONE = 0;
|
|
43
|
|
44 /**
|
|
45 * Constant for a dialog with an error image (value 1).
|
|
46 */
|
|
47 public const static int ERROR = 1;
|
|
48
|
|
49 /**
|
|
50 * Constant for a dialog with an info image (value 2).
|
|
51 */
|
|
52 public const static int INFORMATION = 2;
|
|
53
|
|
54 /**
|
|
55 * Constant for a dialog with a question image (value 3).
|
|
56 */
|
|
57 public const static int QUESTION = 3;
|
|
58
|
|
59 /**
|
|
60 * Constant for a dialog with a warning image (value 4).
|
|
61 */
|
|
62 public const static int WARNING = 4;
|
|
63
|
|
64 /**
|
|
65 * Labels for buttons in the button bar (localized strings).
|
|
66 */
|
|
67 private String[] buttonLabels;
|
|
68
|
|
69 /**
|
|
70 * The buttons. Parallels <code>buttonLabels</code>.
|
|
71 */
|
|
72 private Button[] buttons;
|
|
73
|
|
74 /**
|
|
75 * Index into <code>buttonLabels</code> of the default button.
|
|
76 */
|
|
77 private int defaultButtonIndex;
|
|
78
|
|
79 /**
|
|
80 * Dialog title (a localized string).
|
|
81 */
|
|
82 private String title;
|
|
83
|
|
84 /**
|
|
85 * Dialog title image.
|
|
86 */
|
|
87 private Image titleImage;
|
|
88
|
|
89 /**
|
|
90 * Image, or <code>null</code> if none.
|
|
91 */
|
|
92 private Image image = null;
|
|
93
|
|
94 /**
|
|
95 * The custom dialog area.
|
|
96 */
|
|
97 private Control customArea;
|
|
98
|
|
99 /**
|
|
100 * Create a message dialog. Note that the dialog will have no visual
|
|
101 * representation (no widgets) until it is told to open.
|
|
102 * <p>
|
|
103 * The labels of the buttons to appear in the button bar are supplied in
|
|
104 * this constructor as an array. The <code>open</code> method will return
|
|
105 * the index of the label in this array corresponding to the button that was
|
|
106 * pressed to close the dialog. If the dialog was dismissed without pressing
|
|
107 * a button (ESC, etc.) then -1 is returned. Note that the <code>open</code>
|
|
108 * method blocks.
|
|
109 * </p>
|
|
110 *
|
|
111 * @param parentShell
|
|
112 * the parent shell
|
|
113 * @param dialogTitle
|
|
114 * the dialog title, or <code>null</code> if none
|
|
115 * @param dialogTitleImage
|
|
116 * the dialog title image, or <code>null</code> if none
|
|
117 * @param dialogMessage
|
|
118 * the dialog message
|
|
119 * @param dialogImageType
|
|
120 * one of the following values:
|
|
121 * <ul>
|
|
122 * <li><code>MessageDialog.NONE</code> for a dialog with no
|
|
123 * image</li>
|
|
124 * <li><code>MessageDialog.ERROR</code> for a dialog with an
|
|
125 * error image</li>
|
|
126 * <li><code>MessageDialog.INFORMATION</code> for a dialog
|
|
127 * with an information image</li>
|
|
128 * <li><code>MessageDialog.QUESTION </code> for a dialog with a
|
|
129 * question image</li>
|
|
130 * <li><code>MessageDialog.WARNING</code> for a dialog with a
|
|
131 * warning image</li>
|
|
132 * </ul>
|
|
133 * @param dialogButtonLabels
|
|
134 * an array of labels for the buttons in the button bar
|
|
135 * @param defaultIndex
|
|
136 * the index in the button label array of the default button
|
|
137 */
|
|
138 public this(Shell parentShell, String dialogTitle,
|
|
139 Image dialogTitleImage, String dialogMessage, int dialogImageType,
|
|
140 String[] dialogButtonLabels, int defaultIndex) {
|
|
141 super(parentShell);
|
|
142 this.title = dialogTitle;
|
|
143 this.titleImage = dialogTitleImage;
|
|
144 this.message = dialogMessage;
|
|
145
|
|
146 switch (dialogImageType) {
|
|
147 case ERROR: {
|
|
148 this.image = getErrorImage();
|
|
149 break;
|
|
150 }
|
|
151 case INFORMATION: {
|
|
152 this.image = getInfoImage();
|
|
153 break;
|
|
154 }
|
|
155 case QUESTION: {
|
|
156 this.image = getQuestionImage();
|
|
157 break;
|
|
158 }
|
|
159 case WARNING: {
|
|
160 this.image = getWarningImage();
|
|
161 break;
|
|
162 }
|
|
163 }
|
|
164 this.buttonLabels = dialogButtonLabels;
|
|
165 this.defaultButtonIndex = defaultIndex;
|
|
166 }
|
|
167
|
|
168 /*
|
|
169 * (non-Javadoc)
|
|
170 * @see dwtx.jface.dialogs.Dialog#buttonPressed(int)
|
|
171 */
|
|
172 protected void buttonPressed(int buttonId) {
|
|
173 setReturnCode(buttonId);
|
|
174 close();
|
|
175 }
|
|
176
|
|
177 /*
|
|
178 * (non-Javadoc)
|
|
179 * @see dwtx.jface.window.Window#configureShell(dwt.widgets.Shell)
|
|
180 */
|
|
181 protected void configureShell(Shell shell) {
|
|
182 super.configureShell(shell);
|
|
183 if (title !is null) {
|
|
184 shell.setText(title);
|
|
185 }
|
|
186 if (titleImage !is null) {
|
|
187 shell.setImage(titleImage);
|
|
188 }
|
|
189 }
|
|
190
|
|
191 /*
|
|
192 * (non-Javadoc) Method declared on Dialog.
|
|
193 */
|
|
194 protected void createButtonsForButtonBar(Composite parent) {
|
|
195 buttons = new Button[buttonLabels.length];
|
|
196 for (int i = 0; i < buttonLabels.length; i++) {
|
|
197 String label = buttonLabels[i];
|
|
198 Button button = createButton(parent, i, label,
|
|
199 defaultButtonIndex is i);
|
|
200 buttons[i] = button;
|
|
201 }
|
|
202 }
|
|
203
|
|
204 /**
|
|
205 * Creates and returns the contents of an area of the dialog which appears
|
|
206 * below the message and above the button bar.
|
|
207 * <p>
|
|
208 * The default implementation of this framework method returns
|
|
209 * <code>null</code>. Subclasses may override.
|
|
210 * </p>
|
|
211 *
|
|
212 * @param parent
|
|
213 * parent composite to contain the custom area
|
|
214 * @return the custom area control, or <code>null</code>
|
|
215 */
|
|
216 protected Control createCustomArea(Composite parent) {
|
|
217 return null;
|
|
218 }
|
|
219
|
|
220 /**
|
|
221 * This implementation of the <code>Dialog</code> framework method creates
|
|
222 * and lays out a composite and calls <code>createMessageArea</code> and
|
|
223 * <code>createCustomArea</code> to populate it. Subclasses should
|
|
224 * override <code>createCustomArea</code> to add contents below the
|
|
225 * message.
|
|
226 */
|
|
227 protected Control createDialogArea(Composite parent) {
|
|
228 // create message area
|
|
229 createMessageArea(parent);
|
|
230 // create the top level composite for the dialog area
|
|
231 Composite composite = new Composite(parent, DWT.NONE);
|
|
232 GridLayout layout = new GridLayout();
|
|
233 layout.marginHeight = 0;
|
|
234 layout.marginWidth = 0;
|
|
235 composite.setLayout(layout);
|
|
236 GridData data = new GridData(GridData.FILL_BOTH);
|
|
237 data.horizontalSpan = 2;
|
|
238 composite.setLayoutData(data);
|
|
239 // allow subclasses to add custom controls
|
|
240 customArea = createCustomArea(composite);
|
|
241 //If it is null create a dummy label for spacing purposes
|
|
242 if (customArea is null) {
|
|
243 customArea = new Label(composite, DWT.NULL);
|
|
244 }
|
|
245 return composite;
|
|
246 }
|
|
247
|
|
248 /**
|
|
249 * Gets a button in this dialog's button bar.
|
|
250 *
|
|
251 * @param index
|
|
252 * the index of the button in the dialog's button bar
|
|
253 * @return a button in the dialog's button bar
|
|
254 */
|
|
255 protected Button getButton(int index) {
|
|
256 return buttons[index];
|
|
257 }
|
|
258
|
|
259 /**
|
|
260 * Returns the minimum message area width in pixels This determines the
|
|
261 * minimum width of the dialog.
|
|
262 * <p>
|
|
263 * Subclasses may override.
|
|
264 * </p>
|
|
265 *
|
|
266 * @return the minimum message area width (in pixels)
|
|
267 */
|
|
268 protected int getMinimumMessageWidth() {
|
|
269 return convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
|
|
270 }
|
|
271
|
|
272 /**
|
|
273 * Handle the shell close. Set the return code to <code>DWT.DEFAULT</code>
|
|
274 * as there has been no explicit close by the user.
|
|
275 *
|
|
276 * @see dwtx.jface.window.Window#handleShellCloseEvent()
|
|
277 */
|
|
278 protected void handleShellCloseEvent() {
|
|
279 //Sets a return code of DWT.DEFAULT since none of the dialog buttons
|
|
280 // were pressed to close the dialog.
|
|
281 super.handleShellCloseEvent();
|
|
282 setReturnCode(DWT.DEFAULT);
|
|
283 }
|
|
284
|
|
285 /**
|
|
286 * Convenience method to open a simple confirm (OK/Cancel) dialog.
|
|
287 *
|
|
288 * @param parent
|
|
289 * the parent shell of the dialog, or <code>null</code> if none
|
|
290 * @param title
|
|
291 * the dialog's title, or <code>null</code> if none
|
|
292 * @param message
|
|
293 * the message
|
|
294 * @return <code>true</code> if the user presses the OK button,
|
|
295 * <code>false</code> otherwise
|
|
296 */
|
|
297 public static bool openConfirm(Shell parent, String title, String message) {
|
|
298 MessageDialog dialog = new MessageDialog(parent, title, null, // accept
|
|
299 // the
|
|
300 // default
|
|
301 // window
|
|
302 // icon
|
|
303 message, QUESTION, [ IDialogConstants.OK_LABEL,
|
|
304 IDialogConstants.CANCEL_LABEL ], 0); // OK is the
|
|
305 // default
|
|
306 return dialog.open() is 0;
|
|
307 }
|
|
308
|
|
309 /**
|
|
310 * Convenience method to open a standard error dialog.
|
|
311 *
|
|
312 * @param parent
|
|
313 * the parent shell of the dialog, or <code>null</code> if none
|
|
314 * @param title
|
|
315 * the dialog's title, or <code>null</code> if none
|
|
316 * @param message
|
|
317 * the message
|
|
318 */
|
|
319 public static void openError(Shell parent, String title, String message) {
|
|
320 MessageDialog dialog = new MessageDialog(parent, title, null, // accept
|
|
321 // the
|
|
322 // default
|
|
323 // window
|
|
324 // icon
|
|
325 message, ERROR, [ IDialogConstants.OK_LABEL ], 0); // ok
|
|
326 // is
|
|
327 // the
|
|
328 // default
|
|
329 dialog.open();
|
|
330 return;
|
|
331 }
|
|
332
|
|
333 /**
|
|
334 * Convenience method to open a standard information dialog.
|
|
335 *
|
|
336 * @param parent
|
|
337 * the parent shell of the dialog, or <code>null</code> if none
|
|
338 * @param title
|
|
339 * the dialog's title, or <code>null</code> if none
|
|
340 * @param message
|
|
341 * the message
|
|
342 */
|
|
343 public static void openInformation(Shell parent, String title,
|
|
344 String message) {
|
|
345 MessageDialog dialog = new MessageDialog(parent, title, null, // accept
|
|
346 // the
|
|
347 // default
|
|
348 // window
|
|
349 // icon
|
|
350 message, INFORMATION,
|
|
351 [ IDialogConstants.OK_LABEL ], 0);
|
|
352 // ok is the default
|
|
353 dialog.open();
|
|
354 return;
|
|
355 }
|
|
356
|
|
357 /**
|
|
358 * Convenience method to open a simple Yes/No question dialog.
|
|
359 *
|
|
360 * @param parent
|
|
361 * the parent shell of the dialog, or <code>null</code> if none
|
|
362 * @param title
|
|
363 * the dialog's title, or <code>null</code> if none
|
|
364 * @param message
|
|
365 * the message
|
|
366 * @return <code>true</code> if the user presses the OK button,
|
|
367 * <code>false</code> otherwise
|
|
368 */
|
|
369 public static bool openQuestion(Shell parent, String title,
|
|
370 String message) {
|
|
371 MessageDialog dialog = new MessageDialog(parent, title, null, // accept
|
|
372 // the
|
|
373 // default
|
|
374 // window
|
|
375 // icon
|
|
376 message, QUESTION, [ IDialogConstants.YES_LABEL,
|
|
377 IDialogConstants.NO_LABEL ], 0); // yes is the default
|
|
378 return dialog.open() is 0;
|
|
379 }
|
|
380
|
|
381 /**
|
|
382 * Convenience method to open a standard warning dialog.
|
|
383 *
|
|
384 * @param parent
|
|
385 * the parent shell of the dialog, or <code>null</code> if none
|
|
386 * @param title
|
|
387 * the dialog's title, or <code>null</code> if none
|
|
388 * @param message
|
|
389 * the message
|
|
390 */
|
|
391 public static void openWarning(Shell parent, String title, String message) {
|
|
392 MessageDialog dialog = new MessageDialog(parent, title, null, // accept
|
|
393 // the
|
|
394 // default
|
|
395 // window
|
|
396 // icon
|
|
397 message, WARNING, [ IDialogConstants.OK_LABEL ], 0); // ok
|
|
398 // is
|
|
399 // the
|
|
400 // default
|
|
401 dialog.open();
|
|
402 return;
|
|
403 }
|
|
404
|
|
405 /*
|
|
406 * @see dwtx.jface.dialogs.Dialog#createButton(dwt.widgets.Composite,
|
|
407 * int, java.lang.String, bool)
|
|
408 */
|
|
409 protected Button createButton(Composite parent, int id, String label,
|
|
410 bool defaultButton) {
|
|
411 Button button = super.createButton(parent, id, label, defaultButton);
|
|
412 //Be sure to set the focus if the custom area cannot so as not
|
|
413 //to lose the defaultButton.
|
|
414 if (defaultButton && !customShouldTakeFocus()) {
|
|
415 button.setFocus();
|
|
416 }
|
|
417 return button;
|
|
418 }
|
|
419
|
|
420 /**
|
|
421 * Return whether or not we should apply the workaround where we take focus
|
|
422 * for the default button or if that should be determined by the dialog. By
|
|
423 * default only return true if the custom area is a label or CLabel that
|
|
424 * cannot take focus.
|
|
425 *
|
|
426 * @return bool
|
|
427 */
|
|
428 protected bool customShouldTakeFocus() {
|
|
429 if (cast(Label) customArea ) {
|
|
430 return false;
|
|
431 }
|
|
432 if (cast(CLabel) customArea ) {
|
|
433 return (customArea.getStyle() & DWT.NO_FOCUS) > 0;
|
|
434 }
|
|
435 return true;
|
|
436 }
|
|
437
|
|
438 /*
|
|
439 * (non-Javadoc)
|
|
440 * @see dwtx.jface.dialogs.IconAndMessageDialog#getImage()
|
|
441 */
|
|
442 public Image getImage() {
|
|
443 return image;
|
|
444 }
|
|
445
|
|
446 /**
|
|
447 * An accessor for the labels to use on the buttons.
|
|
448 *
|
|
449 * @return The button labels to used; never <code>null</code>.
|
|
450 */
|
|
451 protected String[] getButtonLabels() {
|
|
452 return buttonLabels;
|
|
453 }
|
|
454
|
|
455 /**
|
|
456 * An accessor for the index of the default button in the button array.
|
|
457 *
|
|
458 * @return The default button index.
|
|
459 */
|
|
460 protected int getDefaultButtonIndex() {
|
|
461 return defaultButtonIndex;
|
|
462 }
|
|
463
|
|
464 /**
|
|
465 * A mutator for the array of buttons in the button bar.
|
|
466 *
|
|
467 * @param buttons
|
|
468 * The buttons in the button bar; must not be <code>null</code>.
|
|
469 */
|
|
470 protected void setButtons(Button[] buttons) {
|
|
471 if (buttons is null) {
|
|
472 throw new NullPointerException(
|
|
473 "The array of buttons cannot be null.");} //$NON-NLS-1$
|
|
474 this.buttons = buttons;
|
|
475 }
|
|
476
|
|
477 /**
|
|
478 * A mutator for the button labels.
|
|
479 *
|
|
480 * @param buttonLabels
|
|
481 * The button labels to use; must not be <code>null</code>.
|
|
482 */
|
|
483 protected void setButtonLabels(String[] buttonLabels) {
|
|
484 if (buttonLabels is null) {
|
|
485 throw new NullPointerException(
|
|
486 "The array of button labels cannot be null.");} //$NON-NLS-1$
|
|
487 this.buttonLabels = buttonLabels;
|
|
488 }
|
|
489 }
|