double buffer swing
[nikiroo-utils.git] / src / jexer / demos / DemoMainWindow.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * License: LGPLv3 or later
5 *
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.
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
27 *
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 * @version 1
30 */
31 package jexer.demos;
32
33 import jexer.*;
34 import jexer.event.*;
35 import jexer.menu.*;
36
37 /**
38 * This is the main "demo" application window. It makes use of the TTimer,
39 * TProgressBox, TLabel, TButton, and TField widgets.
40 */
41 class DemoMainWindow extends TWindow {
42
43 // Timer that increments a number.
44 private TTimer timer;
45
46 // Timer label is updated with timer ticks.
47 TLabel timerLabel;
48
49 /**
50 * We need to override onClose so that the timer will no longer be called
51 * after we close the window. TTimers currently are completely unaware
52 * of the rest of the UI classes.
53 */
54 @Override
55 public void onClose() {
56 getApplication().removeTimer(timer);
57 }
58
59 /**
60 * Construct demo window. It will be centered on screen.
61 *
62 * @param parent the main application
63 */
64 public DemoMainWindow(final TApplication parent) {
65 this(parent, CENTERED | RESIZABLE);
66 }
67
68 // These are used by the timer loop. They have to be at class scope so
69 // that they can be accessed by the anonymous TAction class.
70 int timerI = 0;
71 TProgressBar progressBar;
72
73 /**
74 * Constructor.
75 *
76 * @param parent the main application
77 * @param flags bitmask of MODAL, CENTERED, or RESIZABLE
78 */
79 private DemoMainWindow(final TApplication parent, final int flags) {
80 // Construct a demo window. X and Y don't matter because it will be
81 // centered on screen.
82 super(parent, "Demo Window", 0, 0, 60, 23, flags);
83
84 int row = 1;
85
86 // Add some widgets
87 if (!isModal()) {
88 addLabel("Message Boxes", 1, row);
89 addButton("&MessageBoxes", 35, row,
90 new TAction() {
91 public void DO() {
92 new DemoMsgBoxWindow(getApplication());
93 }
94 }
95 );
96 }
97 row += 2;
98
99 addLabel("Open me as modal", 1, row);
100 addButton("W&indow", 35, row,
101 new TAction() {
102 public void DO() {
103 new DemoMainWindow(getApplication(), MODAL);
104 }
105 }
106 );
107
108 row += 2;
109
110 addLabel("Variable-width text field:", 1, row);
111 addField(35, row++, 15, false, "Field text");
112 addLabel("Fixed-width text field:", 1, row);
113 addField(35, row++, 15, true);
114 addLabel("Variable-width password:", 1, row);
115 addPasswordField(35, row++, 15, false);
116 addLabel("Fixed-width password:", 1, row);
117 addPasswordField(35, row++, 15, true, "hunter2");
118 row += 2;
119
120 if (!isModal()) {
121 addLabel("Radio buttons and checkboxes", 1, row);
122 addButton("&Checkboxes", 35, row,
123 new TAction() {
124 public void DO() {
125 new DemoCheckboxWindow(getApplication());
126 }
127 }
128 );
129 }
130 row += 2;
131
132 /*
133 if (!isModal()) {
134 addLabel("Editor window", 1, row);
135 addButton("Edito&r", 35, row,
136 {
137 new TEditor(application, 0, 0, 60, 15);
138 }
139 );
140 }
141 row += 2;
142 */
143
144 if (!isModal()) {
145 addLabel("Text areas", 1, row);
146 addButton("&Text", 35, row,
147 new TAction() {
148 public void DO() {
149 new DemoTextWindow(getApplication());
150 }
151 }
152 );
153 }
154 row += 2;
155
156 /*
157 if (!isModal()) {
158 addLabel("Tree views", 1, row);
159 addButton("Tree&View", 35, row,
160 {
161 new DemoTreeViewWindow(application);
162 }
163 );
164 }
165 row += 2;
166 */
167
168 if (!isModal()) {
169 addLabel("Terminal", 1, row);
170 addButton("Termi&nal", 35, row,
171 new TAction() {
172 public void DO() {
173 getApplication().openTerminal(0, 0);
174 }
175 }
176 );
177 }
178 row += 2;
179
180 progressBar = addProgressBar(1, row, 22, 0);
181 row++;
182 timerLabel = addLabel("Timer", 1, row);
183 timer = getApplication().addTimer(250, true,
184 new TAction() {
185
186 public void DO() {
187 timerLabel.setLabel(String.format("Timer: %d", timerI));
188 timerLabel.setWidth(timerLabel.getLabel().length());
189 if (timerI < 100) {
190 timerI++;
191 }
192 progressBar.setValue(timerI);
193 }
194 }
195 );
196 }
197 }