a179b0746eed115b90c9b79176982dcc259ba574
2 * Jexer - Java Text User Interface - demonstration program
6 * Author: Kevin Lamonte, <a href="mailto:kevin.lamonte@gmail.com">kevin.lamonte@gmail.com</a>
8 * License: LGPLv3 or later
10 * Copyright: This module is licensed under the GNU Lesser General
11 * Public License Version 3. Please see the file "COPYING" in this
12 * directory for more information about the GNU Lesser General Public
15 * Copyright (C) 2015 Kevin Lamonte
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public License
19 * as published by the Free Software Foundation; either version 3 of
20 * the License, or (at your option) any later version.
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * General Public License for more details.
27 * You should have received a copy of the GNU Lesser General Public
28 * License along with this program; if not, see
29 * http://www.gnu.org/licenses/, or write to the Free Software
30 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
38 class DemoTextWindow
extends TWindow
{
41 * Hang onto my TText so I can resize it with the window.
43 private TText textField
;
48 public DemoTextWindow(TApplication parent
) {
49 super(parent
, "Text Areas", 0, 0, 44, 20, RESIZABLE
);
52 "This is an example of a reflowable text field. Some example text follows.\n" +
54 "This library implements a text-based windowing system loosely\n" +
55 "reminiscient of Borland's [Turbo\n" +
56 "Vision](http://en.wikipedia.org/wiki/Turbo_Vision) library. For those\n" +
57 "wishing to use the actual C++ Turbo Vision library, see [Sergio\n" +
58 "Sigala's updated version](http://tvision.sourceforge.net/) that runs\n" +
59 "on many more platforms.\n" +
61 "Currently the only console platform supported is Posix (tested on\n" +
62 "Linux). Input/output is handled through terminal escape sequences\n" +
63 "generated by the library itself: ncurses is not required or linked to. \n" +
64 "xterm mouse tracking using UTF8 coordinates is supported.\n" +
66 "This library is licensed LGPL (\"GNU Lesser General Public License\")\n" +
67 "version 3 or greater. See the file COPYING for the full license text,\n" +
68 "which includes both the GPL v3 and the LGPL supplemental terms.\n" +
74 * Handle window/screen resize events.
76 * @param event resize event
79 public void onResize(final TResizeEvent event
) {
80 if (event
.getType() == TResizeEvent
.Type
.WIDGET
) {
81 // Resize the text field
82 textField
.setWidth(event
.getWidth() - 4);
83 textField
.setHeight(event
.getHeight() - 4);
88 // Pass to children instead
89 for (TWidget widget
: getChildren()) {
90 widget
.onResize(event
);
95 class DemoCheckboxWindow
extends TWindow
{
100 DemoCheckboxWindow(TApplication parent
) {
101 this(parent
, CENTERED
| RESIZABLE
);
107 DemoCheckboxWindow(TApplication parent
, int flags
) {
108 // Construct a demo window. X and Y don't matter because it
109 // will be centered on screen.
110 super(parent
, "Radiobuttons and Checkboxes", 0, 0, 60, 15, flags
);
115 addLabel("Check box example 1", 1, row
);
116 addCheckbox(35, row
++, "Checkbox 1", false);
117 addLabel("Check box example 2", 1, row
);
118 addCheckbox(35, row
++, "Checkbox 2", true);
121 TRadioGroup group
= addRadioGroup(1, row
, "Group 1");
122 group
.addRadioButton("Radio option 1");
123 group
.addRadioButton("Radio option 2");
124 group
.addRadioButton("Radio option 3");
126 addButton("&Close Window", (getWidth() - 14) / 2, getHeight() - 4,
129 DemoCheckboxWindow
.this.getApplication().closeWindow(DemoCheckboxWindow
.this);
138 class DemoMsgBoxWindow
extends TWindow
{
140 private void openYNCMessageBox() {
141 application.messageBox("Yes/No/Cancel MessageBox",
143 This is an example of a Yes/No/Cancel MessageBox.
145 Note that the MessageBox text can span multiple
148 The default result (if someone hits the top-left
149 close button) is CANCEL.
151 TMessageBox.Type.YESNOCANCEL);
154 private void openYNMessageBox() {
155 application.messageBox("Yes/No MessageBox",
157 This is an example of a Yes/No MessageBox.
159 Note that the MessageBox text can span multiple
162 The default result (if someone hits the top-left
165 TMessageBox.Type.YESNO);
168 private void openOKCMessageBox() {
169 application.messageBox("OK/Cancel MessageBox",
171 This is an example of a OK/Cancel MessageBox.
173 Note that the MessageBox text can span multiple
176 The default result (if someone hits the top-left
177 close button) is CANCEL.
179 TMessageBox.Type.OKCANCEL);
182 private void openOKMessageBox() {
183 application.messageBox("OK MessageBox",
185 This is an example of a OK MessageBox. This is the
188 Note that the MessageBox text can span multiple
191 The default result (if someone hits the top-left
194 TMessageBox.Type.OK);
202 DemoMsgBoxWindow(final TApplication parent
) {
203 this(parent
, TWindow
.CENTERED
| TWindow
.RESIZABLE
);
209 DemoMsgBoxWindow(final TApplication parent
, final int flags
) {
210 // Construct a demo window. X and Y don't matter because it
211 // will be centered on screen.
212 super(parent
, "Message Boxes", 0, 0, 60, 15, flags
);
217 addLabel("Default OK message box", 1, row);
218 addButton("Open O&K MB", 35, row, &openOKMessageBox);
221 addLabel("OK/Cancel message box", 1, row);
222 addButton("O&pen OKC MB", 35, row, &openOKCMessageBox);
225 addLabel("Yes/No message box", 1, row);
226 addButton("Open &YN MB", 35, row, &openYNMessageBox);
229 addLabel("Yes/No/Cancel message box", 1, row);
230 addButton("Ope&n YNC MB", 35, row, &openYNCMessageBox);
233 addLabel("Input box", 1, row);
234 addButton("Open &input box", 35, row,
236 application.inputBox("Input Box",
238 This is an example of an InputBox.
240 Note that the InputBox text can span multiple
247 addButton("&Close Window", (width - 14) / 2, height - 4,
249 application.closeWindow(this);
257 class DemoMainWindow
extends TWindow
{
259 // Timer that increments a number.
260 private TTimer timer
;
262 // Timer label is updated with timer ticks.
266 // The modal window is a more low-level example of controlling a window
267 // "from the outside". Most windows will probably subclass TWindow and
268 // do this kind of logic on their own.
269 private TWindow modalWindow;
270 private void openModalWindow() {
271 modalWindow = application.addWindow("Demo Modal Window", 0, 0,
272 58, 15, TWindow.Flag.MODAL);
273 modalWindow.addLabel("This is an example of a very braindead modal window.", 1, 1);
274 modalWindow.addLabel("Modal windows are centered by default.", 1, 2);
275 modalWindow.addButton("&Close", (modalWindow.width - 8)/2,
276 modalWindow.height - 4, &modalWindowClose);
278 private void modalWindowClose() {
279 application.closeWindow(modalWindow);
284 * We need to override onClose so that the timer will no longer be called
285 * after we close the window. TTimers currently are completely unaware
286 * of the rest of the UI classes.
289 public void onClose() {
290 getApplication().removeTimer(timer
);
294 * Construct demo window. It will be centered on screen.
296 public DemoMainWindow(TApplication parent
) {
297 this(parent
, CENTERED
| RESIZABLE
);
301 TProgressBar progressBar
;
306 private DemoMainWindow(TApplication parent
, int flags
) {
307 // Construct a demo window. X and Y don't matter because it will be
308 // centered on screen.
309 super(parent
, "Demo Window", 0, 0, 60, 23, flags
);
315 addLabel("Message Boxes", 1, row
);
316 addButton("&MessageBoxes", 35, row
,
319 new DemoMsgBoxWindow(getApplication());
326 addLabel("Open me as modal", 1, row
);
327 addButton("W&indow", 35, row
,
330 new DemoMainWindow(getApplication(), MODAL
);
337 addLabel("Variable-width text field:", 1, row
);
338 addField(35, row
++, 15, false, "Field text");
340 addLabel("Fixed-width text field:", 1, row
);
341 addField(35, row
, 15, true);
345 addLabel("Radio buttons and checkboxes", 1, row
);
346 addButton("&Checkboxes", 35, row
,
349 new DemoCheckboxWindow(getApplication());
358 addLabel("Editor window", 1, row);
359 addButton("Edito&r", 35, row,
361 new TEditor(application, 0, 0, 60, 15);
369 addLabel("Text areas", 1, row
);
370 addButton("&Text", 35, row
,
373 new DemoTextWindow(getApplication());
382 addLabel("Tree views", 1, row);
383 addButton("Tree&View", 35, row,
385 new DemoTreeViewWindow(application);
392 addLabel("Terminal", 1, row);
393 addButton("Termi&nal", 35, row,
395 application.openTerminal(0, 0);
402 progressBar
= addProgressBar(1, row
, 22, 0);
404 timerLabel
= addLabel("Timer", 1, row
);
405 timer
= getApplication().addTimer(100, true,
409 timerLabel
.setText(String
.format("Timer: %d", timerI
));
410 timerLabel
.setWidth(timerLabel
.getText().length());
414 progressBar
.setValue(timerI
);
415 DemoMainWindow
.this.setRepaint();
423 * The demo application itself.
425 class DemoApplication
extends TApplication
{
430 public DemoApplication() throws Exception
{
432 new DemoMainWindow(this);
438 TMenu demoMenu
= addMenu("&Demo");
439 TMenuItem item
= demoMenu
.addItem(2000, "&Checkable");
440 item
.setCheckable(true);
441 item
= demoMenu
.addItem(2001, "Disabled");
442 item
.setEnabled(false);
443 item
= demoMenu
.addItem(2002, "&Normal");
444 TSubMenu subMenu
= demoMenu
.addSubMenu("Sub-&Menu");
445 item
= demoMenu
.addItem(2010, "N&ormal A&&D");
447 item
= subMenu
.addItem(2000, "&Checkable (sub)");
448 item
.setCheckable(true);
449 item
= subMenu
.addItem(2001, "Disabled (sub)");
450 item
.setEnabled(false);
451 item
= subMenu
.addItem(2002, "&Normal (sub)");
453 subMenu
= subMenu
.addSubMenu("Sub-&Menu");
454 item
= subMenu
.addItem(2000, "&Checkable (sub)");
455 item
.setCheckable(true);
456 item
= subMenu
.addItem(2001, "Disabled (sub)");
457 item
.setEnabled(false);
458 item
= subMenu
.addItem(2002, "&Normal (sub)");
466 * This class provides a simple demonstration of Jexer's capabilities.
472 * @param args Command line arguments
474 public static void main(final String
[] args
) {
476 DemoApplication app
= new DemoApplication();
478 } catch (Exception e
) {