2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
32 import java
.io
.InputStream
;
33 import java
.io
.IOException
;
34 import java
.io
.OutputStream
;
35 import java
.io
.PrintWriter
;
36 import java
.io
.Reader
;
37 import java
.io
.UnsupportedEncodingException
;
38 import java
.util
.ResourceBundle
;
40 import jexer
.TApplication
;
41 import jexer
.TEditColorThemeWindow
;
42 import jexer
.TEditorWindow
;
43 import jexer
.event
.TMenuEvent
;
44 import jexer
.menu
.TMenu
;
45 import jexer
.menu
.TMenuItem
;
46 import jexer
.menu
.TSubMenu
;
47 import jexer
.backend
.Backend
;
48 import jexer
.backend
.SwingTerminal
;
51 * The demo application itself.
53 public class DemoApplication
extends TApplication
{
58 private static final ResourceBundle i18n
= ResourceBundle
.getBundle(DemoApplication
.class.getName());
60 // ------------------------------------------------------------------------
61 // Constructors -----------------------------------------------------------
62 // ------------------------------------------------------------------------
67 * @param input an InputStream connected to the remote user, or null for
68 * System.in. If System.in is used, then on non-Windows systems it will
69 * be put in raw mode; shutdown() will (blindly!) put System.in in cooked
70 * mode. input is always converted to a Reader with UTF-8 encoding.
71 * @param output an OutputStream connected to the remote user, or null
72 * for System.out. output is always converted to a Writer with UTF-8
74 * @throws UnsupportedEncodingException if an exception is thrown when
75 * creating the InputStreamReader
77 public DemoApplication(final InputStream input
,
78 final OutputStream output
) throws UnsupportedEncodingException
{
82 getBackend().setTitle(i18n
.getString("applicationTitle"));
88 * @param input the InputStream underlying 'reader'. Its available()
89 * method is used to determine if reader.read() will block or not.
90 * @param reader a Reader connected to the remote user.
91 * @param writer a PrintWriter connected to the remote user.
92 * @param setRawMode if true, set System.in into raw mode with stty.
93 * This should in general not be used. It is here solely for Demo3,
94 * which uses System.in.
95 * @throws IllegalArgumentException if input, reader, or writer are null.
97 public DemoApplication(final InputStream input
, final Reader reader
,
98 final PrintWriter writer
, final boolean setRawMode
) {
99 super(input
, reader
, writer
, setRawMode
);
102 getBackend().setTitle(i18n
.getString("applicationTitle"));
106 * Public constructor.
108 * @param input the InputStream underlying 'reader'. Its available()
109 * method is used to determine if reader.read() will block or not.
110 * @param reader a Reader connected to the remote user.
111 * @param writer a PrintWriter connected to the remote user.
112 * @throws IllegalArgumentException if input, reader, or writer are null.
114 public DemoApplication(final InputStream input
, final Reader reader
,
115 final PrintWriter writer
) {
117 this(input
, reader
, writer
, false);
121 * Public constructor.
123 * @param backend a Backend that is already ready to go.
125 public DemoApplication(final Backend backend
) {
132 * Public constructor.
134 * @param backendType one of the TApplication.BackendType values
135 * @throws Exception if TApplication can't instantiate the Backend.
137 public DemoApplication(final BackendType backendType
) throws Exception
{
140 getBackend().setTitle(i18n
.getString("applicationTitle"));
143 // ------------------------------------------------------------------------
144 // TApplication -----------------------------------------------------------
145 // ------------------------------------------------------------------------
148 * Handle menu events.
150 * @param menu menu event
151 * @return if true, the event was processed and should not be passed onto
155 public boolean onMenu(final TMenuEvent menu
) {
157 if (menu
.getId() == 3000) {
159 assert (getScreen() instanceof SwingTerminal
);
160 SwingTerminal terminal
= (SwingTerminal
) getScreen();
161 terminal
.setFontSize(terminal
.getFontSize() + 2);
164 if (menu
.getId() == 3001) {
166 assert (getScreen() instanceof SwingTerminal
);
167 SwingTerminal terminal
= (SwingTerminal
) getScreen();
168 terminal
.setFontSize(terminal
.getFontSize() - 2);
172 if (menu
.getId() == 2050) {
173 new TEditColorThemeWindow(this);
177 if (menu
.getId() == TMenu
.MID_OPEN_FILE
) {
179 String filename
= fileOpenBox(".");
180 if (filename
!= null) {
182 new TEditorWindow(this, new File(filename
));
183 } catch (IOException e
) {
187 } catch (IOException e
) {
192 return super.onMenu(menu
);
195 // ------------------------------------------------------------------------
196 // DemoApplication --------------------------------------------------------
197 // ------------------------------------------------------------------------
200 * Add all the widgets of the demo.
202 private void addAllWidgets() {
203 new DemoMainWindow(this);
210 TMenu demoMenu
= addMenu(i18n
.getString("demo"));
211 TMenuItem item
= demoMenu
.addItem(2000, i18n
.getString("checkable"));
212 item
.setCheckable(true);
213 item
= demoMenu
.addItem(2001, i18n
.getString("disabled"));
214 item
.setEnabled(false);
215 item
= demoMenu
.addItem(2002, i18n
.getString("normal"));
216 TSubMenu subMenu
= demoMenu
.addSubMenu(i18n
.getString("subMenu"));
217 item
= demoMenu
.addItem(2010, i18n
.getString("normal"));
218 item
= demoMenu
.addItem(2050, i18n
.getString("colors"));
220 item
= subMenu
.addItem(2000, i18n
.getString("checkableSub"));
221 item
.setCheckable(true);
222 item
= subMenu
.addItem(2001, i18n
.getString("disabledSub"));
223 item
.setEnabled(false);
224 item
= subMenu
.addItem(2002, i18n
.getString("normalSub"));
226 subMenu
= subMenu
.addSubMenu(i18n
.getString("subMenu"));
227 item
= subMenu
.addItem(2000, i18n
.getString("checkableSub"));
228 item
.setCheckable(true);
229 item
= subMenu
.addItem(2001, i18n
.getString("disabledSub"));
230 item
.setEnabled(false);
231 item
= subMenu
.addItem(2002, i18n
.getString("normalSub"));
233 if (getScreen() instanceof SwingTerminal
) {
234 TMenu swingMenu
= addMenu(i18n
.getString("swing"));
235 item
= swingMenu
.addItem(3000, i18n
.getString("bigger"));
236 item
= swingMenu
.addItem(3001, i18n
.getString("smaller"));