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