X-Git-Url: http://git.nikiroo.be/?p=jvcard.git;a=blobdiff_plain;f=src%2Fcom%2Fgooglecode%2Flanterna%2Fgui2%2Fdialogs%2FTextInputDialog.java;fp=src%2Fcom%2Fgooglecode%2Flanterna%2Fgui2%2Fdialogs%2FTextInputDialog.java;h=0000000000000000000000000000000000000000;hp=2107e1c73646084ea349d5707b5cd7226fb83cc5;hb=f06c81000632cfb5f525ca458f719338f55f9f66;hpb=a73a906356c971b080c36368e71a15d87e8b8d31 diff --git a/src/com/googlecode/lanterna/gui2/dialogs/TextInputDialog.java b/src/com/googlecode/lanterna/gui2/dialogs/TextInputDialog.java deleted file mode 100644 index 2107e1c..0000000 --- a/src/com/googlecode/lanterna/gui2/dialogs/TextInputDialog.java +++ /dev/null @@ -1,156 +0,0 @@ -package com.googlecode.lanterna.gui2.dialogs; - -import com.googlecode.lanterna.TerminalSize; -import com.googlecode.lanterna.gui2.*; - -import java.math.BigInteger; -import java.util.regex.Pattern; - -/** - * {@code TextInputDialog} is a modal text input dialog that prompts the user to enter a text string. The class supports - * validation and password masking. The builder class to help setup {@code TextInputDialog}s is - * {@code TextInputDialogBuilder}. - */ -public class TextInputDialog extends DialogWindow { - - private final TextBox textBox; - private final TextInputDialogResultValidator validator; - private String result; - - TextInputDialog( - String title, - String description, - TerminalSize textBoxPreferredSize, - String initialContent, - TextInputDialogResultValidator validator, - boolean password) { - - super(title); - this.result = null; - this.textBox = new TextBox(textBoxPreferredSize, initialContent); - this.validator = validator; - - if(password) { - textBox.setMask('*'); - } - - Panel buttonPanel = new Panel(); - buttonPanel.setLayoutManager(new GridLayout(2).setHorizontalSpacing(1)); - buttonPanel.addComponent(new Button(LocalizedString.OK.toString(), new Runnable() { - @Override - public void run() { - onOK(); - } - }).setLayoutData(GridLayout.createLayoutData(GridLayout.Alignment.CENTER, GridLayout.Alignment.CENTER, true, false))); - buttonPanel.addComponent(new Button(LocalizedString.Cancel.toString(), new Runnable() { - @Override - public void run() { - onCancel(); - } - })); - - Panel mainPanel = new Panel(); - mainPanel.setLayoutManager( - new GridLayout(1) - .setLeftMarginSize(1) - .setRightMarginSize(1)); - if(description != null) { - mainPanel.addComponent(new Label(description)); - } - mainPanel.addComponent(new EmptySpace(TerminalSize.ONE)); - textBox.setLayoutData( - GridLayout.createLayoutData( - GridLayout.Alignment.FILL, - GridLayout.Alignment.CENTER, - true, - false)) - .addTo(mainPanel); - mainPanel.addComponent(new EmptySpace(TerminalSize.ONE)); - buttonPanel.setLayoutData( - GridLayout.createLayoutData( - GridLayout.Alignment.END, - GridLayout.Alignment.CENTER, - false, - false)) - .addTo(mainPanel); - setComponent(mainPanel); - } - - private void onOK() { - String text = textBox.getText(); - if(validator != null) { - String errorMessage = validator.validate(text); - if(errorMessage != null) { - MessageDialog.showMessageDialog(getTextGUI(), getTitle(), errorMessage, MessageDialogButton.OK); - return; - } - } - result = text; - close(); - } - - private void onCancel() { - close(); - } - - @Override - public String showDialog(WindowBasedTextGUI textGUI) { - result = null; - super.showDialog(textGUI); - return result; - } - - /** - * Shortcut for quickly showing a {@code TextInputDialog} - * @param textGUI GUI to show the dialog on - * @param title Title of the dialog - * @param description Description of the dialog - * @param initialContent What content to place in the text box initially - * @return The string the user typed into the text box, or {@code null} if the dialog was cancelled - */ - public static String showDialog(WindowBasedTextGUI textGUI, String title, String description, String initialContent) { - TextInputDialog textInputDialog = new TextInputDialogBuilder() - .setTitle(title) - .setDescription(description) - .setInitialContent(initialContent) - .build(); - return textInputDialog.showDialog(textGUI); - } - - /** - * Shortcut for quickly showing a {@code TextInputDialog} that only accepts numbers - * @param textGUI GUI to show the dialog on - * @param title Title of the dialog - * @param description Description of the dialog - * @param initialContent What content to place in the text box initially - * @return The number the user typed into the text box, or {@code null} if the dialog was cancelled - */ - public static BigInteger showNumberDialog(WindowBasedTextGUI textGUI, String title, String description, String initialContent) { - TextInputDialog textInputDialog = new TextInputDialogBuilder() - .setTitle(title) - .setDescription(description) - .setInitialContent(initialContent) - .setValidationPattern(Pattern.compile("[0-9]+"), "Not a number") - .build(); - String numberString = textInputDialog.showDialog(textGUI); - return numberString != null ? new BigInteger(numberString) : null; - } - - /** - * Shortcut for quickly showing a {@code TextInputDialog} with password masking - * @param textGUI GUI to show the dialog on - * @param title Title of the dialog - * @param description Description of the dialog - * @param initialContent What content to place in the text box initially - * @return The string the user typed into the text box, or {@code null} if the dialog was cancelled - */ - public static String showPasswordDialog(WindowBasedTextGUI textGUI, String title, String description, String initialContent) { - TextInputDialog textInputDialog = new TextInputDialogBuilder() - .setTitle(title) - .setDescription(description) - .setInitialContent(initialContent) - .setPasswordInput(true) - .build(); - return textInputDialog.showDialog(textGUI); - } -}