| 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 jexer.menu.*; |
| 36 | import jexer.backend.Backend; |
| 37 | import jexer.backend.SwingTerminal; |
| 38 | |
| 39 | /** |
| 40 | * The demo application itself. |
| 41 | */ |
| 42 | public class DemoApplication extends TApplication { |
| 43 | |
| 44 | /** |
| 45 | * Add all the widgets of the demo. |
| 46 | */ |
| 47 | private void addAllWidgets() { |
| 48 | new DemoMainWindow(this); |
| 49 | |
| 50 | // Add the menus |
| 51 | addFileMenu(); |
| 52 | addEditMenu(); |
| 53 | |
| 54 | TMenu demoMenu = addMenu("&Demo"); |
| 55 | TMenuItem item = demoMenu.addItem(2000, "&Checkable"); |
| 56 | item.setCheckable(true); |
| 57 | item = demoMenu.addItem(2001, "Disabled"); |
| 58 | item.setEnabled(false); |
| 59 | item = demoMenu.addItem(2002, "&Normal"); |
| 60 | TSubMenu subMenu = demoMenu.addSubMenu("Sub-&Menu"); |
| 61 | item = demoMenu.addItem(2010, "N&ormal A&&D"); |
| 62 | item = demoMenu.addItem(2050, "Co&lors..."); |
| 63 | |
| 64 | item = subMenu.addItem(2000, "&Checkable (sub)"); |
| 65 | item.setCheckable(true); |
| 66 | item = subMenu.addItem(2001, "Disabled (sub)"); |
| 67 | item.setEnabled(false); |
| 68 | item = subMenu.addItem(2002, "&Normal (sub)"); |
| 69 | |
| 70 | subMenu = subMenu.addSubMenu("Sub-&Menu"); |
| 71 | item = subMenu.addItem(2000, "&Checkable (sub)"); |
| 72 | item.setCheckable(true); |
| 73 | item = subMenu.addItem(2001, "Disabled (sub)"); |
| 74 | item.setEnabled(false); |
| 75 | item = subMenu.addItem(2002, "&Normal (sub)"); |
| 76 | |
| 77 | if (getScreen() instanceof SwingTerminal) { |
| 78 | TMenu swingMenu = addMenu("Swin&g"); |
| 79 | item = swingMenu.addItem(3000, "&Bigger +2"); |
| 80 | item = swingMenu.addItem(3001, "&Smaller -2"); |
| 81 | } |
| 82 | |
| 83 | addWindowMenu(); |
| 84 | addHelpMenu(); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Public constructor. |
| 89 | * |
| 90 | * @param input an InputStream connected to the remote user, or null for |
| 91 | * System.in. If System.in is used, then on non-Windows systems it will |
| 92 | * be put in raw mode; shutdown() will (blindly!) put System.in in cooked |
| 93 | * mode. input is always converted to a Reader with UTF-8 encoding. |
| 94 | * @param output an OutputStream connected to the remote user, or null |
| 95 | * for System.out. output is always converted to a Writer with UTF-8 |
| 96 | * encoding. |
| 97 | * @throws UnsupportedEncodingException if an exception is thrown when |
| 98 | * creating the InputStreamReader |
| 99 | */ |
| 100 | public DemoApplication(final InputStream input, |
| 101 | final OutputStream output) throws UnsupportedEncodingException { |
| 102 | super(input, output); |
| 103 | addAllWidgets(); |
| 104 | |
| 105 | getBackend().setTitle("Jexer Demo Application"); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Public constructor. |
| 110 | * |
| 111 | * @param input the InputStream underlying 'reader'. Its available() |
| 112 | * method is used to determine if reader.read() will block or not. |
| 113 | * @param reader a Reader connected to the remote user. |
| 114 | * @param writer a PrintWriter connected to the remote user. |
| 115 | * @param setRawMode if true, set System.in into raw mode with stty. |
| 116 | * This should in general not be used. It is here solely for Demo3, |
| 117 | * which uses System.in. |
| 118 | * @throws IllegalArgumentException if input, reader, or writer are null. |
| 119 | */ |
| 120 | public DemoApplication(final InputStream input, final Reader reader, |
| 121 | final PrintWriter writer, final boolean setRawMode) { |
| 122 | super(input, reader, writer, setRawMode); |
| 123 | addAllWidgets(); |
| 124 | |
| 125 | getBackend().setTitle("Jexer Demo Application"); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Public constructor. |
| 130 | * |
| 131 | * @param input the InputStream underlying 'reader'. Its available() |
| 132 | * method is used to determine if reader.read() will block or not. |
| 133 | * @param reader a Reader connected to the remote user. |
| 134 | * @param writer a PrintWriter connected to the remote user. |
| 135 | * @throws IllegalArgumentException if input, reader, or writer are null. |
| 136 | */ |
| 137 | public DemoApplication(final InputStream input, final Reader reader, |
| 138 | final PrintWriter writer) { |
| 139 | |
| 140 | this(input, reader, writer, false); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Public constructor. |
| 145 | * |
| 146 | * @param backend a Backend that is already ready to go. |
| 147 | */ |
| 148 | public DemoApplication(final Backend backend) { |
| 149 | super(backend); |
| 150 | |
| 151 | addAllWidgets(); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Handle menu events. |
| 156 | * |
| 157 | * @param menu menu event |
| 158 | * @return if true, the event was processed and should not be passed onto |
| 159 | * a window |
| 160 | */ |
| 161 | @Override |
| 162 | public boolean onMenu(final TMenuEvent menu) { |
| 163 | |
| 164 | if (menu.getId() == 3000) { |
| 165 | // Bigger +2 |
| 166 | assert (getScreen() instanceof SwingTerminal); |
| 167 | SwingTerminal terminal = (SwingTerminal) getScreen(); |
| 168 | terminal.setFontSize(terminal.getFontSize() + 2); |
| 169 | return true; |
| 170 | } |
| 171 | if (menu.getId() == 3001) { |
| 172 | // Smaller -2 |
| 173 | assert (getScreen() instanceof SwingTerminal); |
| 174 | SwingTerminal terminal = (SwingTerminal) getScreen(); |
| 175 | terminal.setFontSize(terminal.getFontSize() - 2); |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | if (menu.getId() == 2050) { |
| 180 | new TEditColorThemeWindow(this); |
| 181 | return true; |
| 182 | } |
| 183 | |
| 184 | if (menu.getId() == TMenu.MID_OPEN_FILE) { |
| 185 | try { |
| 186 | String filename = fileOpenBox("."); |
| 187 | if (filename != null) { |
| 188 | try { |
| 189 | new TEditorWindow(this, new File(filename)); |
| 190 | } catch (IOException e) { |
| 191 | e.printStackTrace(); |
| 192 | } |
| 193 | } |
| 194 | } catch (IOException e) { |
| 195 | e.printStackTrace(); |
| 196 | } |
| 197 | return true; |
| 198 | } |
| 199 | return super.onMenu(menu); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Public constructor. |
| 204 | * |
| 205 | * @param backendType one of the TApplication.BackendType values |
| 206 | * @throws Exception if TApplication can't instantiate the Backend. |
| 207 | */ |
| 208 | public DemoApplication(final BackendType backendType) throws Exception { |
| 209 | super(backendType); |
| 210 | addAllWidgets(); |
| 211 | getBackend().setTitle("Jexer Demo Application"); |
| 212 | } |
| 213 | } |