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