Mercurial > projects > dwt-addons
annotate dwtx/jface/dialogs/MessageDialog.d @ 192:c3583c6ec027
Added missing default cases for switch statements
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 03 Nov 2008 22:52:26 +0100 |
parents | ea8ff534f622 |
children |
rev | line source |
---|---|
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 } | |
192
c3583c6ec027
Added missing default cases for switch statements
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
163 default: |
21 | 164 } |
165 this.buttonLabels = dialogButtonLabels; | |
166 this.defaultButtonIndex = defaultIndex; | |
167 } | |
168 | |
169 /* | |
170 * (non-Javadoc) | |
171 * @see dwtx.jface.dialogs.Dialog#buttonPressed(int) | |
172 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
21
diff
changeset
|
173 protected override void buttonPressed(int buttonId) { |
21 | 174 setReturnCode(buttonId); |
175 close(); | |
176 } | |
177 | |
178 /* | |
179 * (non-Javadoc) | |
180 * @see dwtx.jface.window.Window#configureShell(dwt.widgets.Shell) | |
181 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
21
diff
changeset
|
182 protected override void configureShell(Shell shell) { |
21 | 183 super.configureShell(shell); |
184 if (title !is null) { | |
185 shell.setText(title); | |
186 } | |
187 if (titleImage !is null) { | |
188 shell.setImage(titleImage); | |
189 } | |
190 } | |
191 | |
192 /* | |
193 * (non-Javadoc) Method declared on Dialog. | |
194 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
21
diff
changeset
|
195 protected override void createButtonsForButtonBar(Composite parent) { |
21 | 196 buttons = new Button[buttonLabels.length]; |
197 for (int i = 0; i < buttonLabels.length; i++) { | |
198 String label = buttonLabels[i]; | |
199 Button button = createButton(parent, i, label, | |
200 defaultButtonIndex is i); | |
201 buttons[i] = button; | |
202 } | |
203 } | |
204 | |
205 /** | |
206 * Creates and returns the contents of an area of the dialog which appears | |
207 * below the message and above the button bar. | |
208 * <p> | |
209 * The default implementation of this framework method returns | |
210 * <code>null</code>. Subclasses may override. | |
211 * </p> | |
212 * | |
213 * @param parent | |
214 * parent composite to contain the custom area | |
215 * @return the custom area control, or <code>null</code> | |
216 */ | |
217 protected Control createCustomArea(Composite parent) { | |
218 return null; | |
219 } | |
220 | |
221 /** | |
222 * This implementation of the <code>Dialog</code> framework method creates | |
223 * and lays out a composite and calls <code>createMessageArea</code> and | |
224 * <code>createCustomArea</code> to populate it. Subclasses should | |
225 * override <code>createCustomArea</code> to add contents below the | |
226 * message. | |
227 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
21
diff
changeset
|
228 protected override Control createDialogArea(Composite parent) { |
21 | 229 // create message area |
230 createMessageArea(parent); | |
231 // create the top level composite for the dialog area | |
232 Composite composite = new Composite(parent, DWT.NONE); | |
233 GridLayout layout = new GridLayout(); | |
234 layout.marginHeight = 0; | |
235 layout.marginWidth = 0; | |
236 composite.setLayout(layout); | |
237 GridData data = new GridData(GridData.FILL_BOTH); | |
238 data.horizontalSpan = 2; | |
239 composite.setLayoutData(data); | |
240 // allow subclasses to add custom controls | |
241 customArea = createCustomArea(composite); | |
242 //If it is null create a dummy label for spacing purposes | |
243 if (customArea is null) { | |
244 customArea = new Label(composite, DWT.NULL); | |
245 } | |
246 return composite; | |
247 } | |
248 | |
249 /** | |
250 * Gets a button in this dialog's button bar. | |
251 * | |
252 * @param index | |
253 * the index of the button in the dialog's button bar | |
254 * @return a button in the dialog's button bar | |
255 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
21
diff
changeset
|
256 protected override Button getButton(int index) { |
21 | 257 return buttons[index]; |
258 } | |
259 | |
260 /** | |
261 * Returns the minimum message area width in pixels This determines the | |
262 * minimum width of the dialog. | |
263 * <p> | |
264 * Subclasses may override. | |
265 * </p> | |
266 * | |
267 * @return the minimum message area width (in pixels) | |
268 */ | |
269 protected int getMinimumMessageWidth() { | |
270 return convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH); | |
271 } | |
272 | |
273 /** | |
274 * Handle the shell close. Set the return code to <code>DWT.DEFAULT</code> | |
275 * as there has been no explicit close by the user. | |
276 * | |
277 * @see dwtx.jface.window.Window#handleShellCloseEvent() | |
278 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
21
diff
changeset
|
279 protected override void handleShellCloseEvent() { |
21 | 280 //Sets a return code of DWT.DEFAULT since none of the dialog buttons |
281 // were pressed to close the dialog. | |
282 super.handleShellCloseEvent(); | |
283 setReturnCode(DWT.DEFAULT); | |
284 } | |
285 | |
286 /** | |
287 * Convenience method to open a simple confirm (OK/Cancel) dialog. | |
288 * | |
289 * @param parent | |
290 * the parent shell of the dialog, or <code>null</code> if none | |
291 * @param title | |
292 * the dialog's title, or <code>null</code> if none | |
293 * @param message | |
294 * the message | |
295 * @return <code>true</code> if the user presses the OK button, | |
296 * <code>false</code> otherwise | |
297 */ | |
298 public static bool openConfirm(Shell parent, String title, String message) { | |
299 MessageDialog dialog = new MessageDialog(parent, title, null, // accept | |
300 // the | |
301 // default | |
302 // window | |
303 // icon | |
304 message, QUESTION, [ IDialogConstants.OK_LABEL, | |
305 IDialogConstants.CANCEL_LABEL ], 0); // OK is the | |
306 // default | |
307 return dialog.open() is 0; | |
308 } | |
309 | |
310 /** | |
311 * Convenience method to open a standard error dialog. | |
312 * | |
313 * @param parent | |
314 * the parent shell of the dialog, or <code>null</code> if none | |
315 * @param title | |
316 * the dialog's title, or <code>null</code> if none | |
317 * @param message | |
318 * the message | |
319 */ | |
320 public static void openError(Shell parent, String title, String message) { | |
321 MessageDialog dialog = new MessageDialog(parent, title, null, // accept | |
322 // the | |
323 // default | |
324 // window | |
325 // icon | |
326 message, ERROR, [ IDialogConstants.OK_LABEL ], 0); // ok | |
327 // is | |
328 // the | |
329 // default | |
330 dialog.open(); | |
331 return; | |
332 } | |
333 | |
334 /** | |
335 * Convenience method to open a standard information dialog. | |
336 * | |
337 * @param parent | |
338 * the parent shell of the dialog, or <code>null</code> if none | |
339 * @param title | |
340 * the dialog's title, or <code>null</code> if none | |
341 * @param message | |
342 * the message | |
343 */ | |
344 public static void openInformation(Shell parent, String title, | |
345 String message) { | |
346 MessageDialog dialog = new MessageDialog(parent, title, null, // accept | |
347 // the | |
348 // default | |
349 // window | |
350 // icon | |
351 message, INFORMATION, | |
352 [ IDialogConstants.OK_LABEL ], 0); | |
353 // ok is the default | |
354 dialog.open(); | |
355 return; | |
356 } | |
357 | |
358 /** | |
359 * Convenience method to open a simple Yes/No question dialog. | |
360 * | |
361 * @param parent | |
362 * the parent shell of the dialog, or <code>null</code> if none | |
363 * @param title | |
364 * the dialog's title, or <code>null</code> if none | |
365 * @param message | |
366 * the message | |
367 * @return <code>true</code> if the user presses the OK button, | |
368 * <code>false</code> otherwise | |
369 */ | |
370 public static bool openQuestion(Shell parent, String title, | |
371 String message) { | |
372 MessageDialog dialog = new MessageDialog(parent, title, null, // accept | |
373 // the | |
374 // default | |
375 // window | |
376 // icon | |
377 message, QUESTION, [ IDialogConstants.YES_LABEL, | |
378 IDialogConstants.NO_LABEL ], 0); // yes is the default | |
379 return dialog.open() is 0; | |
380 } | |
381 | |
382 /** | |
383 * Convenience method to open a standard warning dialog. | |
384 * | |
385 * @param parent | |
386 * the parent shell of the dialog, or <code>null</code> if none | |
387 * @param title | |
388 * the dialog's title, or <code>null</code> if none | |
389 * @param message | |
390 * the message | |
391 */ | |
392 public static void openWarning(Shell parent, String title, String message) { | |
393 MessageDialog dialog = new MessageDialog(parent, title, null, // accept | |
394 // the | |
395 // default | |
396 // window | |
397 // icon | |
398 message, WARNING, [ IDialogConstants.OK_LABEL ], 0); // ok | |
399 // is | |
400 // the | |
401 // default | |
402 dialog.open(); | |
403 return; | |
404 } | |
405 | |
406 /* | |
407 * @see dwtx.jface.dialogs.Dialog#createButton(dwt.widgets.Composite, | |
408 * int, java.lang.String, bool) | |
409 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
21
diff
changeset
|
410 protected override Button createButton(Composite parent, int id, String label, |
21 | 411 bool defaultButton) { |
412 Button button = super.createButton(parent, id, label, defaultButton); | |
413 //Be sure to set the focus if the custom area cannot so as not | |
414 //to lose the defaultButton. | |
415 if (defaultButton && !customShouldTakeFocus()) { | |
416 button.setFocus(); | |
417 } | |
418 return button; | |
419 } | |
420 | |
421 /** | |
422 * Return whether or not we should apply the workaround where we take focus | |
423 * for the default button or if that should be determined by the dialog. By | |
424 * default only return true if the custom area is a label or CLabel that | |
425 * cannot take focus. | |
426 * | |
427 * @return bool | |
428 */ | |
429 protected bool customShouldTakeFocus() { | |
430 if (cast(Label) customArea ) { | |
431 return false; | |
432 } | |
433 if (cast(CLabel) customArea ) { | |
434 return (customArea.getStyle() & DWT.NO_FOCUS) > 0; | |
435 } | |
436 return true; | |
437 } | |
438 | |
439 /* | |
440 * (non-Javadoc) | |
441 * @see dwtx.jface.dialogs.IconAndMessageDialog#getImage() | |
442 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
21
diff
changeset
|
443 public override Image getImage() { |
21 | 444 return image; |
445 } | |
446 | |
447 /** | |
448 * An accessor for the labels to use on the buttons. | |
449 * | |
450 * @return The button labels to used; never <code>null</code>. | |
451 */ | |
452 protected String[] getButtonLabels() { | |
453 return buttonLabels; | |
454 } | |
455 | |
456 /** | |
457 * An accessor for the index of the default button in the button array. | |
458 * | |
459 * @return The default button index. | |
460 */ | |
461 protected int getDefaultButtonIndex() { | |
462 return defaultButtonIndex; | |
463 } | |
464 | |
465 /** | |
466 * A mutator for the array of buttons in the button bar. | |
467 * | |
468 * @param buttons | |
469 * The buttons in the button bar; must not be <code>null</code>. | |
470 */ | |
471 protected void setButtons(Button[] buttons) { | |
472 if (buttons is null) { | |
473 throw new NullPointerException( | |
474 "The array of buttons cannot be null.");} //$NON-NLS-1$ | |
475 this.buttons = buttons; | |
476 } | |
477 | |
478 /** | |
479 * A mutator for the button labels. | |
480 * | |
481 * @param buttonLabels | |
482 * The button labels to use; must not be <code>null</code>. | |
483 */ | |
484 protected void setButtonLabels(String[] buttonLabels) { | |
485 if (buttonLabels is null) { | |
486 throw new NullPointerException( | |
487 "The array of button labels cannot be null.");} //$NON-NLS-1$ | |
488 this.buttonLabels = buttonLabels; | |
489 } | |
490 } |