2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 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
;
134 // ------------------------------------------------------------------------
135 // Constructors -----------------------------------------------------------
136 // ------------------------------------------------------------------------
139 * Public constructor. The message box will be centered on screen.
141 * @param application TApplication that manages this window
142 * @param title window title, will be centered along the top border
143 * @param caption message to display. Use embedded newlines to get a
146 public TMessageBox(final TApplication application
, final String title
,
147 final String caption
) {
149 this(application
, title
, caption
, Type
.OK
, true);
153 * Public constructor. The message box will be centered on screen.
155 * @param application TApplication that manages this window
156 * @param title window title, will be centered along the top border
157 * @param caption message to display. Use embedded newlines to get a
159 * @param type one of the Type constants. Default is Type.OK.
161 public TMessageBox(final TApplication application
, final String title
,
162 final String caption
, final Type type
) {
164 this(application
, title
, caption
, type
, true);
168 * Public constructor. The message box will be centered on screen.
170 * @param application TApplication that manages this window
171 * @param title window title, will be centered along the top border
172 * @param caption message to display. Use embedded newlines to get a
174 * @param type one of the Type constants. Default is Type.OK.
175 * @param yield if true, yield this Thread. Subclasses need to set this
176 * to false and yield at their end of their constructor intead.
178 protected TMessageBox(final TApplication application
, final String title
,
179 final String caption
, final Type type
, final boolean yield
) {
181 // Start as 100x100 at (1, 1). These will be changed later.
182 super(application
, title
, 1, 1, 100, 100, CENTERED
| MODAL
);
184 // Hang onto type so that we can provide more convenience in
188 // Determine width and height
189 String
[] lines
= caption
.split("\n");
190 int width
= title
.length() + 12;
191 setHeight(6 + lines
.length
);
192 for (String line
: lines
) {
193 if (line
.length() + 4 > width
) {
194 width
= line
.length() + 4;
198 if (getWidth() > getScreen().getWidth()) {
199 setWidth(getScreen().getWidth());
201 // Re-center window to get an appropriate (x, y)
204 // Now add my elements
206 for (String line
: lines
) {
207 addLabel(line
, 1, lineI
, "twindow.background.modal");
213 buttons
= new ArrayList
<TButton
>();
217 // Setup button actions
222 if (getWidth() < 15) {
225 buttonX
= (getWidth() - 11) / 2;
226 buttons
.add(addButton(i18n
.getString("okButton"), buttonX
, lineI
,
230 getApplication().closeWindow(TMessageBox
.this);
238 result
= Result
.CANCEL
;
239 if (getWidth() < 26) {
242 buttonX
= (getWidth() - 22) / 2;
243 buttons
.add(addButton(i18n
.getString("okButton"), buttonX
, lineI
,
247 getApplication().closeWindow(TMessageBox
.this);
253 buttons
.add(addButton(i18n
.getString("cancelButton"), buttonX
, lineI
,
256 result
= Result
.CANCEL
;
257 getApplication().closeWindow(TMessageBox
.this);
266 if (getWidth() < 20) {
269 buttonX
= (getWidth() - 16) / 2;
270 buttons
.add(addButton(i18n
.getString("yesButton"), buttonX
, lineI
,
274 getApplication().closeWindow(TMessageBox
.this);
280 buttons
.add(addButton(i18n
.getString("noButton"), buttonX
, lineI
,
284 getApplication().closeWindow(TMessageBox
.this);
292 result
= Result
.CANCEL
;
293 if (getWidth() < 31) {
296 buttonX
= (getWidth() - 27) / 2;
297 buttons
.add(addButton(i18n
.getString("yesButton"), buttonX
, lineI
,
301 getApplication().closeWindow(TMessageBox
.this);
307 buttons
.add(addButton(i18n
.getString("noButton"), buttonX
, lineI
,
311 getApplication().closeWindow(TMessageBox
.this);
317 buttons
.add(addButton(i18n
.getString("cancelButton"), buttonX
,
321 result
= Result
.CANCEL
;
322 getApplication().closeWindow(TMessageBox
.this);
330 throw new IllegalArgumentException("Invalid message box type: " +
335 // Set the secondaryThread to run me
336 getApplication().enableSecondaryEventReceiver(this);
338 // Yield to the secondary thread. When I come back from the
339 // constructor response will already be set.
340 getApplication().yield();
344 // ------------------------------------------------------------------------
345 // TWindow ----------------------------------------------------------------
346 // ------------------------------------------------------------------------
351 * @param keypress keystroke event
354 public void onKeypress(final TKeypressEvent keypress
) {
356 if (this instanceof TInputBox
) {
357 super.onKeypress(keypress
);
361 // Some convenience for message boxes: Alt won't be needed for the
366 if (keypress
.equals(kbO
)) {
367 buttons
.get(0).dispatch();
373 if (keypress
.equals(kbO
)) {
374 buttons
.get(0).dispatch();
376 } else if (keypress
.equals(kbC
)) {
377 buttons
.get(1).dispatch();
383 if (keypress
.equals(kbY
)) {
384 buttons
.get(0).dispatch();
386 } else if (keypress
.equals(kbN
)) {
387 buttons
.get(1).dispatch();
393 if (keypress
.equals(kbY
)) {
394 buttons
.get(0).dispatch();
396 } else if (keypress
.equals(kbN
)) {
397 buttons
.get(1).dispatch();
399 } else if (keypress
.equals(kbC
)) {
400 buttons
.get(2).dispatch();
406 throw new IllegalArgumentException("Invalid message box type: " +
410 super.onKeypress(keypress
);
413 // ------------------------------------------------------------------------
414 // TMessageBox ------------------------------------------------------------
415 // ------------------------------------------------------------------------
420 * @return the result: OK, CANCEL, YES, or NO.
422 public final Result
getResult() {
427 * See if the user clicked YES.
429 * @return true if the user clicked YES
431 public final boolean isYes() {
432 return (result
== Result
.YES
);
436 * See if the user clicked NO.
438 * @return true if the user clicked NO
440 public final boolean isNo() {
441 return (result
== Result
.NO
);
445 * See if the user clicked OK.
447 * @return true if the user clicked OK
449 public final boolean isOk() {
450 return (result
== Result
.OK
);
454 * See if the user clicked CANCEL.
456 * @return true if the user clicked CANCEL
458 public final boolean isCancel() {
459 return (result
== Result
.CANCEL
);