Merge branch 'master' of https://github.com/klamonte/jexer
[fanfix.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
KL
31import java.io.*;
32
e3dfbd23 33import jexer.*;
a043164f 34import jexer.event.*;
e3dfbd23 35import jexer.menu.*;
42873e30
KL
36import jexer.backend.Backend;
37import jexer.backend.SwingTerminal;
e3dfbd23
KL
38
39/**
40 * The demo application itself.
41 */
7668cb45 42public class DemoApplication extends TApplication {
e3dfbd23
KL
43
44 /**
55b4f29b 45 * Add all the widgets of the demo.
e3dfbd23 46 */
55b4f29b 47 private void addAllWidgets() {
e3dfbd23
KL
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");
3649b921 62 item = demoMenu.addItem(2050, "Co&lors...");
e3dfbd23
KL
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
42873e30 77 if (getScreen() instanceof SwingTerminal) {
71a389c9 78 TMenu swingMenu = addMenu("Swin&g");
42873e30
KL
79 item = swingMenu.addItem(3000, "&Bigger +2");
80 item = swingMenu.addItem(3001, "&Smaller -2");
81 }
82
e3dfbd23 83 addWindowMenu();
55d2b2c2 84 addHelpMenu();
55b4f29b 85 }
a043164f 86
55b4f29b
KL
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();
55d2b2c2
KL
104
105 getBackend().setTitle("Jexer Demo Application");
55b4f29b 106 }
a043164f 107
6985c572
KL
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();
55d2b2c2
KL
124
125 getBackend().setTitle("Jexer Demo Application");
6985c572
KL
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
42873e30
KL
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
a043164f
KL
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
329fd62e 162 public boolean onMenu(final TMenuEvent menu) {
3649b921 163
42873e30
KL
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
3649b921
KL
179 if (menu.getId() == 2050) {
180 new TEditColorThemeWindow(this);
181 return true;
182 }
183
a043164f
KL
184 if (menu.getId() == TMenu.MID_OPEN_FILE) {
185 try {
186 String filename = fileOpenBox(".");
187 if (filename != null) {
188 try {
df602ccf 189 new TEditorWindow(this, new File(filename));
a043164f
KL
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
55b4f29b
KL
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 */
329fd62e 208 public DemoApplication(final BackendType backendType) throws Exception {
55b4f29b
KL
209 super(backendType);
210 addAllWidgets();
55d2b2c2 211 getBackend().setTitle("Jexer Demo Application");
e3dfbd23
KL
212 }
213}