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