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