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
45 * box = application.messageBox(title, caption,
46 * TMessageBox.Type.OK | TMessageBox.Type.CANCEL);
48 * if (box.getResult() == TMessageBox.OK) {
49 * ... the user pressed OK, do stuff ...
55 public class TMessageBox
extends TWindow
{
60 private static final ResourceBundle i18n
= ResourceBundle
.getBundle(TMessageBox
.class.getName());
63 * Message boxes have these supported types.
72 * Show both OK and Cancel buttons.
77 * Show both Yes and No buttons.
82 * Show Yes, No, and Cancel buttons.
88 * The type of this message box.
95 private List
<TButton
> buttons
;
98 * Message boxes have these possible results.
107 * User clicked "Cancel".
112 * User clicked "Yes".
123 * Which button was clicked: OK, CANCEL, YES, or NO.
125 private Result result
= Result
.OK
;
130 * @return the result: OK, CANCEL, YES, or NO.
132 public final Result
getResult() {
137 * Public constructor. The message box will be centered on screen.
139 * @param application TApplication that manages this window
140 * @param title window title, will be centered along the top border
141 * @param caption message to display. Use embedded newlines to get a
144 public TMessageBox(final TApplication application
, final String title
,
145 final String caption
) {
147 this(application
, title
, caption
, Type
.OK
, true);
151 * Public constructor. The message box will be centered on screen.
153 * @param application TApplication that manages this window
154 * @param title window title, will be centered along the top border
155 * @param caption message to display. Use embedded newlines to get a
157 * @param type one of the Type constants. Default is Type.OK.
159 public TMessageBox(final TApplication application
, final String title
,
160 final String caption
, final Type type
) {
162 this(application
, title
, caption
, type
, true);
166 * Public constructor. The message box will be centered on screen.
168 * @param application TApplication that manages this window
169 * @param title window title, will be centered along the top border
170 * @param caption message to display. Use embedded newlines to get a
172 * @param type one of the Type constants. Default is Type.OK.
173 * @param yield if true, yield this Thread. Subclasses need to set this
174 * to false and yield at their end of their constructor intead.
176 protected TMessageBox(final TApplication application
, final String title
,
177 final String caption
, final Type type
, final boolean yield
) {
179 // Start as 50x50 at (1, 1). These will be changed later.
180 super(application
, title
, 1, 1, 100, 100, CENTERED
| MODAL
);
182 // Hang onto type so that we can provide more convenience in
186 // Determine width and height
187 String
[] lines
= caption
.split("\n");
188 int width
= title
.length() + 12;
189 setHeight(6 + lines
.length
);
190 for (String line
: lines
) {
191 if (line
.length() + 4 > width
) {
192 width
= line
.length() + 4;
196 if (getWidth() > getScreen().getWidth()) {
197 setWidth(getScreen().getWidth());
199 // Re-center window to get an appropriate (x, y)
202 // Now add my elements
204 for (String line
: lines
) {
205 addLabel(line
, 1, lineI
, "twindow.background.modal");
211 buttons
= new ArrayList
<TButton
>();
215 // Setup button actions
220 if (getWidth() < 15) {
223 buttonX
= (getWidth() - 11) / 2;
224 buttons
.add(addButton(i18n
.getString("okButton"), buttonX
, lineI
,
228 getApplication().closeWindow(TMessageBox
.this);
236 result
= Result
.CANCEL
;
237 if (getWidth() < 26) {
240 buttonX
= (getWidth() - 22) / 2;
241 buttons
.add(addButton(i18n
.getString("okButton"), buttonX
, lineI
,
245 getApplication().closeWindow(TMessageBox
.this);
251 buttons
.add(addButton(i18n
.getString("cancelButton"), buttonX
, lineI
,
254 result
= Result
.CANCEL
;
255 getApplication().closeWindow(TMessageBox
.this);
264 if (getWidth() < 20) {
267 buttonX
= (getWidth() - 16) / 2;
268 buttons
.add(addButton(i18n
.getString("yesButton"), buttonX
, lineI
,
272 getApplication().closeWindow(TMessageBox
.this);
278 buttons
.add(addButton(i18n
.getString("noButton"), buttonX
, lineI
,
282 getApplication().closeWindow(TMessageBox
.this);
290 result
= Result
.CANCEL
;
291 if (getWidth() < 31) {
294 buttonX
= (getWidth() - 27) / 2;
295 buttons
.add(addButton(i18n
.getString("yesButton"), buttonX
, lineI
,
299 getApplication().closeWindow(TMessageBox
.this);
305 buttons
.add(addButton(i18n
.getString("noButton"), buttonX
, lineI
,
309 getApplication().closeWindow(TMessageBox
.this);
315 buttons
.add(addButton(i18n
.getString("cancelButton"), buttonX
,
319 result
= Result
.CANCEL
;
320 getApplication().closeWindow(TMessageBox
.this);
328 throw new IllegalArgumentException("Invalid message box type: " + type
);
331 // Set the secondaryThread to run me
332 getApplication().enableSecondaryEventReceiver(this);
335 // Yield to the secondary thread. When I come back from the
336 // constructor response will already be set.
337 getApplication().yield();
344 * @param keypress keystroke event
347 public void onKeypress(final TKeypressEvent keypress
) {
349 if (this instanceof TInputBox
) {
350 super.onKeypress(keypress
);
354 // Some convenience for message boxes: Alt won't be needed for the
359 if (keypress
.equals(kbO
)) {
360 buttons
.get(0).dispatch();
366 if (keypress
.equals(kbO
)) {
367 buttons
.get(0).dispatch();
369 } else if (keypress
.equals(kbC
)) {
370 buttons
.get(1).dispatch();
376 if (keypress
.equals(kbY
)) {
377 buttons
.get(0).dispatch();
379 } else if (keypress
.equals(kbN
)) {
380 buttons
.get(1).dispatch();
386 if (keypress
.equals(kbY
)) {
387 buttons
.get(0).dispatch();
389 } else if (keypress
.equals(kbN
)) {
390 buttons
.get(1).dispatch();
392 } else if (keypress
.equals(kbC
)) {
393 buttons
.get(2).dispatch();
399 throw new IllegalArgumentException("Invalid message box type: " + type
);
402 super.onKeypress(keypress
);