f1cbe2122cdbb54ee2088a3fbed979d805cecfdd
[fanfix.git] / src / jexer / demos / DemoMainWindow.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2017 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.*;
32 import java.util.*;
33
34 import jexer.*;
35 import jexer.event.*;
36 import static jexer.TCommand.*;
37 import static jexer.TKeypress.*;
38
39 /**
40 * This is the main "demo" application window. It makes use of the TTimer,
41 * TProgressBox, TLabel, TButton, and TField widgets.
42 */
43 public class DemoMainWindow extends TWindow {
44
45 // ------------------------------------------------------------------------
46 // Variables --------------------------------------------------------------
47 // ------------------------------------------------------------------------
48
49 /**
50 * Timer that increments a number.
51 */
52 private TTimer timer;
53
54 /**
55 * Timer label is updated with timer ticks.
56 */
57 TLabel timerLabel;
58
59 /**
60 * Timer increment used by the timer loop. Has to be at class scope so
61 * that it can be accessed by the anonymous TAction class.
62 */
63 int timerI = 0;
64
65 /**
66 * Progress bar used by the timer loop. Has to be at class scope so that
67 * it can be accessed by the anonymous TAction class.
68 */
69 TProgressBar progressBar;
70
71 /**
72 * Day of week label is updated with TSpinner clicks.
73 */
74 TLabel dayOfWeekLabel;
75
76 /**
77 * Day of week to demonstrate TSpinner. Has to be at class scope so that
78 * it can be accessed by the anonymous TAction class.
79 */
80 GregorianCalendar calendar = new GregorianCalendar();
81
82 // ------------------------------------------------------------------------
83 // Constructors -----------------------------------------------------------
84 // ------------------------------------------------------------------------
85
86 /**
87 * Construct demo window. It will be centered on screen.
88 *
89 * @param parent the main application
90 */
91 public DemoMainWindow(final TApplication parent) {
92 this(parent, CENTERED | RESIZABLE);
93 }
94
95 /**
96 * Constructor.
97 *
98 * @param parent the main application
99 * @param flags bitmask of MODAL, CENTERED, or RESIZABLE
100 */
101 private DemoMainWindow(final TApplication parent, final int flags) {
102 // Construct a demo window. X and Y don't matter because it will be
103 // centered on screen.
104 super(parent, "Demo Window", 0, 0, 64, 23, flags);
105
106 int row = 1;
107
108 // Add some widgets
109 addLabel("Message Boxes", 1, row);
110 TWidget first = addButton("&MessageBoxes", 35, row,
111 new TAction() {
112 public void DO() {
113 new DemoMsgBoxWindow(getApplication());
114 }
115 }
116 );
117 row += 2;
118
119 addLabel("Open me as modal", 1, row);
120 addButton("W&indow", 35, row,
121 new TAction() {
122 public void DO() {
123 new DemoMainWindow(getApplication(), MODAL);
124 }
125 }
126 );
127 row += 2;
128
129 addLabel("Text fields and calendar", 1, row);
130 addButton("Field&s", 35, row,
131 new TAction() {
132 public void DO() {
133 new DemoTextFieldWindow(getApplication());
134 }
135 }
136 );
137 row += 2;
138
139 addLabel("Radio buttons, check and combobox", 1, row);
140 addButton("&CheckBoxes", 35, row,
141 new TAction() {
142 public void DO() {
143 new DemoCheckBoxWindow(getApplication());
144 }
145 }
146 );
147 row += 2;
148
149 addLabel("Editor window", 1, row);
150 addButton("&1 Widget", 35, row,
151 new TAction() {
152 public void DO() {
153 new DemoEditorWindow(getApplication());
154 }
155 }
156 );
157 addButton("&2 Window", 48, row,
158 new TAction() {
159 public void DO() {
160 new TEditorWindow(getApplication());
161 }
162 }
163 );
164 row += 2;
165
166 addLabel("Text areas", 1, row);
167 addButton("&Text", 35, row,
168 new TAction() {
169 public void DO() {
170 new DemoTextWindow(getApplication());
171 }
172 }
173 );
174 row += 2;
175
176 addLabel("Tree views", 1, row);
177 addButton("Tree&View", 35, row,
178 new TAction() {
179 public void DO() {
180 try {
181 new DemoTreeViewWindow(getApplication());
182 } catch (Exception e) {
183 e.printStackTrace();
184 }
185 }
186 }
187 );
188 row += 2;
189
190 addLabel("Terminal", 1, row);
191 addButton("Termi&nal", 35, row,
192 new TAction() {
193 public void DO() {
194 getApplication().openTerminal(0, 0);
195 }
196 }
197 );
198 row += 2;
199
200 addLabel("Color editor", 1, row);
201 addButton("Co&lors", 35, row,
202 new TAction() {
203 public void DO() {
204 new TEditColorThemeWindow(getApplication());
205 }
206 }
207 );
208 row += 2;
209
210 progressBar = addProgressBar(1, row, 22, 0);
211 row++;
212 timerLabel = addLabel("Timer", 1, row);
213 timer = getApplication().addTimer(250, true,
214 new TAction() {
215
216 public void DO() {
217 timerLabel.setLabel(String.format("Timer: %d", timerI));
218 timerLabel.setWidth(timerLabel.getLabel().length());
219 if (timerI < 100) {
220 timerI++;
221 } else {
222 timer.setRecurring(false);
223 }
224 progressBar.setValue(timerI);
225 }
226 }
227 );
228
229 dayOfWeekLabel = addLabel("Wednesday-", 35, row - 1, "tmenu", false);
230 dayOfWeekLabel.setLabel(String.format("%-10s",
231 calendar.getDisplayName(Calendar.DAY_OF_WEEK,
232 Calendar.LONG, Locale.getDefault())));
233
234 addSpinner(35 + dayOfWeekLabel.getWidth(), row - 1,
235 new TAction() {
236 public void DO() {
237 calendar.add(Calendar.DAY_OF_WEEK, 1);
238 dayOfWeekLabel.setLabel(String.format("%-10s",
239 calendar.getDisplayName(
240 Calendar.DAY_OF_WEEK, Calendar.LONG,
241 Locale.getDefault())));
242 }
243 },
244 new TAction() {
245 public void DO() {
246 calendar.add(Calendar.DAY_OF_WEEK, -1);
247 dayOfWeekLabel.setLabel(String.format("%-10s",
248 calendar.getDisplayName(
249 Calendar.DAY_OF_WEEK, Calendar.LONG,
250 Locale.getDefault())));
251 }
252 }
253 );
254
255
256 activate(first);
257
258 statusBar = newStatusBar("Demo Main Window");
259 statusBar.addShortcutKeypress(kbF1, cmHelp, "Help");
260 statusBar.addShortcutKeypress(kbF2, cmShell, "Shell");
261 statusBar.addShortcutKeypress(kbF3, cmOpen, "Open");
262 statusBar.addShortcutKeypress(kbF10, cmExit, "Exit");
263 }
264
265 // ------------------------------------------------------------------------
266 // TWindow ----------------------------------------------------------------
267 // ------------------------------------------------------------------------
268
269 /**
270 * We need to override onClose so that the timer will no longer be called
271 * after we close the window. TTimers currently are completely unaware
272 * of the rest of the UI classes.
273 */
274 @Override
275 public void onClose() {
276 getApplication().removeTimer(timer);
277 }
278
279 /**
280 * Method that subclasses can override to handle posted command events.
281 *
282 * @param command command event
283 */
284 @Override
285 public void onCommand(final TCommandEvent command) {
286 if (command.equals(cmOpen)) {
287 try {
288 String filename = fileOpenBox(".");
289 if (filename != null) {
290 try {
291 new TEditorWindow(getApplication(), new File(filename));
292 } catch (IOException e) {
293 messageBox("Error", "Error reading file: " +
294 e.getMessage());
295 }
296 }
297 } catch (IOException e) {
298 messageBox("Error", "Error opening file dialog: " +
299 e.getMessage());
300 }
301 return;
302 }
303
304 // Didn't handle it, let children get it instead
305 super.onCommand(command);
306 }
307
308 }