Commit | Line | Data |
---|---|---|
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 | */ | |
29 | package jexer.demos; | |
30 | ||
55b4f29b | 31 | import java.io.*; |
a043164f | 32 | import java.util.*; |
55b4f29b | 33 | |
e3dfbd23 | 34 | import jexer.*; |
a043164f | 35 | import jexer.event.*; |
e3dfbd23 KL |
36 | import jexer.menu.*; |
37 | ||
38 | /** | |
39 | * The demo application itself. | |
40 | */ | |
7668cb45 | 41 | public class DemoApplication extends TApplication { |
e3dfbd23 KL |
42 | |
43 | /** | |
55b4f29b | 44 | * Add all the widgets of the demo. |
e3dfbd23 | 45 | */ |
55b4f29b | 46 | private void addAllWidgets() { |
e3dfbd23 KL |
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"); | |
3649b921 | 61 | item = demoMenu.addItem(2050, "Co&lors..."); |
e3dfbd23 KL |
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(); | |
55d2b2c2 | 77 | addHelpMenu(); |
55b4f29b | 78 | } |
a043164f | 79 | |
55b4f29b KL |
80 | /** |
81 | * Public constructor. | |
82 | * | |
83 | * @param input an InputStream connected to the remote user, or null for | |
84 | * System.in. If System.in is used, then on non-Windows systems it will | |
85 | * be put in raw mode; shutdown() will (blindly!) put System.in in cooked | |
86 | * mode. input is always converted to a Reader with UTF-8 encoding. | |
87 | * @param output an OutputStream connected to the remote user, or null | |
88 | * for System.out. output is always converted to a Writer with UTF-8 | |
89 | * encoding. | |
90 | * @throws UnsupportedEncodingException if an exception is thrown when | |
91 | * creating the InputStreamReader | |
92 | */ | |
93 | public DemoApplication(final InputStream input, | |
94 | final OutputStream output) throws UnsupportedEncodingException { | |
95 | super(input, output); | |
96 | addAllWidgets(); | |
55d2b2c2 KL |
97 | |
98 | getBackend().setTitle("Jexer Demo Application"); | |
55b4f29b | 99 | } |
a043164f | 100 | |
6985c572 KL |
101 | /** |
102 | * Public constructor. | |
103 | * | |
104 | * @param input the InputStream underlying 'reader'. Its available() | |
105 | * method is used to determine if reader.read() will block or not. | |
106 | * @param reader a Reader connected to the remote user. | |
107 | * @param writer a PrintWriter connected to the remote user. | |
108 | * @param setRawMode if true, set System.in into raw mode with stty. | |
109 | * This should in general not be used. It is here solely for Demo3, | |
110 | * which uses System.in. | |
111 | * @throws IllegalArgumentException if input, reader, or writer are null. | |
112 | */ | |
113 | public DemoApplication(final InputStream input, final Reader reader, | |
114 | final PrintWriter writer, final boolean setRawMode) { | |
115 | super(input, reader, writer, setRawMode); | |
116 | addAllWidgets(); | |
55d2b2c2 KL |
117 | |
118 | getBackend().setTitle("Jexer Demo Application"); | |
6985c572 KL |
119 | } |
120 | ||
121 | /** | |
122 | * Public constructor. | |
123 | * | |
124 | * @param input the InputStream underlying 'reader'. Its available() | |
125 | * method is used to determine if reader.read() will block or not. | |
126 | * @param reader a Reader connected to the remote user. | |
127 | * @param writer a PrintWriter connected to the remote user. | |
128 | * @throws IllegalArgumentException if input, reader, or writer are null. | |
129 | */ | |
130 | public DemoApplication(final InputStream input, final Reader reader, | |
131 | final PrintWriter writer) { | |
132 | ||
133 | this(input, reader, writer, false); | |
134 | } | |
135 | ||
a043164f KL |
136 | /** |
137 | * Handle menu events. | |
138 | * | |
139 | * @param menu menu event | |
140 | * @return if true, the event was processed and should not be passed onto | |
141 | * a window | |
142 | */ | |
143 | @Override | |
329fd62e | 144 | public boolean onMenu(final TMenuEvent menu) { |
3649b921 KL |
145 | |
146 | if (menu.getId() == 2050) { | |
147 | new TEditColorThemeWindow(this); | |
148 | return true; | |
149 | } | |
150 | ||
a043164f KL |
151 | if (menu.getId() == TMenu.MID_OPEN_FILE) { |
152 | try { | |
153 | String filename = fileOpenBox("."); | |
154 | if (filename != null) { | |
155 | try { | |
156 | File file = new File(filename); | |
157 | StringBuilder fileContents = new StringBuilder(); | |
158 | Scanner scanner = new Scanner(file); | |
159 | String EOL = System.getProperty("line.separator"); | |
160 | ||
161 | try { | |
329fd62e | 162 | while (scanner.hasNextLine()) { |
a043164f KL |
163 | fileContents.append(scanner.nextLine() + EOL); |
164 | } | |
165 | new DemoTextWindow(this, filename, | |
166 | fileContents.toString()); | |
167 | } finally { | |
168 | scanner.close(); | |
169 | } | |
170 | } catch (IOException e) { | |
171 | e.printStackTrace(); | |
172 | } | |
173 | } | |
174 | } catch (IOException e) { | |
175 | e.printStackTrace(); | |
176 | } | |
177 | return true; | |
178 | } | |
179 | return super.onMenu(menu); | |
180 | } | |
181 | ||
55b4f29b KL |
182 | /** |
183 | * Public constructor. | |
184 | * | |
185 | * @param backendType one of the TApplication.BackendType values | |
186 | * @throws Exception if TApplication can't instantiate the Backend. | |
187 | */ | |
329fd62e | 188 | public DemoApplication(final BackendType backendType) throws Exception { |
55b4f29b KL |
189 | super(backendType); |
190 | addAllWidgets(); | |
55d2b2c2 | 191 | getBackend().setTitle("Jexer Demo Application"); |
e3dfbd23 KL |
192 | } |
193 | } |