2 * Jexer - Java Text User Interface
4 * License: LGPLv3 or later
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.
10 * Copyright (C) 2015 Kevin Lamonte
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.
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.
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
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
33 import java
.util
.ArrayList
;
34 import java
.util
.List
;
37 * TMessageBox is a system-modal dialog with buttons for OK, Cancel, Yes, or
43 * box = application.messageBox(title, caption,
44 * TMessageBox.Type.OK | TMessageBox.Type.CANCEL);
46 * if (box.getResult() == TMessageBox.OK) {
47 * ... the user pressed OK, do stuff ...
53 public class TMessageBox
extends TWindow
{
56 * Message boxes have these supported types.
65 * Show both OK and Cancel buttons.
70 * Show both Yes and No buttons.
75 * Show Yes, No, and Cancel buttons.
81 * Message boxes have these possible results.
90 * User clicked "Cancel".
107 * Which button was clicked: OK, CANCEL, YES, or NO.
109 private Result result
= Result
.OK
;
114 * @return the result: OK, CANCEL, YES, or NO.
116 public final Result
getResult() {
121 * Public constructor. The message box will be centered on screen.
123 * @param application TApplication that manages this window
124 * @param title window title, will be centered along the top border
125 * @param caption message to display. Use embedded newlines to get a
128 public TMessageBox(final TApplication application
, final String title
,
129 final String caption
) {
131 this(application
, title
, caption
, Type
.OK
, true);
135 * Public constructor. The message box will be centered on screen.
137 * @param application TApplication that manages this window
138 * @param title window title, will be centered along the top border
139 * @param caption message to display. Use embedded newlines to get a
141 * @param type one of the Type constants. Default is Type.OK.
143 public TMessageBox(final TApplication application
, final String title
,
144 final String caption
, final Type type
) {
146 this(application
, title
, caption
, type
, 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.
157 * @param yield if true, yield this Thread. Subclasses need to set this
158 * to false and yield at their end of their constructor intead.
160 protected TMessageBox(final TApplication application
, final String title
,
161 final String caption
, final Type type
, final boolean yield
) {
163 // Start as 50x50 at (1, 1). These will be changed later.
164 super(application
, title
, 1, 1, 100, 100, CENTERED
| MODAL
);
166 // Determine width and height
167 String
[] lines
= caption
.split("\n");
168 int width
= title
.length() + 12;
169 setHeight(6 + lines
.length
);
170 for (String line
: lines
) {
171 if (line
.length() + 4 > width
) {
172 width
= line
.length() + 4;
176 if (getWidth() > getScreen().getWidth()) {
177 setWidth(getScreen().getWidth());
179 // Re-center window to get an appropriate (x, y)
182 // Now add my elements
184 for (String line
: lines
) {
185 addLabel(line
, 1, lineI
, "twindow.background.modal");
191 List
<TButton
> buttons
= new ArrayList
<TButton
>();
195 // Setup button actions
200 if (getWidth() < 15) {
203 buttonX
= (getWidth() - 11) / 2;
204 buttons
.add(addButton(" &OK ", buttonX
, lineI
,
208 getApplication().closeWindow(TMessageBox
.this);
216 result
= Result
.CANCEL
;
217 if (getWidth() < 26) {
220 buttonX
= (getWidth() - 22) / 2;
221 buttons
.add(addButton(" &OK ", buttonX
, lineI
,
225 getApplication().closeWindow(TMessageBox
.this);
231 buttons
.add(addButton("&Cancel", buttonX
, lineI
,
234 result
= Result
.CANCEL
;
235 getApplication().closeWindow(TMessageBox
.this);
244 if (getWidth() < 20) {
247 buttonX
= (getWidth() - 16) / 2;
248 buttons
.add(addButton("&Yes", buttonX
, lineI
,
252 getApplication().closeWindow(TMessageBox
.this);
258 buttons
.add(addButton("&No", buttonX
, lineI
,
262 getApplication().closeWindow(TMessageBox
.this);
270 result
= Result
.CANCEL
;
271 if (getWidth() < 31) {
274 buttonX
= (getWidth() - 27) / 2;
275 buttons
.add(addButton("&Yes", buttonX
, lineI
,
279 getApplication().closeWindow(TMessageBox
.this);
285 buttons
.add(addButton("&No", buttonX
, lineI
,
289 getApplication().closeWindow(TMessageBox
.this);
295 buttons
.add(addButton("&Cancel", buttonX
, lineI
,
298 result
= Result
.CANCEL
;
299 getApplication().closeWindow(TMessageBox
.this);
307 throw new IllegalArgumentException("Invalid message box type: " + type
);
310 // Set the secondaryThread to run me
311 getApplication().enableSecondaryEventReceiver(this);
314 // Yield to the secondary thread. When I come back from the
315 // constructor response will already be set.
316 getApplication().yield();