-
Notifications
You must be signed in to change notification settings - Fork 98
/
OpalDialogSnippet.java
391 lines (333 loc) · 14.2 KB
/
OpalDialogSnippet.java
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
/*******************************************************************************
* Copyright (c) 2011-2019 Laurent CARON
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Laurent CARON (laurent.caron at gmail dot com) - initial API and implementation
* Eugene Ryzhikov - Author of the Oxbow Project (http://code.google.com/p/oxbow/) - Inspiration
* Stefan Nöbauer - Bug 550437, Bug 550659
*******************************************************************************/
package org.eclipse.nebula.widgets.opal.dialog.snippets;
import java.math.BigDecimal;
import java.util.Arrays;
import org.eclipse.nebula.widgets.opal.commons.ResourceManager;
import org.eclipse.nebula.widgets.opal.commons.SWTGraphicUtil;
import org.eclipse.nebula.widgets.opal.commons.StringUtil;
import org.eclipse.nebula.widgets.opal.dialog.ChoiceItem;
import org.eclipse.nebula.widgets.opal.dialog.Dialog;
import org.eclipse.nebula.widgets.opal.dialog.Dialog.OpalDialogType;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
/**
* This snippet demonstrates the OpalDialog component
*
*/
public class OpalDialogSnippet {
public static void main(final String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Dialog Sample");
shell.setLayout(new GridLayout(3, true));
final Button button1 = new Button(shell, SWT.PUSH);
button1.setText("Hello world !");
button1.setLayoutData(new GridData(GridData.FILL, GridData.FILL, false, false));
button1.addSelectionListener(new SelectionAdapter() {
/**
* @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
*/
@Override
public void widgetSelected(final SelectionEvent e) {
displayHelloWorld();
}
});
final Button button2 = new Button(shell, SWT.PUSH);
button2.setText("Crash and burn");
button2.setLayoutData(new GridData(GridData.FILL, GridData.FILL, false, false));
button2.addListener(SWT.Selection, e -> {
displayCrashAndBurn();
});
final Button button3 = new Button(shell, SWT.PUSH);
button3.setText("You won !");
button3.setLayoutData(new GridData(GridData.FILL, GridData.FILL, false, false));
button3.addListener(SWT.Selection, e -> {
displayYouWon();
});
final Button button4 = new Button(shell, SWT.PUSH);
button4.setText("Confirm exit");
button4.setLayoutData(new GridData(GridData.FILL, GridData.FILL, false, false));
button4.addListener(SWT.Selection, e -> {
displayConfirmExit();
});
final Button button5 = new Button(shell, SWT.PUSH);
button5.setText("Radio choice");
button5.setLayoutData(new GridData(GridData.FILL, GridData.FILL, false, false));
button5.addListener(SWT.Selection, e -> {
displayRadioChoice();
});
final Button button6 = new Button(shell, SWT.PUSH);
button6.setText("Exception viewer");
button6.setLayoutData(new GridData(GridData.FILL, GridData.FILL, false, false));
button6.addListener(SWT.Selection, e -> {
displayException();
});
final Button button7 = new Button(shell, SWT.PUSH);
button7.setText("Input box");
button7.setLayoutData(new GridData(GridData.FILL, GridData.FILL, false, false));
button7.addListener(SWT.Selection, e -> {
displayInput();
});
final Button button8 = new Button(shell, SWT.PUSH);
button8.setText("Choice...");
button8.setLayoutData(new GridData(GridData.FILL, GridData.FILL, false, false));
button8.addListener(SWT.Selection, e -> {
displayChoice();
});
final Button button9 = new Button(shell, SWT.PUSH);
button9.setText("Delayed quit");
button9.setLayoutData(new GridData(GridData.FILL, GridData.FILL, false, false));
button9.addListener(SWT.Selection, e -> {
displayDelayedQuit();
});
final Button button10 = new Button(shell, SWT.PUSH);
button10.setText("Progress bar");
button10.setLayoutData(new GridData(GridData.FILL, GridData.FILL, false, false));
button10.addListener(SWT.Selection, e -> {
displayProgressBar();
});
final Button button11 = new Button(shell, SWT.PUSH);
button11.setText("Complex Example 1");
button11.setLayoutData(new GridData(GridData.FILL, GridData.FILL, false, false));
button11.addListener(SWT.Selection, e -> {
displaySecurityWarning();
});
final Button button12 = new Button(shell, SWT.PUSH);
button12.setText("Complex Example 2");
button12.setLayoutData(new GridData(GridData.FILL, GridData.FILL, false, false));
button12.addListener(SWT.Selection, e -> {
displayComplex();
});
final Button button13 = new Button(shell, SWT.PUSH);
button13.setText("Large Text Example");
button13.setLayoutData(new GridData(GridData.FILL, GridData.FILL, false, false));
button13.addListener(SWT.Selection, e -> {
displayLargeText();
});
final Button button14 = new Button(shell, SWT.PUSH);
button14.setText("Issue 29");
button14.setLayoutData(new GridData(GridData.FILL, GridData.FILL, false, false));
button14.addListener(SWT.Selection, e -> {
testIssue29(shell);
});
final Button button15 = new Button(shell, SWT.PUSH);
button15.setText("Issue 45");
button15.setLayoutData(new GridData(GridData.FILL, GridData.FILL, false, false));
button15.addListener(SWT.Selection, e -> {
testIssue45();
});
final Button button16 = new Button(shell, SWT.PUSH);
button16.setText("Bug 533776");
button16.setLayoutData(new GridData(GridData.FILL, GridData.FILL, false, false));
button16.addListener(SWT.Selection, e -> {
testBug533776();
});
final Button button17 = new Button(shell, SWT.PUSH);
button17.setText("Complex exception");
button17.setLayoutData(new GridData(GridData.FILL, GridData.FILL, false, false));
button17.addListener(SWT.Selection, e -> {
displayComplex1();
});
// Open the shell
shell.pack();
SWTGraphicUtil.centerShell(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
private static void displayHelloWorld() {
final Dialog dialog = new Dialog();
dialog.getMessageArea().setText("Hello world");
dialog.setButtonType(OpalDialogType.OK);
dialog.show();
}
private static void displayCrashAndBurn() {
Dialog.error("CRASH AND BURN !",
"The application has performed an illegal action. This action has been logged and reported. This text may also a bit longer as expected");
}
private static void displayYouWon() {
Dialog.inform("You've won!", "The game is over with the 15:3 score");
}
private static void displayConfirmExit() {
final boolean confirm = Dialog.isConfirmed("Are you sure you want to quit?", "Please do not quit yet!");
System.out.println("Choice is..." + confirm);
}
private static void displayRadioChoice() {
final int choice = Dialog.radioChoice("You've got selection to make", "Go ahead", 1, "Yes", "No", "May be");
System.out.println("Choice is..." + choice);
}
private static void displayException() {
try {
new BigDecimal("seven");
} catch (final Throwable ex) {
Dialog dialog = Dialog.buildExceptionDialog(ex);
Image mail = new Image(Display.getCurrent(), OpalDialogSnippet.class.getResourceAsStream("mail.png"));
Image mailHot = new Image(Display.getCurrent(), OpalDialogSnippet.class.getResourceAsStream("mail_hover.png"));
Image[] images = Arrays.asList(mail, mailHot).toArray(new Image[0]);
dialog.getFooterArea().addFooterAction(()-> "Send Mail", d -> System.out.println(StringUtil.stackStraceAsString(d.getMessageArea().getException())), images);
dialog.show();
}
}
private static void displayChoice() {
final int choice = Dialog.choice("What do you want to do with your game in\nprogress?", "", 1,
new ChoiceItem("Exit and save my game",
"Save your game in progress, then exit. " + "This will\noverwrite any previously saved games."),
new ChoiceItem("Exit and don't save",
"Exit without saving your game. " + "This is counted\nas a loss in your statistics."),
new ChoiceItem("Don't exit", "Return to your game progress"));
System.out.println("Choice is..." + choice);
}
private static void displayDelayedQuit() {
final boolean choice = Dialog.isConfirmed("Are you sure you want to quit?", "Please do not quit yet!", 10);
System.out.println("Choice is..." + choice);
}
private static void displaySecurityWarning() {
final Dialog dialog = new Dialog();
dialog.setTitle("Security Warning");
dialog.setMinimumWidth(400);
dialog.getMessageArea().setTitle("The publisher cannot be verified.\nDo you want to run this software?") //
.setIcon(Display.getCurrent().getSystemImage(SWT.ICON_WARNING)) //
.setText("Name: C:\\Program Files\\eclipse\\eclipse.exe<br/>" + //
"Publisher: <b>Unknown Publisher</b><br/>" + //
"Type: Application<br/>");
dialog.getFooterArea().addCheckBox("Always ask before opening this file", false).setButtonLabels("Run",
"Cancel");
dialog.show();
System.out.println(
"The choice is " + dialog.getSelectedButton() + ", the checkbox value is " + dialog.getCheckboxValue());
}
private static void displayProgressBar() {
final Dialog dialog = new Dialog();
dialog.setTitle("Copying...");
dialog.setMinimumWidth(400);
dialog.getMessageArea().setTitle("Copying files") //
.setIcon(Display.getCurrent().getSystemImage(SWT.ICON_INFORMATION)) //
.setText("Location : from 'Others' to 'Others'<br/>" + //
"File Name : <b>photo.jpg</b>")
.//
addProgressBar(0, 100, 0);
final int[] counter = new int[1];
counter[0] = 10;
Display.getCurrent().timerExec(500, new Runnable() {
@Override
public void run() {
dialog.getMessageArea().setProgressBarValue(counter[0]);
dialog.getMessageArea().setText("Location : from 'Others' to 'Others'<br/>" + //
"File Name : <b>photo" + counter[0] + ".jpg</b>");
counter[0] += 10;
if (counter[0] < 120) {
Display.getCurrent().timerExec(500, this);
} else {
dialog.close();
}
}
});
dialog.show();
}
private static void displayInput() {
final String input = Dialog.ask("Enter you name",
"or any other text if you prefer Somthing longer may stress the layout", "Laurent CARON");
System.out.println("Choice is..." + input);
}
private static void displayComplex() {
final Dialog dialog = new Dialog();
dialog.setTitle("Application Error");
dialog.getMessageArea().setTitle("CRASH AND BURN !").//
setText("The application has performed an illegal action. This action has been logged and reported.").//
setIcon(Display.getCurrent().getSystemImage(SWT.ICON_ERROR));
dialog.setButtonType(OpalDialogType.OK);
dialog.getFooterArea().setExpanded(false).addCheckBox("Don't show me this error next time", true)
.setDetailText("More explanations to come...");
dialog.getFooterArea().setFooterText("Your application crashed because a developer forgot to write a unit test")
.setIcon(new Image(null, OpalDialogSnippet.class.getResourceAsStream("warning.png")));
dialog.show();
}
private static void displayComplex1() {
try {
new BigDecimal("seven");
} catch (final Throwable ex) {
final Dialog dialog = new Dialog();
dialog.setTitle(ResourceManager.getLabel(ResourceManager.EXCEPTION));
final String msg = ex.getMessage();
final String className = ex.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(ex).addCheckBox("Don't show this again", false);
dialog.getFooterArea().setExpanded(true);
dialog.setButtonType(OpalDialogType.CLOSE);
dialog.show();
}
}
private static void displayLargeText() {
final StringBuilder stringBuilder = new StringBuilder();
for (int t = 0; t < 20; t++) {
stringBuilder.append("A <b>very</b> <size=10>long text (10)</size> " + t + "");
stringBuilder.append("A <b>very</b> <size=+12>long text (+12)</size> " + t + "");
stringBuilder.append("A <b>very</b> <size=-4>long text (-4)</size> " + t + "");
stringBuilder.append("A <b>very</b> <color=#088A29>long text</color> " + t + "");
stringBuilder.append("A <b>very</b> <color=255,0,255>long text</color> " + t + "");
stringBuilder.append("A <b>very</b> <color=navy>long text</color> " + t + "");
stringBuilder.append("A <b>very</b> <backgroundcolor=255,0,0>long text</backgroundcolor> " + t + "");
stringBuilder.append("A <b>very</b> <backgroundcolor=#FFFFCC>long text</backgroundcolor> " + t + "");
stringBuilder.append("A <b>very</b> <backgroundcolor=lavender>long text</backgroundcolor> " + t + "");
stringBuilder.append("A very long text " + t + "<br/>");
stringBuilder.append("..." + "<br/>");
}
final Dialog dialog = new Dialog(true);
dialog.getMessageArea().setVerticalScrollbar(true);
dialog.getMessageArea().setHeight(200);
dialog.getMessageArea().setText(stringBuilder.toString());
dialog.setButtonType(OpalDialogType.OK);
dialog.show();
}
private static void testIssue29(Shell parent) {
final Dialog d = new Dialog(parent);
d.setCenterPolicy(Dialog.CenterOption.CENTER_ON_DIALOG);
d.setTitle("foo title");
d.getMessageArea().setTitle("aaaa").setText("bbbb");
d.getFooterArea().setButtonLabels(Arrays.asList("Don't Save please", "Cancel"));
d.show();
}
private static void testIssue45() {
final Dialog dialog = new Dialog(true);
dialog.getMessageArea().setVerticalScrollbar(true);
dialog.getMessageArea().setHeight(200);
dialog.getMessageArea().setText("Illegal format <key>:<value>");
dialog.setButtonType(OpalDialogType.OK);
dialog.show();
}
private static void testBug533776() {
final Dialog dialog = new Dialog(true);
dialog.getMessageArea().setVerticalScrollbar(true);
dialog.getMessageArea().setHeight(200);
dialog.getMessageArea().setText("<color=red><b>test</b></color>");
dialog.setButtonType(OpalDialogType.OK);
dialog.show();
}
}