Commit | Line | Data |
---|---|---|
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 | */ | |
29 | package jexer.demos; | |
30 | ||
a69ed767 KL |
31 | import java.io.File; |
32 | import java.io.IOException; | |
33 | import java.text.MessageFormat; | |
a69ed767 KL |
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; | |
1dac6b8d | 42 | import jexer.TTableWindow; |
a69ed767 KL |
43 | import jexer.TTimer; |
44 | import jexer.TWidget; | |
45 | import jexer.TWindow; | |
46 | import jexer.event.TCommandEvent; | |
2ce6dab2 KL |
47 | import static jexer.TCommand.*; |
48 | import static jexer.TKeypress.*; | |
e3dfbd23 KL |
49 | |
50 | /** | |
51 | * This is the main "demo" application window. It makes use of the TTimer, | |
52 | * TProgressBox, TLabel, TButton, and TField widgets. | |
53 | */ | |
7668cb45 | 54 | public class DemoMainWindow extends TWindow { |
e3dfbd23 | 55 | |
a69ed767 KL |
56 | /** |
57 | * Translated strings. | |
58 | */ | |
59 | private static final ResourceBundle i18n = ResourceBundle.getBundle(DemoMainWindow.class.getName()); | |
60 | ||
43ad7b6c KL |
61 | // ------------------------------------------------------------------------ |
62 | // Variables -------------------------------------------------------------- | |
63 | // ------------------------------------------------------------------------ | |
64 | ||
65 | /** | |
66 | * Timer that increments a number. | |
67 | */ | |
e3dfbd23 KL |
68 | private TTimer timer; |
69 | ||
43ad7b6c KL |
70 | /** |
71 | * Timer label is updated with timer ticks. | |
72 | */ | |
e3dfbd23 KL |
73 | TLabel timerLabel; |
74 | ||
75 | /** | |
43ad7b6c KL |
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. | |
e3dfbd23 | 78 | */ |
43ad7b6c KL |
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 | // ------------------------------------------------------------------------ | |
e3dfbd23 KL |
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 | ||
e3dfbd23 KL |
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. | |
3096642b | 109 | super(parent, i18n.getString("windowTitle"), 0, 0, 64, 23, flags); |
e3dfbd23 KL |
110 | |
111 | int row = 1; | |
112 | ||
113 | // Add some widgets | |
a69ed767 KL |
114 | addLabel(i18n.getString("messageBoxLabel"), 1, row); |
115 | TWidget first = addButton(i18n.getString("messageBoxButton"), 35, row, | |
2ce6dab2 KL |
116 | new TAction() { |
117 | public void DO() { | |
118 | new DemoMsgBoxWindow(getApplication()); | |
e3dfbd23 | 119 | } |
2ce6dab2 KL |
120 | } |
121 | ); | |
e3dfbd23 KL |
122 | row += 2; |
123 | ||
a69ed767 KL |
124 | addLabel(i18n.getString("openModalLabel"), 1, row); |
125 | addButton(i18n.getString("openModalButton"), 35, row, | |
e3dfbd23 KL |
126 | new TAction() { |
127 | public void DO() { | |
128 | new DemoMainWindow(getApplication(), MODAL); | |
129 | } | |
130 | } | |
131 | ); | |
e3dfbd23 KL |
132 | row += 2; |
133 | ||
a69ed767 KL |
134 | addLabel(i18n.getString("textFieldLabel"), 1, row); |
135 | addButton(i18n.getString("textFieldButton"), 35, row, | |
2ce6dab2 KL |
136 | new TAction() { |
137 | public void DO() { | |
138 | new DemoTextFieldWindow(getApplication()); | |
139 | } | |
140 | } | |
141 | ); | |
142 | row += 2; | |
e3dfbd23 | 143 | |
a69ed767 KL |
144 | addLabel(i18n.getString("radioButtonLabel"), 1, row); |
145 | addButton(i18n.getString("radioButtonButton"), 35, row, | |
2ce6dab2 KL |
146 | new TAction() { |
147 | public void DO() { | |
051e2913 | 148 | new DemoCheckBoxWindow(getApplication()); |
e3dfbd23 | 149 | } |
2ce6dab2 KL |
150 | } |
151 | ); | |
e3dfbd23 KL |
152 | row += 2; |
153 | ||
a69ed767 KL |
154 | addLabel(i18n.getString("editorLabel"), 1, row); |
155 | addButton(i18n.getString("editorButton1"), 35, row, | |
12b55d76 KL |
156 | new TAction() { |
157 | public void DO() { | |
158 | new DemoEditorWindow(getApplication()); | |
e3dfbd23 | 159 | } |
12b55d76 KL |
160 | } |
161 | ); | |
a69ed767 | 162 | addButton(i18n.getString("editorButton2"), 48, row, |
71a389c9 KL |
163 | new TAction() { |
164 | public void DO() { | |
165 | new TEditorWindow(getApplication()); | |
166 | } | |
167 | } | |
168 | ); | |
e3dfbd23 | 169 | row += 2; |
e3dfbd23 | 170 | |
a69ed767 KL |
171 | addLabel(i18n.getString("textAreaLabel"), 1, row); |
172 | addButton(i18n.getString("textAreaButton"), 35, row, | |
2ce6dab2 KL |
173 | new TAction() { |
174 | public void DO() { | |
175 | new DemoTextWindow(getApplication()); | |
e3dfbd23 | 176 | } |
2ce6dab2 KL |
177 | } |
178 | ); | |
1dac6b8d KL |
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 | // TODO | |
186 | } | |
187 | } | |
188 | ); | |
189 | addButton(i18n.getString("ttableButton2"), 48, row, | |
190 | new TAction() { | |
191 | public void DO() { | |
192 | new TTableWindow(getApplication(), | |
193 | i18n.getString("tableDemo")); | |
194 | } | |
195 | } | |
196 | ); | |
e3dfbd23 KL |
197 | row += 2; |
198 | ||
a69ed767 KL |
199 | addLabel(i18n.getString("treeViewLabel"), 1, row); |
200 | addButton(i18n.getString("treeViewButton"), 35, row, | |
2ce6dab2 KL |
201 | new TAction() { |
202 | public void DO() { | |
203 | try { | |
204 | new DemoTreeViewWindow(getApplication()); | |
205 | } catch (Exception e) { | |
206 | e.printStackTrace(); | |
7668cb45 | 207 | } |
e3dfbd23 | 208 | } |
2ce6dab2 KL |
209 | } |
210 | ); | |
e3dfbd23 | 211 | row += 2; |
e3dfbd23 | 212 | |
a69ed767 KL |
213 | addLabel(i18n.getString("terminalLabel"), 1, row); |
214 | addButton(i18n.getString("terminalButton"), 35, row, | |
2ce6dab2 KL |
215 | new TAction() { |
216 | public void DO() { | |
217 | getApplication().openTerminal(0, 0); | |
e3dfbd23 | 218 | } |
2ce6dab2 KL |
219 | } |
220 | ); | |
e3dfbd23 KL |
221 | row += 2; |
222 | ||
a69ed767 KL |
223 | addLabel(i18n.getString("colorEditorLabel"), 1, row); |
224 | addButton(i18n.getString("colorEditorButton"), 35, row, | |
2ce6dab2 KL |
225 | new TAction() { |
226 | public void DO() { | |
227 | new TEditColorThemeWindow(getApplication()); | |
3649b921 | 228 | } |
2ce6dab2 KL |
229 | } |
230 | ); | |
3649b921 | 231 | |
3096642b KL |
232 | row = 15; |
233 | progressBar = addProgressBar(48, row, 12, 0); | |
e3dfbd23 | 234 | row++; |
3096642b | 235 | timerLabel = addLabel(i18n.getString("timerLabel"), 48, row); |
e3dfbd23 KL |
236 | timer = getApplication().addTimer(250, true, |
237 | new TAction() { | |
238 | ||
239 | public void DO() { | |
a69ed767 KL |
240 | timerLabel.setLabel(String.format(i18n. |
241 | getString("timerText"), timerI)); | |
e3dfbd23 KL |
242 | timerLabel.setWidth(timerLabel.getLabel().length()); |
243 | if (timerI < 100) { | |
244 | timerI++; | |
be72cb5c KL |
245 | } else { |
246 | timer.setRecurring(false); | |
e3dfbd23 KL |
247 | } |
248 | progressBar.setValue(timerI); | |
249 | } | |
250 | } | |
251 | ); | |
2ce6dab2 | 252 | |
051e2913 KL |
253 | activate(first); |
254 | ||
a69ed767 KL |
255 | statusBar = newStatusBar(i18n.getString("statusBar")); |
256 | statusBar.addShortcutKeypress(kbF1, cmHelp, | |
257 | i18n.getString("statusBarHelp")); | |
258 | statusBar.addShortcutKeypress(kbF2, cmShell, | |
259 | i18n.getString("statusBarShell")); | |
260 | statusBar.addShortcutKeypress(kbF3, cmOpen, | |
261 | i18n.getString("statusBarOpen")); | |
262 | statusBar.addShortcutKeypress(kbF10, cmExit, | |
263 | i18n.getString("statusBarExit")); | |
e3dfbd23 | 264 | } |
df602ccf | 265 | |
43ad7b6c KL |
266 | // ------------------------------------------------------------------------ |
267 | // TWindow ---------------------------------------------------------------- | |
268 | // ------------------------------------------------------------------------ | |
269 | ||
270 | /** | |
271 | * We need to override onClose so that the timer will no longer be called | |
272 | * after we close the window. TTimers currently are completely unaware | |
273 | * of the rest of the UI classes. | |
274 | */ | |
275 | @Override | |
276 | public void onClose() { | |
277 | getApplication().removeTimer(timer); | |
278 | } | |
279 | ||
df602ccf KL |
280 | /** |
281 | * Method that subclasses can override to handle posted command events. | |
282 | * | |
283 | * @param command command event | |
284 | */ | |
285 | @Override | |
286 | public void onCommand(final TCommandEvent command) { | |
287 | if (command.equals(cmOpen)) { | |
288 | try { | |
289 | String filename = fileOpenBox("."); | |
290 | if (filename != null) { | |
291 | try { | |
a69ed767 KL |
292 | new TEditorWindow(getApplication(), |
293 | new File(filename)); | |
df602ccf | 294 | } catch (IOException e) { |
a69ed767 KL |
295 | messageBox(i18n.getString("errorTitle"), |
296 | MessageFormat.format(i18n. | |
297 | getString("errorReadingFile"), e.getMessage())); | |
df602ccf KL |
298 | } |
299 | } | |
300 | } catch (IOException e) { | |
a69ed767 KL |
301 | messageBox(i18n.getString("errorTitle"), |
302 | MessageFormat.format(i18n. | |
303 | getString("errorOpeningFile"), e.getMessage())); | |
df602ccf KL |
304 | } |
305 | return; | |
306 | } | |
307 | ||
308 | // Didn't handle it, let children get it instead | |
309 | super.onCommand(command); | |
310 | } | |
311 | ||
e3dfbd23 | 312 | } |