dd25f023f8bbbd7f6b778de5c68b6e986c5d96c1
[nikiroo-utils.git] / src / jexer / demos / DemoApplication.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * License: LGPLv3 or later
5 *
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
9 *
10 * Copyright (C) 2015 Kevin Lamonte
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26 * 02110-1301 USA
27 *
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 * @version 1
30 */
31 package jexer.demos;
32
33 import java.io.*;
34 import java.util.*;
35
36 import jexer.*;
37 import jexer.event.*;
38 import jexer.menu.*;
39
40 /**
41 * The demo application itself.
42 */
43 public class DemoApplication extends TApplication {
44
45 /**
46 * Add all the widgets of the demo.
47 */
48 private void addAllWidgets() {
49 new DemoMainWindow(this);
50
51 // Add the menus
52 addFileMenu();
53 addEditMenu();
54
55 TMenu demoMenu = addMenu("&Demo");
56 TMenuItem item = demoMenu.addItem(2000, "&Checkable");
57 item.setCheckable(true);
58 item = demoMenu.addItem(2001, "Disabled");
59 item.setEnabled(false);
60 item = demoMenu.addItem(2002, "&Normal");
61 TSubMenu subMenu = demoMenu.addSubMenu("Sub-&Menu");
62 item = demoMenu.addItem(2010, "N&ormal A&&D");
63 item = demoMenu.addItem(2050, "Co&lors...");
64
65 item = subMenu.addItem(2000, "&Checkable (sub)");
66 item.setCheckable(true);
67 item = subMenu.addItem(2001, "Disabled (sub)");
68 item.setEnabled(false);
69 item = subMenu.addItem(2002, "&Normal (sub)");
70
71 subMenu = subMenu.addSubMenu("Sub-&Menu");
72 item = subMenu.addItem(2000, "&Checkable (sub)");
73 item.setCheckable(true);
74 item = subMenu.addItem(2001, "Disabled (sub)");
75 item.setEnabled(false);
76 item = subMenu.addItem(2002, "&Normal (sub)");
77
78 addWindowMenu();
79 }
80
81 /**
82 * Public constructor.
83 *
84 * @param input an InputStream connected to the remote user, or null for
85 * System.in. If System.in is used, then on non-Windows systems it will
86 * be put in raw mode; shutdown() will (blindly!) put System.in in cooked
87 * mode. input is always converted to a Reader with UTF-8 encoding.
88 * @param output an OutputStream connected to the remote user, or null
89 * for System.out. output is always converted to a Writer with UTF-8
90 * encoding.
91 * @throws UnsupportedEncodingException if an exception is thrown when
92 * creating the InputStreamReader
93 */
94 public DemoApplication(final InputStream input,
95 final OutputStream output) throws UnsupportedEncodingException {
96 super(input, output);
97 addAllWidgets();
98 }
99
100 /**
101 * Handle menu events.
102 *
103 * @param menu menu event
104 * @return if true, the event was processed and should not be passed onto
105 * a window
106 */
107 @Override
108 public boolean onMenu(final TMenuEvent menu) {
109
110 if (menu.getId() == 2050) {
111 new TEditColorThemeWindow(this);
112 return true;
113 }
114
115 if (menu.getId() == TMenu.MID_OPEN_FILE) {
116 try {
117 String filename = fileOpenBox(".");
118 if (filename != null) {
119 try {
120 File file = new File(filename);
121 StringBuilder fileContents = new StringBuilder();
122 Scanner scanner = new Scanner(file);
123 String EOL = System.getProperty("line.separator");
124
125 try {
126 while (scanner.hasNextLine()) {
127 fileContents.append(scanner.nextLine() + EOL);
128 }
129 new DemoTextWindow(this, filename,
130 fileContents.toString());
131 } finally {
132 scanner.close();
133 }
134 } catch (IOException e) {
135 e.printStackTrace();
136 }
137 }
138 } catch (IOException e) {
139 e.printStackTrace();
140 }
141 return true;
142 }
143 return super.onMenu(menu);
144 }
145
146 /**
147 * Public constructor.
148 *
149 * @param backendType one of the TApplication.BackendType values
150 * @throws Exception if TApplication can't instantiate the Backend.
151 */
152 public DemoApplication(final BackendType backendType) throws Exception {
153 super(backendType);
154 addAllWidgets();
155 }
156 }