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