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.
72 * Timer label is updated with timer ticks.
77 * Timer increment used by the timer loop. Has to be at class scope so
78 * that it can be accessed by the anonymous TAction class.
83 * Progress bar used by the timer loop. Has to be at class scope so that
84 * it can be accessed by the anonymous TAction class.
86 TProgressBar progressBar
;
88 // ------------------------------------------------------------------------
89 // Constructors -----------------------------------------------------------
90 // ------------------------------------------------------------------------
93 * Construct demo window. It will be centered on screen.
95 * @param parent the main application
97 public DemoMainWindow(final TApplication parent
) {
98 this(parent
, CENTERED
| RESIZABLE
);
104 * @param parent the main application
105 * @param flags bitmask of MODAL, CENTERED, or RESIZABLE
107 private DemoMainWindow(final TApplication parent
, final int flags
) {
108 // Construct a demo window. X and Y don't matter because it will be
109 // centered on screen.
110 super(parent
, i18n
.getString("windowTitle"), 0, 0, 64, 23, flags
);
112 setLayoutManager(new StretchLayoutManager(getWidth(), getHeight()));
117 addLabel(i18n
.getString("messageBoxLabel"), 1, row
);
118 TWidget first
= addButton(i18n
.getString("messageBoxButton"), 35, row
,
121 new DemoMsgBoxWindow(getApplication());
127 addLabel(i18n
.getString("openModalLabel"), 1, row
);
128 addButton(i18n
.getString("openModalButton"), 35, row
,
131 new DemoMainWindow(getApplication(), MODAL
);
137 addLabel(i18n
.getString("textFieldLabel"), 1, row
);
138 addButton(i18n
.getString("textFieldButton"), 35, row
,
141 new DemoTextFieldWindow(getApplication());
147 addLabel(i18n
.getString("radioButtonLabel"), 1, row
);
148 addButton(i18n
.getString("radioButtonButton"), 35, row
,
151 new DemoCheckBoxWindow(getApplication());
157 addLabel(i18n
.getString("editorLabel"), 1, row
);
158 addButton(i18n
.getString("editorButton1"), 35, row
,
161 new DemoEditorWindow(getApplication());
165 addButton(i18n
.getString("editorButton2"), 48, row
,
168 new TEditorWindow(getApplication());
174 addLabel(i18n
.getString("textAreaLabel"), 1, row
);
175 addButton(i18n
.getString("textAreaButton"), 35, row
,
178 new DemoTextWindow(getApplication());
184 addLabel(i18n
.getString("ttableLabel"), 1, row
);
185 addButton(i18n
.getString("ttableButton1"), 35, row
,
188 new DemoTableWindow(getApplication(),
189 i18n
.getString("tableWidgetDemo"));
193 addButton(i18n
.getString("ttableButton2"), 48, row
,
196 new TTableWindow(getApplication(),
197 i18n
.getString("tableDemo"));
203 addLabel(i18n
.getString("treeViewLabel"), 1, row
);
204 addButton(i18n
.getString("treeViewButton"), 35, row
,
208 new DemoTreeViewWindow(getApplication());
209 } catch (Exception e
) {
217 addLabel(i18n
.getString("terminalLabel"), 1, row
);
218 addButton(i18n
.getString("terminalButton"), 35, row
,
221 getApplication().openTerminal(0, 0);
227 addLabel(i18n
.getString("colorEditorLabel"), 1, row
);
228 addButton(i18n
.getString("colorEditorButton"), 35, row
,
231 new TEditColorThemeWindow(getApplication());
237 progressBar
= addProgressBar(48, row
, 12, 0);
239 timerLabel
= addLabel(i18n
.getString("timerLabel"), 48, row
);
240 timer
= getApplication().addTimer(250, true,
244 timerLabel
.setLabel(String
.format(i18n
.
245 getString("timerText"), timerI
));
246 timerLabel
.setWidth(timerLabel
.getLabel().length());
250 timer
.setRecurring(false);
252 progressBar
.setValue(timerI
);
258 addButton("Exception", 35, row + 3,
262 throw new RuntimeException("FUBAR'd!");
263 } catch (Exception e) {
264 new jexer.TExceptionDialog(getApplication(), e);
273 statusBar
= newStatusBar(i18n
.getString("statusBar"));
274 statusBar
.addShortcutKeypress(kbF1
, cmHelp
,
275 i18n
.getString("statusBarHelp"));
276 statusBar
.addShortcutKeypress(kbF2
, cmShell
,
277 i18n
.getString("statusBarShell"));
278 statusBar
.addShortcutKeypress(kbF3
, cmOpen
,
279 i18n
.getString("statusBarOpen"));
280 statusBar
.addShortcutKeypress(kbF10
, cmExit
,
281 i18n
.getString("statusBarExit"));
284 // ------------------------------------------------------------------------
285 // TWindow ----------------------------------------------------------------
286 // ------------------------------------------------------------------------
289 * We need to override onClose so that the timer will no longer be called
290 * after we close the window. TTimers currently are completely unaware
291 * of the rest of the UI classes.
294 public void onClose() {
295 getApplication().removeTimer(timer
);
299 * Method that subclasses can override to handle posted command events.
301 * @param command command event
304 public void onCommand(final TCommandEvent command
) {
305 if (command
.equals(cmOpen
)) {
307 String filename
= fileOpenBox(".");
308 if (filename
!= null) {
310 new TEditorWindow(getApplication(),
312 } catch (IOException e
) {
313 messageBox(i18n
.getString("errorTitle"),
314 MessageFormat
.format(i18n
.
315 getString("errorReadingFile"), e
.getMessage()));
318 } catch (IOException e
) {
319 messageBox(i18n
.getString("errorTitle"),
320 MessageFormat
.format(i18n
.
321 getString("errorOpeningFile"), e
.getMessage()));
326 // Didn't handle it, let children get it instead
327 super.onCommand(command
);