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