resizing fixes
[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
d8dc8aea
KL
112 setLayoutManager(new StretchLayoutManager(getWidth(), getHeight()));
113
e3dfbd23
KL
114 int row = 1;
115
116 // Add some widgets
a69ed767
KL
117 addLabel(i18n.getString("messageBoxLabel"), 1, row);
118 TWidget first = addButton(i18n.getString("messageBoxButton"), 35, row,
2ce6dab2
KL
119 new TAction() {
120 public void DO() {
121 new DemoMsgBoxWindow(getApplication());
e3dfbd23 122 }
2ce6dab2
KL
123 }
124 );
e3dfbd23
KL
125 row += 2;
126
a69ed767
KL
127 addLabel(i18n.getString("openModalLabel"), 1, row);
128 addButton(i18n.getString("openModalButton"), 35, row,
e3dfbd23
KL
129 new TAction() {
130 public void DO() {
131 new DemoMainWindow(getApplication(), MODAL);
132 }
133 }
134 );
e3dfbd23
KL
135 row += 2;
136
a69ed767
KL
137 addLabel(i18n.getString("textFieldLabel"), 1, row);
138 addButton(i18n.getString("textFieldButton"), 35, row,
2ce6dab2
KL
139 new TAction() {
140 public void DO() {
141 new DemoTextFieldWindow(getApplication());
142 }
143 }
144 );
145 row += 2;
e3dfbd23 146
a69ed767
KL
147 addLabel(i18n.getString("radioButtonLabel"), 1, row);
148 addButton(i18n.getString("radioButtonButton"), 35, row,
2ce6dab2
KL
149 new TAction() {
150 public void DO() {
051e2913 151 new DemoCheckBoxWindow(getApplication());
e3dfbd23 152 }
2ce6dab2
KL
153 }
154 );
e3dfbd23
KL
155 row += 2;
156
a69ed767
KL
157 addLabel(i18n.getString("editorLabel"), 1, row);
158 addButton(i18n.getString("editorButton1"), 35, row,
12b55d76
KL
159 new TAction() {
160 public void DO() {
161 new DemoEditorWindow(getApplication());
e3dfbd23 162 }
12b55d76
KL
163 }
164 );
a69ed767 165 addButton(i18n.getString("editorButton2"), 48, row,
71a389c9
KL
166 new TAction() {
167 public void DO() {
168 new TEditorWindow(getApplication());
169 }
170 }
171 );
e3dfbd23 172 row += 2;
e3dfbd23 173
a69ed767
KL
174 addLabel(i18n.getString("textAreaLabel"), 1, row);
175 addButton(i18n.getString("textAreaButton"), 35, row,
2ce6dab2
KL
176 new TAction() {
177 public void DO() {
178 new DemoTextWindow(getApplication());
e3dfbd23 179 }
2ce6dab2
KL
180 }
181 );
1dac6b8d
KL
182 row += 2;
183
184 addLabel(i18n.getString("ttableLabel"), 1, row);
185 addButton(i18n.getString("ttableButton1"), 35, row,
186 new TAction() {
187 public void DO() {
766e7308
KL
188 new DemoTableWindow(getApplication(),
189 i18n.getString("tableWidgetDemo"));
1dac6b8d
KL
190 }
191 }
192 );
193 addButton(i18n.getString("ttableButton2"), 48, row,
194 new TAction() {
195 public void DO() {
196 new TTableWindow(getApplication(),
197 i18n.getString("tableDemo"));
198 }
199 }
200 );
e3dfbd23
KL
201 row += 2;
202
a69ed767
KL
203 addLabel(i18n.getString("treeViewLabel"), 1, row);
204 addButton(i18n.getString("treeViewButton"), 35, row,
2ce6dab2
KL
205 new TAction() {
206 public void DO() {
207 try {
208 new DemoTreeViewWindow(getApplication());
209 } catch (Exception e) {
210 e.printStackTrace();
7668cb45 211 }
e3dfbd23 212 }
2ce6dab2
KL
213 }
214 );
e3dfbd23 215 row += 2;
e3dfbd23 216
a69ed767
KL
217 addLabel(i18n.getString("terminalLabel"), 1, row);
218 addButton(i18n.getString("terminalButton"), 35, row,
2ce6dab2
KL
219 new TAction() {
220 public void DO() {
221 getApplication().openTerminal(0, 0);
e3dfbd23 222 }
2ce6dab2
KL
223 }
224 );
e3dfbd23
KL
225 row += 2;
226
a69ed767
KL
227 addLabel(i18n.getString("colorEditorLabel"), 1, row);
228 addButton(i18n.getString("colorEditorButton"), 35, row,
2ce6dab2
KL
229 new TAction() {
230 public void DO() {
231 new TEditColorThemeWindow(getApplication());
3649b921 232 }
2ce6dab2
KL
233 }
234 );
3649b921 235
3096642b
KL
236 row = 15;
237 progressBar = addProgressBar(48, row, 12, 0);
e3dfbd23 238 row++;
3096642b 239 timerLabel = addLabel(i18n.getString("timerLabel"), 48, row);
e3dfbd23
KL
240 timer = getApplication().addTimer(250, true,
241 new TAction() {
242
243 public void DO() {
a69ed767
KL
244 timerLabel.setLabel(String.format(i18n.
245 getString("timerText"), timerI));
e3dfbd23
KL
246 timerLabel.setWidth(timerLabel.getLabel().length());
247 if (timerI < 100) {
248 timerI++;
be72cb5c
KL
249 } else {
250 timer.setRecurring(false);
e3dfbd23
KL
251 }
252 progressBar.setValue(timerI);
253 }
254 }
255 );
2ce6dab2 256
2b427404
KL
257 /*
258 addButton("Exception", 35, row + 3,
259 new TAction() {
260 public void DO() {
261 try {
262 throw new RuntimeException("FUBAR'd!");
263 } catch (Exception e) {
264 new jexer.TExceptionDialog(getApplication(), e);
265 }
266 }
267 }
268 );
269 */
270
051e2913
KL
271 activate(first);
272
a69ed767
KL
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"));
e3dfbd23 282 }
df602ccf 283
43ad7b6c
KL
284 // ------------------------------------------------------------------------
285 // TWindow ----------------------------------------------------------------
286 // ------------------------------------------------------------------------
287
288 /**
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.
292 */
293 @Override
294 public void onClose() {
295 getApplication().removeTimer(timer);
296 }
297
df602ccf
KL
298 /**
299 * Method that subclasses can override to handle posted command events.
300 *
301 * @param command command event
302 */
303 @Override
304 public void onCommand(final TCommandEvent command) {
305 if (command.equals(cmOpen)) {
306 try {
307 String filename = fileOpenBox(".");
308 if (filename != null) {
309 try {
a69ed767
KL
310 new TEditorWindow(getApplication(),
311 new File(filename));
df602ccf 312 } catch (IOException e) {
a69ed767
KL
313 messageBox(i18n.getString("errorTitle"),
314 MessageFormat.format(i18n.
315 getString("errorReadingFile"), e.getMessage()));
df602ccf
KL
316 }
317 }
318 } catch (IOException e) {
a69ed767
KL
319 messageBox(i18n.getString("errorTitle"),
320 MessageFormat.format(i18n.
321 getString("errorOpeningFile"), e.getMessage()));
df602ccf
KL
322 }
323 return;
324 }
325
326 // Didn't handle it, let children get it instead
327 super.onCommand(command);
328 }
329
e3dfbd23 330}