| 1 | /** |
| 2 | * Jexer - Java Text User Interface - demonstration program |
| 3 | * |
| 4 | * Version: $Id$ |
| 5 | * |
| 6 | * Author: Kevin Lamonte, <a href="mailto:kevin.lamonte@gmail.com">kevin.lamonte@gmail.com</a> |
| 7 | * |
| 8 | * License: LGPLv3 or later |
| 9 | * |
| 10 | * Copyright: This module is licensed under the GNU Lesser General |
| 11 | * Public License Version 3. Please see the file "COPYING" in this |
| 12 | * directory for more information about the GNU Lesser General Public |
| 13 | * License Version 3. |
| 14 | * |
| 15 | * Copyright (C) 2015 Kevin Lamonte |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or |
| 18 | * modify it under the terms of the GNU Lesser General Public License |
| 19 | * as published by the Free Software Foundation; either version 3 of |
| 20 | * the License, or (at your option) any later version. |
| 21 | * |
| 22 | * This program is distributed in the hope that it will be useful, but |
| 23 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 25 | * General Public License for more details. |
| 26 | * |
| 27 | * You should have received a copy of the GNU Lesser General Public |
| 28 | * License along with this program; if not, see |
| 29 | * http://www.gnu.org/licenses/, or write to the Free Software |
| 30 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 31 | * 02110-1301 USA |
| 32 | */ |
| 33 | |
| 34 | import jexer.*; |
| 35 | import jexer.menu.*; |
| 36 | |
| 37 | class DemoCheckboxWindow extends TWindow { |
| 38 | |
| 39 | /** |
| 40 | * Constructor |
| 41 | */ |
| 42 | DemoCheckboxWindow(TApplication parent) { |
| 43 | this(parent, CENTERED | RESIZABLE); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Constructor |
| 48 | */ |
| 49 | DemoCheckboxWindow(TApplication parent, int flags) { |
| 50 | // Construct a demo window. X and Y don't matter because it |
| 51 | // will be centered on screen. |
| 52 | super(parent, "Radiobuttons and Checkboxes", 0, 0, 60, 15, flags); |
| 53 | |
| 54 | int row = 1; |
| 55 | |
| 56 | // Add some widgets |
| 57 | addLabel("Check box example 1", 1, row); |
| 58 | addCheckbox(35, row++, "Checkbox 1", false); |
| 59 | addLabel("Check box example 2", 1, row); |
| 60 | addCheckbox(35, row++, "Checkbox 2", true); |
| 61 | row += 2; |
| 62 | |
| 63 | TRadioGroup group = addRadioGroup(1, row, "Group 1"); |
| 64 | group.addRadioButton("Radio option 1"); |
| 65 | group.addRadioButton("Radio option 2"); |
| 66 | group.addRadioButton("Radio option 3"); |
| 67 | |
| 68 | addButton("&Close Window", (getWidth() - 14) / 2, getHeight() - 4, |
| 69 | new TAction() { |
| 70 | public void DO() { |
| 71 | DemoCheckboxWindow.this.getApplication().closeWindow(DemoCheckboxWindow.this); |
| 72 | } |
| 73 | } |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | } |
| 78 | |
| 79 | |
| 80 | class DemoMsgBoxWindow extends TWindow { |
| 81 | /* |
| 82 | private void openYNCMessageBox() { |
| 83 | application.messageBox("Yes/No/Cancel MessageBox", |
| 84 | q"EOS |
| 85 | This is an example of a Yes/No/Cancel MessageBox. |
| 86 | |
| 87 | Note that the MessageBox text can span multiple |
| 88 | lines. |
| 89 | |
| 90 | The default result (if someone hits the top-left |
| 91 | close button) is CANCEL. |
| 92 | EOS", |
| 93 | TMessageBox.Type.YESNOCANCEL); |
| 94 | } |
| 95 | |
| 96 | private void openYNMessageBox() { |
| 97 | application.messageBox("Yes/No MessageBox", |
| 98 | q"EOS |
| 99 | This is an example of a Yes/No MessageBox. |
| 100 | |
| 101 | Note that the MessageBox text can span multiple |
| 102 | lines. |
| 103 | |
| 104 | The default result (if someone hits the top-left |
| 105 | close button) is NO. |
| 106 | EOS", |
| 107 | TMessageBox.Type.YESNO); |
| 108 | } |
| 109 | |
| 110 | private void openOKCMessageBox() { |
| 111 | application.messageBox("OK/Cancel MessageBox", |
| 112 | q"EOS |
| 113 | This is an example of a OK/Cancel MessageBox. |
| 114 | |
| 115 | Note that the MessageBox text can span multiple |
| 116 | lines. |
| 117 | |
| 118 | The default result (if someone hits the top-left |
| 119 | close button) is CANCEL. |
| 120 | EOS", |
| 121 | TMessageBox.Type.OKCANCEL); |
| 122 | } |
| 123 | |
| 124 | private void openOKMessageBox() { |
| 125 | application.messageBox("OK MessageBox", |
| 126 | q"EOS |
| 127 | This is an example of a OK MessageBox. This is the |
| 128 | default MessageBox. |
| 129 | |
| 130 | Note that the MessageBox text can span multiple |
| 131 | lines. |
| 132 | |
| 133 | The default result (if someone hits the top-left |
| 134 | close button) is OK. |
| 135 | EOS", |
| 136 | TMessageBox.Type.OK); |
| 137 | } |
| 138 | |
| 139 | */ |
| 140 | |
| 141 | /** |
| 142 | * Constructor. |
| 143 | */ |
| 144 | DemoMsgBoxWindow(final TApplication parent) { |
| 145 | this(parent, TWindow.CENTERED | TWindow.RESIZABLE); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Constructor. |
| 150 | */ |
| 151 | DemoMsgBoxWindow(final TApplication parent, final int flags) { |
| 152 | // Construct a demo window. X and Y don't matter because it |
| 153 | // will be centered on screen. |
| 154 | super(parent, "Message Boxes", 0, 0, 60, 15, flags); |
| 155 | /* |
| 156 | uint row = 1; |
| 157 | |
| 158 | // Add some widgets |
| 159 | addLabel("Default OK message box", 1, row); |
| 160 | addButton("Open O&K MB", 35, row, &openOKMessageBox); |
| 161 | row += 2; |
| 162 | |
| 163 | addLabel("OK/Cancel message box", 1, row); |
| 164 | addButton("O&pen OKC MB", 35, row, &openOKCMessageBox); |
| 165 | row += 2; |
| 166 | |
| 167 | addLabel("Yes/No message box", 1, row); |
| 168 | addButton("Open &YN MB", 35, row, &openYNMessageBox); |
| 169 | row += 2; |
| 170 | |
| 171 | addLabel("Yes/No/Cancel message box", 1, row); |
| 172 | addButton("Ope&n YNC MB", 35, row, &openYNCMessageBox); |
| 173 | row += 2; |
| 174 | |
| 175 | addLabel("Input box", 1, row); |
| 176 | addButton("Open &input box", 35, row, |
| 177 | { |
| 178 | application.inputBox("Input Box", |
| 179 | q"EOS |
| 180 | This is an example of an InputBox. |
| 181 | |
| 182 | Note that the InputBox text can span multiple |
| 183 | lines. |
| 184 | EOS", |
| 185 | "some input text"); |
| 186 | } |
| 187 | ); |
| 188 | |
| 189 | addButton("&Close Window", (width - 14) / 2, height - 4, |
| 190 | { |
| 191 | application.closeWindow(this); |
| 192 | } |
| 193 | ); |
| 194 | */ |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | |
| 199 | class DemoMainWindow extends TWindow { |
| 200 | // Timer that increments a number |
| 201 | private TTimer timer; |
| 202 | |
| 203 | // Timer label is updated with timerrr ticks |
| 204 | TLabel timerLabel; |
| 205 | |
| 206 | /* |
| 207 | // The modal window is a more low-level example of controlling a window |
| 208 | // "from the outside". Most windows will probably subclass TWindow and |
| 209 | // do this kind of logic on their own. |
| 210 | private TWindow modalWindow; |
| 211 | private void openModalWindow() { |
| 212 | modalWindow = application.addWindow("Demo Modal Window", 0, 0, |
| 213 | 58, 15, TWindow.Flag.MODAL); |
| 214 | modalWindow.addLabel("This is an example of a very braindead modal window.", 1, 1); |
| 215 | modalWindow.addLabel("Modal windows are centered by default.", 1, 2); |
| 216 | modalWindow.addButton("&Close", (modalWindow.width - 8)/2, |
| 217 | modalWindow.height - 4, &modalWindowClose); |
| 218 | } |
| 219 | private void modalWindowClose() { |
| 220 | application.closeWindow(modalWindow); |
| 221 | } |
| 222 | */ |
| 223 | |
| 224 | /** |
| 225 | * We need to override onClose so that the timer will no longer be called |
| 226 | * after we close the window. TTimers currently are completely unaware |
| 227 | * of the rest of the UI classes. |
| 228 | */ |
| 229 | @Override |
| 230 | public void onClose() { |
| 231 | getApplication().removeTimer(timer); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Construct demo window. It will be centered on screen. |
| 236 | */ |
| 237 | public DemoMainWindow(TApplication parent) { |
| 238 | this(parent, CENTERED | RESIZABLE); |
| 239 | } |
| 240 | |
| 241 | int timerI = 0; |
| 242 | TProgressBar progressBar; |
| 243 | |
| 244 | /** |
| 245 | * Constructor. |
| 246 | */ |
| 247 | private DemoMainWindow(TApplication parent, int flags) { |
| 248 | // Construct a demo window. X and Y don't matter because it will be |
| 249 | // centered on screen. |
| 250 | super(parent, "Demo Window", 0, 0, 60, 23, flags); |
| 251 | |
| 252 | int row = 1; |
| 253 | |
| 254 | // Add some widgets |
| 255 | if (!isModal()) { |
| 256 | addLabel("Message Boxes", 1, row); |
| 257 | addButton("&MessageBoxes", 35, row, |
| 258 | new TAction() { |
| 259 | public void DO() { |
| 260 | new DemoMsgBoxWindow(getApplication()); |
| 261 | } |
| 262 | } |
| 263 | ); |
| 264 | } |
| 265 | row += 2; |
| 266 | |
| 267 | addLabel("Open me as modal", 1, row); |
| 268 | addButton("W&indow", 35, row, |
| 269 | new TAction() { |
| 270 | public void DO() { |
| 271 | new DemoMainWindow(getApplication(), MODAL); |
| 272 | } |
| 273 | } |
| 274 | ); |
| 275 | |
| 276 | row += 2; |
| 277 | |
| 278 | addLabel("Variable-width text field:", 1, row); |
| 279 | addField(35, row++, 15, false, "Field text"); |
| 280 | |
| 281 | addLabel("Fixed-width text field:", 1, row); |
| 282 | addField(35, row, 15, true); |
| 283 | row += 2; |
| 284 | |
| 285 | if (!isModal()) { |
| 286 | addLabel("Radio buttons and checkboxes", 1, row); |
| 287 | addButton("&Checkboxes", 35, row, |
| 288 | new TAction() { |
| 289 | public void DO() { |
| 290 | new DemoCheckboxWindow(getApplication()); |
| 291 | } |
| 292 | } |
| 293 | ); |
| 294 | } |
| 295 | row += 2; |
| 296 | |
| 297 | /* |
| 298 | if (!isModal()) { |
| 299 | addLabel("Editor window", 1, row); |
| 300 | addButton("Edito&r", 35, row, |
| 301 | { |
| 302 | new TEditor(application, 0, 0, 60, 15); |
| 303 | } |
| 304 | ); |
| 305 | } |
| 306 | row += 2; |
| 307 | |
| 308 | if (!isModal()) { |
| 309 | addLabel("Text areas", 1, row); |
| 310 | addButton("&Text", 35, row, |
| 311 | { |
| 312 | new DemoTextWindow(application); |
| 313 | } |
| 314 | ); |
| 315 | } |
| 316 | row += 2; |
| 317 | |
| 318 | if (!isModal()) { |
| 319 | addLabel("Tree views", 1, row); |
| 320 | addButton("Tree&View", 35, row, |
| 321 | { |
| 322 | new DemoTreeViewWindow(application); |
| 323 | } |
| 324 | ); |
| 325 | } |
| 326 | row += 2; |
| 327 | |
| 328 | if (!isModal()) { |
| 329 | addLabel("Terminal", 1, row); |
| 330 | addButton("Termi&nal", 35, row, |
| 331 | { |
| 332 | application.openTerminal(0, 0); |
| 333 | } |
| 334 | ); |
| 335 | } |
| 336 | row += 2; |
| 337 | */ |
| 338 | |
| 339 | progressBar = addProgressBar(1, row, 22, 0); |
| 340 | row++; |
| 341 | timerLabel = addLabel("Timer", 1, row); |
| 342 | timer = getApplication().addTimer(100, true, |
| 343 | new TAction() { |
| 344 | |
| 345 | public void DO() { |
| 346 | timerLabel.setText(String.format("Timer: %d", timerI)); |
| 347 | timerLabel.setWidth(timerLabel.getText().length()); |
| 348 | if (timerI < 100) { |
| 349 | timerI++; |
| 350 | } |
| 351 | progressBar.setValue(timerI); |
| 352 | DemoMainWindow.this.setRepaint(); |
| 353 | } |
| 354 | } |
| 355 | ); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * The demo application itself. |
| 361 | */ |
| 362 | class DemoApplication extends TApplication { |
| 363 | /** |
| 364 | * Public constructor |
| 365 | */ |
| 366 | public DemoApplication() throws Exception { |
| 367 | super(null, null); |
| 368 | new DemoMainWindow(this); |
| 369 | |
| 370 | // Add the menus |
| 371 | addFileMenu(); |
| 372 | addEditMenu(); |
| 373 | |
| 374 | TMenu demoMenu = addMenu("&Demo"); |
| 375 | TMenuItem item = demoMenu.addItem(2000, "&Checkable"); |
| 376 | item.setCheckable(true); |
| 377 | item = demoMenu.addItem(2001, "Disabled"); |
| 378 | item.setEnabled(false); |
| 379 | item = demoMenu.addItem(2002, "&Normal"); |
| 380 | TSubMenu subMenu = demoMenu.addSubMenu("Sub-&Menu"); |
| 381 | item = demoMenu.addItem(2010, "N&ormal A&&D"); |
| 382 | |
| 383 | item = subMenu.addItem(2000, "&Checkable (sub)"); |
| 384 | item.setCheckable(true); |
| 385 | item = subMenu.addItem(2001, "Disabled (sub)"); |
| 386 | item.setEnabled(false); |
| 387 | item = subMenu.addItem(2002, "&Normal (sub)"); |
| 388 | |
| 389 | subMenu = subMenu.addSubMenu("Sub-&Menu"); |
| 390 | item = subMenu.addItem(2000, "&Checkable (sub)"); |
| 391 | item.setCheckable(true); |
| 392 | item = subMenu.addItem(2001, "Disabled (sub)"); |
| 393 | item.setEnabled(false); |
| 394 | item = subMenu.addItem(2002, "&Normal (sub)"); |
| 395 | |
| 396 | addWindowMenu(); |
| 397 | |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * This class provides a simple demonstration of Jexer's capabilities. |
| 403 | */ |
| 404 | public class Demo1 { |
| 405 | /** |
| 406 | * Main entry point. |
| 407 | * |
| 408 | * @param args Command line arguments |
| 409 | */ |
| 410 | public static void main(String [] args) { |
| 411 | try { |
| 412 | DemoApplication app = new DemoApplication(); |
| 413 | app.run(); |
| 414 | } catch (Exception e) { |
| 415 | e.printStackTrace(); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | } |