message box and input box
[fanfix.git] / src / jexer / demos / Demo1.java
CommitLineData
70f5b2bb
KL
1/*
2 * Jexer - Java Text User Interface
7d4115a5
KL
3 *
4 * License: LGPLv3 or later
5 *
70f5b2bb
KL
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.
7d4115a5
KL
9 *
10 * Copyright (C) 2015 Kevin Lamonte
11 *
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.
16 *
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.
21 *
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
26 * 02110-1301 USA
70f5b2bb
KL
27 *
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 * @version 1
7d4115a5 30 */
70f5b2bb 31package jexer.demos;
2420f903 32
a06459bd 33import jexer.*;
cc99cba8 34import jexer.event.*;
8e688b92 35import jexer.menu.*;
a06459bd 36
70f5b2bb
KL
37/**
38 * This window demonstates the TText, THScroller, and TVScroller widgets.
39 */
cc99cba8
KL
40class DemoTextWindow extends TWindow {
41
42 /**
43 * Hang onto my TText so I can resize it with the window.
44 */
45 private TText textField;
46
47 /**
48 * Public constructor.
70f5b2bb
KL
49 *
50 * @param parent the main application
cc99cba8 51 */
70f5b2bb 52 public DemoTextWindow(final TApplication parent) {
cc99cba8
KL
53 super(parent, "Text Areas", 0, 0, 44, 20, RESIZABLE);
54
55 textField = addText(
56"This is an example of a reflowable text field. Some example text follows.\n" +
57"\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" +
64"\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" +
69"\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" +
73"\n",
74 1, 1, 40, 16);
75 }
76
77 /**
78 * Handle window/screen resize events.
79 *
80 * @param event resize event
81 */
82 @Override
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);
88 textField.reflow();
89 return;
90 }
91
92 // Pass to children instead
93 for (TWidget widget: getChildren()) {
94 widget.onResize(event);
95 }
96 }
97}
98
70f5b2bb
KL
99/**
100 * This window demonstates the TRadioGroup, TRadioButton, and TCheckbox
101 * widgets.
102 */
7272e49f
KL
103class DemoCheckboxWindow extends TWindow {
104
105 /**
cc99cba8 106 * Constructor.
70f5b2bb
KL
107 *
108 * @param parent the main application
7272e49f 109 */
70f5b2bb 110 DemoCheckboxWindow(final TApplication parent) {
7272e49f
KL
111 this(parent, CENTERED | RESIZABLE);
112 }
113
114 /**
cc99cba8 115 * Constructor.
70f5b2bb
KL
116 *
117 * @param parent the main application
118 * @param flags bitmask of MODAL, CENTERED, or RESIZABLE
7272e49f 119 */
70f5b2bb
KL
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.
7272e49f
KL
123 super(parent, "Radiobuttons and Checkboxes", 0, 0, 60, 15, flags);
124
125 int row = 1;
126
127 // Add some widgets
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);
132 row += 2;
133
00d2622b 134 TRadioGroup group = addRadioGroup(1, row, "Group 1");
7272e49f
KL
135 group.addRadioButton("Radio option 1");
136 group.addRadioButton("Radio option 2");
137 group.addRadioButton("Radio option 3");
138
00d2622b
KL
139 addButton("&Close Window", (getWidth() - 14) / 2, getHeight() - 4,
140 new TAction() {
141 public void DO() {
70f5b2bb
KL
142 DemoCheckboxWindow.this.getApplication()
143 .closeWindow(DemoCheckboxWindow.this);
00d2622b 144 }
7272e49f 145 }
7272e49f 146 );
7272e49f
KL
147 }
148
149}
150
151
70f5b2bb
KL
152/**
153 * This window demonstates the TMessageBox and TInputBox widgets.
154 */
30d336cc 155class DemoMsgBoxWindow extends TWindow {
30d336cc
KL
156
157 /**
158 * Constructor.
70f5b2bb
KL
159 *
160 * @param parent the main application
30d336cc
KL
161 */
162 DemoMsgBoxWindow(final TApplication parent) {
7272e49f 163 this(parent, TWindow.CENTERED | TWindow.RESIZABLE);
30d336cc
KL
164 }
165
166 /**
167 * Constructor.
70f5b2bb
KL
168 *
169 * @param parent the main application
170 * @param flags bitmask of MODAL, CENTERED, or RESIZABLE
30d336cc
KL
171 */
172 DemoMsgBoxWindow(final TApplication parent, final int flags) {
7272e49f
KL
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);
c6940ed9
KL
176
177 int row = 1;
7272e49f
KL
178
179 // Add some widgets
180 addLabel("Default OK message box", 1, row);
c6940ed9
KL
181 addButton("Open O&K MB", 35, row,
182 new TAction() {
183 public void DO() {
184 getApplication().messageBox("OK MessageBox",
185"This is an example of a OK MessageBox. This is the\n" +
186"default MessageBox.\n" +
187"\n" +
188"Note that the MessageBox text can span multiple\n" +
189"lines.\n" +
190"\n" +
191"The default result (if someone hits the top-left\n" +
192"close button) is OK.\n",
193 TMessageBox.Type.OK);
194 }
195 }
196 );
7272e49f
KL
197 row += 2;
198
199 addLabel("OK/Cancel message box", 1, row);
c6940ed9
KL
200 addButton("O&pen OKC MB", 35, row,
201 new TAction() {
202 public void DO() {
203 getApplication().messageBox("OK/Cancel MessageBox",
204"This is an example of a OK/Cancel MessageBox.\n" +
205"\n" +
206"Note that the MessageBox text can span multiple\n" +
207"lines.\n" +
208"\n" +
209"The default result (if someone hits the top-left\n" +
210"close button) is CANCEL.\n",
211 TMessageBox.Type.OKCANCEL);
212 }
213 }
214 );
7272e49f
KL
215 row += 2;
216
217 addLabel("Yes/No message box", 1, row);
c6940ed9
KL
218 addButton("Open &YN MB", 35, row,
219 new TAction() {
220 public void DO() {
221 getApplication().messageBox("Yes/No MessageBox",
222"This is an example of a Yes/No MessageBox.\n" +
223"\n" +
224"Note that the MessageBox text can span multiple\n" +
225"lines.\n" +
226"\n" +
227"The default result (if someone hits the top-left\n" +
228"close button) is NO.\n",
229 TMessageBox.Type.YESNO);
230 }
231 }
232 );
7272e49f
KL
233 row += 2;
234
235 addLabel("Yes/No/Cancel message box", 1, row);
c6940ed9
KL
236 addButton("Ope&n YNC MB", 35, row,
237 new TAction() {
238 public void DO() {
239 getApplication().messageBox("Yes/No/Cancel MessageBox",
240"This is an example of a Yes/No/Cancel MessageBox.\n" +
241"\n" +
242"Note that the MessageBox text can span multiple\n" +
243"lines.\n" +
244"\n" +
245"The default result (if someone hits the top-left\n" +
246"close button) is CANCEL.\n",
247 TMessageBox.Type.YESNOCANCEL);
248 }
249 }
250 );
7272e49f
KL
251 row += 2;
252
253 addLabel("Input box", 1, row);
254 addButton("Open &input box", 35, row,
c6940ed9
KL
255 new TAction() {
256 public void DO() {
257 TInputBox in = getApplication().inputBox("Input Box",
258"This is an example of an InputBox.\n" +
259"\n" +
260"Note that the InputBox text can span multiple\n" +
261"lines.\n",
262 "some input text");
263 getApplication().messageBox("Your InputBox Answer",
264 "You entered: " + in.getText());
265 }
7272e49f
KL
266 }
267 );
268
c6940ed9
KL
269 addButton("&Close Window", (getWidth() - 14) / 2, getHeight() - 4,
270 new TAction() {
271 public void DO() {
272 getApplication().closeWindow(DemoMsgBoxWindow.this);
273 }
7272e49f
KL
274 }
275 );
30d336cc
KL
276 }
277}
278
70f5b2bb
KL
279/**
280 * This is the main "demo" application window. It makes use of the TTimer,
281 * TProgressBox, TLabel, TButton, and TField widgets.
282 */
a06459bd 283class DemoMainWindow extends TWindow {
cc99cba8
KL
284
285 // Timer that increments a number.
a06459bd
KL
286 private TTimer timer;
287
cc99cba8 288 // Timer label is updated with timer ticks.
d502a0e9
KL
289 TLabel timerLabel;
290
291 /*
a06459bd
KL
292 // The modal window is a more low-level example of controlling a window
293 // "from the outside". Most windows will probably subclass TWindow and
294 // do this kind of logic on their own.
295 private TWindow modalWindow;
296 private void openModalWindow() {
c6940ed9 297 modalWindow = getApplication().addWindow("Demo Modal Window", 0, 0,
8e688b92
KL
298 58, 15, TWindow.Flag.MODAL);
299 modalWindow.addLabel("This is an example of a very braindead modal window.", 1, 1);
300 modalWindow.addLabel("Modal windows are centered by default.", 1, 2);
301 modalWindow.addButton("&Close", (modalWindow.width - 8)/2,
302 modalWindow.height - 4, &modalWindowClose);
a06459bd
KL
303 }
304 private void modalWindowClose() {
c6940ed9 305 getApplication().closeWindow(modalWindow);
a06459bd 306 }
d502a0e9 307 */
a06459bd 308
d502a0e9
KL
309 /**
310 * We need to override onClose so that the timer will no longer be called
311 * after we close the window. TTimers currently are completely unaware
312 * of the rest of the UI classes.
a06459bd 313 */
d502a0e9
KL
314 @Override
315 public void onClose() {
316 getApplication().removeTimer(timer);
317 }
a06459bd 318
fca67db0
KL
319 /**
320 * Construct demo window. It will be centered on screen.
70f5b2bb
KL
321 *
322 * @param parent the main application
fca67db0 323 */
70f5b2bb 324 public DemoMainWindow(final TApplication parent) {
8e688b92 325 this(parent, CENTERED | RESIZABLE);
a06459bd
KL
326 }
327
70f5b2bb
KL
328 // These are used by the timer loop. They have to be at class scope so
329 // that they can be accessed by the anonymous TAction class.
d502a0e9
KL
330 int timerI = 0;
331 TProgressBar progressBar;
332
fca67db0
KL
333 /**
334 * Constructor.
70f5b2bb
KL
335 *
336 * @param parent the main application
337 * @param flags bitmask of MODAL, CENTERED, or RESIZABLE
fca67db0 338 */
70f5b2bb 339 private DemoMainWindow(final TApplication parent, final int flags) {
8e688b92
KL
340 // Construct a demo window. X and Y don't matter because it will be
341 // centered on screen.
342 super(parent, "Demo Window", 0, 0, 60, 23, flags);
a06459bd 343
8e688b92
KL
344 int row = 1;
345
346 // Add some widgets
30d336cc 347 if (!isModal()) {
8e688b92
KL
348 addLabel("Message Boxes", 1, row);
349 addButton("&MessageBoxes", 35, row,
30d336cc
KL
350 new TAction() {
351 public void DO() {
352 new DemoMsgBoxWindow(getApplication());
353 }
8e688b92
KL
354 }
355 );
356 }
357 row += 2;
358
359 addLabel("Open me as modal", 1, row);
360 addButton("W&indow", 35, row,
7272e49f
KL
361 new TAction() {
362 public void DO() {
363 new DemoMainWindow(getApplication(), MODAL);
364 }
8e688b92
KL
365 }
366 );
367
368 row += 2;
369
370 addLabel("Variable-width text field:", 1, row);
371 addField(35, row++, 15, false, "Field text");
372
373 addLabel("Fixed-width text field:", 1, row);
374 addField(35, row, 15, true);
375 row += 2;
376
7272e49f 377 if (!isModal()) {
8e688b92 378 addLabel("Radio buttons and checkboxes", 1, row);
7272e49f
KL
379 addButton("&Checkboxes", 35, row,
380 new TAction() {
381 public void DO() {
d502a0e9 382 new DemoCheckboxWindow(getApplication());
7272e49f
KL
383 }
384 }
385 );
8e688b92
KL
386 }
387 row += 2;
388
7272e49f
KL
389 /*
390 if (!isModal()) {
8e688b92
KL
391 addLabel("Editor window", 1, row);
392 addButton("Edito&r", 35, row,
393 {
394 new TEditor(application, 0, 0, 60, 15);
395 }
396 );
397 }
398 row += 2;
cc99cba8 399 */
8e688b92 400
7272e49f 401 if (!isModal()) {
8e688b92
KL
402 addLabel("Text areas", 1, row);
403 addButton("&Text", 35, row,
cc99cba8
KL
404 new TAction() {
405 public void DO() {
406 new DemoTextWindow(getApplication());
407 }
8e688b92
KL
408 }
409 );
410 }
411 row += 2;
412
cc99cba8 413 /*
7272e49f 414 if (!isModal()) {
8e688b92
KL
415 addLabel("Tree views", 1, row);
416 addButton("Tree&View", 35, row,
417 {
418 new DemoTreeViewWindow(application);
419 }
420 );
421 }
422 row += 2;
423
7272e49f
KL
424 if (!isModal()) {
425 addLabel("Terminal", 1, row);
426 addButton("Termi&nal", 35, row,
427 {
c6940ed9 428 getApplication().openTerminal(0, 0);
7272e49f
KL
429 }
430 );
8e688b92 431 }
7272e49f 432 row += 2;
d502a0e9 433 */
8e688b92 434
d502a0e9 435 progressBar = addProgressBar(1, row, 22, 0);
8e688b92 436 row++;
d502a0e9
KL
437 timerLabel = addLabel("Timer", 1, row);
438 timer = getApplication().addTimer(100, true,
439 new TAction() {
440
441 public void DO() {
442 timerLabel.setText(String.format("Timer: %d", timerI));
443 timerLabel.setWidth(timerLabel.getText().length());
444 if (timerI < 100) {
445 timerI++;
446 }
447 progressBar.setValue(timerI);
448 DemoMainWindow.this.setRepaint();
8e688b92 449 }
d502a0e9
KL
450 }
451 );
a06459bd
KL
452 }
453}
7d4115a5
KL
454
455/**
456 * The demo application itself.
457 */
458class DemoApplication extends TApplication {
cc99cba8 459
2420f903 460 /**
70f5b2bb
KL
461 * Public constructor.
462 *
463 * @throws Exception if TApplication can't instantiate the Backend.
2420f903 464 */
4328bb42 465 public DemoApplication() throws Exception {
8e688b92
KL
466 super(null, null);
467 new DemoMainWindow(this);
468
8e688b92
KL
469 // Add the menus
470 addFileMenu();
471 addEditMenu();
472
473 TMenu demoMenu = addMenu("&Demo");
474 TMenuItem item = demoMenu.addItem(2000, "&Checkable");
475 item.setCheckable(true);
476 item = demoMenu.addItem(2001, "Disabled");
477 item.setEnabled(false);
478 item = demoMenu.addItem(2002, "&Normal");
479 TSubMenu subMenu = demoMenu.addSubMenu("Sub-&Menu");
480 item = demoMenu.addItem(2010, "N&ormal A&&D");
481
482 item = subMenu.addItem(2000, "&Checkable (sub)");
483 item.setCheckable(true);
484 item = subMenu.addItem(2001, "Disabled (sub)");
485 item.setEnabled(false);
486 item = subMenu.addItem(2002, "&Normal (sub)");
487
488 subMenu = subMenu.addSubMenu("Sub-&Menu");
489 item = subMenu.addItem(2000, "&Checkable (sub)");
490 item.setCheckable(true);
491 item = subMenu.addItem(2001, "Disabled (sub)");
492 item.setEnabled(false);
493 item = subMenu.addItem(2002, "&Normal (sub)");
494
495 addWindowMenu();
496
2420f903 497 }
7d4115a5
KL
498}
499
500/**
501 * This class provides a simple demonstration of Jexer's capabilities.
502 */
503public class Demo1 {
504 /**
505 * Main entry point.
506 *
cc99cba8 507 * @param args Command line arguments
7d4115a5 508 */
cc99cba8 509 public static void main(final String [] args) {
8e688b92
KL
510 try {
511 DemoApplication app = new DemoApplication();
512 app.run();
513 } catch (Exception e) {
514 e.printStackTrace();
515 }
7d4115a5
KL
516 }
517
518}