1 package com
.googlecode
.lanterna
.gui2
.dialogs
;
3 import com
.googlecode
.lanterna
.TerminalSize
;
5 import java
.util
.regex
.Matcher
;
6 import java
.util
.regex
.Pattern
;
9 * Dialog builder for the {@code TextInputDialog} class, use this to create instances of that class and to customize
13 public class TextInputDialogBuilder
extends AbstractDialogBuilder
<TextInputDialogBuilder
, TextInputDialog
> {
14 private String initialContent
;
15 private TerminalSize textBoxSize
;
16 private TextInputDialogResultValidator validator
;
17 private boolean passwordInput
;
22 public TextInputDialogBuilder() {
23 super("TextInputDialog");
24 this.initialContent
= "";
25 this.textBoxSize
= null;
26 this.validator
= null;
27 this.passwordInput
= false;
31 protected TextInputDialogBuilder
self() {
35 protected TextInputDialog
buildDialog() {
36 TerminalSize size
= textBoxSize
;
37 if ((initialContent
== null || initialContent
.trim().equals("")) && size
== null) {
38 size
= new TerminalSize(40, 1);
40 return new TextInputDialog(
50 * Sets the initial content the dialog will have
51 * @param initialContent Initial content the dialog will have
54 public TextInputDialogBuilder
setInitialContent(String initialContent
) {
55 this.initialContent
= initialContent
;
60 * Returns the initial content the dialog will have
61 * @return Initial content the dialog will have
63 public String
getInitialContent() {
64 return initialContent
;
68 * Sets the size of the text box the dialog will have
69 * @param textBoxSize Size of the text box the dialog will have
72 public TextInputDialogBuilder
setTextBoxSize(TerminalSize textBoxSize
) {
73 this.textBoxSize
= textBoxSize
;
78 * Returns the size of the text box the dialog will have
79 * @return Size of the text box the dialog will have
81 public TerminalSize
getTextBoxSize() {
86 * Sets the validator that will be attached to the text box in the dialog
87 * @param validator Validator that will be attached to the text box in the dialog
90 public TextInputDialogBuilder
setValidator(TextInputDialogResultValidator validator
) {
91 this.validator
= validator
;
96 * Returns the validator that will be attached to the text box in the dialog
97 * @return validator that will be attached to the text box in the dialog
99 public TextInputDialogResultValidator
getValidator() {
104 * Helper method that assigned a validator to the text box the dialog will have which matches the pattern supplied
105 * @param pattern Pattern to validate the text box
106 * @param errorMessage Error message to show when the pattern doesn't match
109 public TextInputDialogBuilder
setValidationPattern(final Pattern pattern
, final String errorMessage
) {
110 return setValidator(new TextInputDialogResultValidator() {
112 public String
validate(String content
) {
113 Matcher matcher
= pattern
.matcher(content
);
114 if(!matcher
.matches()) {
115 if(errorMessage
== null) {
116 return "Invalid input";
126 * Sets if the text box the dialog will have contains a password and should be masked (default: {@code false})
127 * @param passwordInput {@code true} if the text box should be password masked, {@code false} otherwise
130 public TextInputDialogBuilder
setPasswordInput(boolean passwordInput
) {
131 this.passwordInput
= passwordInput
;
136 * Returns {@code true} if the text box the dialog will have contains a password and should be masked
137 * @return {@code true} if the text box the dialog will have contains a password and should be masked
139 public boolean isPasswordInput() {
140 return passwordInput
;