2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
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
;
40 import jexer
.TApplication
;
41 import jexer
.TEditColorThemeWindow
;
42 import jexer
.TEditorWindow
;
44 import jexer
.TProgressBar
;
48 import jexer
.event
.TCommandEvent
;
49 import static jexer
.TCommand
.*;
50 import static jexer
.TKeypress
.*;
53 * This is the main "demo" application window. It makes use of the TTimer,
54 * TProgressBox, TLabel, TButton, and TField widgets.
56 public class DemoMainWindow
extends TWindow
{
61 private static final ResourceBundle i18n
= ResourceBundle
.getBundle(DemoMainWindow
.class.getName());
63 // ------------------------------------------------------------------------
64 // Variables --------------------------------------------------------------
65 // ------------------------------------------------------------------------
68 * Timer that increments a number.
73 * Timer label is updated with timer ticks.
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.
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.
87 TProgressBar progressBar
;
90 * Day of week label is updated with TSpinner clicks.
92 TLabel dayOfWeekLabel
;
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.
98 GregorianCalendar calendar
= new GregorianCalendar();
100 // ------------------------------------------------------------------------
101 // Constructors -----------------------------------------------------------
102 // ------------------------------------------------------------------------
105 * Construct demo window. It will be centered on screen.
107 * @param parent the main application
109 public DemoMainWindow(final TApplication parent
) {
110 this(parent
, CENTERED
| RESIZABLE
);
116 * @param parent the main application
117 * @param flags bitmask of MODAL, CENTERED, or RESIZABLE
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
);
127 addLabel(i18n
.getString("messageBoxLabel"), 1, row
);
128 TWidget first
= addButton(i18n
.getString("messageBoxButton"), 35, row
,
131 new DemoMsgBoxWindow(getApplication());
137 addLabel(i18n
.getString("openModalLabel"), 1, row
);
138 addButton(i18n
.getString("openModalButton"), 35, row
,
141 new DemoMainWindow(getApplication(), MODAL
);
147 addLabel(i18n
.getString("textFieldLabel"), 1, row
);
148 addButton(i18n
.getString("textFieldButton"), 35, row
,
151 new DemoTextFieldWindow(getApplication());
157 addLabel(i18n
.getString("radioButtonLabel"), 1, row
);
158 addButton(i18n
.getString("radioButtonButton"), 35, row
,
161 new DemoCheckBoxWindow(getApplication());
167 addLabel(i18n
.getString("editorLabel"), 1, row
);
168 addButton(i18n
.getString("editorButton1"), 35, row
,
171 new DemoEditorWindow(getApplication());
175 addButton(i18n
.getString("editorButton2"), 48, row
,
178 new TEditorWindow(getApplication());
184 addLabel(i18n
.getString("textAreaLabel"), 1, row
);
185 addButton(i18n
.getString("textAreaButton"), 35, row
,
188 new DemoTextWindow(getApplication());
194 addLabel(i18n
.getString("treeViewLabel"), 1, row
);
195 addButton(i18n
.getString("treeViewButton"), 35, row
,
199 new DemoTreeViewWindow(getApplication());
200 } catch (Exception e
) {
208 addLabel(i18n
.getString("terminalLabel"), 1, row
);
209 addButton(i18n
.getString("terminalButton"), 35, row
,
212 getApplication().openTerminal(0, 0);
218 addLabel(i18n
.getString("colorEditorLabel"), 1, row
);
219 addButton(i18n
.getString("colorEditorButton"), 35, row
,
222 new TEditColorThemeWindow(getApplication());
228 progressBar
= addProgressBar(1, row
, 22, 0);
230 timerLabel
= addLabel(i18n
.getString("timerLabel"), 1, row
);
231 timer
= getApplication().addTimer(250, true,
235 timerLabel
.setLabel(String
.format(i18n
.
236 getString("timerText"), timerI
));
237 timerLabel
.setWidth(timerLabel
.getLabel().length());
241 timer
.setRecurring(false);
243 progressBar
.setValue(timerI
);
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())));
253 addSpinner(35 + dayOfWeekLabel
.getWidth(), row
- 1,
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())));
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())));
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"));
288 // ------------------------------------------------------------------------
289 // TWindow ----------------------------------------------------------------
290 // ------------------------------------------------------------------------
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.
298 public void onClose() {
299 getApplication().removeTimer(timer
);
303 * Method that subclasses can override to handle posted command events.
305 * @param command command event
308 public void onCommand(final TCommandEvent command
) {
309 if (command
.equals(cmOpen
)) {
311 String filename
= fileOpenBox(".");
312 if (filename
!= null) {
314 new TEditorWindow(getApplication(),
316 } catch (IOException e
) {
317 messageBox(i18n
.getString("errorTitle"),
318 MessageFormat
.format(i18n
.
319 getString("errorReadingFile"), e
.getMessage()));
322 } catch (IOException e
) {
323 messageBox(i18n
.getString("errorTitle"),
324 MessageFormat
.format(i18n
.
325 getString("errorOpeningFile"), e
.getMessage()));
330 // Didn't handle it, let children get it instead
331 super.onCommand(command
);