1 package com
.googlecode
.lanterna
.gui2
.dialogs
;
3 import com
.googlecode
.lanterna
.TerminalSize
;
4 import com
.googlecode
.lanterna
.gui2
.*;
6 import java
.math
.BigInteger
;
7 import java
.util
.regex
.Pattern
;
10 * {@code TextInputDialog} is a modal text input dialog that prompts the user to enter a text string. The class supports
11 * validation and password masking. The builder class to help setup {@code TextInputDialog}s is
12 * {@code TextInputDialogBuilder}.
14 public class TextInputDialog
extends DialogWindow
{
16 private final TextBox textBox
;
17 private final TextInputDialogResultValidator validator
;
18 private String result
;
23 TerminalSize textBoxPreferredSize
,
24 String initialContent
,
25 TextInputDialogResultValidator validator
,
30 this.textBox
= new TextBox(textBoxPreferredSize
, initialContent
);
31 this.validator
= validator
;
37 Panel buttonPanel
= new Panel();
38 buttonPanel
.setLayoutManager(new GridLayout(2).setHorizontalSpacing(1));
39 buttonPanel
.addComponent(new Button(LocalizedString
.OK
.toString(), new Runnable() {
44 }).setLayoutData(GridLayout
.createLayoutData(GridLayout
.Alignment
.CENTER
, GridLayout
.Alignment
.CENTER
, true, false)));
45 buttonPanel
.addComponent(new Button(LocalizedString
.Cancel
.toString(), new Runnable() {
52 Panel mainPanel
= new Panel();
53 mainPanel
.setLayoutManager(
56 .setRightMarginSize(1));
57 if(description
!= null) {
58 mainPanel
.addComponent(new Label(description
));
60 mainPanel
.addComponent(new EmptySpace(TerminalSize
.ONE
));
61 textBox
.setLayoutData(
62 GridLayout
.createLayoutData(
63 GridLayout
.Alignment
.FILL
,
64 GridLayout
.Alignment
.CENTER
,
68 mainPanel
.addComponent(new EmptySpace(TerminalSize
.ONE
));
69 buttonPanel
.setLayoutData(
70 GridLayout
.createLayoutData(
71 GridLayout
.Alignment
.END
,
72 GridLayout
.Alignment
.CENTER
,
76 setComponent(mainPanel
);
80 String text
= textBox
.getText();
81 if(validator
!= null) {
82 String errorMessage
= validator
.validate(text
);
83 if(errorMessage
!= null) {
84 MessageDialog
.showMessageDialog(getTextGUI(), getTitle(), errorMessage
, MessageDialogButton
.OK
);
92 private void onCancel() {
97 public String
showDialog(WindowBasedTextGUI textGUI
) {
99 super.showDialog(textGUI
);
104 * Shortcut for quickly showing a {@code TextInputDialog}
105 * @param textGUI GUI to show the dialog on
106 * @param title Title of the dialog
107 * @param description Description of the dialog
108 * @param initialContent What content to place in the text box initially
109 * @return The string the user typed into the text box, or {@code null} if the dialog was cancelled
111 public static String
showDialog(WindowBasedTextGUI textGUI
, String title
, String description
, String initialContent
) {
112 TextInputDialog textInputDialog
= new TextInputDialogBuilder()
114 .setDescription(description
)
115 .setInitialContent(initialContent
)
117 return textInputDialog
.showDialog(textGUI
);
121 * Shortcut for quickly showing a {@code TextInputDialog} that only accepts numbers
122 * @param textGUI GUI to show the dialog on
123 * @param title Title of the dialog
124 * @param description Description of the dialog
125 * @param initialContent What content to place in the text box initially
126 * @return The number the user typed into the text box, or {@code null} if the dialog was cancelled
128 public static BigInteger
showNumberDialog(WindowBasedTextGUI textGUI
, String title
, String description
, String initialContent
) {
129 TextInputDialog textInputDialog
= new TextInputDialogBuilder()
131 .setDescription(description
)
132 .setInitialContent(initialContent
)
133 .setValidationPattern(Pattern
.compile("[0-9]+"), "Not a number")
135 String numberString
= textInputDialog
.showDialog(textGUI
);
136 return numberString
!= null ?
new BigInteger(numberString
) : null;
140 * Shortcut for quickly showing a {@code TextInputDialog} with password masking
141 * @param textGUI GUI to show the dialog on
142 * @param title Title of the dialog
143 * @param description Description of the dialog
144 * @param initialContent What content to place in the text box initially
145 * @return The string the user typed into the text box, or {@code null} if the dialog was cancelled
147 public static String
showPasswordDialog(WindowBasedTextGUI textGUI
, String title
, String description
, String initialContent
) {
148 TextInputDialog textInputDialog
= new TextInputDialogBuilder()
150 .setDescription(description
)
151 .setInitialContent(initialContent
)
152 .setPasswordInput(true)
154 return textInputDialog
.showDialog(textGUI
);