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 | import java.math.BigInteger; | |
7 | import java.util.regex.Pattern; | |
8 | ||
9 | /** | |
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}. | |
13 | */ | |
14 | public class TextInputDialog extends DialogWindow { | |
15 | ||
16 | private final TextBox textBox; | |
17 | private final TextInputDialogResultValidator validator; | |
18 | private String result; | |
19 | ||
20 | TextInputDialog( | |
21 | String title, | |
22 | String description, | |
23 | TerminalSize textBoxPreferredSize, | |
24 | String initialContent, | |
25 | TextInputDialogResultValidator validator, | |
26 | boolean password) { | |
27 | ||
28 | super(title); | |
29 | this.result = null; | |
30 | this.textBox = new TextBox(textBoxPreferredSize, initialContent); | |
31 | this.validator = validator; | |
32 | ||
33 | if(password) { | |
34 | textBox.setMask('*'); | |
35 | } | |
36 | ||
37 | Panel buttonPanel = new Panel(); | |
38 | buttonPanel.setLayoutManager(new GridLayout(2).setHorizontalSpacing(1)); | |
39 | buttonPanel.addComponent(new Button(LocalizedString.OK.toString(), new Runnable() { | |
40 | @Override | |
41 | public void run() { | |
42 | onOK(); | |
43 | } | |
44 | }).setLayoutData(GridLayout.createLayoutData(GridLayout.Alignment.CENTER, GridLayout.Alignment.CENTER, true, false))); | |
45 | buttonPanel.addComponent(new Button(LocalizedString.Cancel.toString(), new Runnable() { | |
46 | @Override | |
47 | public void run() { | |
48 | onCancel(); | |
49 | } | |
50 | })); | |
51 | ||
52 | Panel mainPanel = new Panel(); | |
53 | mainPanel.setLayoutManager( | |
54 | new GridLayout(1) | |
55 | .setLeftMarginSize(1) | |
56 | .setRightMarginSize(1)); | |
57 | if(description != null) { | |
58 | mainPanel.addComponent(new Label(description)); | |
59 | } | |
60 | mainPanel.addComponent(new EmptySpace(TerminalSize.ONE)); | |
61 | textBox.setLayoutData( | |
62 | GridLayout.createLayoutData( | |
63 | GridLayout.Alignment.FILL, | |
64 | GridLayout.Alignment.CENTER, | |
65 | true, | |
66 | false)) | |
67 | .addTo(mainPanel); | |
68 | mainPanel.addComponent(new EmptySpace(TerminalSize.ONE)); | |
69 | buttonPanel.setLayoutData( | |
70 | GridLayout.createLayoutData( | |
71 | GridLayout.Alignment.END, | |
72 | GridLayout.Alignment.CENTER, | |
73 | false, | |
74 | false)) | |
75 | .addTo(mainPanel); | |
76 | setComponent(mainPanel); | |
77 | } | |
78 | ||
79 | private void onOK() { | |
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); | |
85 | return; | |
86 | } | |
87 | } | |
88 | result = text; | |
89 | close(); | |
90 | } | |
91 | ||
92 | private void onCancel() { | |
93 | close(); | |
94 | } | |
95 | ||
96 | @Override | |
97 | public String showDialog(WindowBasedTextGUI textGUI) { | |
98 | result = null; | |
99 | super.showDialog(textGUI); | |
100 | return result; | |
101 | } | |
102 | ||
103 | /** | |
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 | |
110 | */ | |
111 | public static String showDialog(WindowBasedTextGUI textGUI, String title, String description, String initialContent) { | |
112 | TextInputDialog textInputDialog = new TextInputDialogBuilder() | |
113 | .setTitle(title) | |
114 | .setDescription(description) | |
115 | .setInitialContent(initialContent) | |
116 | .build(); | |
117 | return textInputDialog.showDialog(textGUI); | |
118 | } | |
119 | ||
120 | /** | |
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 | |
127 | */ | |
128 | public static BigInteger showNumberDialog(WindowBasedTextGUI textGUI, String title, String description, String initialContent) { | |
129 | TextInputDialog textInputDialog = new TextInputDialogBuilder() | |
130 | .setTitle(title) | |
131 | .setDescription(description) | |
132 | .setInitialContent(initialContent) | |
133 | .setValidationPattern(Pattern.compile("[0-9]+"), "Not a number") | |
134 | .build(); | |
135 | String numberString = textInputDialog.showDialog(textGUI); | |
136 | return numberString != null ? new BigInteger(numberString) : null; | |
137 | } | |
138 | ||
139 | /** | |
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 | |
146 | */ | |
147 | public static String showPasswordDialog(WindowBasedTextGUI textGUI, String title, String description, String initialContent) { | |
148 | TextInputDialog textInputDialog = new TextInputDialogBuilder() | |
149 | .setTitle(title) | |
150 | .setDescription(description) | |
151 | .setInitialContent(initialContent) | |
152 | .setPasswordInput(true) | |
153 | .build(); | |
154 | return textInputDialog.showDialog(textGUI); | |
155 | } | |
156 | } |