#14 TDesktop working, TWindow hide/show/max/restore working
[nikiroo-utils.git] / src / jexer / demos / DesktopDemoApplication.java
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 */
29 package jexer.demos;
30
31 import java.io.*;
32 import java.util.*;
33
34 import jexer.*;
35 import jexer.event.*;
36 import jexer.menu.*;
37
38 /**
39 * The demo application itself.
40 */
41 public 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 );
71
72 final TWindow windowA = addWindow("Window A", 25, 14);
73 final TWindow windowB = addWindow("Window B", 25, 14);
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 );
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 );
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 );
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 );
130
131 desktop.addButton("S&how Window B", 25, 2,
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 );
159 desktop.addButton("&Create Window C", 25, 15,
160 new TAction() {
161 public void DO() {
162 desktop.getApplication().addWindow("Window C",
163 30, 20);
164 }
165 }
166 );
167
168
169 }
170
171 /**
172 * Handle menu events.
173 *
174 * @param menu menu event
175 * @return if true, the event was processed and should not be passed onto
176 * a window
177 */
178 @Override
179 public boolean onMenu(final TMenuEvent menu) {
180
181 if (menu.getId() == TMenu.MID_OPEN_FILE) {
182 try {
183 String filename = fileOpenBox(".");
184 if (filename != null) {
185 try {
186 File file = new File(filename);
187 StringBuilder fileContents = new StringBuilder();
188 Scanner scanner = new Scanner(file);
189 String EOL = System.getProperty("line.separator");
190
191 try {
192 while (scanner.hasNextLine()) {
193 fileContents.append(scanner.nextLine() + EOL);
194 }
195 new DemoTextWindow(this, filename,
196 fileContents.toString());
197 } finally {
198 scanner.close();
199 }
200 } catch (IOException e) {
201 e.printStackTrace();
202 }
203 }
204 } catch (IOException e) {
205 e.printStackTrace();
206 }
207 return true;
208 }
209 return super.onMenu(menu);
210 }
211
212 /**
213 * Public constructor.
214 *
215 * @param backendType one of the TApplication.BackendType values
216 * @throws Exception if TApplication can't instantiate the Backend.
217 */
218 public DesktopDemoApplication(final BackendType backendType) throws Exception {
219 super(backendType);
220 addAllWidgets();
221 getBackend().setTitle("Jexer Demo Application");
222 }
223 }