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