| 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 | // 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. |
| 83 | super(parent, "Demo Window", 0, 0, 64, 23, flags); |
| 84 | |
| 85 | int row = 1; |
| 86 | |
| 87 | // Add some widgets |
| 88 | addLabel("Message Boxes", 1, row); |
| 89 | addButton("&MessageBoxes", 35, row, |
| 90 | new TAction() { |
| 91 | public void DO() { |
| 92 | new DemoMsgBoxWindow(getApplication()); |
| 93 | } |
| 94 | } |
| 95 | ); |
| 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 | ); |
| 106 | row += 2; |
| 107 | |
| 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; |
| 117 | |
| 118 | addLabel("Radio buttons and checkboxes", 1, row); |
| 119 | addButton("&Checkboxes", 35, row, |
| 120 | new TAction() { |
| 121 | public void DO() { |
| 122 | new DemoCheckboxWindow(getApplication()); |
| 123 | } |
| 124 | } |
| 125 | ); |
| 126 | row += 2; |
| 127 | |
| 128 | addLabel("Editor window", 1, row); |
| 129 | addButton("&1 Widget", 35, row, |
| 130 | new TAction() { |
| 131 | public void DO() { |
| 132 | new DemoEditorWindow(getApplication()); |
| 133 | } |
| 134 | } |
| 135 | ); |
| 136 | addButton("&2 Window", 48, row, |
| 137 | new TAction() { |
| 138 | public void DO() { |
| 139 | new TEditorWindow(getApplication()); |
| 140 | } |
| 141 | } |
| 142 | ); |
| 143 | row += 2; |
| 144 | |
| 145 | addLabel("Text areas", 1, row); |
| 146 | addButton("&Text", 35, row, |
| 147 | new TAction() { |
| 148 | public void DO() { |
| 149 | new DemoTextWindow(getApplication()); |
| 150 | } |
| 151 | } |
| 152 | ); |
| 153 | row += 2; |
| 154 | |
| 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(); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | ); |
| 167 | row += 2; |
| 168 | |
| 169 | addLabel("Terminal", 1, row); |
| 170 | addButton("Termi&nal", 35, row, |
| 171 | new TAction() { |
| 172 | public void DO() { |
| 173 | getApplication().openTerminal(0, 0); |
| 174 | } |
| 175 | } |
| 176 | ); |
| 177 | row += 2; |
| 178 | |
| 179 | addLabel("Color editor", 1, row); |
| 180 | addButton("Co&lors", 35, row, |
| 181 | new TAction() { |
| 182 | public void DO() { |
| 183 | new TEditColorThemeWindow(getApplication()); |
| 184 | } |
| 185 | } |
| 186 | ); |
| 187 | row += 2; |
| 188 | |
| 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++; |
| 200 | } else { |
| 201 | timer.setRecurring(false); |
| 202 | } |
| 203 | progressBar.setValue(timerI); |
| 204 | } |
| 205 | } |
| 206 | ); |
| 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"); |
| 213 | } |
| 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 | |
| 244 | } |