Merge branch 'subtree'
[nikiroo-utils.git] / src / jexer / demos / DemoMainWindow.java
CommitLineData
e3dfbd23
KL
1/*
2 * Jexer - Java Text User Interface
3 *
e16dda65 4 * The MIT License (MIT)
e3dfbd23 5 *
a69ed767 6 * Copyright (C) 2019 Kevin Lamonte
e3dfbd23 7 *
e16dda65
KL
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
e3dfbd23 14 *
e16dda65
KL
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
e3dfbd23 17 *
e16dda65
KL
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
e3dfbd23
KL
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29package jexer.demos;
30
a69ed767
KL
31import java.io.File;
32import java.io.IOException;
33import java.text.MessageFormat;
a69ed767
KL
34import java.util.ResourceBundle;
35
36import jexer.TAction;
37import jexer.TApplication;
38import jexer.TEditColorThemeWindow;
39import jexer.TEditorWindow;
40import jexer.TLabel;
41import jexer.TProgressBar;
1dac6b8d 42import jexer.TTableWindow;
a69ed767
KL
43import jexer.TTimer;
44import jexer.TWidget;
45import jexer.TWindow;
46import jexer.event.TCommandEvent;
d8dc8aea 47import jexer.layout.StretchLayoutManager;
2ce6dab2
KL
48import static jexer.TCommand.*;
49import static jexer.TKeypress.*;
e3dfbd23
KL
50
51/**
52 * This is the main "demo" application window. It makes use of the TTimer,
53 * TProgressBox, TLabel, TButton, and TField widgets.
54 */
7668cb45 55public class DemoMainWindow extends TWindow {
e3dfbd23 56
a69ed767
KL
57 /**
58 * Translated strings.
59 */
60 private static final ResourceBundle i18n = ResourceBundle.getBundle(DemoMainWindow.class.getName());
61
43ad7b6c
KL
62 // ------------------------------------------------------------------------
63 // Variables --------------------------------------------------------------
64 // ------------------------------------------------------------------------
65
66 /**
67 * Timer that increments a number.
68 */
e307b724
KL
69 private TTimer timer1;
70
71 /**
72 * Timer that increments a number.
73 */
74 private TTimer timer2;
e3dfbd23 75
43ad7b6c
KL
76 /**
77 * Timer label is updated with timer ticks.
78 */
e3dfbd23
KL
79 TLabel timerLabel;
80
81 /**
43ad7b6c
KL
82 * Timer increment used by the timer loop. Has to be at class scope so
83 * that it can be accessed by the anonymous TAction class.
e3dfbd23 84 */
e307b724
KL
85 int timer1I = 0;
86
87 /**
88 * Timer increment used by the timer loop. Has to be at class scope so
89 * that it can be accessed by the anonymous TAction class.
90 */
91 int timer2I = 0;
43ad7b6c
KL
92
93 /**
94 * Progress bar used by the timer loop. Has to be at class scope so that
95 * it can be accessed by the anonymous TAction class.
96 */
e307b724
KL
97 TProgressBar progressBar1;
98
99 /**
100 * Progress bar used by the timer loop. Has to be at class scope so that
101 * it can be accessed by the anonymous TAction class.
102 */
103 TProgressBar progressBar2;
43ad7b6c
KL
104
105 // ------------------------------------------------------------------------
106 // Constructors -----------------------------------------------------------
107 // ------------------------------------------------------------------------
e3dfbd23
KL
108
109 /**
110 * Construct demo window. It will be centered on screen.
111 *
112 * @param parent the main application
113 */
114 public DemoMainWindow(final TApplication parent) {
115 this(parent, CENTERED | RESIZABLE);
116 }
117
e3dfbd23
KL
118 /**
119 * Constructor.
120 *
121 * @param parent the main application
122 * @param flags bitmask of MODAL, CENTERED, or RESIZABLE
123 */
124 private DemoMainWindow(final TApplication parent, final int flags) {
125 // Construct a demo window. X and Y don't matter because it will be
126 // centered on screen.
3096642b 127 super(parent, i18n.getString("windowTitle"), 0, 0, 64, 23, flags);
e3dfbd23 128
fc2af494
KL
129 setLayoutManager(new StretchLayoutManager(getWidth() - 2,
130 getHeight() - 2));
d8dc8aea 131
e3dfbd23
KL
132 int row = 1;
133
134 // Add some widgets
a69ed767
KL
135 addLabel(i18n.getString("messageBoxLabel"), 1, row);
136 TWidget first = addButton(i18n.getString("messageBoxButton"), 35, row,
2ce6dab2
KL
137 new TAction() {
138 public void DO() {
139 new DemoMsgBoxWindow(getApplication());
e3dfbd23 140 }
2ce6dab2
KL
141 }
142 );
e3dfbd23
KL
143 row += 2;
144
a69ed767
KL
145 addLabel(i18n.getString("openModalLabel"), 1, row);
146 addButton(i18n.getString("openModalButton"), 35, row,
e3dfbd23
KL
147 new TAction() {
148 public void DO() {
149 new DemoMainWindow(getApplication(), MODAL);
150 }
151 }
152 );
e3dfbd23
KL
153 row += 2;
154
a69ed767
KL
155 addLabel(i18n.getString("textFieldLabel"), 1, row);
156 addButton(i18n.getString("textFieldButton"), 35, row,
2ce6dab2
KL
157 new TAction() {
158 public void DO() {
159 new DemoTextFieldWindow(getApplication());
160 }
161 }
162 );
163 row += 2;
e3dfbd23 164
a69ed767
KL
165 addLabel(i18n.getString("radioButtonLabel"), 1, row);
166 addButton(i18n.getString("radioButtonButton"), 35, row,
2ce6dab2
KL
167 new TAction() {
168 public void DO() {
051e2913 169 new DemoCheckBoxWindow(getApplication());
e3dfbd23 170 }
2ce6dab2
KL
171 }
172 );
e3dfbd23
KL
173 row += 2;
174
a69ed767
KL
175 addLabel(i18n.getString("editorLabel"), 1, row);
176 addButton(i18n.getString("editorButton1"), 35, row,
12b55d76
KL
177 new TAction() {
178 public void DO() {
179 new DemoEditorWindow(getApplication());
e3dfbd23 180 }
12b55d76
KL
181 }
182 );
a69ed767 183 addButton(i18n.getString("editorButton2"), 48, row,
71a389c9
KL
184 new TAction() {
185 public void DO() {
186 new TEditorWindow(getApplication());
187 }
188 }
189 );
e3dfbd23 190 row += 2;
e3dfbd23 191
a69ed767
KL
192 addLabel(i18n.getString("textAreaLabel"), 1, row);
193 addButton(i18n.getString("textAreaButton"), 35, row,
2ce6dab2
KL
194 new TAction() {
195 public void DO() {
196 new DemoTextWindow(getApplication());
e3dfbd23 197 }
2ce6dab2
KL
198 }
199 );
1dac6b8d
KL
200 row += 2;
201
202 addLabel(i18n.getString("ttableLabel"), 1, row);
203 addButton(i18n.getString("ttableButton1"), 35, row,
204 new TAction() {
205 public void DO() {
766e7308
KL
206 new DemoTableWindow(getApplication(),
207 i18n.getString("tableWidgetDemo"));
1dac6b8d
KL
208 }
209 }
210 );
211 addButton(i18n.getString("ttableButton2"), 48, row,
212 new TAction() {
213 public void DO() {
214 new TTableWindow(getApplication(),
215 i18n.getString("tableDemo"));
216 }
217 }
218 );
e3dfbd23
KL
219 row += 2;
220
a69ed767
KL
221 addLabel(i18n.getString("treeViewLabel"), 1, row);
222 addButton(i18n.getString("treeViewButton"), 35, row,
2ce6dab2
KL
223 new TAction() {
224 public void DO() {
225 try {
226 new DemoTreeViewWindow(getApplication());
227 } catch (Exception e) {
228 e.printStackTrace();
7668cb45 229 }
e3dfbd23 230 }
2ce6dab2
KL
231 }
232 );
e3dfbd23 233 row += 2;
e3dfbd23 234
a69ed767
KL
235 addLabel(i18n.getString("terminalLabel"), 1, row);
236 addButton(i18n.getString("terminalButton"), 35, row,
2ce6dab2
KL
237 new TAction() {
238 public void DO() {
239 getApplication().openTerminal(0, 0);
e3dfbd23 240 }
2ce6dab2
KL
241 }
242 );
e3dfbd23
KL
243 row += 2;
244
a69ed767
KL
245 addLabel(i18n.getString("colorEditorLabel"), 1, row);
246 addButton(i18n.getString("colorEditorButton"), 35, row,
2ce6dab2
KL
247 new TAction() {
248 public void DO() {
249 new TEditColorThemeWindow(getApplication());
3649b921 250 }
2ce6dab2
KL
251 }
252 );
3649b921 253
3096642b 254 row = 15;
e307b724 255 progressBar1 = addProgressBar(48, row, 12, 0);
e3dfbd23 256 row++;
3096642b 257 timerLabel = addLabel(i18n.getString("timerLabel"), 48, row);
e307b724 258 timer1 = getApplication().addTimer(250, true,
e3dfbd23
KL
259 new TAction() {
260
261 public void DO() {
a69ed767 262 timerLabel.setLabel(String.format(i18n.
e307b724 263 getString("timerText"), timer1I));
e3dfbd23 264 timerLabel.setWidth(timerLabel.getLabel().length());
e307b724
KL
265 if (timer1I < 100) {
266 timer1I++;
267 } else {
268 timer1.setRecurring(false);
269 }
270 progressBar1.setValue(timer1I);
271 }
272 }
273 );
274
275 row += 2;
276 progressBar2 = addProgressBar(48, row, 12, 0);
277 progressBar2.setLeftBorderChar('\u255e');
278 progressBar2.setRightBorderChar('\u2561');
279 progressBar2.setCompletedChar('\u2592');
280 progressBar2.setRemainingChar('\u2550');
281 row++;
282 timer2 = getApplication().addTimer(125, true,
283 new TAction() {
284
285 public void DO() {
286 if (timer2I < 100) {
287 timer2I++;
be72cb5c 288 } else {
e307b724 289 timer2.setRecurring(false);
e3dfbd23 290 }
e307b724 291 progressBar2.setValue(timer2I);
e3dfbd23
KL
292 }
293 }
294 );
2ce6dab2 295
2b427404
KL
296 /*
297 addButton("Exception", 35, row + 3,
298 new TAction() {
299 public void DO() {
300 try {
301 throw new RuntimeException("FUBAR'd!");
302 } catch (Exception e) {
303 new jexer.TExceptionDialog(getApplication(), e);
304 }
305 }
306 }
307 );
308 */
309
051e2913
KL
310 activate(first);
311
a69ed767
KL
312 statusBar = newStatusBar(i18n.getString("statusBar"));
313 statusBar.addShortcutKeypress(kbF1, cmHelp,
314 i18n.getString("statusBarHelp"));
315 statusBar.addShortcutKeypress(kbF2, cmShell,
316 i18n.getString("statusBarShell"));
317 statusBar.addShortcutKeypress(kbF3, cmOpen,
318 i18n.getString("statusBarOpen"));
319 statusBar.addShortcutKeypress(kbF10, cmExit,
320 i18n.getString("statusBarExit"));
e3dfbd23 321 }
df602ccf 322
43ad7b6c
KL
323 // ------------------------------------------------------------------------
324 // TWindow ----------------------------------------------------------------
325 // ------------------------------------------------------------------------
326
327 /**
328 * We need to override onClose so that the timer will no longer be called
329 * after we close the window. TTimers currently are completely unaware
330 * of the rest of the UI classes.
331 */
332 @Override
333 public void onClose() {
e307b724
KL
334 getApplication().removeTimer(timer1);
335 getApplication().removeTimer(timer2);
43ad7b6c
KL
336 }
337
df602ccf
KL
338 /**
339 * Method that subclasses can override to handle posted command events.
340 *
341 * @param command command event
342 */
343 @Override
344 public void onCommand(final TCommandEvent command) {
345 if (command.equals(cmOpen)) {
346 try {
347 String filename = fileOpenBox(".");
348 if (filename != null) {
349 try {
a69ed767
KL
350 new TEditorWindow(getApplication(),
351 new File(filename));
df602ccf 352 } catch (IOException e) {
a69ed767
KL
353 messageBox(i18n.getString("errorTitle"),
354 MessageFormat.format(i18n.
355 getString("errorReadingFile"), e.getMessage()));
df602ccf
KL
356 }
357 }
358 } catch (IOException e) {
a69ed767
KL
359 messageBox(i18n.getString("errorTitle"),
360 MessageFormat.format(i18n.
361 getString("errorOpeningFile"), e.getMessage()));
df602ccf
KL
362 }
363 return;
364 }
365
366 // Didn't handle it, let children get it instead
367 super.onCommand(command);
368 }
369
e3dfbd23 370}