spelling
[fanfix.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;
34import java.util.Calendar;
35import java.util.GregorianCalendar;
36import java.util.Locale;
37import java.util.ResourceBundle;
38
39import jexer.TAction;
40import jexer.TApplication;
41import jexer.TEditColorThemeWindow;
42import jexer.TEditorWindow;
43import jexer.TLabel;
44import jexer.TProgressBar;
1dac6b8d 45import jexer.TTableWindow;
a69ed767
KL
46import jexer.TTimer;
47import jexer.TWidget;
48import jexer.TWindow;
49import jexer.event.TCommandEvent;
2ce6dab2
KL
50import static jexer.TCommand.*;
51import static jexer.TKeypress.*;
e3dfbd23
KL
52
53/**
54 * This is the main "demo" application window. It makes use of the TTimer,
55 * TProgressBox, TLabel, TButton, and TField widgets.
56 */
7668cb45 57public class DemoMainWindow extends TWindow {
e3dfbd23 58
a69ed767
KL
59 /**
60 * Translated strings.
61 */
62 private static final ResourceBundle i18n = ResourceBundle.getBundle(DemoMainWindow.class.getName());
63
43ad7b6c
KL
64 // ------------------------------------------------------------------------
65 // Variables --------------------------------------------------------------
66 // ------------------------------------------------------------------------
67
68 /**
69 * Timer that increments a number.
70 */
e3dfbd23
KL
71 private TTimer timer;
72
43ad7b6c
KL
73 /**
74 * Timer label is updated with timer ticks.
75 */
e3dfbd23
KL
76 TLabel timerLabel;
77
78 /**
43ad7b6c
KL
79 * Timer increment used by the timer loop. Has to be at class scope so
80 * that it can be accessed by the anonymous TAction class.
e3dfbd23 81 */
43ad7b6c
KL
82 int timerI = 0;
83
84 /**
85 * Progress bar used by the timer loop. Has to be at class scope so that
86 * it can be accessed by the anonymous TAction class.
87 */
88 TProgressBar progressBar;
89
051e2913
KL
90 /**
91 * Day of week label is updated with TSpinner clicks.
92 */
93 TLabel dayOfWeekLabel;
94
95 /**
96 * Day of week to demonstrate TSpinner. Has to be at class scope so that
97 * it can be accessed by the anonymous TAction class.
98 */
99 GregorianCalendar calendar = new GregorianCalendar();
100
43ad7b6c
KL
101 // ------------------------------------------------------------------------
102 // Constructors -----------------------------------------------------------
103 // ------------------------------------------------------------------------
e3dfbd23
KL
104
105 /**
106 * Construct demo window. It will be centered on screen.
107 *
108 * @param parent the main application
109 */
110 public DemoMainWindow(final TApplication parent) {
111 this(parent, CENTERED | RESIZABLE);
112 }
113
e3dfbd23
KL
114 /**
115 * Constructor.
116 *
117 * @param parent the main application
118 * @param flags bitmask of MODAL, CENTERED, or RESIZABLE
119 */
120 private DemoMainWindow(final TApplication parent, final int flags) {
121 // Construct a demo window. X and Y don't matter because it will be
122 // centered on screen.
1dac6b8d 123 super(parent, i18n.getString("windowTitle"), 0, 0, 64, 25, flags);
e3dfbd23
KL
124
125 int row = 1;
126
127 // Add some widgets
a69ed767
KL
128 addLabel(i18n.getString("messageBoxLabel"), 1, row);
129 TWidget first = addButton(i18n.getString("messageBoxButton"), 35, row,
2ce6dab2
KL
130 new TAction() {
131 public void DO() {
132 new DemoMsgBoxWindow(getApplication());
e3dfbd23 133 }
2ce6dab2
KL
134 }
135 );
e3dfbd23
KL
136 row += 2;
137
a69ed767
KL
138 addLabel(i18n.getString("openModalLabel"), 1, row);
139 addButton(i18n.getString("openModalButton"), 35, row,
e3dfbd23
KL
140 new TAction() {
141 public void DO() {
142 new DemoMainWindow(getApplication(), MODAL);
143 }
144 }
145 );
e3dfbd23
KL
146 row += 2;
147
a69ed767
KL
148 addLabel(i18n.getString("textFieldLabel"), 1, row);
149 addButton(i18n.getString("textFieldButton"), 35, row,
2ce6dab2
KL
150 new TAction() {
151 public void DO() {
152 new DemoTextFieldWindow(getApplication());
153 }
154 }
155 );
156 row += 2;
e3dfbd23 157
a69ed767
KL
158 addLabel(i18n.getString("radioButtonLabel"), 1, row);
159 addButton(i18n.getString("radioButtonButton"), 35, row,
2ce6dab2
KL
160 new TAction() {
161 public void DO() {
051e2913 162 new DemoCheckBoxWindow(getApplication());
e3dfbd23 163 }
2ce6dab2
KL
164 }
165 );
e3dfbd23
KL
166 row += 2;
167
a69ed767
KL
168 addLabel(i18n.getString("editorLabel"), 1, row);
169 addButton(i18n.getString("editorButton1"), 35, row,
12b55d76
KL
170 new TAction() {
171 public void DO() {
172 new DemoEditorWindow(getApplication());
e3dfbd23 173 }
12b55d76
KL
174 }
175 );
a69ed767 176 addButton(i18n.getString("editorButton2"), 48, row,
71a389c9
KL
177 new TAction() {
178 public void DO() {
179 new TEditorWindow(getApplication());
180 }
181 }
182 );
e3dfbd23 183 row += 2;
e3dfbd23 184
a69ed767
KL
185 addLabel(i18n.getString("textAreaLabel"), 1, row);
186 addButton(i18n.getString("textAreaButton"), 35, row,
2ce6dab2
KL
187 new TAction() {
188 public void DO() {
189 new DemoTextWindow(getApplication());
e3dfbd23 190 }
2ce6dab2
KL
191 }
192 );
1dac6b8d
KL
193 row += 2;
194
195 addLabel(i18n.getString("ttableLabel"), 1, row);
196 addButton(i18n.getString("ttableButton1"), 35, row,
197 new TAction() {
198 public void DO() {
199 // TODO
200 }
201 }
202 );
203 addButton(i18n.getString("ttableButton2"), 48, row,
204 new TAction() {
205 public void DO() {
206 new TTableWindow(getApplication(),
207 i18n.getString("tableDemo"));
208 }
209 }
210 );
e3dfbd23
KL
211 row += 2;
212
a69ed767
KL
213 addLabel(i18n.getString("treeViewLabel"), 1, row);
214 addButton(i18n.getString("treeViewButton"), 35, row,
2ce6dab2
KL
215 new TAction() {
216 public void DO() {
217 try {
218 new DemoTreeViewWindow(getApplication());
219 } catch (Exception e) {
220 e.printStackTrace();
7668cb45 221 }
e3dfbd23 222 }
2ce6dab2
KL
223 }
224 );
e3dfbd23 225 row += 2;
e3dfbd23 226
a69ed767
KL
227 addLabel(i18n.getString("terminalLabel"), 1, row);
228 addButton(i18n.getString("terminalButton"), 35, row,
2ce6dab2
KL
229 new TAction() {
230 public void DO() {
231 getApplication().openTerminal(0, 0);
e3dfbd23 232 }
2ce6dab2
KL
233 }
234 );
e3dfbd23
KL
235 row += 2;
236
a69ed767
KL
237 addLabel(i18n.getString("colorEditorLabel"), 1, row);
238 addButton(i18n.getString("colorEditorButton"), 35, row,
2ce6dab2
KL
239 new TAction() {
240 public void DO() {
241 new TEditColorThemeWindow(getApplication());
3649b921 242 }
2ce6dab2
KL
243 }
244 );
3649b921
KL
245 row += 2;
246
e3dfbd23
KL
247 progressBar = addProgressBar(1, row, 22, 0);
248 row++;
a69ed767 249 timerLabel = addLabel(i18n.getString("timerLabel"), 1, row);
e3dfbd23
KL
250 timer = getApplication().addTimer(250, true,
251 new TAction() {
252
253 public void DO() {
a69ed767
KL
254 timerLabel.setLabel(String.format(i18n.
255 getString("timerText"), timerI));
e3dfbd23
KL
256 timerLabel.setWidth(timerLabel.getLabel().length());
257 if (timerI < 100) {
258 timerI++;
be72cb5c
KL
259 } else {
260 timer.setRecurring(false);
e3dfbd23
KL
261 }
262 progressBar.setValue(timerI);
263 }
264 }
265 );
2ce6dab2 266
051e2913
KL
267 dayOfWeekLabel = addLabel("Wednesday-", 35, row - 1, "tmenu", false);
268 dayOfWeekLabel.setLabel(String.format("%-10s",
269 calendar.getDisplayName(Calendar.DAY_OF_WEEK,
270 Calendar.LONG, Locale.getDefault())));
271
272 addSpinner(35 + dayOfWeekLabel.getWidth(), row - 1,
273 new TAction() {
274 public void DO() {
275 calendar.add(Calendar.DAY_OF_WEEK, 1);
276 dayOfWeekLabel.setLabel(String.format("%-10s",
277 calendar.getDisplayName(
278 Calendar.DAY_OF_WEEK, Calendar.LONG,
279 Locale.getDefault())));
280 }
281 },
282 new TAction() {
283 public void DO() {
284 calendar.add(Calendar.DAY_OF_WEEK, -1);
285 dayOfWeekLabel.setLabel(String.format("%-10s",
286 calendar.getDisplayName(
287 Calendar.DAY_OF_WEEK, Calendar.LONG,
288 Locale.getDefault())));
289 }
290 }
291 );
292
293
294 activate(first);
295
a69ed767
KL
296 statusBar = newStatusBar(i18n.getString("statusBar"));
297 statusBar.addShortcutKeypress(kbF1, cmHelp,
298 i18n.getString("statusBarHelp"));
299 statusBar.addShortcutKeypress(kbF2, cmShell,
300 i18n.getString("statusBarShell"));
301 statusBar.addShortcutKeypress(kbF3, cmOpen,
302 i18n.getString("statusBarOpen"));
303 statusBar.addShortcutKeypress(kbF10, cmExit,
304 i18n.getString("statusBarExit"));
e3dfbd23 305 }
df602ccf 306
43ad7b6c
KL
307 // ------------------------------------------------------------------------
308 // TWindow ----------------------------------------------------------------
309 // ------------------------------------------------------------------------
310
311 /**
312 * We need to override onClose so that the timer will no longer be called
313 * after we close the window. TTimers currently are completely unaware
314 * of the rest of the UI classes.
315 */
316 @Override
317 public void onClose() {
318 getApplication().removeTimer(timer);
319 }
320
df602ccf
KL
321 /**
322 * Method that subclasses can override to handle posted command events.
323 *
324 * @param command command event
325 */
326 @Override
327 public void onCommand(final TCommandEvent command) {
328 if (command.equals(cmOpen)) {
329 try {
330 String filename = fileOpenBox(".");
331 if (filename != null) {
332 try {
a69ed767
KL
333 new TEditorWindow(getApplication(),
334 new File(filename));
df602ccf 335 } catch (IOException e) {
a69ed767
KL
336 messageBox(i18n.getString("errorTitle"),
337 MessageFormat.format(i18n.
338 getString("errorReadingFile"), e.getMessage()));
df602ccf
KL
339 }
340 }
341 } catch (IOException e) {
a69ed767
KL
342 messageBox(i18n.getString("errorTitle"),
343 MessageFormat.format(i18n.
344 getString("errorOpeningFile"), e.getMessage()));
df602ccf
KL
345 }
346 return;
347 }
348
349 // Didn't handle it, let children get it instead
350 super.onCommand(command);
351 }
352
e3dfbd23 353}