Merge branch 'master' of https://github.com/klamonte/jexer
[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
KL
31import java.io.*;
32
e3dfbd23 33import jexer.*;
df602ccf 34import jexer.event.*;
2ce6dab2
KL
35import static jexer.TCommand.*;
36import static jexer.TKeypress.*;
e3dfbd23
KL
37
38/**
39 * This is the main "demo" application window. It makes use of the TTimer,
40 * TProgressBox, TLabel, TButton, and TField widgets.
41 */
7668cb45 42public class DemoMainWindow extends TWindow {
e3dfbd23
KL
43
44 // Timer that increments a number.
45 private TTimer timer;
46
47 // Timer label is updated with timer ticks.
48 TLabel timerLabel;
49
50 /**
51 * We need to override onClose so that the timer will no longer be called
52 * after we close the window. TTimers currently are completely unaware
53 * of the rest of the UI classes.
54 */
55 @Override
56 public void onClose() {
57 getApplication().removeTimer(timer);
58 }
59
60 /**
61 * Construct demo window. It will be centered on screen.
62 *
63 * @param parent the main application
64 */
65 public DemoMainWindow(final TApplication parent) {
66 this(parent, CENTERED | RESIZABLE);
67 }
68
69 // These are used by the timer loop. They have to be at class scope so
70 // that they can be accessed by the anonymous TAction class.
71 int timerI = 0;
72 TProgressBar progressBar;
73
74 /**
75 * Constructor.
76 *
77 * @param parent the main application
78 * @param flags bitmask of MODAL, CENTERED, or RESIZABLE
79 */
80 private DemoMainWindow(final TApplication parent, final int flags) {
81 // Construct a demo window. X and Y don't matter because it will be
82 // centered on screen.
71a389c9 83 super(parent, "Demo Window", 0, 0, 64, 23, flags);
e3dfbd23
KL
84
85 int row = 1;
86
87 // Add some widgets
2ce6dab2
KL
88 addLabel("Message Boxes", 1, row);
89 addButton("&MessageBoxes", 35, row,
90 new TAction() {
91 public void DO() {
92 new DemoMsgBoxWindow(getApplication());
e3dfbd23 93 }
2ce6dab2
KL
94 }
95 );
e3dfbd23
KL
96 row += 2;
97
98 addLabel("Open me as modal", 1, row);
99 addButton("W&indow", 35, row,
100 new TAction() {
101 public void DO() {
102 new DemoMainWindow(getApplication(), MODAL);
103 }
104 }
105 );
e3dfbd23
KL
106 row += 2;
107
2ce6dab2
KL
108 addLabel("Text fields", 1, row);
109 addButton("Field&s", 35, row,
110 new TAction() {
111 public void DO() {
112 new DemoTextFieldWindow(getApplication());
113 }
114 }
115 );
116 row += 2;
e3dfbd23 117
2ce6dab2
KL
118 addLabel("Radio buttons and checkboxes", 1, row);
119 addButton("&Checkboxes", 35, row,
120 new TAction() {
121 public void DO() {
122 new DemoCheckboxWindow(getApplication());
e3dfbd23 123 }
2ce6dab2
KL
124 }
125 );
e3dfbd23
KL
126 row += 2;
127
12b55d76 128 addLabel("Editor window", 1, row);
71a389c9 129 addButton("&1 Widget", 35, row,
12b55d76
KL
130 new TAction() {
131 public void DO() {
132 new DemoEditorWindow(getApplication());
e3dfbd23 133 }
12b55d76
KL
134 }
135 );
71a389c9
KL
136 addButton("&2 Window", 48, row,
137 new TAction() {
138 public void DO() {
139 new TEditorWindow(getApplication());
140 }
141 }
142 );
e3dfbd23 143 row += 2;
e3dfbd23 144
2ce6dab2
KL
145 addLabel("Text areas", 1, row);
146 addButton("&Text", 35, row,
147 new TAction() {
148 public void DO() {
149 new DemoTextWindow(getApplication());
e3dfbd23 150 }
2ce6dab2
KL
151 }
152 );
e3dfbd23
KL
153 row += 2;
154
2ce6dab2
KL
155 addLabel("Tree views", 1, row);
156 addButton("Tree&View", 35, row,
157 new TAction() {
158 public void DO() {
159 try {
160 new DemoTreeViewWindow(getApplication());
161 } catch (Exception e) {
162 e.printStackTrace();
7668cb45 163 }
e3dfbd23 164 }
2ce6dab2
KL
165 }
166 );
e3dfbd23 167 row += 2;
e3dfbd23 168
2ce6dab2
KL
169 addLabel("Terminal", 1, row);
170 addButton("Termi&nal", 35, row,
171 new TAction() {
172 public void DO() {
173 getApplication().openTerminal(0, 0);
e3dfbd23 174 }
2ce6dab2
KL
175 }
176 );
e3dfbd23
KL
177 row += 2;
178
2ce6dab2
KL
179 addLabel("Color editor", 1, row);
180 addButton("Co&lors", 35, row,
181 new TAction() {
182 public void DO() {
183 new TEditColorThemeWindow(getApplication());
3649b921 184 }
2ce6dab2
KL
185 }
186 );
3649b921
KL
187 row += 2;
188
e3dfbd23
KL
189 progressBar = addProgressBar(1, row, 22, 0);
190 row++;
191 timerLabel = addLabel("Timer", 1, row);
192 timer = getApplication().addTimer(250, true,
193 new TAction() {
194
195 public void DO() {
196 timerLabel.setLabel(String.format("Timer: %d", timerI));
197 timerLabel.setWidth(timerLabel.getLabel().length());
198 if (timerI < 100) {
199 timerI++;
be72cb5c
KL
200 } else {
201 timer.setRecurring(false);
e3dfbd23
KL
202 }
203 progressBar.setValue(timerI);
204 }
205 }
206 );
2ce6dab2
KL
207
208 statusBar = newStatusBar("Demo Main Window");
209 statusBar.addShortcutKeypress(kbF1, cmHelp, "Help");
210 statusBar.addShortcutKeypress(kbF2, cmShell, "Shell");
211 statusBar.addShortcutKeypress(kbF3, cmOpen, "Open");
212 statusBar.addShortcutKeypress(kbF10, cmExit, "Exit");
e3dfbd23 213 }
df602ccf
KL
214
215 /**
216 * Method that subclasses can override to handle posted command events.
217 *
218 * @param command command event
219 */
220 @Override
221 public void onCommand(final TCommandEvent command) {
222 if (command.equals(cmOpen)) {
223 try {
224 String filename = fileOpenBox(".");
225 if (filename != null) {
226 try {
227 new TEditorWindow(getApplication(), new File(filename));
228 } catch (IOException e) {
229 messageBox("Error", "Error reading file: " +
230 e.getMessage());
231 }
232 }
233 } catch (IOException e) {
234 messageBox("Error", "Error opening file dialog: " +
235 e.getMessage());
236 }
237 return;
238 }
239
240 // Didn't handle it, let children get it instead
241 super.onCommand(command);
242 }
243
e3dfbd23 244}