#14 TDesktop bug fixes, more TWindow API
[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
KL
71
72 final TWindow windowA = addWindow("Window A", 20, 14);
73 final TWindow windowB = addWindow("Window B", 20, 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 windowB.addButton("&Show Window A", 2, 2,
89 new TAction() {
90 public void DO() {
91 windowA.show();
92 }
93 }
94 );
95 windowB.addButton("H&ide Window A", 2, 4,
96 new TAction() {
97 public void DO() {
98 windowA.hide();
99 }
100 }
101 );
102
103 desktop.addButton("&Show Window B", 25, 2,
104 new TAction() {
105 public void DO() {
106 windowB.show();
107 }
108 }
109 );
110 desktop.addButton("H&ide Window B", 25, 5,
111 new TAction() {
112 public void DO() {
113 windowB.hide();
114 }
115 }
116 );
117 desktop.addButton("Sh&ow Window A", 25, 8,
118 new TAction() {
119 public void DO() {
120 windowA.show();
121 }
122 }
123 );
124 desktop.addButton("Hid&e Window A", 25, 11,
125 new TAction() {
126 public void DO() {
127 windowA.hide();
128 }
129 }
130 );
131
132
0ee88b6d
KL
133 }
134
135 /**
136 * Handle menu events.
137 *
138 * @param menu menu event
139 * @return if true, the event was processed and should not be passed onto
140 * a window
141 */
142 @Override
143 public boolean onMenu(final TMenuEvent menu) {
144
145 if (menu.getId() == TMenu.MID_OPEN_FILE) {
146 try {
147 String filename = fileOpenBox(".");
148 if (filename != null) {
149 try {
150 File file = new File(filename);
151 StringBuilder fileContents = new StringBuilder();
152 Scanner scanner = new Scanner(file);
153 String EOL = System.getProperty("line.separator");
154
155 try {
156 while (scanner.hasNextLine()) {
157 fileContents.append(scanner.nextLine() + EOL);
158 }
159 new DemoTextWindow(this, filename,
160 fileContents.toString());
161 } finally {
162 scanner.close();
163 }
164 } catch (IOException e) {
165 e.printStackTrace();
166 }
167 }
168 } catch (IOException e) {
169 e.printStackTrace();
170 }
171 return true;
172 }
173 return super.onMenu(menu);
174 }
175
176 /**
177 * Public constructor.
178 *
179 * @param backendType one of the TApplication.BackendType values
180 * @throws Exception if TApplication can't instantiate the Backend.
181 */
182 public DesktopDemoApplication(final BackendType backendType) throws Exception {
183 super(backendType);
184 addAllWidgets();
185 getBackend().setTitle("Jexer Demo Application");
186 }
187}