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
.ResourceBundle
;
37 import jexer
.TApplication
;
38 import jexer
.TEditColorThemeWindow
;
39 import jexer
.TEditorWindow
;
41 import jexer
.TProgressBar
;
42 import jexer
.TTableWindow
;
46 import jexer
.event
.TCommandEvent
;
47 import jexer
.layout
.StretchLayoutManager
;
48 import static jexer
.TCommand
.*;
49 import static jexer
.TKeypress
.*;
52 * This is the main "demo" application window. It makes use of the TTimer,
53 * TProgressBox, TLabel, TButton, and TField widgets.
55 public class DemoMainWindow
extends TWindow
{
60 private static final ResourceBundle i18n
= ResourceBundle
.getBundle(DemoMainWindow
.class.getName());
62 // ------------------------------------------------------------------------
63 // Variables --------------------------------------------------------------
64 // ------------------------------------------------------------------------
67 * Timer that increments a number.
69 private TTimer timer1
;
72 * Timer that increments a number.
74 private TTimer timer2
;
77 * Timer label is updated with timer ticks.
82 * Timer increment used by the timer loop. Has to be at class scope so
83 * that it can be accessed by the anonymous TAction class.
88 * Timer increment used by the timer loop. Has to be at class scope so
89 * that it can be accessed by the anonymous TAction class.
94 * Progress bar used by the timer loop. Has to be at class scope so that
95 * it can be accessed by the anonymous TAction class.
97 TProgressBar progressBar1
;
100 * Progress bar used by the timer loop. Has to be at class scope so that
101 * it can be accessed by the anonymous TAction class.
103 TProgressBar progressBar2
;
105 // ------------------------------------------------------------------------
106 // Constructors -----------------------------------------------------------
107 // ------------------------------------------------------------------------
110 * Construct demo window. It will be centered on screen.
112 * @param parent the main application
114 public DemoMainWindow(final TApplication parent
) {
115 this(parent
, CENTERED
| RESIZABLE
);
121 * @param parent the main application
122 * @param flags bitmask of MODAL, CENTERED, or RESIZABLE
124 private DemoMainWindow(final TApplication parent
, final int flags
) {
125 // Construct a demo window. X and Y don't matter because it will be
126 // centered on screen.
127 super(parent
, i18n
.getString("windowTitle"), 0, 0, 64, 23, flags
);
129 setLayoutManager(new StretchLayoutManager(getWidth() - 2,
135 addLabel(i18n
.getString("messageBoxLabel"), 1, row
);
136 TWidget first
= addButton(i18n
.getString("messageBoxButton"), 35, row
,
139 new DemoMsgBoxWindow(getApplication());
145 addLabel(i18n
.getString("openModalLabel"), 1, row
);
146 addButton(i18n
.getString("openModalButton"), 35, row
,
149 new DemoMainWindow(getApplication(), MODAL
);
155 addLabel(i18n
.getString("textFieldLabel"), 1, row
);
156 addButton(i18n
.getString("textFieldButton"), 35, row
,
159 new DemoTextFieldWindow(getApplication());
165 addLabel(i18n
.getString("radioButtonLabel"), 1, row
);
166 addButton(i18n
.getString("radioButtonButton"), 35, row
,
169 new DemoCheckBoxWindow(getApplication());
175 addLabel(i18n
.getString("editorLabel"), 1, row
);
176 addButton(i18n
.getString("editorButton1"), 35, row
,
179 new DemoEditorWindow(getApplication());
183 addButton(i18n
.getString("editorButton2"), 48, row
,
186 new TEditorWindow(getApplication());
192 addLabel(i18n
.getString("textAreaLabel"), 1, row
);
193 addButton(i18n
.getString("textAreaButton"), 35, row
,
196 new DemoTextWindow(getApplication());
202 addLabel(i18n
.getString("ttableLabel"), 1, row
);
203 addButton(i18n
.getString("ttableButton1"), 35, row
,
206 new DemoTableWindow(getApplication(),
207 i18n
.getString("tableWidgetDemo"));
211 addButton(i18n
.getString("ttableButton2"), 48, row
,
214 new TTableWindow(getApplication(),
215 i18n
.getString("tableDemo"));
221 addLabel(i18n
.getString("treeViewLabel"), 1, row
);
222 addButton(i18n
.getString("treeViewButton"), 35, row
,
226 new DemoTreeViewWindow(getApplication());
227 } catch (Exception e
) {
235 addLabel(i18n
.getString("terminalLabel"), 1, row
);
236 addButton(i18n
.getString("terminalButton"), 35, row
,
239 getApplication().openTerminal(0, 0);
245 addLabel(i18n
.getString("colorEditorLabel"), 1, row
);
246 addButton(i18n
.getString("colorEditorButton"), 35, row
,
249 new TEditColorThemeWindow(getApplication());
255 progressBar1
= addProgressBar(48, row
, 12, 0);
257 timerLabel
= addLabel(i18n
.getString("timerLabel"), 48, row
);
258 timer1
= getApplication().addTimer(250, true,
262 timerLabel
.setLabel(String
.format(i18n
.
263 getString("timerText"), timer1I
));
264 timerLabel
.setWidth(timerLabel
.getLabel().length());
268 timer1
.setRecurring(false);
270 progressBar1
.setValue(timer1I
);
276 progressBar2
= addProgressBar(48, row
, 12, 0);
277 progressBar2
.setLeftBorderChar('\u255e');
278 progressBar2
.setRightBorderChar('\u2561');
279 progressBar2
.setCompletedChar('\u2592');
280 progressBar2
.setRemainingChar('\u2550');
282 timer2
= getApplication().addTimer(125, true,
289 timer2
.setRecurring(false);
291 progressBar2
.setValue(timer2I
);
297 addButton("Exception", 35, row + 3,
301 throw new RuntimeException("FUBAR'd!");
302 } catch (Exception e) {
303 new jexer.TExceptionDialog(getApplication(), e);
312 statusBar
= newStatusBar(i18n
.getString("statusBar"));
313 statusBar
.addShortcutKeypress(kbF1
, cmHelp
,
314 i18n
.getString("statusBarHelp"));
315 statusBar
.addShortcutKeypress(kbF2
, cmShell
,
316 i18n
.getString("statusBarShell"));
317 statusBar
.addShortcutKeypress(kbF3
, cmOpen
,
318 i18n
.getString("statusBarOpen"));
319 statusBar
.addShortcutKeypress(kbF10
, cmExit
,
320 i18n
.getString("statusBarExit"));
323 // ------------------------------------------------------------------------
324 // TWindow ----------------------------------------------------------------
325 // ------------------------------------------------------------------------
328 * We need to override onClose so that the timer will no longer be called
329 * after we close the window. TTimers currently are completely unaware
330 * of the rest of the UI classes.
333 public void onClose() {
334 getApplication().removeTimer(timer1
);
335 getApplication().removeTimer(timer2
);
339 * Method that subclasses can override to handle posted command events.
341 * @param command command event
344 public void onCommand(final TCommandEvent command
) {
345 if (command
.equals(cmOpen
)) {
347 String filename
= fileOpenBox(".");
348 if (filename
!= null) {
350 new TEditorWindow(getApplication(),
352 } catch (IOException e
) {
353 messageBox(i18n
.getString("errorTitle"),
354 MessageFormat
.format(i18n
.
355 getString("errorReadingFile"), e
.getMessage()));
358 } catch (IOException e
) {
359 messageBox(i18n
.getString("errorTitle"),
360 MessageFormat
.format(i18n
.
361 getString("errorOpeningFile"), e
.getMessage()));
366 // Didn't handle it, let children get it instead
367 super.onCommand(command
);