minor cleanup
[nikiroo-utils.git] / src / jexer / demos / DemoApplication.java
CommitLineData
e3dfbd23
KL
1/*
2 * Jexer - Java Text User Interface
3 *
e16dda65 4 * The MIT License (MIT)
e3dfbd23 5 *
a69ed767 6 * Copyright (C) 2019 Kevin Lamonte
e3dfbd23 7 *
e16dda65
KL
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:
e3dfbd23 14 *
e16dda65
KL
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
e3dfbd23 17 *
e16dda65
KL
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.
e3dfbd23
KL
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29package jexer.demos;
30
a69ed767
KL
31import java.io.File;
32import java.io.InputStream;
33import java.io.IOException;
34import java.io.OutputStream;
35import java.io.PrintWriter;
36import java.io.Reader;
37import java.io.UnsupportedEncodingException;
38import java.util.ResourceBundle;
39
40import jexer.TApplication;
41import jexer.TEditColorThemeWindow;
42import jexer.TEditorWindow;
43import jexer.event.TMenuEvent;
44import jexer.menu.TMenu;
45import jexer.menu.TMenuItem;
46import jexer.menu.TSubMenu;
42873e30
KL
47import jexer.backend.Backend;
48import jexer.backend.SwingTerminal;
e3dfbd23
KL
49
50/**
51 * The demo application itself.
52 */
7668cb45 53public class DemoApplication extends TApplication {
e3dfbd23 54
a69ed767
KL
55 /**
56 * Translated strings.
57 */
8994e796 58 private static final ResourceBundle i18n = ResourceBundle.getBundle(DemoApplication.class.getName());
a69ed767 59
43ad7b6c
KL
60 // ------------------------------------------------------------------------
61 // Constructors -----------------------------------------------------------
62 // ------------------------------------------------------------------------
a043164f 63
55b4f29b
KL
64 /**
65 * Public constructor.
66 *
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
73 * encoding.
74 * @throws UnsupportedEncodingException if an exception is thrown when
75 * creating the InputStreamReader
76 */
77 public DemoApplication(final InputStream input,
78 final OutputStream output) throws UnsupportedEncodingException {
79 super(input, output);
80 addAllWidgets();
55d2b2c2 81
a69ed767 82 getBackend().setTitle(i18n.getString("applicationTitle"));
55b4f29b 83 }
a043164f 84
6985c572
KL
85 /**
86 * Public constructor.
87 *
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.
96 */
97 public DemoApplication(final InputStream input, final Reader reader,
98 final PrintWriter writer, final boolean setRawMode) {
99 super(input, reader, writer, setRawMode);
100 addAllWidgets();
55d2b2c2 101
a69ed767 102 getBackend().setTitle(i18n.getString("applicationTitle"));
6985c572
KL
103 }
104
105 /**
106 * Public constructor.
107 *
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.
113 */
114 public DemoApplication(final InputStream input, final Reader reader,
115 final PrintWriter writer) {
116
117 this(input, reader, writer, false);
118 }
119
42873e30
KL
120 /**
121 * Public constructor.
122 *
123 * @param backend a Backend that is already ready to go.
124 */
125 public DemoApplication(final Backend backend) {
126 super(backend);
127
128 addAllWidgets();
129 }
130
43ad7b6c
KL
131 /**
132 * Public constructor.
133 *
134 * @param backendType one of the TApplication.BackendType values
135 * @throws Exception if TApplication can't instantiate the Backend.
136 */
137 public DemoApplication(final BackendType backendType) throws Exception {
a75902fa
KL
138 // For the Swing demo, use an initial size of 82x28 so that a
139 // terminal window precisely fits the window.
140 super(backendType, (backendType == BackendType.SWING ? 82 : -1),
141 (backendType == BackendType.SWING ? 28 : -1), 20);
43ad7b6c 142 addAllWidgets();
a69ed767 143 getBackend().setTitle(i18n.getString("applicationTitle"));
43ad7b6c
KL
144 }
145
146 // ------------------------------------------------------------------------
147 // TApplication -----------------------------------------------------------
148 // ------------------------------------------------------------------------
149
a043164f
KL
150 /**
151 * Handle menu events.
152 *
153 * @param menu menu event
154 * @return if true, the event was processed and should not be passed onto
155 * a window
156 */
157 @Override
329fd62e 158 public boolean onMenu(final TMenuEvent menu) {
3649b921 159
42873e30
KL
160 if (menu.getId() == 3000) {
161 // Bigger +2
162 assert (getScreen() instanceof SwingTerminal);
163 SwingTerminal terminal = (SwingTerminal) getScreen();
164 terminal.setFontSize(terminal.getFontSize() + 2);
165 return true;
166 }
167 if (menu.getId() == 3001) {
168 // Smaller -2
169 assert (getScreen() instanceof SwingTerminal);
170 SwingTerminal terminal = (SwingTerminal) getScreen();
171 terminal.setFontSize(terminal.getFontSize() - 2);
172 return true;
173 }
174
3649b921
KL
175 if (menu.getId() == 2050) {
176 new TEditColorThemeWindow(this);
177 return true;
178 }
179
a043164f
KL
180 if (menu.getId() == TMenu.MID_OPEN_FILE) {
181 try {
182 String filename = fileOpenBox(".");
183 if (filename != null) {
184 try {
df602ccf 185 new TEditorWindow(this, new File(filename));
a043164f
KL
186 } catch (IOException e) {
187 e.printStackTrace();
188 }
189 }
190 } catch (IOException e) {
191 e.printStackTrace();
192 }
193 return true;
194 }
195 return super.onMenu(menu);
196 }
197
43ad7b6c
KL
198 // ------------------------------------------------------------------------
199 // DemoApplication --------------------------------------------------------
200 // ------------------------------------------------------------------------
201
55b4f29b 202 /**
43ad7b6c 203 * Add all the widgets of the demo.
55b4f29b 204 */
43ad7b6c
KL
205 private void addAllWidgets() {
206 new DemoMainWindow(this);
207
208 // Add the menus
e23ea538 209 addToolMenu();
43ad7b6c
KL
210 addFileMenu();
211 addEditMenu();
212
a69ed767
KL
213 TMenu demoMenu = addMenu(i18n.getString("demo"));
214 TMenuItem item = demoMenu.addItem(2000, i18n.getString("checkable"));
43ad7b6c 215 item.setCheckable(true);
a69ed767 216 item = demoMenu.addItem(2001, i18n.getString("disabled"));
43ad7b6c 217 item.setEnabled(false);
a69ed767
KL
218 item = demoMenu.addItem(2002, i18n.getString("normal"));
219 TSubMenu subMenu = demoMenu.addSubMenu(i18n.getString("subMenu"));
220 item = demoMenu.addItem(2010, i18n.getString("normal"));
221 item = demoMenu.addItem(2050, i18n.getString("colors"));
43ad7b6c 222
a69ed767 223 item = subMenu.addItem(2000, i18n.getString("checkableSub"));
43ad7b6c 224 item.setCheckable(true);
a69ed767 225 item = subMenu.addItem(2001, i18n.getString("disabledSub"));
43ad7b6c 226 item.setEnabled(false);
a69ed767 227 item = subMenu.addItem(2002, i18n.getString("normalSub"));
43ad7b6c 228
a69ed767
KL
229 subMenu = subMenu.addSubMenu(i18n.getString("subMenu"));
230 item = subMenu.addItem(2000, i18n.getString("checkableSub"));
43ad7b6c 231 item.setCheckable(true);
a69ed767 232 item = subMenu.addItem(2001, i18n.getString("disabledSub"));
43ad7b6c 233 item.setEnabled(false);
a69ed767 234 item = subMenu.addItem(2002, i18n.getString("normalSub"));
43ad7b6c
KL
235
236 if (getScreen() instanceof SwingTerminal) {
a69ed767
KL
237 TMenu swingMenu = addMenu(i18n.getString("swing"));
238 item = swingMenu.addItem(3000, i18n.getString("bigger"));
239 item = swingMenu.addItem(3001, i18n.getString("smaller"));
43ad7b6c
KL
240 }
241
1dac6b8d 242 addTableMenu();
43ad7b6c
KL
243 addWindowMenu();
244 addHelpMenu();
e3dfbd23 245 }
43ad7b6c 246
e3dfbd23 247}