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