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
;
45 import jexer
.TTableWindow
;
49 import jexer
.event
.TCommandEvent
;
50 import static jexer
.TCommand
.*;
51 import static jexer
.TKeypress
.*;
54 * This is the main "demo" application window. It makes use of the TTimer,
55 * TProgressBox, TLabel, TButton, and TField widgets.
57 public class DemoMainWindow
extends TWindow
{
62 private static final ResourceBundle i18n
= ResourceBundle
.getBundle(DemoMainWindow
.class.getName());
64 // ------------------------------------------------------------------------
65 // Variables --------------------------------------------------------------
66 // ------------------------------------------------------------------------
69 * Timer that increments a number.
74 * Timer label is updated with timer ticks.
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.
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.
88 TProgressBar progressBar
;
91 * Day of week label is updated with TSpinner clicks.
93 TLabel dayOfWeekLabel
;
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.
99 GregorianCalendar calendar
= new GregorianCalendar();
101 // ------------------------------------------------------------------------
102 // Constructors -----------------------------------------------------------
103 // ------------------------------------------------------------------------
106 * Construct demo window. It will be centered on screen.
108 * @param parent the main application
110 public DemoMainWindow(final TApplication parent
) {
111 this(parent
, CENTERED
| RESIZABLE
);
117 * @param parent the main application
118 * @param flags bitmask of MODAL, CENTERED, or RESIZABLE
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.
123 super(parent
, i18n
.getString("windowTitle"), 0, 0, 64, 25, flags
);
128 addLabel(i18n
.getString("messageBoxLabel"), 1, row
);
129 TWidget first
= addButton(i18n
.getString("messageBoxButton"), 35, row
,
132 new DemoMsgBoxWindow(getApplication());
138 addLabel(i18n
.getString("openModalLabel"), 1, row
);
139 addButton(i18n
.getString("openModalButton"), 35, row
,
142 new DemoMainWindow(getApplication(), MODAL
);
148 addLabel(i18n
.getString("textFieldLabel"), 1, row
);
149 addButton(i18n
.getString("textFieldButton"), 35, row
,
152 new DemoTextFieldWindow(getApplication());
158 addLabel(i18n
.getString("radioButtonLabel"), 1, row
);
159 addButton(i18n
.getString("radioButtonButton"), 35, row
,
162 new DemoCheckBoxWindow(getApplication());
168 addLabel(i18n
.getString("editorLabel"), 1, row
);
169 addButton(i18n
.getString("editorButton1"), 35, row
,
172 new DemoEditorWindow(getApplication());
176 addButton(i18n
.getString("editorButton2"), 48, row
,
179 new TEditorWindow(getApplication());
185 addLabel(i18n
.getString("textAreaLabel"), 1, row
);
186 addButton(i18n
.getString("textAreaButton"), 35, row
,
189 new DemoTextWindow(getApplication());
195 addLabel(i18n
.getString("ttableLabel"), 1, row
);
196 addButton(i18n
.getString("ttableButton1"), 35, row
,
203 addButton(i18n
.getString("ttableButton2"), 48, row
,
206 new TTableWindow(getApplication(),
207 i18n
.getString("tableDemo"));
213 addLabel(i18n
.getString("treeViewLabel"), 1, row
);
214 addButton(i18n
.getString("treeViewButton"), 35, row
,
218 new DemoTreeViewWindow(getApplication());
219 } catch (Exception e
) {
227 addLabel(i18n
.getString("terminalLabel"), 1, row
);
228 addButton(i18n
.getString("terminalButton"), 35, row
,
231 getApplication().openTerminal(0, 0);
237 addLabel(i18n
.getString("colorEditorLabel"), 1, row
);
238 addButton(i18n
.getString("colorEditorButton"), 35, row
,
241 new TEditColorThemeWindow(getApplication());
247 progressBar
= addProgressBar(1, row
, 22, 0);
249 timerLabel
= addLabel(i18n
.getString("timerLabel"), 1, row
);
250 timer
= getApplication().addTimer(250, true,
254 timerLabel
.setLabel(String
.format(i18n
.
255 getString("timerText"), timerI
));
256 timerLabel
.setWidth(timerLabel
.getLabel().length());
260 timer
.setRecurring(false);
262 progressBar
.setValue(timerI
);
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())));
272 addSpinner(35 + dayOfWeekLabel
.getWidth(), row
- 1,
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())));
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())));
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"));
307 // ------------------------------------------------------------------------
308 // TWindow ----------------------------------------------------------------
309 // ------------------------------------------------------------------------
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.
317 public void onClose() {
318 getApplication().removeTimer(timer
);
322 * Method that subclasses can override to handle posted command events.
324 * @param command command event
327 public void onCommand(final TCommandEvent command
) {
328 if (command
.equals(cmOpen
)) {
330 String filename
= fileOpenBox(".");
331 if (filename
!= null) {
333 new TEditorWindow(getApplication(),
335 } catch (IOException e
) {
336 messageBox(i18n
.getString("errorTitle"),
337 MessageFormat
.format(i18n
.
338 getString("errorReadingFile"), e
.getMessage()));
341 } catch (IOException e
) {
342 messageBox(i18n
.getString("errorTitle"),
343 MessageFormat
.format(i18n
.
344 getString("errorOpeningFile"), e
.getMessage()));
349 // Didn't handle it, let children get it instead
350 super.onCommand(command
);