| 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 | import java.util.*; |
| 33 | |
| 34 | import jexer.*; |
| 35 | import jexer.event.*; |
| 36 | import jexer.menu.*; |
| 37 | |
| 38 | /** |
| 39 | * The demo application itself. |
| 40 | */ |
| 41 | public class DesktopDemoApplication extends TApplication { |
| 42 | |
| 43 | /** |
| 44 | * Add all the widgets of the demo. |
| 45 | */ |
| 46 | private void addAllWidgets() { |
| 47 | |
| 48 | // Add the menus |
| 49 | addFileMenu(); |
| 50 | addEditMenu(); |
| 51 | addWindowMenu(); |
| 52 | addHelpMenu(); |
| 53 | |
| 54 | final DesktopDemo desktop = new DesktopDemo(this); |
| 55 | setDesktop(desktop); |
| 56 | |
| 57 | desktop.addButton("&Remove HATCH", 2, 5, |
| 58 | new TAction() { |
| 59 | public void DO() { |
| 60 | desktop.drawHatch = false; |
| 61 | } |
| 62 | } |
| 63 | ); |
| 64 | desktop.addButton("&Show HATCH", 2, 8, |
| 65 | new TAction() { |
| 66 | public void DO() { |
| 67 | desktop.drawHatch = true; |
| 68 | } |
| 69 | } |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Handle menu events. |
| 75 | * |
| 76 | * @param menu menu event |
| 77 | * @return if true, the event was processed and should not be passed onto |
| 78 | * a window |
| 79 | */ |
| 80 | @Override |
| 81 | public boolean onMenu(final TMenuEvent menu) { |
| 82 | |
| 83 | if (menu.getId() == TMenu.MID_OPEN_FILE) { |
| 84 | try { |
| 85 | String filename = fileOpenBox("."); |
| 86 | if (filename != null) { |
| 87 | try { |
| 88 | File file = new File(filename); |
| 89 | StringBuilder fileContents = new StringBuilder(); |
| 90 | Scanner scanner = new Scanner(file); |
| 91 | String EOL = System.getProperty("line.separator"); |
| 92 | |
| 93 | try { |
| 94 | while (scanner.hasNextLine()) { |
| 95 | fileContents.append(scanner.nextLine() + EOL); |
| 96 | } |
| 97 | new DemoTextWindow(this, filename, |
| 98 | fileContents.toString()); |
| 99 | } finally { |
| 100 | scanner.close(); |
| 101 | } |
| 102 | } catch (IOException e) { |
| 103 | e.printStackTrace(); |
| 104 | } |
| 105 | } |
| 106 | } catch (IOException e) { |
| 107 | e.printStackTrace(); |
| 108 | } |
| 109 | return true; |
| 110 | } |
| 111 | return super.onMenu(menu); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Public constructor. |
| 116 | * |
| 117 | * @param backendType one of the TApplication.BackendType values |
| 118 | * @throws Exception if TApplication can't instantiate the Backend. |
| 119 | */ |
| 120 | public DesktopDemoApplication(final BackendType backendType) throws Exception { |
| 121 | super(backendType); |
| 122 | addAllWidgets(); |
| 123 | getBackend().setTitle("Jexer Demo Application"); |
| 124 | } |
| 125 | } |