radio buttons
[nikiroo-utils.git] / demos / Demo1.java
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
34 import jexer.*;
35 import jexer.menu.*;
36
37 class 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 TRadioGroup group = addRadioGroup(1, row, "Group 1");
64 group.addRadioButton("Radio option 1");
65 group.addRadioButton("Radio option 2");
66 group.addRadioButton("Radio option 3");
67
68 addButton("&Close Window", (getWidth() - 14) / 2, getHeight() - 4,
69 new TAction() {
70 public void DO() {
71 DemoCheckboxWindow.this.getApplication().closeWindow(DemoCheckboxWindow.this);
72 }
73 }
74 );
75 }
76
77 }
78
79
80 class DemoMsgBoxWindow extends TWindow {
81 /*
82 private void openYNCMessageBox() {
83 application.messageBox("Yes/No/Cancel MessageBox",
84 q"EOS
85 This is an example of a Yes/No/Cancel MessageBox.
86
87 Note that the MessageBox text can span multiple
88 lines.
89
90 The default result (if someone hits the top-left
91 close button) is CANCEL.
92 EOS",
93 TMessageBox.Type.YESNOCANCEL);
94 }
95
96 private void openYNMessageBox() {
97 application.messageBox("Yes/No MessageBox",
98 q"EOS
99 This is an example of a Yes/No MessageBox.
100
101 Note that the MessageBox text can span multiple
102 lines.
103
104 The default result (if someone hits the top-left
105 close button) is NO.
106 EOS",
107 TMessageBox.Type.YESNO);
108 }
109
110 private void openOKCMessageBox() {
111 application.messageBox("OK/Cancel MessageBox",
112 q"EOS
113 This is an example of a OK/Cancel MessageBox.
114
115 Note that the MessageBox text can span multiple
116 lines.
117
118 The default result (if someone hits the top-left
119 close button) is CANCEL.
120 EOS",
121 TMessageBox.Type.OKCANCEL);
122 }
123
124 private void openOKMessageBox() {
125 application.messageBox("OK MessageBox",
126 q"EOS
127 This is an example of a OK MessageBox. This is the
128 default MessageBox.
129
130 Note that the MessageBox text can span multiple
131 lines.
132
133 The default result (if someone hits the top-left
134 close button) is OK.
135 EOS",
136 TMessageBox.Type.OK);
137 }
138
139 */
140
141 /**
142 * Constructor.
143 */
144 DemoMsgBoxWindow(final TApplication parent) {
145 this(parent, TWindow.CENTERED | TWindow.RESIZABLE);
146 }
147
148 /**
149 * Constructor.
150 */
151 DemoMsgBoxWindow(final TApplication parent, final int flags) {
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);
155 /*
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
180 This is an example of an InputBox.
181
182 Note that the InputBox text can span multiple
183 lines.
184 EOS",
185 "some input text");
186 }
187 );
188
189 addButton("&Close Window", (width - 14) / 2, height - 4,
190 {
191 application.closeWindow(this);
192 }
193 );
194 */
195 }
196 }
197
198
199 class DemoMainWindow extends TWindow {
200 // Timer that increments a number
201 private TTimer timer;
202
203 // Timer label is updated with timerrr ticks
204 TLabel timerLabel;
205
206 /*
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() {
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);
218 }
219 private void modalWindowClose() {
220 application.closeWindow(modalWindow);
221 }
222 */
223
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.
228 */
229 @Override
230 public void onClose() {
231 getApplication().removeTimer(timer);
232 }
233
234 /**
235 * Construct demo window. It will be centered on screen.
236 */
237 public DemoMainWindow(TApplication parent) {
238 this(parent, CENTERED | RESIZABLE);
239 }
240
241 int timerI = 0;
242 TProgressBar progressBar;
243
244 /**
245 * Constructor.
246 */
247 private DemoMainWindow(TApplication parent, int flags) {
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);
251
252 int row = 1;
253
254 // Add some widgets
255 if (!isModal()) {
256 addLabel("Message Boxes", 1, row);
257 addButton("&MessageBoxes", 35, row,
258 new TAction() {
259 public void DO() {
260 new DemoMsgBoxWindow(getApplication());
261 }
262 }
263 );
264 }
265 row += 2;
266
267 addLabel("Open me as modal", 1, row);
268 addButton("W&indow", 35, row,
269 new TAction() {
270 public void DO() {
271 new DemoMainWindow(getApplication(), MODAL);
272 }
273 }
274 );
275
276 row += 2;
277
278 /*
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;
285 */
286
287 if (!isModal()) {
288 addLabel("Radio buttons and checkboxes", 1, row);
289 addButton("&Checkboxes", 35, row,
290 new TAction() {
291 public void DO() {
292 new DemoCheckboxWindow(getApplication());
293 }
294 }
295 );
296 }
297 row += 2;
298
299 /*
300 if (!isModal()) {
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
310 if (!isModal()) {
311 addLabel("Text areas", 1, row);
312 addButton("&Text", 35, row,
313 {
314 new DemoTextWindow(application);
315 }
316 );
317 }
318 row += 2;
319
320 if (!isModal()) {
321 addLabel("Tree views", 1, row);
322 addButton("Tree&View", 35, row,
323 {
324 new DemoTreeViewWindow(application);
325 }
326 );
327 }
328 row += 2;
329
330 if (!isModal()) {
331 addLabel("Terminal", 1, row);
332 addButton("Termi&nal", 35, row,
333 {
334 application.openTerminal(0, 0);
335 }
336 );
337 }
338 row += 2;
339 */
340
341 progressBar = addProgressBar(1, row, 22, 0);
342 row++;
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();
355 }
356 }
357 );
358 }
359 }
360
361 /**
362 * The demo application itself.
363 */
364 class DemoApplication extends TApplication {
365 /**
366 * Public constructor
367 */
368 public DemoApplication() throws Exception {
369 super(null, null);
370 new DemoMainWindow(this);
371
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
400 }
401 }
402
403 /**
404 * This class provides a simple demonstration of Jexer's capabilities.
405 */
406 public class Demo1 {
407 /**
408 * Main entry point.
409 *
410 * @param args Command line arguments
411 */
412 public static void main(String [] args) {
413 try {
414 DemoApplication app = new DemoApplication();
415 app.run();
416 } catch (Exception e) {
417 e.printStackTrace();
418 }
419 }
420
421 }