Commit | Line | Data |
---|---|---|
a3b510ab NR |
1 | package com.googlecode.lanterna.gui2.dialogs; |
2 | ||
3 | import com.googlecode.lanterna.TerminalSize; | |
4 | import com.googlecode.lanterna.gui2.*; | |
5 | ||
6 | /** | |
7 | * Simple message dialog that displays a message and has optional selection/confirmation buttons | |
8 | * | |
9 | * @author Martin | |
10 | */ | |
11 | public class MessageDialog extends DialogWindow { | |
12 | ||
13 | private MessageDialogButton result; | |
14 | ||
15 | MessageDialog( | |
16 | String title, | |
17 | String text, | |
18 | MessageDialogButton... buttons) { | |
19 | ||
20 | super(title); | |
21 | this.result = null; | |
22 | if(buttons == null || buttons.length == 0) { | |
23 | buttons = new MessageDialogButton[] { MessageDialogButton.OK }; | |
24 | } | |
25 | ||
26 | Panel buttonPanel = new Panel(); | |
27 | buttonPanel.setLayoutManager(new GridLayout(buttons.length).setHorizontalSpacing(1)); | |
28 | for(final MessageDialogButton button: buttons) { | |
29 | buttonPanel.addComponent(new Button(button.toString(), new Runnable() { | |
30 | @Override | |
31 | public void run() { | |
32 | result = button; | |
33 | close(); | |
34 | } | |
35 | })); | |
36 | } | |
37 | ||
38 | Panel mainPanel = new Panel(); | |
39 | mainPanel.setLayoutManager( | |
40 | new GridLayout(1) | |
41 | .setLeftMarginSize(1) | |
42 | .setRightMarginSize(1)); | |
43 | mainPanel.addComponent(new Label(text)); | |
44 | mainPanel.addComponent(new EmptySpace(TerminalSize.ONE)); | |
45 | buttonPanel.setLayoutData( | |
46 | GridLayout.createLayoutData( | |
47 | GridLayout.Alignment.END, | |
48 | GridLayout.Alignment.CENTER, | |
49 | false, | |
50 | false)) | |
51 | .addTo(mainPanel); | |
52 | setComponent(mainPanel); | |
53 | } | |
54 | ||
55 | /** | |
56 | * {@inheritDoc} | |
57 | * @param textGUI Text GUI to add the dialog to | |
58 | * @return The selected button's enum value | |
59 | */ | |
60 | @Override | |
61 | public MessageDialogButton showDialog(WindowBasedTextGUI textGUI) { | |
62 | result = null; | |
63 | super.showDialog(textGUI); | |
64 | return result; | |
65 | } | |
66 | ||
67 | /** | |
68 | * Shortcut for quickly displaying a message box | |
69 | * @param textGUI The GUI to display the message box on | |
70 | * @param title Title of the message box | |
71 | * @param text Main message of the message box | |
72 | * @param buttons Buttons that the user can confirm the message box with | |
73 | * @return Which button the user selected | |
74 | */ | |
75 | public static MessageDialogButton showMessageDialog( | |
76 | WindowBasedTextGUI textGUI, | |
77 | String title, | |
78 | String text, | |
79 | MessageDialogButton... buttons) { | |
80 | MessageDialogBuilder builder = new MessageDialogBuilder() | |
81 | .setTitle(title) | |
82 | .setText(text); | |
83 | if(buttons.length == 0) { | |
84 | builder.addButton(MessageDialogButton.OK); | |
85 | } | |
86 | for(MessageDialogButton button: buttons) { | |
87 | builder.addButton(button); | |
88 | } | |
89 | return builder.build().showDialog(textGUI); | |
90 | } | |
91 | } |