text field working
[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
278 addLabel("Variable-width text field:", 1, row);
279 addField(35, row++, 15, false, "Field text");
280
281 addLabel("Fixed-width text field:", 1, row);
282 addField(35, row, 15, true);
283 row += 2;
284
7272e49f 285 if (!isModal()) {
8e688b92 286 addLabel("Radio buttons and checkboxes", 1, row);
7272e49f
KL
287 addButton("&Checkboxes", 35, row,
288 new TAction() {
289 public void DO() {
d502a0e9 290 new DemoCheckboxWindow(getApplication());
7272e49f
KL
291 }
292 }
293 );
8e688b92
KL
294 }
295 row += 2;
296
7272e49f
KL
297 /*
298 if (!isModal()) {
8e688b92
KL
299 addLabel("Editor window", 1, row);
300 addButton("Edito&r", 35, row,
301 {
302 new TEditor(application, 0, 0, 60, 15);
303 }
304 );
305 }
306 row += 2;
307
7272e49f 308 if (!isModal()) {
8e688b92
KL
309 addLabel("Text areas", 1, row);
310 addButton("&Text", 35, row,
311 {
312 new DemoTextWindow(application);
313 }
314 );
315 }
316 row += 2;
317
7272e49f 318 if (!isModal()) {
8e688b92
KL
319 addLabel("Tree views", 1, row);
320 addButton("Tree&View", 35, row,
321 {
322 new DemoTreeViewWindow(application);
323 }
324 );
325 }
326 row += 2;
327
7272e49f
KL
328 if (!isModal()) {
329 addLabel("Terminal", 1, row);
330 addButton("Termi&nal", 35, row,
331 {
332 application.openTerminal(0, 0);
333 }
334 );
8e688b92 335 }
7272e49f 336 row += 2;
d502a0e9 337 */
8e688b92 338
d502a0e9 339 progressBar = addProgressBar(1, row, 22, 0);
8e688b92 340 row++;
d502a0e9
KL
341 timerLabel = addLabel("Timer", 1, row);
342 timer = getApplication().addTimer(100, true,
343 new TAction() {
344
345 public void DO() {
346 timerLabel.setText(String.format("Timer: %d", timerI));
347 timerLabel.setWidth(timerLabel.getText().length());
348 if (timerI < 100) {
349 timerI++;
350 }
351 progressBar.setValue(timerI);
352 DemoMainWindow.this.setRepaint();
8e688b92 353 }
d502a0e9
KL
354 }
355 );
a06459bd
KL
356 }
357}
7d4115a5
KL
358
359/**
360 * The demo application itself.
361 */
362class DemoApplication extends TApplication {
2420f903
KL
363 /**
364 * Public constructor
365 */
4328bb42 366 public DemoApplication() throws Exception {
8e688b92
KL
367 super(null, null);
368 new DemoMainWindow(this);
369
8e688b92
KL
370 // Add the menus
371 addFileMenu();
372 addEditMenu();
373
374 TMenu demoMenu = addMenu("&Demo");
375 TMenuItem item = demoMenu.addItem(2000, "&Checkable");
376 item.setCheckable(true);
377 item = demoMenu.addItem(2001, "Disabled");
378 item.setEnabled(false);
379 item = demoMenu.addItem(2002, "&Normal");
380 TSubMenu subMenu = demoMenu.addSubMenu("Sub-&Menu");
381 item = demoMenu.addItem(2010, "N&ormal A&&D");
382
383 item = subMenu.addItem(2000, "&Checkable (sub)");
384 item.setCheckable(true);
385 item = subMenu.addItem(2001, "Disabled (sub)");
386 item.setEnabled(false);
387 item = subMenu.addItem(2002, "&Normal (sub)");
388
389 subMenu = subMenu.addSubMenu("Sub-&Menu");
390 item = subMenu.addItem(2000, "&Checkable (sub)");
391 item.setCheckable(true);
392 item = subMenu.addItem(2001, "Disabled (sub)");
393 item.setEnabled(false);
394 item = subMenu.addItem(2002, "&Normal (sub)");
395
396 addWindowMenu();
397
2420f903 398 }
7d4115a5
KL
399}
400
401/**
402 * This class provides a simple demonstration of Jexer's capabilities.
403 */
404public class Demo1 {
405 /**
406 * Main entry point.
407 *
408 * @param args Command line arguments
409 */
410 public static void main(String [] args) {
8e688b92
KL
411 try {
412 DemoApplication app = new DemoApplication();
413 app.run();
414 } catch (Exception e) {
415 e.printStackTrace();
416 }
7d4115a5
KL
417 }
418
419}