Not dead note
[nikiroo-utils.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 *
a2018e99 6 * Copyright (C) 2017 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
df602ccf 31import java.io.*;
051e2913 32import java.util.*;
df602ccf 33
e3dfbd23 34import jexer.*;
df602ccf 35import jexer.event.*;
2ce6dab2
KL
36import static jexer.TCommand.*;
37import static jexer.TKeypress.*;
e3dfbd23
KL
38
39/**
40 * This is the main "demo" application window. It makes use of the TTimer,
41 * TProgressBox, TLabel, TButton, and TField widgets.
42 */
7668cb45 43public class DemoMainWindow extends TWindow {
e3dfbd23 44
43ad7b6c
KL
45 // ------------------------------------------------------------------------
46 // Variables --------------------------------------------------------------
47 // ------------------------------------------------------------------------
48
49 /**
50 * Timer that increments a number.
51 */
e3dfbd23
KL
52 private TTimer timer;
53
43ad7b6c
KL
54 /**
55 * Timer label is updated with timer ticks.
56 */
e3dfbd23
KL
57 TLabel timerLabel;
58
59 /**
43ad7b6c
KL
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.
e3dfbd23 62 */
43ad7b6c
KL
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
051e2913
KL
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
43ad7b6c
KL
82 // ------------------------------------------------------------------------
83 // Constructors -----------------------------------------------------------
84 // ------------------------------------------------------------------------
e3dfbd23
KL
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
e3dfbd23
KL
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.
71a389c9 104 super(parent, "Demo Window", 0, 0, 64, 23, flags);
e3dfbd23
KL
105
106 int row = 1;
107
108 // Add some widgets
2ce6dab2 109 addLabel("Message Boxes", 1, row);
051e2913 110 TWidget first = addButton("&MessageBoxes", 35, row,
2ce6dab2
KL
111 new TAction() {
112 public void DO() {
113 new DemoMsgBoxWindow(getApplication());
e3dfbd23 114 }
2ce6dab2
KL
115 }
116 );
e3dfbd23
KL
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 );
e3dfbd23
KL
127 row += 2;
128
051e2913 129 addLabel("Text fields and calendar", 1, row);
2ce6dab2
KL
130 addButton("Field&s", 35, row,
131 new TAction() {
132 public void DO() {
133 new DemoTextFieldWindow(getApplication());
134 }
135 }
136 );
137 row += 2;
e3dfbd23 138
051e2913
KL
139 addLabel("Radio buttons, check and combobox", 1, row);
140 addButton("&CheckBoxes", 35, row,
2ce6dab2
KL
141 new TAction() {
142 public void DO() {
051e2913 143 new DemoCheckBoxWindow(getApplication());
e3dfbd23 144 }
2ce6dab2
KL
145 }
146 );
e3dfbd23
KL
147 row += 2;
148
12b55d76 149 addLabel("Editor window", 1, row);
71a389c9 150 addButton("&1 Widget", 35, row,
12b55d76
KL
151 new TAction() {
152 public void DO() {
153 new DemoEditorWindow(getApplication());
e3dfbd23 154 }
12b55d76
KL
155 }
156 );
71a389c9
KL
157 addButton("&2 Window", 48, row,
158 new TAction() {
159 public void DO() {
160 new TEditorWindow(getApplication());
161 }
162 }
163 );
e3dfbd23 164 row += 2;
e3dfbd23 165
2ce6dab2
KL
166 addLabel("Text areas", 1, row);
167 addButton("&Text", 35, row,
168 new TAction() {
169 public void DO() {
170 new DemoTextWindow(getApplication());
e3dfbd23 171 }
2ce6dab2
KL
172 }
173 );
e3dfbd23
KL
174 row += 2;
175
2ce6dab2
KL
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();
7668cb45 184 }
e3dfbd23 185 }
2ce6dab2
KL
186 }
187 );
e3dfbd23 188 row += 2;
e3dfbd23 189
2ce6dab2
KL
190 addLabel("Terminal", 1, row);
191 addButton("Termi&nal", 35, row,
192 new TAction() {
193 public void DO() {
194 getApplication().openTerminal(0, 0);
e3dfbd23 195 }
2ce6dab2
KL
196 }
197 );
e3dfbd23
KL
198 row += 2;
199
2ce6dab2
KL
200 addLabel("Color editor", 1, row);
201 addButton("Co&lors", 35, row,
202 new TAction() {
203 public void DO() {
204 new TEditColorThemeWindow(getApplication());
3649b921 205 }
2ce6dab2
KL
206 }
207 );
3649b921
KL
208 row += 2;
209
e3dfbd23
KL
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++;
be72cb5c
KL
221 } else {
222 timer.setRecurring(false);
e3dfbd23
KL
223 }
224 progressBar.setValue(timerI);
225 }
226 }
227 );
2ce6dab2 228
051e2913
KL
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
2ce6dab2
KL
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");
e3dfbd23 263 }
df602ccf 264
43ad7b6c
KL
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
df602ccf
KL
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
e3dfbd23 308}