Expose ECMA48 Reader and Writer
[fanfix.git] / src / jexer / demos / DemoApplication.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2016 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 import java.util.*;
33
34 import jexer.*;
35 import jexer.event.*;
36 import jexer.menu.*;
37
38 /**
39 * The demo application itself.
40 */
41 public class DemoApplication extends TApplication {
42
43 /**
44 * Add all the widgets of the demo.
45 */
46 private void addAllWidgets() {
47 new DemoMainWindow(this);
48
49 // Add the menus
50 addFileMenu();
51 addEditMenu();
52
53 TMenu demoMenu = addMenu("&Demo");
54 TMenuItem item = demoMenu.addItem(2000, "&Checkable");
55 item.setCheckable(true);
56 item = demoMenu.addItem(2001, "Disabled");
57 item.setEnabled(false);
58 item = demoMenu.addItem(2002, "&Normal");
59 TSubMenu subMenu = demoMenu.addSubMenu("Sub-&Menu");
60 item = demoMenu.addItem(2010, "N&ormal A&&D");
61 item = demoMenu.addItem(2050, "Co&lors...");
62
63 item = subMenu.addItem(2000, "&Checkable (sub)");
64 item.setCheckable(true);
65 item = subMenu.addItem(2001, "Disabled (sub)");
66 item.setEnabled(false);
67 item = subMenu.addItem(2002, "&Normal (sub)");
68
69 subMenu = subMenu.addSubMenu("Sub-&Menu");
70 item = subMenu.addItem(2000, "&Checkable (sub)");
71 item.setCheckable(true);
72 item = subMenu.addItem(2001, "Disabled (sub)");
73 item.setEnabled(false);
74 item = subMenu.addItem(2002, "&Normal (sub)");
75
76 addWindowMenu();
77 }
78
79 /**
80 * Public constructor.
81 *
82 * @param input an InputStream connected to the remote user, or null for
83 * System.in. If System.in is used, then on non-Windows systems it will
84 * be put in raw mode; shutdown() will (blindly!) put System.in in cooked
85 * mode. input is always converted to a Reader with UTF-8 encoding.
86 * @param output an OutputStream connected to the remote user, or null
87 * for System.out. output is always converted to a Writer with UTF-8
88 * encoding.
89 * @throws UnsupportedEncodingException if an exception is thrown when
90 * creating the InputStreamReader
91 */
92 public DemoApplication(final InputStream input,
93 final OutputStream output) throws UnsupportedEncodingException {
94 super(input, output);
95 addAllWidgets();
96 }
97
98 /**
99 * Public constructor.
100 *
101 * @param input the InputStream underlying 'reader'. Its available()
102 * method is used to determine if reader.read() will block or not.
103 * @param reader a Reader connected to the remote user.
104 * @param writer a PrintWriter connected to the remote user.
105 * @param setRawMode if true, set System.in into raw mode with stty.
106 * This should in general not be used. It is here solely for Demo3,
107 * which uses System.in.
108 * @throws IllegalArgumentException if input, reader, or writer are null.
109 */
110 public DemoApplication(final InputStream input, final Reader reader,
111 final PrintWriter writer, final boolean setRawMode) {
112 super(input, reader, writer, setRawMode);
113 addAllWidgets();
114 }
115
116 /**
117 * Public constructor.
118 *
119 * @param input the InputStream underlying 'reader'. Its available()
120 * method is used to determine if reader.read() will block or not.
121 * @param reader a Reader connected to the remote user.
122 * @param writer a PrintWriter connected to the remote user.
123 * @throws IllegalArgumentException if input, reader, or writer are null.
124 */
125 public DemoApplication(final InputStream input, final Reader reader,
126 final PrintWriter writer) {
127
128 this(input, reader, writer, false);
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() == 2050) {
142 new TEditColorThemeWindow(this);
143 return true;
144 }
145
146 if (menu.getId() == TMenu.MID_OPEN_FILE) {
147 try {
148 String filename = fileOpenBox(".");
149 if (filename != null) {
150 try {
151 File file = new File(filename);
152 StringBuilder fileContents = new StringBuilder();
153 Scanner scanner = new Scanner(file);
154 String EOL = System.getProperty("line.separator");
155
156 try {
157 while (scanner.hasNextLine()) {
158 fileContents.append(scanner.nextLine() + EOL);
159 }
160 new DemoTextWindow(this, filename,
161 fileContents.toString());
162 } finally {
163 scanner.close();
164 }
165 } catch (IOException e) {
166 e.printStackTrace();
167 }
168 }
169 } catch (IOException e) {
170 e.printStackTrace();
171 }
172 return true;
173 }
174 return super.onMenu(menu);
175 }
176
177 /**
178 * Public constructor.
179 *
180 * @param backendType one of the TApplication.BackendType values
181 * @throws Exception if TApplication can't instantiate the Backend.
182 */
183 public DemoApplication(final BackendType backendType) throws Exception {
184 super(backendType);
185 addAllWidgets();
186 }
187 }