timer and progress bar working
[fanfix.git] / demos / Demo1.java
... / ...
CommitLineData
1/**
2 * Jexer - Java Text User Interface - demonstration program
3 *
4 * Version: $Id$
5 *
6 * Author: Kevin Lamonte, <a href="mailto:kevin.lamonte@gmail.com">kevin.lamonte@gmail.com</a>
7 *
8 * License: LGPLv3 or later
9 *
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
13 * License Version 3.
14 *
15 * Copyright (C) 2015 Kevin Lamonte
16 *
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.
21 *
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.
26 *
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
31 * 02110-1301 USA
32 */
33
34import jexer.*;
35import jexer.menu.*;
36
37class DemoCheckboxWindow extends TWindow {
38
39 /**
40 * Constructor
41 */
42 DemoCheckboxWindow(TApplication parent) {
43 this(parent, CENTERED | RESIZABLE);
44 }
45
46 /**
47 * Constructor
48 */
49 DemoCheckboxWindow(TApplication parent, int flags) {
50 // Construct a demo window. X and Y don't matter because it
51 // will be centered on screen.
52 super(parent, "Radiobuttons and Checkboxes", 0, 0, 60, 15, flags);
53
54 int row = 1;
55
56 // Add some widgets
57 addLabel("Check box example 1", 1, row);
58 addCheckbox(35, row++, "Checkbox 1", false);
59 addLabel("Check box example 2", 1, row);
60 addCheckbox(35, row++, "Checkbox 2", true);
61 row += 2;
62
63 /*
64 auto group = addRadioGroup(1, row, "Group 1");
65 group.addRadioButton("Radio option 1");
66 group.addRadioButton("Radio option 2");
67 group.addRadioButton("Radio option 3");
68
69 addButton("&Close Window", (width - 14) / 2, height - 4,
70 {
71 application.closeWindow(this);
72 }
73
74 );
75 */
76 }
77
78}
79
80
81class DemoMsgBoxWindow extends TWindow {
82 /*
83 private void openYNCMessageBox() {
84 application.messageBox("Yes/No/Cancel MessageBox",
85 q"EOS
86This is an example of a Yes/No/Cancel MessageBox.
87
88Note that the MessageBox text can span multiple
89lines.
90
91The default result (if someone hits the top-left
92close button) is CANCEL.
93EOS",
94 TMessageBox.Type.YESNOCANCEL);
95 }
96
97 private void openYNMessageBox() {
98 application.messageBox("Yes/No MessageBox",
99 q"EOS
100This is an example of a Yes/No MessageBox.
101
102Note that the MessageBox text can span multiple
103lines.
104
105The default result (if someone hits the top-left
106close button) is NO.
107EOS",
108 TMessageBox.Type.YESNO);
109 }
110
111 private void openOKCMessageBox() {
112 application.messageBox("OK/Cancel MessageBox",
113 q"EOS
114This is an example of a OK/Cancel MessageBox.
115
116Note that the MessageBox text can span multiple
117lines.
118
119The default result (if someone hits the top-left
120close button) is CANCEL.
121EOS",
122 TMessageBox.Type.OKCANCEL);
123 }
124
125 private void openOKMessageBox() {
126 application.messageBox("OK MessageBox",
127 q"EOS
128This is an example of a OK MessageBox. This is the
129default MessageBox.
130
131Note that the MessageBox text can span multiple
132lines.
133
134The default result (if someone hits the top-left
135close button) is OK.
136EOS",
137 TMessageBox.Type.OK);
138 }
139
140 */
141
142 /**
143 * Constructor.
144 */
145 DemoMsgBoxWindow(final TApplication parent) {
146 this(parent, TWindow.CENTERED | TWindow.RESIZABLE);
147 }
148
149 /**
150 * Constructor.
151 */
152 DemoMsgBoxWindow(final TApplication parent, final int flags) {
153 // Construct a demo window. X and Y don't matter because it
154 // will be centered on screen.
155 super(parent, "Message Boxes", 0, 0, 60, 15, flags);
156 /*
157 uint row = 1;
158
159 // Add some widgets
160 addLabel("Default OK message box", 1, row);
161 addButton("Open O&K MB", 35, row, &openOKMessageBox);
162 row += 2;
163
164 addLabel("OK/Cancel message box", 1, row);
165 addButton("O&pen OKC MB", 35, row, &openOKCMessageBox);
166 row += 2;
167
168 addLabel("Yes/No message box", 1, row);
169 addButton("Open &YN MB", 35, row, &openYNMessageBox);
170 row += 2;
171
172 addLabel("Yes/No/Cancel message box", 1, row);
173 addButton("Ope&n YNC MB", 35, row, &openYNCMessageBox);
174 row += 2;
175
176 addLabel("Input box", 1, row);
177 addButton("Open &input box", 35, row,
178 {
179 application.inputBox("Input Box",
180 q"EOS
181This is an example of an InputBox.
182
183Note that the InputBox text can span multiple
184lines.
185EOS",
186 "some input text");
187 }
188 );
189
190 addButton("&Close Window", (width - 14) / 2, height - 4,
191 {
192 application.closeWindow(this);
193 }
194 );
195 */
196 }
197}
198
199
200class DemoMainWindow extends TWindow {
201 // Timer that increments a number
202 private TTimer timer;
203
204 // Timer label is updated with timerrr ticks
205 TLabel timerLabel;
206
207 /*
208 // The modal window is a more low-level example of controlling a window
209 // "from the outside". Most windows will probably subclass TWindow and
210 // do this kind of logic on their own.
211 private TWindow modalWindow;
212 private void openModalWindow() {
213 modalWindow = application.addWindow("Demo Modal Window", 0, 0,
214 58, 15, TWindow.Flag.MODAL);
215 modalWindow.addLabel("This is an example of a very braindead modal window.", 1, 1);
216 modalWindow.addLabel("Modal windows are centered by default.", 1, 2);
217 modalWindow.addButton("&Close", (modalWindow.width - 8)/2,
218 modalWindow.height - 4, &modalWindowClose);
219 }
220 private void modalWindowClose() {
221 application.closeWindow(modalWindow);
222 }
223 */
224
225 /**
226 * We need to override onClose so that the timer will no longer be called
227 * after we close the window. TTimers currently are completely unaware
228 * of the rest of the UI classes.
229 */
230 @Override
231 public void onClose() {
232 getApplication().removeTimer(timer);
233 }
234
235 /**
236 * Construct demo window. It will be centered on screen.
237 */
238 public DemoMainWindow(TApplication parent) {
239 this(parent, CENTERED | RESIZABLE);
240 }
241
242 int timerI = 0;
243 TProgressBar progressBar;
244
245 /**
246 * Constructor.
247 */
248 private DemoMainWindow(TApplication parent, int flags) {
249 // Construct a demo window. X and Y don't matter because it will be
250 // centered on screen.
251 super(parent, "Demo Window", 0, 0, 60, 23, flags);
252
253 int row = 1;
254
255 // Add some widgets
256 if (!isModal()) {
257 addLabel("Message Boxes", 1, row);
258 addButton("&MessageBoxes", 35, row,
259 new TAction() {
260 public void DO() {
261 new DemoMsgBoxWindow(getApplication());
262 }
263 }
264 );
265 }
266 row += 2;
267
268 addLabel("Open me as modal", 1, row);
269 addButton("W&indow", 35, row,
270 new TAction() {
271 public void DO() {
272 new DemoMainWindow(getApplication(), MODAL);
273 }
274 }
275 );
276
277 row += 2;
278
279 /*
280 addLabel("Variable-width text field:", 1, row);
281 addField(35, row++, 15, false, "Field text");
282
283 addLabel("Fixed-width text field:", 1, row);
284 addField(35, row, 15, true);
285 row += 2;
286 */
287
288 if (!isModal()) {
289 addLabel("Radio buttons and checkboxes", 1, row);
290 addButton("&Checkboxes", 35, row,
291 new TAction() {
292 public void DO() {
293 new DemoCheckboxWindow(getApplication());
294 }
295 }
296 );
297 }
298 row += 2;
299
300 /*
301 if (!isModal()) {
302 addLabel("Editor window", 1, row);
303 addButton("Edito&r", 35, row,
304 {
305 new TEditor(application, 0, 0, 60, 15);
306 }
307 );
308 }
309 row += 2;
310
311 if (!isModal()) {
312 addLabel("Text areas", 1, row);
313 addButton("&Text", 35, row,
314 {
315 new DemoTextWindow(application);
316 }
317 );
318 }
319 row += 2;
320
321 if (!isModal()) {
322 addLabel("Tree views", 1, row);
323 addButton("Tree&View", 35, row,
324 {
325 new DemoTreeViewWindow(application);
326 }
327 );
328 }
329 row += 2;
330
331 if (!isModal()) {
332 addLabel("Terminal", 1, row);
333 addButton("Termi&nal", 35, row,
334 {
335 application.openTerminal(0, 0);
336 }
337 );
338 }
339 row += 2;
340 */
341
342 progressBar = addProgressBar(1, row, 22, 0);
343 row++;
344 timerLabel = addLabel("Timer", 1, row);
345 timer = getApplication().addTimer(100, true,
346 new TAction() {
347
348 public void DO() {
349 timerLabel.setText(String.format("Timer: %d", timerI));
350 timerLabel.setWidth(timerLabel.getText().length());
351 if (timerI < 100) {
352 timerI++;
353 }
354 progressBar.setValue(timerI);
355 DemoMainWindow.this.setRepaint();
356 }
357 }
358 );
359 }
360}
361
362/**
363 * The demo application itself.
364 */
365class DemoApplication extends TApplication {
366 /**
367 * Public constructor
368 */
369 public DemoApplication() throws Exception {
370 super(null, null);
371 new DemoMainWindow(this);
372
373 // Add the menus
374 addFileMenu();
375 addEditMenu();
376
377 TMenu demoMenu = addMenu("&Demo");
378 TMenuItem item = demoMenu.addItem(2000, "&Checkable");
379 item.setCheckable(true);
380 item = demoMenu.addItem(2001, "Disabled");
381 item.setEnabled(false);
382 item = demoMenu.addItem(2002, "&Normal");
383 TSubMenu subMenu = demoMenu.addSubMenu("Sub-&Menu");
384 item = demoMenu.addItem(2010, "N&ormal A&&D");
385
386 item = subMenu.addItem(2000, "&Checkable (sub)");
387 item.setCheckable(true);
388 item = subMenu.addItem(2001, "Disabled (sub)");
389 item.setEnabled(false);
390 item = subMenu.addItem(2002, "&Normal (sub)");
391
392 subMenu = subMenu.addSubMenu("Sub-&Menu");
393 item = subMenu.addItem(2000, "&Checkable (sub)");
394 item.setCheckable(true);
395 item = subMenu.addItem(2001, "Disabled (sub)");
396 item.setEnabled(false);
397 item = subMenu.addItem(2002, "&Normal (sub)");
398
399 addWindowMenu();
400
401 }
402}
403
404/**
405 * This class provides a simple demonstration of Jexer's capabilities.
406 */
407public class Demo1 {
408 /**
409 * Main entry point.
410 *
411 * @param args Command line arguments
412 */
413 public static void main(String [] args) {
414 try {
415 DemoApplication app = new DemoApplication();
416 app.run();
417 } catch (Exception e) {
418 e.printStackTrace();
419 }
420 }
421
422}