-
Notifications
You must be signed in to change notification settings - Fork 12
Dialog
This is the Dialog
widget comes from the OPAL project, with some refactoring of the implementaion.
You can format the message using some pseudo-HTML tags :
-
<br/>
for adding a line break -
<i>...</i>
to render text in italic -
<u>...</u>
to render text in underline -
<b>...</b>
to render text in bold -
<size>...</size>
to increase/decrease text size. You can use the following syntaxes:<size=10>
(10px),<size=+4>
,<size=-4>
-
<color>...</color>
to change foreground color. You can use the following syntaxes:<color=#FFCCAA>
(HTML color code),<color=9,255,10>
(RGB values) and<color=aliceblue>
(HTML color code) -
<backgroundcolor>...</backgroundcolor>
to change background color. You can use the following syntaxes:<backgroundcolor=#FFCCAA>
(HTML color code),<backgroundcolor=9,255,10>
(RGB values) and<backgroundcolor=aliceblue>
(HTML color code)
Common text in the dialog can be shown in any language by putting locale specific resource bundle named messages_<locale_id>.properties on the classpath and switching default locale. The library has following built-in bundles under ’src/main/resources’:
Bundle | Locale |
---|---|
Default | en |
Simplified Chinese | zh_CN |
The Dialog
class provides two static methods :
public static void error(String title, String message);
public static void error(Shell shell, String title, String message);
where title
is the title of the dialog box, and message
the error message to display. If no shell is specified, the dialog box will be a on-top window.
Dialog.error("CRASH AND BURN !",
"The application has performed an illegal action. "
+ "This action has been logged and reported.");
The Dialog
class provides two static methods :
public static void inform(String title, String text);
public static void inform(Shell shell, String title, String text);
where title
is the title of the dialog box, and text
is the text to display. If no shell
is specified, the dialog box will be a on-top window.
Dialog.inform("You've won!", "The game is over with the 15:3 score");
The Dialog
class provides four static methods :
public static boolean isConfirmed(String title, String text);
public static boolean isConfirmed(Shell shell, String title, String text)
public static boolean isConfirmed(String title, String text, int timer)
public static boolean isConfirmed(Shell shell, String title, String text, int timer)
where title
is the title of the dialog box, and text
is the text to display. If no shell
is specified, the dialog box will be a on-top window. If a timer
is specified, this is the time in second before the "Yes" button is enabled.
Example:
boolean confirm = Dialog.isConfirmed("Are you sure you want to quit?",
"Please do not quit yet!");
System.out.println("Choice is..." + confirm);
boolean choice = Dialog.isConfirmed("Are you sure you want to quit?",
"Please do not quit yet!", 10);
System.out.println("Choice is..." + choice);
The Dialog
class provides two static methods :
public static int radioChoice(String title, String text, int defaultSelection,
String... values)
public static int radioChoice(Shell shell, String title, String text,
int defaultSelection, String... values)
where title
is the title of the dialog box, text
is the text to display, defaultSelection
is the index of the default selection (0-based index) and values
is a list of propositions. If no shell
is specified, the dialog box will be a on-top window.
int choice = Dialog.radioChoice("You've got selection to make",
"Go ahead", 1, "Yes", "No", "May be");
System.out.println("Choice is..." + choice);
The Dialog
class provides two static methods :
public static int choice(String title, String text, int defaultSelection,
ChoiceItem... items);
public static int choice(Shell shell, String title, String text,
int defaultSelection, ChoiceItem... items)
where title
is the title of the dialog box, text
is the text to display, defaultSelection
is the index of the default selection (0-based index) and items
is a list of propositions.
ChoiceItem
is a POJO used to display the proposition, which is composed of a short text and a description.
If no shell is specified, the dialog box will be a on-top window.
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);
The Dialog class provides a static method :
public static void showException(Throwable exception)
where exception
is the exception to display.
try {
new BigDecimal("seven");
} catch (final Throwable e) {
Dialog.showException(e);
}
The Dialog
class provides two static methods :
public static String ask(String title, String text, String defaultValue)
public static String ask(Shell shell, String title, String text, String defaultValue)
where title
is the title of the dialog box, text
is the text to display and defaultValue
is the default value of the input box. If no shell
is specified, the dialog box will be a on-top window.
String input = Dialog.ask("Enter you name",
"or any other text if you prefer",
"Bill Gates");
System.out.println("Choice is..." + input);
A dialog box is composed of two parts :
- A message area
- A footer area
A message area is composed of 3 parts :
- A title
- An icon
- A text
- Eventually a progress bar
Each setter returns the message area object, so you can join statements.
A footer area is composed of different optional parts
- A checkbox : use by the method
addCheckbox(text, default selection)
- A list of button labels : use the method
setButtonLabels(String... labels)
- A collapsable section
- Use
setExpanded(boolean expanded)
to set the collapse/expanded state - Use
setDetailText(String text)
to set the text displayed when the panel is expanded
- Use
- A footer
- Use
setFooterText(String text)
to set the text displayed in the footer - Use
setImage(Image image)
to set the image displayed in the footer, on the left side
- Use
Each setter returns the footer area object, so you can join statements.
Once the message area and the footer area are built, use the method show() to open the dialog box. It returns the zero-based index of the selected button. You can also set the minimum height and width of the dialog box (by using the appropriate setters) or specifiy the buttons (by using a value of the enumeration Dialog.Type
: CLOSE
, NO_BUTTON
, OK
, OK_CANCEL
, SELECT_CANCEL
, SELECT_CANCEL
, YES_NO
).
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);
Dialog dialog = new Dialog();
dialog.setTitle("Security Warning");
dialog.setMinimumWidth(400);
dialog.getMessageArea()
.setTitle("The publisher cannot be verified.\n" +
"Do 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();
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(Dialog.Type.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(SWTResourceManager.getImage(this.getClass(),
"images/warning.png"));
dialog.show();
An example is located in the source repository:
src/test/java/com/github/haixing_hu/swt/window/DialogExample.java