| 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 jexer.layout.StretchLayoutManager; |
| 48 | import static jexer.TCommand.*; |
| 49 | import static jexer.TKeypress.*; |
| 50 | |
| 51 | /** |
| 52 | * This is the main "demo" application window. It makes use of the TTimer, |
| 53 | * TProgressBox, TLabel, TButton, and TField widgets. |
| 54 | */ |
| 55 | public class DemoMainWindow extends TWindow { |
| 56 | |
| 57 | /** |
| 58 | * Translated strings. |
| 59 | */ |
| 60 | private static final ResourceBundle i18n = ResourceBundle.getBundle(DemoMainWindow.class.getName()); |
| 61 | |
| 62 | // ------------------------------------------------------------------------ |
| 63 | // Variables -------------------------------------------------------------- |
| 64 | // ------------------------------------------------------------------------ |
| 65 | |
| 66 | /** |
| 67 | * Timer that increments a number. |
| 68 | */ |
| 69 | private TTimer timer1; |
| 70 | |
| 71 | /** |
| 72 | * Timer that increments a number. |
| 73 | */ |
| 74 | private TTimer timer2; |
| 75 | |
| 76 | /** |
| 77 | * Timer label is updated with timer ticks. |
| 78 | */ |
| 79 | TLabel timerLabel; |
| 80 | |
| 81 | /** |
| 82 | * Timer increment used by the timer loop. Has to be at class scope so |
| 83 | * that it can be accessed by the anonymous TAction class. |
| 84 | */ |
| 85 | int timer1I = 0; |
| 86 | |
| 87 | /** |
| 88 | * Timer increment used by the timer loop. Has to be at class scope so |
| 89 | * that it can be accessed by the anonymous TAction class. |
| 90 | */ |
| 91 | int timer2I = 0; |
| 92 | |
| 93 | /** |
| 94 | * Progress bar used by the timer loop. Has to be at class scope so that |
| 95 | * it can be accessed by the anonymous TAction class. |
| 96 | */ |
| 97 | TProgressBar progressBar1; |
| 98 | |
| 99 | /** |
| 100 | * Progress bar used by the timer loop. Has to be at class scope so that |
| 101 | * it can be accessed by the anonymous TAction class. |
| 102 | */ |
| 103 | TProgressBar progressBar2; |
| 104 | |
| 105 | // ------------------------------------------------------------------------ |
| 106 | // Constructors ----------------------------------------------------------- |
| 107 | // ------------------------------------------------------------------------ |
| 108 | |
| 109 | /** |
| 110 | * Construct demo window. It will be centered on screen. |
| 111 | * |
| 112 | * @param parent the main application |
| 113 | */ |
| 114 | public DemoMainWindow(final TApplication parent) { |
| 115 | this(parent, CENTERED | RESIZABLE); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Constructor. |
| 120 | * |
| 121 | * @param parent the main application |
| 122 | * @param flags bitmask of MODAL, CENTERED, or RESIZABLE |
| 123 | */ |
| 124 | private DemoMainWindow(final TApplication parent, final int flags) { |
| 125 | // Construct a demo window. X and Y don't matter because it will be |
| 126 | // centered on screen. |
| 127 | super(parent, i18n.getString("windowTitle"), 0, 0, 64, 23, flags); |
| 128 | |
| 129 | setLayoutManager(new StretchLayoutManager(getWidth() - 2, |
| 130 | getHeight() - 2)); |
| 131 | |
| 132 | int row = 1; |
| 133 | |
| 134 | // Add some widgets |
| 135 | addLabel(i18n.getString("messageBoxLabel"), 1, row); |
| 136 | TWidget first = addButton(i18n.getString("messageBoxButton"), 35, row, |
| 137 | new TAction() { |
| 138 | public void DO() { |
| 139 | new DemoMsgBoxWindow(getApplication()); |
| 140 | } |
| 141 | } |
| 142 | ); |
| 143 | row += 2; |
| 144 | |
| 145 | addLabel(i18n.getString("openModalLabel"), 1, row); |
| 146 | addButton(i18n.getString("openModalButton"), 35, row, |
| 147 | new TAction() { |
| 148 | public void DO() { |
| 149 | new DemoMainWindow(getApplication(), MODAL); |
| 150 | } |
| 151 | } |
| 152 | ); |
| 153 | row += 2; |
| 154 | |
| 155 | addLabel(i18n.getString("textFieldLabel"), 1, row); |
| 156 | addButton(i18n.getString("textFieldButton"), 35, row, |
| 157 | new TAction() { |
| 158 | public void DO() { |
| 159 | new DemoTextFieldWindow(getApplication()); |
| 160 | } |
| 161 | } |
| 162 | ); |
| 163 | row += 2; |
| 164 | |
| 165 | addLabel(i18n.getString("radioButtonLabel"), 1, row); |
| 166 | addButton(i18n.getString("radioButtonButton"), 35, row, |
| 167 | new TAction() { |
| 168 | public void DO() { |
| 169 | new DemoCheckBoxWindow(getApplication()); |
| 170 | } |
| 171 | } |
| 172 | ); |
| 173 | row += 2; |
| 174 | |
| 175 | addLabel(i18n.getString("editorLabel"), 1, row); |
| 176 | addButton(i18n.getString("editorButton1"), 35, row, |
| 177 | new TAction() { |
| 178 | public void DO() { |
| 179 | new DemoEditorWindow(getApplication()); |
| 180 | } |
| 181 | } |
| 182 | ); |
| 183 | addButton(i18n.getString("editorButton2"), 48, row, |
| 184 | new TAction() { |
| 185 | public void DO() { |
| 186 | new TEditorWindow(getApplication()); |
| 187 | } |
| 188 | } |
| 189 | ); |
| 190 | row += 2; |
| 191 | |
| 192 | addLabel(i18n.getString("textAreaLabel"), 1, row); |
| 193 | addButton(i18n.getString("textAreaButton"), 35, row, |
| 194 | new TAction() { |
| 195 | public void DO() { |
| 196 | new DemoTextWindow(getApplication()); |
| 197 | } |
| 198 | } |
| 199 | ); |
| 200 | row += 2; |
| 201 | |
| 202 | addLabel(i18n.getString("ttableLabel"), 1, row); |
| 203 | addButton(i18n.getString("ttableButton1"), 35, row, |
| 204 | new TAction() { |
| 205 | public void DO() { |
| 206 | new DemoTableWindow(getApplication(), |
| 207 | i18n.getString("tableWidgetDemo")); |
| 208 | } |
| 209 | } |
| 210 | ); |
| 211 | addButton(i18n.getString("ttableButton2"), 48, row, |
| 212 | new TAction() { |
| 213 | public void DO() { |
| 214 | new TTableWindow(getApplication(), |
| 215 | i18n.getString("tableDemo")); |
| 216 | } |
| 217 | } |
| 218 | ); |
| 219 | row += 2; |
| 220 | |
| 221 | addLabel(i18n.getString("treeViewLabel"), 1, row); |
| 222 | addButton(i18n.getString("treeViewButton"), 35, row, |
| 223 | new TAction() { |
| 224 | public void DO() { |
| 225 | try { |
| 226 | new DemoTreeViewWindow(getApplication()); |
| 227 | } catch (Exception e) { |
| 228 | e.printStackTrace(); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | ); |
| 233 | row += 2; |
| 234 | |
| 235 | addLabel(i18n.getString("terminalLabel"), 1, row); |
| 236 | addButton(i18n.getString("terminalButton"), 35, row, |
| 237 | new TAction() { |
| 238 | public void DO() { |
| 239 | getApplication().openTerminal(0, 0); |
| 240 | } |
| 241 | } |
| 242 | ); |
| 243 | row += 2; |
| 244 | |
| 245 | addLabel(i18n.getString("colorEditorLabel"), 1, row); |
| 246 | addButton(i18n.getString("colorEditorButton"), 35, row, |
| 247 | new TAction() { |
| 248 | public void DO() { |
| 249 | new TEditColorThemeWindow(getApplication()); |
| 250 | } |
| 251 | } |
| 252 | ); |
| 253 | |
| 254 | row = 15; |
| 255 | progressBar1 = addProgressBar(48, row, 12, 0); |
| 256 | row++; |
| 257 | timerLabel = addLabel(i18n.getString("timerLabel"), 48, row); |
| 258 | timer1 = getApplication().addTimer(250, true, |
| 259 | new TAction() { |
| 260 | |
| 261 | public void DO() { |
| 262 | timerLabel.setLabel(String.format(i18n. |
| 263 | getString("timerText"), timer1I)); |
| 264 | timerLabel.setWidth(timerLabel.getLabel().length()); |
| 265 | if (timer1I < 100) { |
| 266 | timer1I++; |
| 267 | } else { |
| 268 | timer1.setRecurring(false); |
| 269 | } |
| 270 | progressBar1.setValue(timer1I); |
| 271 | } |
| 272 | } |
| 273 | ); |
| 274 | |
| 275 | row += 2; |
| 276 | progressBar2 = addProgressBar(48, row, 12, 0); |
| 277 | progressBar2.setLeftBorderChar('\u255e'); |
| 278 | progressBar2.setRightBorderChar('\u2561'); |
| 279 | progressBar2.setCompletedChar('\u2592'); |
| 280 | progressBar2.setRemainingChar('\u2550'); |
| 281 | row++; |
| 282 | timer2 = getApplication().addTimer(125, true, |
| 283 | new TAction() { |
| 284 | |
| 285 | public void DO() { |
| 286 | if (timer2I < 100) { |
| 287 | timer2I++; |
| 288 | } else { |
| 289 | timer2.setRecurring(false); |
| 290 | } |
| 291 | progressBar2.setValue(timer2I); |
| 292 | } |
| 293 | } |
| 294 | ); |
| 295 | |
| 296 | /* |
| 297 | addButton("Exception", 35, row + 3, |
| 298 | new TAction() { |
| 299 | public void DO() { |
| 300 | try { |
| 301 | throw new RuntimeException("FUBAR'd!"); |
| 302 | } catch (Exception e) { |
| 303 | new jexer.TExceptionDialog(getApplication(), e); |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | ); |
| 308 | */ |
| 309 | |
| 310 | activate(first); |
| 311 | |
| 312 | statusBar = newStatusBar(i18n.getString("statusBar")); |
| 313 | statusBar.addShortcutKeypress(kbF1, cmHelp, |
| 314 | i18n.getString("statusBarHelp")); |
| 315 | statusBar.addShortcutKeypress(kbF2, cmShell, |
| 316 | i18n.getString("statusBarShell")); |
| 317 | statusBar.addShortcutKeypress(kbF3, cmOpen, |
| 318 | i18n.getString("statusBarOpen")); |
| 319 | statusBar.addShortcutKeypress(kbF10, cmExit, |
| 320 | i18n.getString("statusBarExit")); |
| 321 | } |
| 322 | |
| 323 | // ------------------------------------------------------------------------ |
| 324 | // TWindow ---------------------------------------------------------------- |
| 325 | // ------------------------------------------------------------------------ |
| 326 | |
| 327 | /** |
| 328 | * We need to override onClose so that the timer will no longer be called |
| 329 | * after we close the window. TTimers currently are completely unaware |
| 330 | * of the rest of the UI classes. |
| 331 | */ |
| 332 | @Override |
| 333 | public void onClose() { |
| 334 | getApplication().removeTimer(timer1); |
| 335 | getApplication().removeTimer(timer2); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Method that subclasses can override to handle posted command events. |
| 340 | * |
| 341 | * @param command command event |
| 342 | */ |
| 343 | @Override |
| 344 | public void onCommand(final TCommandEvent command) { |
| 345 | if (command.equals(cmOpen)) { |
| 346 | try { |
| 347 | String filename = fileOpenBox("."); |
| 348 | if (filename != null) { |
| 349 | try { |
| 350 | new TEditorWindow(getApplication(), |
| 351 | new File(filename)); |
| 352 | } catch (IOException e) { |
| 353 | messageBox(i18n.getString("errorTitle"), |
| 354 | MessageFormat.format(i18n. |
| 355 | getString("errorReadingFile"), e.getMessage())); |
| 356 | } |
| 357 | } |
| 358 | } catch (IOException e) { |
| 359 | messageBox(i18n.getString("errorTitle"), |
| 360 | MessageFormat.format(i18n. |
| 361 | getString("errorOpeningFile"), e.getMessage())); |
| 362 | } |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | // Didn't handle it, let children get it instead |
| 367 | super.onCommand(command); |
| 368 | } |
| 369 | |
| 370 | } |