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());
62 * Message boxes have these supported types.
71 * Show both OK and Cancel buttons.
76 * Show both Yes and No buttons.
81 * Show Yes, No, and Cancel buttons.
87 * The type of this message box.
94 private List
<TButton
> buttons
;
97 * Message boxes have these possible results.
106 * User clicked "Cancel".
111 * User clicked "Yes".
122 * Which button was clicked: OK, CANCEL, YES, or NO.
124 private Result result
= Result
.OK
;
129 * @return the result: OK, CANCEL, YES, or NO.
131 public final Result
getResult() {
136 * Public constructor. The message box will be centered on screen.
138 * @param application TApplication that manages this window
139 * @param title window title, will be centered along the top border
140 * @param caption message to display. Use embedded newlines to get a
143 public TMessageBox(final TApplication application
, final String title
,
144 final String caption
) {
146 this(application
, title
, caption
, Type
.OK
, true);
150 * Public constructor. The message box will be centered on screen.
152 * @param application TApplication that manages this window
153 * @param title window title, will be centered along the top border
154 * @param caption message to display. Use embedded newlines to get a
156 * @param type one of the Type constants. Default is Type.OK.
158 public TMessageBox(final TApplication application
, final String title
,
159 final String caption
, final Type type
) {
161 this(application
, title
, caption
, type
, true);
165 * Public constructor. The message box will be centered on screen.
167 * @param application TApplication that manages this window
168 * @param title window title, will be centered along the top border
169 * @param caption message to display. Use embedded newlines to get a
171 * @param type one of the Type constants. Default is Type.OK.
172 * @param yield if true, yield this Thread. Subclasses need to set this
173 * to false and yield at their end of their constructor intead.
175 protected TMessageBox(final TApplication application
, final String title
,
176 final String caption
, final Type type
, final boolean yield
) {
178 // Start as 50x50 at (1, 1). These will be changed later.
179 super(application
, title
, 1, 1, 100, 100, CENTERED
| MODAL
);
181 // Hang onto type so that we can provide more convenience in
185 // Determine width and height
186 String
[] lines
= caption
.split("\n");
187 int width
= title
.length() + 12;
188 setHeight(6 + lines
.length
);
189 for (String line
: lines
) {
190 if (line
.length() + 4 > width
) {
191 width
= line
.length() + 4;
195 if (getWidth() > getScreen().getWidth()) {
196 setWidth(getScreen().getWidth());
198 // Re-center window to get an appropriate (x, y)
201 // Now add my elements
203 for (String line
: lines
) {
204 addLabel(line
, 1, lineI
, "twindow.background.modal");
210 buttons
= new ArrayList
<TButton
>();
214 // Setup button actions
219 if (getWidth() < 15) {
222 buttonX
= (getWidth() - 11) / 2;
223 buttons
.add(addButton(i18n
.getString("okButton"), buttonX
, lineI
,
227 getApplication().closeWindow(TMessageBox
.this);
235 result
= Result
.CANCEL
;
236 if (getWidth() < 26) {
239 buttonX
= (getWidth() - 22) / 2;
240 buttons
.add(addButton(i18n
.getString("okButton"), buttonX
, lineI
,
244 getApplication().closeWindow(TMessageBox
.this);
250 buttons
.add(addButton(i18n
.getString("cancelButton"), buttonX
, lineI
,
253 result
= Result
.CANCEL
;
254 getApplication().closeWindow(TMessageBox
.this);
263 if (getWidth() < 20) {
266 buttonX
= (getWidth() - 16) / 2;
267 buttons
.add(addButton(i18n
.getString("yesButton"), buttonX
, lineI
,
271 getApplication().closeWindow(TMessageBox
.this);
277 buttons
.add(addButton(i18n
.getString("noButton"), buttonX
, lineI
,
281 getApplication().closeWindow(TMessageBox
.this);
289 result
= Result
.CANCEL
;
290 if (getWidth() < 31) {
293 buttonX
= (getWidth() - 27) / 2;
294 buttons
.add(addButton(i18n
.getString("yesButton"), buttonX
, lineI
,
298 getApplication().closeWindow(TMessageBox
.this);
304 buttons
.add(addButton(i18n
.getString("noButton"), buttonX
, lineI
,
308 getApplication().closeWindow(TMessageBox
.this);
314 buttons
.add(addButton(i18n
.getString("cancelButton"), buttonX
,
318 result
= Result
.CANCEL
;
319 getApplication().closeWindow(TMessageBox
.this);
327 throw new IllegalArgumentException("Invalid message box type: " + type
);
330 // Set the secondaryThread to run me
331 getApplication().enableSecondaryEventReceiver(this);
334 // Yield to the secondary thread. When I come back from the
335 // constructor response will already be set.
336 getApplication().yield();
343 * @param keypress keystroke event
346 public void onKeypress(final TKeypressEvent keypress
) {
348 if (this instanceof TInputBox
) {
349 super.onKeypress(keypress
);
353 // Some convenience for message boxes: Alt won't be needed for the
358 if (keypress
.equals(kbO
)) {
359 buttons
.get(0).dispatch();
365 if (keypress
.equals(kbO
)) {
366 buttons
.get(0).dispatch();
368 } else if (keypress
.equals(kbC
)) {
369 buttons
.get(1).dispatch();
375 if (keypress
.equals(kbY
)) {
376 buttons
.get(0).dispatch();
378 } else if (keypress
.equals(kbN
)) {
379 buttons
.get(1).dispatch();
385 if (keypress
.equals(kbY
)) {
386 buttons
.get(0).dispatch();
388 } else if (keypress
.equals(kbN
)) {
389 buttons
.get(1).dispatch();
391 } else if (keypress
.equals(kbC
)) {
392 buttons
.get(2).dispatch();
398 throw new IllegalArgumentException("Invalid message box type: " + type
);
401 super.onKeypress(keypress
);