Prep for 2019 release
[fanfix.git] / src / jexer / demos / DemoMainWindow.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2019 Kevin Lamonte
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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.
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29 package jexer.demos;
30
31 import java.io.File;
32 import java.io.IOException;
33 import java.text.MessageFormat;
34 import java.util.Calendar;
35 import java.util.GregorianCalendar;
36 import java.util.Locale;
37 import java.util.ResourceBundle;
38
39 import jexer.TAction;
40 import jexer.TApplication;
41 import jexer.TEditColorThemeWindow;
42 import jexer.TEditorWindow;
43 import jexer.TLabel;
44 import jexer.TProgressBar;
45 import jexer.TTimer;
46 import jexer.TWidget;
47 import jexer.TWindow;
48 import jexer.event.TCommandEvent;
49 import static jexer.TCommand.*;
50 import static jexer.TKeypress.*;
51
52 /**
53 * This is the main "demo" application window. It makes use of the TTimer,
54 * TProgressBox, TLabel, TButton, and TField widgets.
55 */
56 public class DemoMainWindow extends TWindow {
57
58 /**
59 * Translated strings.
60 */
61 private static final ResourceBundle i18n = ResourceBundle.getBundle(DemoMainWindow.class.getName());
62
63 // ------------------------------------------------------------------------
64 // Variables --------------------------------------------------------------
65 // ------------------------------------------------------------------------
66
67 /**
68 * Timer that increments a number.
69 */
70 private TTimer timer;
71
72 /**
73 * Timer label is updated with timer ticks.
74 */
75 TLabel timerLabel;
76
77 /**
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.
80 */
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
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
100 // ------------------------------------------------------------------------
101 // Constructors -----------------------------------------------------------
102 // ------------------------------------------------------------------------
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
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.
122 super(parent, i18n.getString("windowTitle"), 0, 0, 64, 23, flags);
123
124 int row = 1;
125
126 // Add some widgets
127 addLabel(i18n.getString("messageBoxLabel"), 1, row);
128 TWidget first = addButton(i18n.getString("messageBoxButton"), 35, row,
129 new TAction() {
130 public void DO() {
131 new DemoMsgBoxWindow(getApplication());
132 }
133 }
134 );
135 row += 2;
136
137 addLabel(i18n.getString("openModalLabel"), 1, row);
138 addButton(i18n.getString("openModalButton"), 35, row,
139 new TAction() {
140 public void DO() {
141 new DemoMainWindow(getApplication(), MODAL);
142 }
143 }
144 );
145 row += 2;
146
147 addLabel(i18n.getString("textFieldLabel"), 1, row);
148 addButton(i18n.getString("textFieldButton"), 35, row,
149 new TAction() {
150 public void DO() {
151 new DemoTextFieldWindow(getApplication());
152 }
153 }
154 );
155 row += 2;
156
157 addLabel(i18n.getString("radioButtonLabel"), 1, row);
158 addButton(i18n.getString("radioButtonButton"), 35, row,
159 new TAction() {
160 public void DO() {
161 new DemoCheckBoxWindow(getApplication());
162 }
163 }
164 );
165 row += 2;
166
167 addLabel(i18n.getString("editorLabel"), 1, row);
168 addButton(i18n.getString("editorButton1"), 35, row,
169 new TAction() {
170 public void DO() {
171 new DemoEditorWindow(getApplication());
172 }
173 }
174 );
175 addButton(i18n.getString("editorButton2"), 48, row,
176 new TAction() {
177 public void DO() {
178 new TEditorWindow(getApplication());
179 }
180 }
181 );
182 row += 2;
183
184 addLabel(i18n.getString("textAreaLabel"), 1, row);
185 addButton(i18n.getString("textAreaButton"), 35, row,
186 new TAction() {
187 public void DO() {
188 new DemoTextWindow(getApplication());
189 }
190 }
191 );
192 row += 2;
193
194 addLabel(i18n.getString("treeViewLabel"), 1, row);
195 addButton(i18n.getString("treeViewButton"), 35, row,
196 new TAction() {
197 public void DO() {
198 try {
199 new DemoTreeViewWindow(getApplication());
200 } catch (Exception e) {
201 e.printStackTrace();
202 }
203 }
204 }
205 );
206 row += 2;
207
208 addLabel(i18n.getString("terminalLabel"), 1, row);
209 addButton(i18n.getString("terminalButton"), 35, row,
210 new TAction() {
211 public void DO() {
212 getApplication().openTerminal(0, 0);
213 }
214 }
215 );
216 row += 2;
217
218 addLabel(i18n.getString("colorEditorLabel"), 1, row);
219 addButton(i18n.getString("colorEditorButton"), 35, row,
220 new TAction() {
221 public void DO() {
222 new TEditColorThemeWindow(getApplication());
223 }
224 }
225 );
226 row += 2;
227
228 progressBar = addProgressBar(1, row, 22, 0);
229 row++;
230 timerLabel = addLabel(i18n.getString("timerLabel"), 1, row);
231 timer = getApplication().addTimer(250, true,
232 new TAction() {
233
234 public void DO() {
235 timerLabel.setLabel(String.format(i18n.
236 getString("timerText"), timerI));
237 timerLabel.setWidth(timerLabel.getLabel().length());
238 if (timerI < 100) {
239 timerI++;
240 } else {
241 timer.setRecurring(false);
242 }
243 progressBar.setValue(timerI);
244 }
245 }
246 );
247
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
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"));
286 }
287
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
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 {
314 new TEditorWindow(getApplication(),
315 new File(filename));
316 } catch (IOException e) {
317 messageBox(i18n.getString("errorTitle"),
318 MessageFormat.format(i18n.
319 getString("errorReadingFile"), e.getMessage()));
320 }
321 }
322 } catch (IOException e) {
323 messageBox(i18n.getString("errorTitle"),
324 MessageFormat.format(i18n.
325 getString("errorOpeningFile"), e.getMessage()));
326 }
327 return;
328 }
329
330 // Didn't handle it, let children get it instead
331 super.onCommand(command);
332 }
333
334 }