#11 NOCLOSEBOX flag
[nikiroo-utils.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
KL
174
175
0ee88b6d
KL
176 }
177
178 /**
179 * Handle menu events.
180 *
181 * @param menu menu event
182 * @return if true, the event was processed and should not be passed onto
183 * a window
184 */
185 @Override
186 public boolean onMenu(final TMenuEvent menu) {
187
188 if (menu.getId() == TMenu.MID_OPEN_FILE) {
189 try {
190 String filename = fileOpenBox(".");
191 if (filename != null) {
192 try {
193 File file = new File(filename);
194 StringBuilder fileContents = new StringBuilder();
195 Scanner scanner = new Scanner(file);
196 String EOL = System.getProperty("line.separator");
197
198 try {
199 while (scanner.hasNextLine()) {
200 fileContents.append(scanner.nextLine() + EOL);
201 }
202 new DemoTextWindow(this, filename,
203 fileContents.toString());
204 } finally {
205 scanner.close();
206 }
207 } catch (IOException e) {
208 e.printStackTrace();
209 }
210 }
211 } catch (IOException e) {
212 e.printStackTrace();
213 }
214 return true;
215 }
216 return super.onMenu(menu);
217 }
218
219 /**
220 * Public constructor.
221 *
222 * @param backendType one of the TApplication.BackendType values
223 * @throws Exception if TApplication can't instantiate the Backend.
224 */
225 public DesktopDemoApplication(final BackendType backendType) throws Exception {
226 super(backendType);
227 addAllWidgets();
228 getBackend().setTitle("Jexer Demo Application");
229 }
230}