7bcab3187899df0ec334d7089c205bd56df2b94f
[nikiroo-utils.git] / src / jexer / TInputBox.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * License: LGPLv3 or later
5 *
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
9 *
10 * Copyright (C) 2015 Kevin Lamonte
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26 * 02110-1301 USA
27 *
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 * @version 1
30 */
31 package jexer;
32
33 /**
34 * TInputBox is a system-modal dialog with an OK button and a text input
35 * field. Call it like:
36 *
37 * <p>
38 * <pre>
39 * {@code
40 * box = application.inputBox(title, caption);
41 * if (box.getText().equals("yes")) {
42 * ... the user entered "yes", do stuff ...
43 * }
44 * }
45 * </pre>
46 *
47 */
48 public final class TInputBox extends TMessageBox {
49
50 /**
51 * The input field.
52 */
53 private TField field;
54
55 /**
56 * Retrieve the answer text.
57 *
58 * @return the answer text
59 */
60 public String getText() {
61 return field.getText();
62 }
63
64 /**
65 * Public constructor. The input box will be centered on screen.
66 *
67 * @param application TApplication that manages this window
68 * @param title window title, will be centered along the top border
69 * @param caption message to display. Use embedded newlines to get a
70 * multi-line box.
71 */
72 public TInputBox(final TApplication application, final String title,
73 final String caption) {
74
75 this(application, title, caption, "");
76 }
77
78 /**
79 * Public constructor. The input box will be centered on screen.
80 *
81 * @param application TApplication that manages this window
82 * @param title window title, will be centered along the top border
83 * @param caption message to display. Use embedded newlines to get a
84 * multi-line box.
85 * @param text initial text to seed the field with
86 */
87 public TInputBox(final TApplication application, final String title,
88 final String caption, final String text) {
89
90 super(application, title, caption, Type.OK, false);
91
92 for (TWidget widget: getChildren()) {
93 if (widget instanceof TButton) {
94 widget.setY(widget.getY() + 2);
95 }
96 }
97
98 setHeight(getHeight() + 2);
99 field = addField(1, getHeight() - 6, getWidth() - 4, false, text);
100
101 // Yield to the secondary thread. When I come back from the
102 // constructor response will already be set.
103 getApplication().yield();
104 }
105
106 }