Fix UTF8 bug, create first executable JAR file
[jvcard.git] / src / com / googlecode / lanterna / gui2 / dialogs / TextInputDialogBuilder.java
CommitLineData
a3b510ab
NR
1package com.googlecode.lanterna.gui2.dialogs;
2
3import com.googlecode.lanterna.TerminalSize;
4
5import java.util.regex.Matcher;
6import java.util.regex.Pattern;
7
8/**
9 * Dialog builder for the {@code TextInputDialog} class, use this to create instances of that class and to customize
10 * them
11 * @author Martin
12 */
13public class TextInputDialogBuilder extends AbstractDialogBuilder<TextInputDialogBuilder, TextInputDialog> {
14 private String initialContent;
15 private TerminalSize textBoxSize;
16 private TextInputDialogResultValidator validator;
17 private boolean passwordInput;
18
19 /**
20 * Default constructor
21 */
22 public TextInputDialogBuilder() {
23 super("TextInputDialog");
24 this.initialContent = "";
25 this.textBoxSize = null;
26 this.validator = null;
27 this.passwordInput = false;
28 }
29
30 @Override
31 protected TextInputDialogBuilder self() {
32 return this;
33 }
34
35 protected TextInputDialog buildDialog() {
36 TerminalSize size = textBoxSize;
37 if ((initialContent == null || initialContent.trim().equals("")) && size == null) {
38 size = new TerminalSize(40, 1);
39 }
40 return new TextInputDialog(
41 title,
42 description,
43 size,
44 initialContent,
45 validator,
46 passwordInput);
47 }
48
49 /**
50 * Sets the initial content the dialog will have
51 * @param initialContent Initial content the dialog will have
52 * @return Itself
53 */
54 public TextInputDialogBuilder setInitialContent(String initialContent) {
55 this.initialContent = initialContent;
56 return this;
57 }
58
59 /**
60 * Returns the initial content the dialog will have
61 * @return Initial content the dialog will have
62 */
63 public String getInitialContent() {
64 return initialContent;
65 }
66
67 /**
68 * Sets the size of the text box the dialog will have
69 * @param textBoxSize Size of the text box the dialog will have
70 * @return Itself
71 */
72 public TextInputDialogBuilder setTextBoxSize(TerminalSize textBoxSize) {
73 this.textBoxSize = textBoxSize;
74 return this;
75 }
76
77 /**
78 * Returns the size of the text box the dialog will have
79 * @return Size of the text box the dialog will have
80 */
81 public TerminalSize getTextBoxSize() {
82 return textBoxSize;
83 }
84
85 /**
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
88 * @return Itself
89 */
90 public TextInputDialogBuilder setValidator(TextInputDialogResultValidator validator) {
91 this.validator = validator;
92 return this;
93 }
94
95 /**
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
98 */
99 public TextInputDialogResultValidator getValidator() {
100 return validator;
101 }
102
103 /**
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
107 * @return Itself
108 */
109 public TextInputDialogBuilder setValidationPattern(final Pattern pattern, final String errorMessage) {
110 return setValidator(new TextInputDialogResultValidator() {
111 @Override
112 public String validate(String content) {
113 Matcher matcher = pattern.matcher(content);
114 if(!matcher.matches()) {
115 if(errorMessage == null) {
116 return "Invalid input";
117 }
118 return errorMessage;
119 }
120 return null;
121 }
122 });
123 }
124
125 /**
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
128 * @return Itself
129 */
130 public TextInputDialogBuilder setPasswordInput(boolean passwordInput) {
131 this.passwordInput = passwordInput;
132 return this;
133 }
134
135 /**
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
138 */
139 public boolean isPasswordInput() {
140 return passwordInput;
141 }
142}