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