2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2017 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]
36 import static jexer
.TCommand
.*;
37 import static jexer
.TKeypress
.*;
40 * This is the main "demo" application window. It makes use of the TTimer,
41 * TProgressBox, TLabel, TButton, and TField widgets.
43 public class DemoMainWindow
extends TWindow
{
45 // ------------------------------------------------------------------------
46 // Variables --------------------------------------------------------------
47 // ------------------------------------------------------------------------
50 * Timer that increments a number.
55 * Timer label is updated with timer ticks.
60 * Timer increment used by the timer loop. Has to be at class scope so
61 * that it can be accessed by the anonymous TAction class.
66 * Progress bar used by the timer loop. Has to be at class scope so that
67 * it can be accessed by the anonymous TAction class.
69 TProgressBar progressBar
;
72 * Day of week label is updated with TSpinner clicks.
74 TLabel dayOfWeekLabel
;
77 * Day of week to demonstrate TSpinner. Has to be at class scope so that
78 * it can be accessed by the anonymous TAction class.
80 GregorianCalendar calendar
= new GregorianCalendar();
82 // ------------------------------------------------------------------------
83 // Constructors -----------------------------------------------------------
84 // ------------------------------------------------------------------------
87 * Construct demo window. It will be centered on screen.
89 * @param parent the main application
91 public DemoMainWindow(final TApplication parent
) {
92 this(parent
, CENTERED
| RESIZABLE
);
98 * @param parent the main application
99 * @param flags bitmask of MODAL, CENTERED, or RESIZABLE
101 private DemoMainWindow(final TApplication parent
, final int flags
) {
102 // Construct a demo window. X and Y don't matter because it will be
103 // centered on screen.
104 super(parent
, "Demo Window", 0, 0, 64, 23, flags
);
109 addLabel("Message Boxes", 1, row
);
110 TWidget first
= addButton("&MessageBoxes", 35, row
,
113 new DemoMsgBoxWindow(getApplication());
119 addLabel("Open me as modal", 1, row
);
120 addButton("W&indow", 35, row
,
123 new DemoMainWindow(getApplication(), MODAL
);
129 addLabel("Text fields and calendar", 1, row
);
130 addButton("Field&s", 35, row
,
133 new DemoTextFieldWindow(getApplication());
139 addLabel("Radio buttons, check and combobox", 1, row
);
140 addButton("&CheckBoxes", 35, row
,
143 new DemoCheckBoxWindow(getApplication());
149 addLabel("Editor window", 1, row
);
150 addButton("&1 Widget", 35, row
,
153 new DemoEditorWindow(getApplication());
157 addButton("&2 Window", 48, row
,
160 new TEditorWindow(getApplication());
166 addLabel("Text areas", 1, row
);
167 addButton("&Text", 35, row
,
170 new DemoTextWindow(getApplication());
176 addLabel("Tree views", 1, row
);
177 addButton("Tree&View", 35, row
,
181 new DemoTreeViewWindow(getApplication());
182 } catch (Exception e
) {
190 addLabel("Terminal", 1, row
);
191 addButton("Termi&nal", 35, row
,
194 getApplication().openTerminal(0, 0);
200 addLabel("Color editor", 1, row
);
201 addButton("Co&lors", 35, row
,
204 new TEditColorThemeWindow(getApplication());
210 progressBar
= addProgressBar(1, row
, 22, 0);
212 timerLabel
= addLabel("Timer", 1, row
);
213 timer
= getApplication().addTimer(250, true,
217 timerLabel
.setLabel(String
.format("Timer: %d", timerI
));
218 timerLabel
.setWidth(timerLabel
.getLabel().length());
222 timer
.setRecurring(false);
224 progressBar
.setValue(timerI
);
229 dayOfWeekLabel
= addLabel("Wednesday-", 35, row
- 1, "tmenu", false);
230 dayOfWeekLabel
.setLabel(String
.format("%-10s",
231 calendar
.getDisplayName(Calendar
.DAY_OF_WEEK
,
232 Calendar
.LONG
, Locale
.getDefault())));
234 addSpinner(35 + dayOfWeekLabel
.getWidth(), row
- 1,
237 calendar
.add(Calendar
.DAY_OF_WEEK
, 1);
238 dayOfWeekLabel
.setLabel(String
.format("%-10s",
239 calendar
.getDisplayName(
240 Calendar
.DAY_OF_WEEK
, Calendar
.LONG
,
241 Locale
.getDefault())));
246 calendar
.add(Calendar
.DAY_OF_WEEK
, -1);
247 dayOfWeekLabel
.setLabel(String
.format("%-10s",
248 calendar
.getDisplayName(
249 Calendar
.DAY_OF_WEEK
, Calendar
.LONG
,
250 Locale
.getDefault())));
258 statusBar
= newStatusBar("Demo Main Window");
259 statusBar
.addShortcutKeypress(kbF1
, cmHelp
, "Help");
260 statusBar
.addShortcutKeypress(kbF2
, cmShell
, "Shell");
261 statusBar
.addShortcutKeypress(kbF3
, cmOpen
, "Open");
262 statusBar
.addShortcutKeypress(kbF10
, cmExit
, "Exit");
265 // ------------------------------------------------------------------------
266 // TWindow ----------------------------------------------------------------
267 // ------------------------------------------------------------------------
270 * We need to override onClose so that the timer will no longer be called
271 * after we close the window. TTimers currently are completely unaware
272 * of the rest of the UI classes.
275 public void onClose() {
276 getApplication().removeTimer(timer
);
280 * Method that subclasses can override to handle posted command events.
282 * @param command command event
285 public void onCommand(final TCommandEvent command
) {
286 if (command
.equals(cmOpen
)) {
288 String filename
= fileOpenBox(".");
289 if (filename
!= null) {
291 new TEditorWindow(getApplication(), new File(filename
));
292 } catch (IOException e
) {
293 messageBox("Error", "Error reading file: " +
297 } catch (IOException e
) {
298 messageBox("Error", "Error opening file dialog: " +
304 // Didn't handle it, let children get it instead
305 super.onCommand(command
);