Add 'src/jexer/' from commit 'cf01c92f5809a0732409e280fb0f32f27393618d'
[fanfix.git] / src / jexer / demos / DesktopDemoApplication.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2019 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.File;
32 import java.io.IOException;
33 import java.util.ResourceBundle;
34 import java.util.Scanner;
35
36 import jexer.TAction;
37 import jexer.TApplication;
38 import jexer.TWindow;
39 import jexer.event.TMenuEvent;
40 import jexer.menu.TMenu;
41
42 /**
43 * The demo application itself.
44 */
45 public class DesktopDemoApplication extends TApplication {
46
47 /**
48 * Translated strings.
49 */
50 private static final ResourceBundle i18n = ResourceBundle.getBundle(DesktopDemoApplication.class.getName());
51
52 // ------------------------------------------------------------------------
53 // Constructors -----------------------------------------------------------
54 // ------------------------------------------------------------------------
55
56 /**
57 * Public constructor.
58 *
59 * @param backendType one of the TApplication.BackendType values
60 * @throws Exception if TApplication can't instantiate the Backend.
61 */
62 public DesktopDemoApplication(final BackendType backendType) throws Exception {
63 super(backendType);
64 addAllWidgets();
65 getBackend().setTitle(i18n.getString("applicationTitle"));
66 }
67
68 // ------------------------------------------------------------------------
69 // TApplication -----------------------------------------------------------
70 // ------------------------------------------------------------------------
71
72 /**
73 * Handle menu events.
74 *
75 * @param menu menu event
76 * @return if true, the event was processed and should not be passed onto
77 * a window
78 */
79 @Override
80 public boolean onMenu(final TMenuEvent menu) {
81
82 if (menu.getId() == TMenu.MID_OPEN_FILE) {
83 try {
84 String filename = fileOpenBox(".");
85 if (filename != null) {
86 try {
87 File file = new File(filename);
88 StringBuilder fileContents = new StringBuilder();
89 Scanner scanner = new Scanner(file);
90 String EOL = System.getProperty("line.separator");
91
92 try {
93 while (scanner.hasNextLine()) {
94 fileContents.append(scanner.nextLine() + EOL);
95 }
96 new DemoTextWindow(this, filename,
97 fileContents.toString());
98 } finally {
99 scanner.close();
100 }
101 } catch (IOException e) {
102 e.printStackTrace();
103 }
104 }
105 } catch (IOException e) {
106 e.printStackTrace();
107 }
108 return true;
109 }
110 return super.onMenu(menu);
111 }
112
113 // ------------------------------------------------------------------------
114 // DesktopDemoApplication -------------------------------------------------
115 // ------------------------------------------------------------------------
116
117 /**
118 * Add all the widgets of the demo.
119 */
120 private void addAllWidgets() {
121
122 // Add the menus
123 addFileMenu();
124 addEditMenu();
125 addWindowMenu();
126 addHelpMenu();
127
128 final DesktopDemo desktop = new DesktopDemo(this);
129 setDesktop(desktop);
130
131 desktop.addButton(i18n.getString("removeHatch"), 2, 5,
132 new TAction() {
133 public void DO() {
134 desktop.drawHatch = false;
135 }
136 }
137 );
138 desktop.addButton(i18n.getString("showHatch"), 2, 8,
139 new TAction() {
140 public void DO() {
141 desktop.drawHatch = true;
142 }
143 }
144 );
145
146 final TWindow windowA = addWindow(i18n.getString("windowATitle"),
147 25, 14);
148 final TWindow windowB = addWindow(i18n.getString("windowBTitle"),
149 25, 14);
150 windowA.addButton(i18n.getString("showWindowB"), 2, 2,
151 new TAction() {
152 public void DO() {
153 windowB.show();
154 }
155 }
156 );
157 windowA.addButton(i18n.getString("hideWindowB"), 2, 4,
158 new TAction() {
159 public void DO() {
160 windowB.hide();
161 }
162 }
163 );
164 windowA.addButton(i18n.getString("maximizeWindowB"), 2, 6,
165 new TAction() {
166 public void DO() {
167 windowB.maximize();
168 }
169 }
170 );
171 windowA.addButton(i18n.getString("restoreWindowB"), 2, 8,
172 new TAction() {
173 public void DO() {
174 windowB.restore();
175 }
176 }
177 );
178 windowB.addButton(i18n.getString("showWindowA"), 2, 2,
179 new TAction() {
180 public void DO() {
181 windowA.show();
182 }
183 }
184 );
185 windowB.addButton(i18n.getString("hideWindowA"), 2, 4,
186 new TAction() {
187 public void DO() {
188 windowA.hide();
189 }
190 }
191 );
192 windowB.addButton(i18n.getString("maximizeWindowA"), 2, 6,
193 new TAction() {
194 public void DO() {
195 windowA.maximize();
196 }
197 }
198 );
199 windowB.addButton(i18n.getString("restoreWindowA"), 2, 8,
200 new TAction() {
201 public void DO() {
202 windowA.restore();
203 }
204 }
205 );
206
207 desktop.addButton(i18n.getString("showWindowB"), 25, 2,
208 new TAction() {
209 public void DO() {
210 windowB.show();
211 }
212 }
213 );
214 desktop.addButton(i18n.getString("hideWindowB"), 25, 5,
215 new TAction() {
216 public void DO() {
217 windowB.hide();
218 }
219 }
220 );
221 desktop.addButton(i18n.getString("showWindowA"), 25, 8,
222 new TAction() {
223 public void DO() {
224 windowA.show();
225 }
226 }
227 );
228 desktop.addButton(i18n.getString("hideWindowA"), 25, 11,
229 new TAction() {
230 public void DO() {
231 windowA.hide();
232 }
233 }
234 );
235 desktop.addButton(i18n.getString("createWindowC"), 25, 15,
236 new TAction() {
237 public void DO() {
238 final TWindow windowC = desktop.getApplication().addWindow(
239 i18n.getString("windowCTitle"), 30, 20,
240 TWindow.NOCLOSEBOX);
241 windowC.addButton(i18n.getString("closeMe"), 5, 5,
242 new TAction() {
243 public void DO() {
244 windowC.close();
245 }
246 }
247 );
248 }
249 }
250 );
251
252 desktop.addButton(i18n.getString("enableFFM"), 25, 18,
253 new TAction() {
254 public void DO() {
255 DesktopDemoApplication.this.setFocusFollowsMouse(true);
256 }
257 }
258 );
259 desktop.addButton(i18n.getString("disableFFM"), 25, 21,
260 new TAction() {
261 public void DO() {
262 DesktopDemoApplication.this.setFocusFollowsMouse(false);
263 }
264 }
265 );
266 }
267
268 }