Merge branch 'subtree'
[fanfix.git] / src / jexer / demos / DesktopDemoApplication.java
CommitLineData
0ee88b6d
KL
1/*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
a69ed767 6 * Copyright (C) 2019 Kevin Lamonte
0ee88b6d
KL
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 */
29package jexer.demos;
30
a69ed767
KL
31import java.io.File;
32import java.io.IOException;
33import java.util.ResourceBundle;
34import java.util.Scanner;
0ee88b6d 35
a69ed767
KL
36import jexer.TAction;
37import jexer.TApplication;
38import jexer.TWindow;
39import jexer.event.TMenuEvent;
40import jexer.menu.TMenu;
0ee88b6d
KL
41
42/**
43 * The demo application itself.
44 */
45public class DesktopDemoApplication extends TApplication {
46
a69ed767
KL
47 /**
48 * Translated strings.
49 */
8994e796 50 private static final ResourceBundle i18n = ResourceBundle.getBundle(DesktopDemoApplication.class.getName());
a69ed767 51
43ad7b6c
KL
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();
a69ed767 65 getBackend().setTitle(i18n.getString("applicationTitle"));
43ad7b6c
KL
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
0ee88b6d
KL
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
a69ed767 131 desktop.addButton(i18n.getString("removeHatch"), 2, 5,
0ee88b6d
KL
132 new TAction() {
133 public void DO() {
134 desktop.drawHatch = false;
135 }
136 }
137 );
a69ed767 138 desktop.addButton(i18n.getString("showHatch"), 2, 8,
0ee88b6d
KL
139 new TAction() {
140 public void DO() {
141 desktop.drawHatch = true;
142 }
143 }
144 );
92453213 145
a69ed767
KL
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,
92453213
KL
151 new TAction() {
152 public void DO() {
153 windowB.show();
154 }
155 }
156 );
a69ed767 157 windowA.addButton(i18n.getString("hideWindowB"), 2, 4,
92453213
KL
158 new TAction() {
159 public void DO() {
160 windowB.hide();
161 }
162 }
163 );
a69ed767 164 windowA.addButton(i18n.getString("maximizeWindowB"), 2, 6,
8c236a98
KL
165 new TAction() {
166 public void DO() {
167 windowB.maximize();
168 }
169 }
170 );
a69ed767 171 windowA.addButton(i18n.getString("restoreWindowB"), 2, 8,
8c236a98
KL
172 new TAction() {
173 public void DO() {
174 windowB.restore();
175 }
176 }
177 );
a69ed767 178 windowB.addButton(i18n.getString("showWindowA"), 2, 2,
92453213
KL
179 new TAction() {
180 public void DO() {
181 windowA.show();
182 }
183 }
184 );
a69ed767 185 windowB.addButton(i18n.getString("hideWindowA"), 2, 4,
92453213
KL
186 new TAction() {
187 public void DO() {
188 windowA.hide();
189 }
190 }
191 );
a69ed767 192 windowB.addButton(i18n.getString("maximizeWindowA"), 2, 6,
8c236a98
KL
193 new TAction() {
194 public void DO() {
195 windowA.maximize();
196 }
197 }
198 );
a69ed767 199 windowB.addButton(i18n.getString("restoreWindowA"), 2, 8,
8c236a98
KL
200 new TAction() {
201 public void DO() {
202 windowA.restore();
203 }
204 }
205 );
92453213 206
a69ed767 207 desktop.addButton(i18n.getString("showWindowB"), 25, 2,
92453213
KL
208 new TAction() {
209 public void DO() {
210 windowB.show();
211 }
212 }
213 );
a69ed767 214 desktop.addButton(i18n.getString("hideWindowB"), 25, 5,
92453213
KL
215 new TAction() {
216 public void DO() {
217 windowB.hide();
218 }
219 }
220 );
a69ed767 221 desktop.addButton(i18n.getString("showWindowA"), 25, 8,
92453213
KL
222 new TAction() {
223 public void DO() {
224 windowA.show();
225 }
226 }
227 );
a69ed767 228 desktop.addButton(i18n.getString("hideWindowA"), 25, 11,
92453213
KL
229 new TAction() {
230 public void DO() {
231 windowA.hide();
232 }
233 }
234 );
a69ed767 235 desktop.addButton(i18n.getString("createWindowC"), 25, 15,
8c236a98
KL
236 new TAction() {
237 public void DO() {
78a56d5d 238 final TWindow windowC = desktop.getApplication().addWindow(
a69ed767
KL
239 i18n.getString("windowCTitle"), 30, 20,
240 TWindow.NOCLOSEBOX);
241 windowC.addButton(i18n.getString("closeMe"), 5, 5,
78a56d5d
KL
242 new TAction() {
243 public void DO() {
244 windowC.close();
245 }
246 }
247 );
8c236a98
KL
248 }
249 }
250 );
92453213 251
a69ed767 252 desktop.addButton(i18n.getString("enableFFM"), 25, 18,
72fca17b
KL
253 new TAction() {
254 public void DO() {
255 DesktopDemoApplication.this.setFocusFollowsMouse(true);
256 }
257 }
258 );
a69ed767 259 desktop.addButton(i18n.getString("disableFFM"), 25, 21,
72fca17b
KL
260 new TAction() {
261 public void DO() {
262 DesktopDemoApplication.this.setFocusFollowsMouse(false);
263 }
264 }
265 );
0ee88b6d
KL
266 }
267
0ee88b6d 268}