#51 wip
[fanfix.git] / src / jexer / demos / DemoMainWindow.java
CommitLineData
e3dfbd23
KL
1/*
2 * Jexer - Java Text User Interface
3 *
e16dda65 4 * The MIT License (MIT)
e3dfbd23 5 *
a69ed767 6 * Copyright (C) 2019 Kevin Lamonte
e3dfbd23 7 *
e16dda65
KL
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:
e3dfbd23 14 *
e16dda65
KL
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
e3dfbd23 17 *
e16dda65
KL
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.
e3dfbd23
KL
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29package jexer.demos;
30
a69ed767
KL
31import java.io.File;
32import java.io.IOException;
33import java.text.MessageFormat;
a69ed767
KL
34import java.util.ResourceBundle;
35
36import jexer.TAction;
37import jexer.TApplication;
38import jexer.TEditColorThemeWindow;
39import jexer.TEditorWindow;
40import jexer.TLabel;
41import jexer.TProgressBar;
1dac6b8d 42import jexer.TTableWindow;
a69ed767
KL
43import jexer.TTimer;
44import jexer.TWidget;
45import jexer.TWindow;
46import jexer.event.TCommandEvent;
d8dc8aea 47import jexer.layout.StretchLayoutManager;
2ce6dab2
KL
48import static jexer.TCommand.*;
49import static jexer.TKeypress.*;
e3dfbd23
KL
50
51/**
52 * This is the main "demo" application window. It makes use of the TTimer,
53 * TProgressBox, TLabel, TButton, and TField widgets.
54 */
7668cb45 55public class DemoMainWindow extends TWindow {
e3dfbd23 56
a69ed767
KL
57 /**
58 * Translated strings.
59 */
60 private static final ResourceBundle i18n = ResourceBundle.getBundle(DemoMainWindow.class.getName());
61
43ad7b6c
KL
62 // ------------------------------------------------------------------------
63 // Variables --------------------------------------------------------------
64 // ------------------------------------------------------------------------
65
66 /**
67 * Timer that increments a number.
68 */
e3dfbd23
KL
69 private TTimer timer;
70
43ad7b6c
KL
71 /**
72 * Timer label is updated with timer ticks.
73 */
e3dfbd23
KL
74 TLabel timerLabel;
75
76 /**
43ad7b6c
KL
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.
e3dfbd23 79 */
43ad7b6c
KL
80 int timerI = 0;
81
82 /**
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.
85 */
86 TProgressBar progressBar;
87
88 // ------------------------------------------------------------------------
89 // Constructors -----------------------------------------------------------
90 // ------------------------------------------------------------------------
e3dfbd23
KL
91
92 /**
93 * Construct demo window. It will be centered on screen.
94 *
95 * @param parent the main application
96 */
97 public DemoMainWindow(final TApplication parent) {
98 this(parent, CENTERED | RESIZABLE);
99 }
100
e3dfbd23
KL
101 /**
102 * Constructor.
103 *
104 * @param parent the main application
105 * @param flags bitmask of MODAL, CENTERED, or RESIZABLE
106 */
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.
3096642b 110 super(parent, i18n.getString("windowTitle"), 0, 0, 64, 23, flags);
e3dfbd23 111
fc2af494
KL
112 setLayoutManager(new StretchLayoutManager(getWidth() - 2,
113 getHeight() - 2));
d8dc8aea 114
e3dfbd23
KL
115 int row = 1;
116
117 // Add some widgets
a69ed767
KL
118 addLabel(i18n.getString("messageBoxLabel"), 1, row);
119 TWidget first = addButton(i18n.getString("messageBoxButton"), 35, row,
2ce6dab2
KL
120 new TAction() {
121 public void DO() {
122 new DemoMsgBoxWindow(getApplication());
e3dfbd23 123 }
2ce6dab2
KL
124 }
125 );
e3dfbd23
KL
126 row += 2;
127
a69ed767
KL
128 addLabel(i18n.getString("openModalLabel"), 1, row);
129 addButton(i18n.getString("openModalButton"), 35, row,
e3dfbd23
KL
130 new TAction() {
131 public void DO() {
132 new DemoMainWindow(getApplication(), MODAL);
133 }
134 }
135 );
e3dfbd23
KL
136 row += 2;
137
a69ed767
KL
138 addLabel(i18n.getString("textFieldLabel"), 1, row);
139 addButton(i18n.getString("textFieldButton"), 35, row,
2ce6dab2
KL
140 new TAction() {
141 public void DO() {
142 new DemoTextFieldWindow(getApplication());
143 }
144 }
145 );
146 row += 2;
e3dfbd23 147
a69ed767
KL
148 addLabel(i18n.getString("radioButtonLabel"), 1, row);
149 addButton(i18n.getString("radioButtonButton"), 35, row,
2ce6dab2
KL
150 new TAction() {
151 public void DO() {
051e2913 152 new DemoCheckBoxWindow(getApplication());
e3dfbd23 153 }
2ce6dab2
KL
154 }
155 );
e3dfbd23
KL
156 row += 2;
157
a69ed767
KL
158 addLabel(i18n.getString("editorLabel"), 1, row);
159 addButton(i18n.getString("editorButton1"), 35, row,
12b55d76
KL
160 new TAction() {
161 public void DO() {
162 new DemoEditorWindow(getApplication());
e3dfbd23 163 }
12b55d76
KL
164 }
165 );
a69ed767 166 addButton(i18n.getString("editorButton2"), 48, row,
71a389c9
KL
167 new TAction() {
168 public void DO() {
169 new TEditorWindow(getApplication());
170 }
171 }
172 );
e3dfbd23 173 row += 2;
e3dfbd23 174
a69ed767
KL
175 addLabel(i18n.getString("textAreaLabel"), 1, row);
176 addButton(i18n.getString("textAreaButton"), 35, row,
2ce6dab2
KL
177 new TAction() {
178 public void DO() {
179 new DemoTextWindow(getApplication());
e3dfbd23 180 }
2ce6dab2
KL
181 }
182 );
1dac6b8d
KL
183 row += 2;
184
185 addLabel(i18n.getString("ttableLabel"), 1, row);
186 addButton(i18n.getString("ttableButton1"), 35, row,
187 new TAction() {
188 public void DO() {
766e7308
KL
189 new DemoTableWindow(getApplication(),
190 i18n.getString("tableWidgetDemo"));
1dac6b8d
KL
191 }
192 }
193 );
194 addButton(i18n.getString("ttableButton2"), 48, row,
195 new TAction() {
196 public void DO() {
197 new TTableWindow(getApplication(),
198 i18n.getString("tableDemo"));
199 }
200 }
201 );
e3dfbd23
KL
202 row += 2;
203
a69ed767
KL
204 addLabel(i18n.getString("treeViewLabel"), 1, row);
205 addButton(i18n.getString("treeViewButton"), 35, row,
2ce6dab2
KL
206 new TAction() {
207 public void DO() {
208 try {
209 new DemoTreeViewWindow(getApplication());
210 } catch (Exception e) {
211 e.printStackTrace();
7668cb45 212 }
e3dfbd23 213 }
2ce6dab2
KL
214 }
215 );
e3dfbd23 216 row += 2;
e3dfbd23 217
a69ed767
KL
218 addLabel(i18n.getString("terminalLabel"), 1, row);
219 addButton(i18n.getString("terminalButton"), 35, row,
2ce6dab2
KL
220 new TAction() {
221 public void DO() {
222 getApplication().openTerminal(0, 0);
e3dfbd23 223 }
2ce6dab2
KL
224 }
225 );
e3dfbd23
KL
226 row += 2;
227
a69ed767
KL
228 addLabel(i18n.getString("colorEditorLabel"), 1, row);
229 addButton(i18n.getString("colorEditorButton"), 35, row,
2ce6dab2
KL
230 new TAction() {
231 public void DO() {
232 new TEditColorThemeWindow(getApplication());
3649b921 233 }
2ce6dab2
KL
234 }
235 );
3649b921 236
3096642b
KL
237 row = 15;
238 progressBar = addProgressBar(48, row, 12, 0);
e3dfbd23 239 row++;
3096642b 240 timerLabel = addLabel(i18n.getString("timerLabel"), 48, row);
e3dfbd23
KL
241 timer = getApplication().addTimer(250, true,
242 new TAction() {
243
244 public void DO() {
a69ed767
KL
245 timerLabel.setLabel(String.format(i18n.
246 getString("timerText"), timerI));
e3dfbd23
KL
247 timerLabel.setWidth(timerLabel.getLabel().length());
248 if (timerI < 100) {
249 timerI++;
be72cb5c
KL
250 } else {
251 timer.setRecurring(false);
e3dfbd23
KL
252 }
253 progressBar.setValue(timerI);
254 }
255 }
256 );
2ce6dab2 257
2b427404
KL
258 /*
259 addButton("Exception", 35, row + 3,
260 new TAction() {
261 public void DO() {
262 try {
263 throw new RuntimeException("FUBAR'd!");
264 } catch (Exception e) {
265 new jexer.TExceptionDialog(getApplication(), e);
266 }
267 }
268 }
269 );
270 */
271
051e2913
KL
272 activate(first);
273
a69ed767
KL
274 statusBar = newStatusBar(i18n.getString("statusBar"));
275 statusBar.addShortcutKeypress(kbF1, cmHelp,
276 i18n.getString("statusBarHelp"));
277 statusBar.addShortcutKeypress(kbF2, cmShell,
278 i18n.getString("statusBarShell"));
279 statusBar.addShortcutKeypress(kbF3, cmOpen,
280 i18n.getString("statusBarOpen"));
281 statusBar.addShortcutKeypress(kbF10, cmExit,
282 i18n.getString("statusBarExit"));
e3dfbd23 283 }
df602ccf 284
43ad7b6c
KL
285 // ------------------------------------------------------------------------
286 // TWindow ----------------------------------------------------------------
287 // ------------------------------------------------------------------------
288
289 /**
290 * We need to override onClose so that the timer will no longer be called
291 * after we close the window. TTimers currently are completely unaware
292 * of the rest of the UI classes.
293 */
294 @Override
295 public void onClose() {
296 getApplication().removeTimer(timer);
297 }
298
df602ccf
KL
299 /**
300 * Method that subclasses can override to handle posted command events.
301 *
302 * @param command command event
303 */
304 @Override
305 public void onCommand(final TCommandEvent command) {
306 if (command.equals(cmOpen)) {
307 try {
308 String filename = fileOpenBox(".");
309 if (filename != null) {
310 try {
a69ed767
KL
311 new TEditorWindow(getApplication(),
312 new File(filename));
df602ccf 313 } catch (IOException e) {
a69ed767
KL
314 messageBox(i18n.getString("errorTitle"),
315 MessageFormat.format(i18n.
316 getString("errorReadingFile"), e.getMessage()));
df602ccf
KL
317 }
318 }
319 } catch (IOException e) {
a69ed767
KL
320 messageBox(i18n.getString("errorTitle"),
321 MessageFormat.format(i18n.
322 getString("errorOpeningFile"), e.getMessage()));
df602ccf
KL
323 }
324 return;
325 }
326
327 // Didn't handle it, let children get it instead
328 super.onCommand(command);
329 }
330
e3dfbd23 331}