2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2017 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
31 import java
.util
.ArrayList
;
32 import java
.util
.List
;
33 import java
.util
.ResourceBundle
;
35 import jexer
.event
.TKeypressEvent
;
36 import static jexer
.TKeypress
.*;
39 * TMessageBox is a system-modal dialog with buttons for OK, Cancel, Yes, or
44 * box = application.messageBox(title, caption,
45 * TMessageBox.Type.OK | TMessageBox.Type.CANCEL);
47 * if (box.getResult() == TMessageBox.OK) {
48 * ... the user pressed OK, do stuff ...
54 public class TMessageBox
extends TWindow
{
59 private static final ResourceBundle i18n
= ResourceBundle
.getBundle(TMessageBox
.class.getName());
61 // ------------------------------------------------------------------------
62 // Constants --------------------------------------------------------------
63 // ------------------------------------------------------------------------
66 * Message boxes have these supported types.
75 * Show both OK and Cancel buttons.
80 * Show both Yes and No buttons.
85 * Show Yes, No, and Cancel buttons.
91 * Message boxes have these possible results.
100 * User clicked "Cancel".
105 * User clicked "Yes".
115 // ------------------------------------------------------------------------
116 // Variables --------------------------------------------------------------
117 // ------------------------------------------------------------------------
120 * The type of this message box.
127 private List
<TButton
> buttons
;
130 * Which button was clicked: OK, CANCEL, YES, or NO.
132 private Result result
= Result
.OK
;
137 * @return the result: OK, CANCEL, YES, or NO.
139 public final Result
getResult() {
143 // ------------------------------------------------------------------------
144 // Constructors -----------------------------------------------------------
145 // ------------------------------------------------------------------------
148 * Public constructor. The message box will be centered on screen.
150 * @param application TApplication that manages this window
151 * @param title window title, will be centered along the top border
152 * @param caption message to display. Use embedded newlines to get a
155 public TMessageBox(final TApplication application
, final String title
,
156 final String caption
) {
158 this(application
, title
, caption
, Type
.OK
, true);
162 * Public constructor. The message box will be centered on screen.
164 * @param application TApplication that manages this window
165 * @param title window title, will be centered along the top border
166 * @param caption message to display. Use embedded newlines to get a
168 * @param type one of the Type constants. Default is Type.OK.
170 public TMessageBox(final TApplication application
, final String title
,
171 final String caption
, final Type type
) {
173 this(application
, title
, caption
, type
, true);
177 * Public constructor. The message box will be centered on screen.
179 * @param application TApplication that manages this window
180 * @param title window title, will be centered along the top border
181 * @param caption message to display. Use embedded newlines to get a
183 * @param type one of the Type constants. Default is Type.OK.
184 * @param yield if true, yield this Thread. Subclasses need to set this
185 * to false and yield at their end of their constructor intead.
187 protected TMessageBox(final TApplication application
, final String title
,
188 final String caption
, final Type type
, final boolean yield
) {
190 // Start as 50x50 at (1, 1). These will be changed later.
191 super(application
, title
, 1, 1, 100, 100, CENTERED
| MODAL
);
193 // Hang onto type so that we can provide more convenience in
197 // Determine width and height
198 String
[] lines
= caption
.split("\n");
199 int width
= title
.length() + 12;
200 setHeight(6 + lines
.length
);
201 for (String line
: lines
) {
202 if (line
.length() + 4 > width
) {
203 width
= line
.length() + 4;
207 if (getWidth() > getScreen().getWidth()) {
208 setWidth(getScreen().getWidth());
210 // Re-center window to get an appropriate (x, y)
213 // Now add my elements
215 for (String line
: lines
) {
216 addLabel(line
, 1, lineI
, "twindow.background.modal");
222 buttons
= new ArrayList
<TButton
>();
226 // Setup button actions
231 if (getWidth() < 15) {
234 buttonX
= (getWidth() - 11) / 2;
235 buttons
.add(addButton(i18n
.getString("okButton"), buttonX
, lineI
,
239 getApplication().closeWindow(TMessageBox
.this);
247 result
= Result
.CANCEL
;
248 if (getWidth() < 26) {
251 buttonX
= (getWidth() - 22) / 2;
252 buttons
.add(addButton(i18n
.getString("okButton"), buttonX
, lineI
,
256 getApplication().closeWindow(TMessageBox
.this);
262 buttons
.add(addButton(i18n
.getString("cancelButton"), buttonX
, lineI
,
265 result
= Result
.CANCEL
;
266 getApplication().closeWindow(TMessageBox
.this);
275 if (getWidth() < 20) {
278 buttonX
= (getWidth() - 16) / 2;
279 buttons
.add(addButton(i18n
.getString("yesButton"), buttonX
, lineI
,
283 getApplication().closeWindow(TMessageBox
.this);
289 buttons
.add(addButton(i18n
.getString("noButton"), buttonX
, lineI
,
293 getApplication().closeWindow(TMessageBox
.this);
301 result
= Result
.CANCEL
;
302 if (getWidth() < 31) {
305 buttonX
= (getWidth() - 27) / 2;
306 buttons
.add(addButton(i18n
.getString("yesButton"), buttonX
, lineI
,
310 getApplication().closeWindow(TMessageBox
.this);
316 buttons
.add(addButton(i18n
.getString("noButton"), buttonX
, lineI
,
320 getApplication().closeWindow(TMessageBox
.this);
326 buttons
.add(addButton(i18n
.getString("cancelButton"), buttonX
,
330 result
= Result
.CANCEL
;
331 getApplication().closeWindow(TMessageBox
.this);
339 throw new IllegalArgumentException("Invalid message box type: " + type
);
342 // Set the secondaryThread to run me
343 getApplication().enableSecondaryEventReceiver(this);
346 // Yield to the secondary thread. When I come back from the
347 // constructor response will already be set.
348 getApplication().yield();
352 // ------------------------------------------------------------------------
353 // TWindow ----------------------------------------------------------------
354 // ------------------------------------------------------------------------
359 * @param keypress keystroke event
362 public void onKeypress(final TKeypressEvent keypress
) {
364 if (this instanceof TInputBox
) {
365 super.onKeypress(keypress
);
369 // Some convenience for message boxes: Alt won't be needed for the
374 if (keypress
.equals(kbO
)) {
375 buttons
.get(0).dispatch();
381 if (keypress
.equals(kbO
)) {
382 buttons
.get(0).dispatch();
384 } else if (keypress
.equals(kbC
)) {
385 buttons
.get(1).dispatch();
391 if (keypress
.equals(kbY
)) {
392 buttons
.get(0).dispatch();
394 } else if (keypress
.equals(kbN
)) {
395 buttons
.get(1).dispatch();
401 if (keypress
.equals(kbY
)) {
402 buttons
.get(0).dispatch();
404 } else if (keypress
.equals(kbN
)) {
405 buttons
.get(1).dispatch();
407 } else if (keypress
.equals(kbC
)) {
408 buttons
.get(2).dispatch();
414 throw new IllegalArgumentException("Invalid message box type: " +
418 super.onKeypress(keypress
);