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]
38 * This window demonstates the TText, THScroller, and TVScroller widgets.
40 class DemoTextWindow
extends TWindow
{
43 * Hang onto my TText so I can resize it with the window.
45 private TText textField
;
50 * @param parent the main application
52 public DemoTextWindow(final TApplication parent
) {
53 super(parent
, "Text Areas", 0, 0, 44, 20, RESIZABLE
);
56 "This is an example of a reflowable text field. Some example text follows.\n" +
58 "This library implements a text-based windowing system loosely\n" +
59 "reminiscient of Borland's [Turbo\n" +
60 "Vision](http://en.wikipedia.org/wiki/Turbo_Vision) library. For those\n" +
61 "wishing to use the actual C++ Turbo Vision library, see [Sergio\n" +
62 "Sigala's updated version](http://tvision.sourceforge.net/) that runs\n" +
63 "on many more platforms.\n" +
65 "Currently the only console platform supported is Posix (tested on\n" +
66 "Linux). Input/output is handled through terminal escape sequences\n" +
67 "generated by the library itself: ncurses is not required or linked to. \n" +
68 "xterm mouse tracking using UTF8 coordinates is supported.\n" +
70 "This library is licensed LGPL (\"GNU Lesser General Public License\")\n" +
71 "version 3 or greater. See the file COPYING for the full license text,\n" +
72 "which includes both the GPL v3 and the LGPL supplemental terms.\n" +
78 * Handle window/screen resize events.
80 * @param event resize event
83 public void onResize(final TResizeEvent event
) {
84 if (event
.getType() == TResizeEvent
.Type
.WIDGET
) {
85 // Resize the text field
86 textField
.setWidth(event
.getWidth() - 4);
87 textField
.setHeight(event
.getHeight() - 4);
92 // Pass to children instead
93 for (TWidget widget
: getChildren()) {
94 widget
.onResize(event
);
100 * This window demonstates the TRadioGroup, TRadioButton, and TCheckbox
103 class DemoCheckboxWindow
extends TWindow
{
108 * @param parent the main application
110 DemoCheckboxWindow(final TApplication parent
) {
111 this(parent
, CENTERED
| RESIZABLE
);
117 * @param parent the main application
118 * @param flags bitmask of MODAL, CENTERED, or RESIZABLE
120 DemoCheckboxWindow(final TApplication parent
, final int flags
) {
121 // Construct a demo window. X and Y don't matter because it will be
122 // centered on screen.
123 super(parent
, "Radiobuttons and Checkboxes", 0, 0, 60, 15, flags
);
128 addLabel("Check box example 1", 1, row
);
129 addCheckbox(35, row
++, "Checkbox 1", false);
130 addLabel("Check box example 2", 1, row
);
131 addCheckbox(35, row
++, "Checkbox 2", true);
134 TRadioGroup group
= addRadioGroup(1, row
, "Group 1");
135 group
.addRadioButton("Radio option 1");
136 group
.addRadioButton("Radio option 2");
137 group
.addRadioButton("Radio option 3");
139 addButton("&Close Window", (getWidth() - 14) / 2, getHeight() - 4,
142 DemoCheckboxWindow
.this.getApplication()
143 .closeWindow(DemoCheckboxWindow
.this);
153 * This window demonstates the TMessageBox and TInputBox widgets.
155 class DemoMsgBoxWindow
extends TWindow
{
160 * @param parent the main application
162 DemoMsgBoxWindow(final TApplication parent
) {
163 this(parent
, TWindow
.CENTERED
| TWindow
.RESIZABLE
);
169 * @param parent the main application
170 * @param flags bitmask of MODAL, CENTERED, or RESIZABLE
172 DemoMsgBoxWindow(final TApplication parent
, final int flags
) {
173 // Construct a demo window. X and Y don't matter because it
174 // will be centered on screen.
175 super(parent
, "Message Boxes", 0, 0, 60, 15, flags
);
180 addLabel("Default OK message box", 1, row
);
181 addButton("Open O&K MB", 35, row
,
184 getApplication().messageBox("OK MessageBox",
185 "This is an example of a OK MessageBox. This is the\n" +
186 "default MessageBox.\n" +
188 "Note that the MessageBox text can span multiple\n" +
191 "The default result (if someone hits the top-left\n" +
192 "close button) is OK.\n",
193 TMessageBox
.Type
.OK
);
199 addLabel("OK/Cancel message box", 1, row
);
200 addButton("O&pen OKC MB", 35, row
,
203 getApplication().messageBox("OK/Cancel MessageBox",
204 "This is an example of a OK/Cancel MessageBox.\n" +
206 "Note that the MessageBox text can span multiple\n" +
209 "The default result (if someone hits the top-left\n" +
210 "close button) is CANCEL.\n",
211 TMessageBox
.Type
.OKCANCEL
);
217 addLabel("Yes/No message box", 1, row
);
218 addButton("Open &YN MB", 35, row
,
221 getApplication().messageBox("Yes/No MessageBox",
222 "This is an example of a Yes/No MessageBox.\n" +
224 "Note that the MessageBox text can span multiple\n" +
227 "The default result (if someone hits the top-left\n" +
228 "close button) is NO.\n",
229 TMessageBox
.Type
.YESNO
);
235 addLabel("Yes/No/Cancel message box", 1, row
);
236 addButton("Ope&n YNC MB", 35, row
,
239 getApplication().messageBox("Yes/No/Cancel MessageBox",
240 "This is an example of a Yes/No/Cancel MessageBox.\n" +
242 "Note that the MessageBox text can span multiple\n" +
245 "The default result (if someone hits the top-left\n" +
246 "close button) is CANCEL.\n",
247 TMessageBox
.Type
.YESNOCANCEL
);
253 addLabel("Input box", 1, row
);
254 addButton("Open &input box", 35, row
,
257 TInputBox in
= getApplication().inputBox("Input Box",
258 "This is an example of an InputBox.\n" +
260 "Note that the InputBox text can span multiple\n" +
263 getApplication().messageBox("Your InputBox Answer",
264 "You entered: " + in
.getText());
269 addButton("&Close Window", (getWidth() - 14) / 2, getHeight() - 4,
272 getApplication().closeWindow(DemoMsgBoxWindow
.this);
280 * This is the main "demo" application window. It makes use of the TTimer,
281 * TProgressBox, TLabel, TButton, and TField widgets.
283 class DemoMainWindow
extends TWindow
{
285 // Timer that increments a number.
286 private TTimer timer
;
288 // Timer label is updated with timer ticks.
292 * We need to override onClose so that the timer will no longer be called
293 * after we close the window. TTimers currently are completely unaware
294 * of the rest of the UI classes.
297 public void onClose() {
298 getApplication().removeTimer(timer
);
302 * Construct demo window. It will be centered on screen.
304 * @param parent the main application
306 public DemoMainWindow(final TApplication parent
) {
307 this(parent
, CENTERED
| RESIZABLE
);
310 // These are used by the timer loop. They have to be at class scope so
311 // that they can be accessed by the anonymous TAction class.
313 TProgressBar progressBar
;
318 * @param parent the main application
319 * @param flags bitmask of MODAL, CENTERED, or RESIZABLE
321 private DemoMainWindow(final TApplication parent
, final int flags
) {
322 // Construct a demo window. X and Y don't matter because it will be
323 // centered on screen.
324 super(parent
, "Demo Window", 0, 0, 60, 23, flags
);
330 addLabel("Message Boxes", 1, row
);
331 addButton("&MessageBoxes", 35, row
,
334 new DemoMsgBoxWindow(getApplication());
341 addLabel("Open me as modal", 1, row
);
342 addButton("W&indow", 35, row
,
345 new DemoMainWindow(getApplication(), MODAL
);
352 addLabel("Variable-width text field:", 1, row
);
353 addField(35, row
++, 15, false, "Field text");
355 addLabel("Fixed-width text field:", 1, row
);
356 addField(35, row
, 15, true);
360 addLabel("Radio buttons and checkboxes", 1, row
);
361 addButton("&Checkboxes", 35, row
,
364 new DemoCheckboxWindow(getApplication());
373 addLabel("Editor window", 1, row);
374 addButton("Edito&r", 35, row,
376 new TEditor(application, 0, 0, 60, 15);
384 addLabel("Text areas", 1, row
);
385 addButton("&Text", 35, row
,
388 new DemoTextWindow(getApplication());
397 addLabel("Tree views", 1, row);
398 addButton("Tree&View", 35, row,
400 new DemoTreeViewWindow(application);
407 addLabel("Terminal", 1, row);
408 addButton("Termi&nal", 35, row,
410 getApplication().openTerminal(0, 0);
417 progressBar
= addProgressBar(1, row
, 22, 0);
419 timerLabel
= addLabel("Timer", 1, row
);
420 timer
= getApplication().addTimer(100, true,
424 timerLabel
.setText(String
.format("Timer: %d", timerI
));
425 timerLabel
.setWidth(timerLabel
.getText().length());
429 progressBar
.setValue(timerI
);
430 DemoMainWindow
.this.setRepaint();
438 * The demo application itself.
440 class DemoApplication
extends TApplication
{
443 * Public constructor.
445 * @throws Exception if TApplication can't instantiate the Backend.
447 public DemoApplication() throws Exception
{
449 new DemoMainWindow(this);
455 TMenu demoMenu
= addMenu("&Demo");
456 TMenuItem item
= demoMenu
.addItem(2000, "&Checkable");
457 item
.setCheckable(true);
458 item
= demoMenu
.addItem(2001, "Disabled");
459 item
.setEnabled(false);
460 item
= demoMenu
.addItem(2002, "&Normal");
461 TSubMenu subMenu
= demoMenu
.addSubMenu("Sub-&Menu");
462 item
= demoMenu
.addItem(2010, "N&ormal A&&D");
464 item
= subMenu
.addItem(2000, "&Checkable (sub)");
465 item
.setCheckable(true);
466 item
= subMenu
.addItem(2001, "Disabled (sub)");
467 item
.setEnabled(false);
468 item
= subMenu
.addItem(2002, "&Normal (sub)");
470 subMenu
= subMenu
.addSubMenu("Sub-&Menu");
471 item
= subMenu
.addItem(2000, "&Checkable (sub)");
472 item
.setCheckable(true);
473 item
= subMenu
.addItem(2001, "Disabled (sub)");
474 item
.setEnabled(false);
475 item
= subMenu
.addItem(2002, "&Normal (sub)");
483 * This class provides a simple demonstration of Jexer's capabilities.
489 * @param args Command line arguments
491 public static void main(final String
[] args
) {
493 DemoApplication app
= new DemoApplication();
495 } catch (Exception e
) {