1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
|
/*******************************************************************************
* Copyright (c) 2011 Laurent CARON
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Laurent CARON (laurent.caron at gmail dot com) - Initial implementation and API
* Eugene Ryzhikov - Author of the Oxbow Project (http://code.google.com/p/oxbow/) - Inspiration
*******************************************************************************/
package org.mihalis.opal.opalDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.mihalis.opal.utils.ResourceManager;
/**
* Instances of this class are dialog box
*/
public class Dialog {
/**
* Types of opal dialog
*/
public enum OpalDialogType {
CLOSE, YES_NO, OK, OK_CANCEL, SELECT_CANCEL, NO_BUTTON, OTHER, NONE
}
private String title;
OpalDialogType buttonType;
private final MessageArea messageArea;
private final FooterArea footerArea;
final Shell shell;
private int minimumWidth = 300;
private int minimumHeight = 150;
/**
* Constructor
*/
public Dialog() {
this(null);
}
/**
* Constructor
*
* @param parent parent shell
*/
public Dialog(final Shell parent) {
if (parent == null) {
this.shell = new Shell(Display.getCurrent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
} else {
this.shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
}
this.messageArea = new MessageArea(this);
this.footerArea = new FooterArea(this);
}
/**
* Show the dialog box
*
* @return the index of the selected button
*/
public int show() {
final GridLayout gd = new GridLayout(1, true);
gd.horizontalSpacing = 0;
gd.verticalSpacing = 0;
gd.marginHeight = gd.marginWidth = 0;
this.shell.setLayout(gd);
this.messageArea.render();
this.footerArea.render();
if (this.title != null) {
this.shell.setText(this.title);
}
pack();
this.shell.open();
final Display display = this.shell.getDisplay();
while (!this.shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
return this.footerArea.getSelectedButton();
}
/**
* Close the dialog box
*/
public void close() {
this.shell.dispose();
}
/**
* Compute the size of the shell
*/
void pack() {
final Point preferredSize = this.shell.computeSize(SWT.DEFAULT, SWT.DEFAULT);
if (preferredSize.x < this.minimumWidth) {
preferredSize.x = this.minimumWidth;
}
if (preferredSize.y < this.minimumHeight) {
preferredSize.y = this.minimumHeight;
}
final Point displaySize = new Point(this.shell.getDisplay().getBounds().width, this.shell.getDisplay().getBounds().height);
final int centerX = (displaySize.x - preferredSize.x) / 2;
final int centerY = (displaySize.y - preferredSize.y) / 2;
this.shell.setBounds(centerX, centerY, preferredSize.x, preferredSize.y);
}
// ------------------------------------------- Convenient methods
/**
* Create a dialog box that asks a question
*
* @param title title of the dialog box
* @param text text of the question
* @param defaultValue default value of the input
* @return the value typed by the user
*/
public static String ask(final String title, final String text, final String defaultValue) {
return ask(null, title, text, defaultValue);
}
/**
* Create a dialog box that asks a question
*
* @shell parent shell
* @param title title of the dialog box
* @param text text of the question
* @param defaultValue default value of the input
* @return the value typed by the user
*/
public static String ask(final Shell shell, final String title, final String text, final String defaultValue) {
final Dialog dialog = new Dialog(shell);
dialog.setTitle(ResourceManager.getLabel(ResourceManager.INPUT));
dialog.getMessageArea().setTitle(title).setText(text).setIcon(Display.getCurrent().getSystemImage(SWT.ICON_INFORMATION)).addTextBox(defaultValue);
dialog.setButtonType(OpalDialogType.OK_CANCEL);
if (dialog.show() == 0) {
return dialog.getMessageArea().getTextBoxValue();
} else {
return null;
}
}
/**
* Create a dialog box that displays an error message
*
* @param title title of the dialog box
* @param errorMessage Error message
*/
public static void error(final String title, final String errorMessage) {
error(null, title, errorMessage);
}
/**
* Create a dialog box that displays an error message
*
* @param shell parent shell
* @param title title of the dialog box
* @param errorMessage Error message
*/
public static void error(final Shell shell, final String title, final String errorMessage) {
final Dialog dialog = new Dialog(shell);
dialog.setTitle(ResourceManager.getLabel(ResourceManager.APPLICATION_ERROR));
dialog.getMessageArea().setTitle(title).//
setText(errorMessage).//
setIcon(Display.getCurrent().getSystemImage(SWT.ICON_ERROR));
dialog.setButtonType(OpalDialogType.OK);
dialog.show();
}
/**
* Create a dialog box that inform the user
*
* @param title title of the dialog box
* @param text text to display
*/
public static void inform(final String title, final String text) {
inform(null, title, text);
}
/**
* Create a dialog box that inform the user
*
* @param shell parent shell
* @param title title of the dialog box
* @param text text to display
*/
public static void inform(final Shell shell, final String title, final String text) {
final Dialog dialog = new Dialog(shell);
dialog.setTitle(ResourceManager.getLabel(ResourceManager.INFORMATION));
dialog.getMessageArea().setTitle(title).setText(text).setIcon(Display.getCurrent().getSystemImage(SWT.ICON_INFORMATION));
dialog.setButtonType(OpalDialogType.CLOSE);
dialog.show();
}
/**
* Create a dialog box that asks the user a confirmation
*
* @param title title of the dialog box
* @param text text to display
* @return <code>true</code> if the user confirmed, <code>false</code>
* otherwise
*/
public static boolean isConfirmed(final String title, final String text) {
return isConfirmed(null, title, text, -1);
}
/**
* Create a dialog box that asks the user a confirmation
*
* @param shell parent shell
* @param title title of the dialog box
* @param text text to display
* @return <code>true</code> if the user confirmed, <code>false</code>
* otherwise
*/
public static boolean isConfirmed(final Shell shell, final String title, final String text) {
return isConfirmed(shell, title, text, -1);
}
/**
* Create a dialog box that asks the user a confirmation. The button "yes"
* is not enabled before timer seconds
*
* @param title title of the dialog box
* @param text text to display
* @param timer number of seconds before enabling the yes button
* @return <code>true</code> if the user confirmed, <code>false</code>
* otherwise
*/
public static boolean isConfirmed(final String title, final String text, final int timer) {
return isConfirmed(null, title, text, timer);
}
/**
* Create a dialog box that asks the user a confirmation. The button "yes"
* is not enabled before timer seconds
*
* @param shell parent shell
* @param title title of the dialog box
* @param text text to display
* @param timer number of seconds before enabling the yes button
* @return <code>true</code> if the user confirmed, <code>false</code>
* otherwise
*/
public static boolean isConfirmed(final Shell shell, final String title, final String text, final int timer) {
final Dialog dialog = new Dialog(shell);
dialog.setTitle(ResourceManager.getLabel(ResourceManager.WARNING));
dialog.getMessageArea().setTitle(title).setText(text).setIcon(Display.getCurrent().getSystemImage(SWT.ICON_WARNING));
dialog.getFooterArea().setTimer(timer).setTimerIndexButton(0);
dialog.setButtonType(OpalDialogType.YES_NO);
return dialog.show() == 0;
}
/**
* Create a dialog box with a radio choice
*
* @param title title of the dialog box
* @param text text to display
* @param defaultSelection index of the default selection
* @param values values to display
* @return the index of the selection
*/
public static int radioChoice(final String title, final String text, final int defaultSelection, final String... values) {
return radioChoice(null, title, text, defaultSelection, values);
}
/**
* Create a dialog box with a radio choice
*
* @param shell parent shell
* @param title title of the dialog box
* @param text text to display
* @param defaultSelection index of the default selection
* @param values values to display
* @return the index of the selection
*/
public static int radioChoice(final Shell shell, final String title, final String text, final int defaultSelection, final String... values) {
final Dialog dialog = new Dialog(shell);
dialog.setTitle(ResourceManager.getLabel(ResourceManager.CHOICE));
dialog.getMessageArea().setTitle(title).setText(text).setIcon(Display.getCurrent().getSystemImage(SWT.ICON_QUESTION)).addRadioButtons(defaultSelection, values);
dialog.setButtonType(OpalDialogType.SELECT_CANCEL);
if (dialog.show() == 0) {
return dialog.getMessageArea().getRadioChoice();
} else {
return -1;
}
}
/**
* Display a dialog box with an exception
*
* @param exception exception to display
*/
public static void showException(final Throwable exception) {
final Dialog dialog = new Dialog();
dialog.setTitle(ResourceManager.getLabel(ResourceManager.EXCEPTION));
final String msg = exception.getMessage();
final String className = exception.getClass().getName();
final boolean noMessage = msg == null || msg.trim().length() == 0;
dialog.getMessageArea().setTitle(noMessage ? className : msg).//
setText(noMessage ? "" : className).//
setIcon(Display.getCurrent().getSystemImage(SWT.ICON_ERROR)).//
setException(exception);
dialog.getFooterArea().setExpanded(true);
dialog.setButtonType(OpalDialogType.CLOSE);
dialog.show();
}
/**
* Create a dialog box with a choice
*
* @param title title of the dialog box
* @param text text to display
* @param defaultSelection index of the default selection
* @param items items to display
* @return the index of the selected value
*/
public static int choice(final String title, final String text, final int defaultSelection, final ChoiceItem... items) {
return choice(null, title, text, defaultSelection, items);
}
/**
* Create a dialog box with a choice
*
* @param shell parent shell
* @param title title of the dialog box
* @param text text to display
* @param defaultSelection index of the default selection
* @param items items to display
* @return the index of the selected value
*/
public static int choice(final Shell shell, final String title, final String text, final int defaultSelection, final ChoiceItem... items) {
final Dialog dialog = new Dialog(shell);
dialog.setTitle(ResourceManager.getLabel(ResourceManager.CHOICE));
dialog.getMessageArea().setTitle(title).setText(text).setIcon(Display.getCurrent().getSystemImage(SWT.ICON_QUESTION)).addChoice(defaultSelection, items);
dialog.setButtonType(OpalDialogType.NONE);
dialog.show();
return dialog.getMessageArea().getChoice();
}
// ------------------------------------------- Getters & Setters
/**
* @return the title
*/
public String getTitle() {
return this.title;
}
/**
* @param title the title to set
*/
public void setTitle(final String title) {
this.title = title;
}
/**
* @return the buttonType
*/
public OpalDialogType getButtonType() {
return this.buttonType;
}
/**
* @param buttonType the buttonType to set
*/
public void setButtonType(final OpalDialogType buttonType) {
this.buttonType = buttonType;
switch (buttonType) {
case CLOSE:
this.footerArea.setButtonLabels(ResourceManager.getLabel(ResourceManager.CLOSE)).setDefaultButtonIndex(0);
break;
case NO_BUTTON:
break;
case OK:
this.footerArea.setButtonLabels(ResourceManager.getLabel(ResourceManager.OK)).setDefaultButtonIndex(0);
break;
case OK_CANCEL:
this.footerArea.setButtonLabels(ResourceManager.getLabel(ResourceManager.OK), ResourceManager.getLabel(ResourceManager.CANCEL)).setDefaultButtonIndex(-1);
break;
case SELECT_CANCEL:
this.footerArea.setButtonLabels(ResourceManager.getLabel(ResourceManager.SELECT), ResourceManager.getLabel(ResourceManager.CANCEL)).setDefaultButtonIndex(-1);
break;
case YES_NO:
this.footerArea.setButtonLabels(ResourceManager.getLabel(ResourceManager.YES), ResourceManager.getLabel(ResourceManager.NO)).setDefaultButtonIndex(0);
break;
default:
break;
}
}
/**
* @return the messageArea
*/
public MessageArea getMessageArea() {
return this.messageArea;
}
/**
* @return the footerArea
*/
public FooterArea getFooterArea() {
return this.footerArea;
}
/**
* @return the shell
*/
public Shell getShell() {
return this.shell;
}
/**
* @return the index of the selected button
*/
public int getSelectedButton() {
return getFooterArea().getSelectedButton();
}
/**
* @return the selection state of the checkbox
*/
public boolean getCheckboxValue() {
return this.footerArea.getCheckBoxValue();
}
/**
* @return the minimum width of the dialog box
*/
public int getMinimumWidth() {
return this.minimumWidth;
}
/**
* @param minimumWidth the minimum width of the dialog box to set
*/
public void setMinimumWidth(final int minimumWidth) {
this.minimumWidth = minimumWidth;
}
/**
* @return the minimum height of the dialog box
*/
public int getMinimumHeight() {
return this.minimumHeight;
}
/**
* @param minimumHeight the minimum height of the dialog box to set
*/
public void setMinimumHeight(final int minimumHeight) {
this.minimumHeight = minimumHeight;
}
}
|