e63abc1b848c37d8a9ea7b06346eefe875af7f31
[nikiroo-utils.git] / Demo5.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2019 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.awt.Font;
32 import java.awt.event.WindowEvent;
33 import java.awt.event.WindowListener;
34 import java.util.ResourceBundle;
35 import javax.swing.JFrame;
36 import javax.swing.JPanel;
37 import javax.swing.JSplitPane;
38
39 import jexer.backend.SwingBackend;
40
41 /**
42 * This class is the main driver for a simple demonstration of Jexer's
43 * capabilities. It shows two Swing demo applications running in the same
44 * Swing UI.
45 */
46 public class Demo5 implements WindowListener {
47
48 /**
49 * Translated strings.
50 */
51 private static final ResourceBundle i18n = ResourceBundle.getBundle(Demo5.class.getName());
52
53 // ------------------------------------------------------------------------
54 // Variables --------------------------------------------------------------
55 // ------------------------------------------------------------------------
56
57 /**
58 * The first demo application instance.
59 */
60 DemoApplication app1 = null;
61
62 /**
63 * The second demo application instance.
64 */
65 DemoApplication app2 = null;
66
67 // ------------------------------------------------------------------------
68 // WindowListener ---------------------------------------------------------
69 // ------------------------------------------------------------------------
70
71 /**
72 * Pass window events into the event queue.
73 *
74 * @param event window event received
75 */
76 public void windowActivated(final WindowEvent event) {
77 // Ignore
78 }
79
80 /**
81 * Pass window events into the event queue.
82 *
83 * @param event window event received
84 */
85 public void windowClosed(final WindowEvent event) {
86 // Ignore
87 }
88
89 /**
90 * Pass window events into the event queue.
91 *
92 * @param event window event received
93 */
94 public void windowClosing(final WindowEvent event) {
95 if (app1 != null) {
96 app1.exit();
97 }
98 if (app2 != null) {
99 app2.exit();
100 }
101 }
102
103 /**
104 * Pass window events into the event queue.
105 *
106 * @param event window event received
107 */
108 public void windowDeactivated(final WindowEvent event) {
109 // Ignore
110 }
111
112 /**
113 * Pass window events into the event queue.
114 *
115 * @param event window event received
116 */
117 public void windowDeiconified(final WindowEvent event) {
118 // Ignore
119 }
120
121 /**
122 * Pass window events into the event queue.
123 *
124 * @param event window event received
125 */
126 public void windowIconified(final WindowEvent event) {
127 // Ignore
128 }
129
130 /**
131 * Pass window events into the event queue.
132 *
133 * @param event window event received
134 */
135 public void windowOpened(final WindowEvent event) {
136 // Ignore
137 }
138
139 // ------------------------------------------------------------------------
140 // Demo5 ------------------------------------------------------------------
141 // ------------------------------------------------------------------------
142
143 /**
144 * Run two demo applications in separate panes.
145 */
146 private void addApplications() {
147
148 /*
149 * In this demo we will create two swing panels with two
150 * independently running applications, each with a different font
151 * size.
152 */
153
154 /*
155 * First we create a panel to put it on. We need this to pass to
156 * SwingBackend's constructor, so that it knows not to create a new
157 * frame.
158 */
159 JPanel app1Panel = new JPanel();
160
161 /*
162 * Next, we create the Swing backend. The "listener" (second
163 * argument, set to null) is what the backend wakes up on every event
164 * received. Typically this is the TApplication. TApplication sets
165 * it in its constructor, so we can pass null here and be fine.
166 */
167 SwingBackend app1Backend = new SwingBackend(app1Panel, null,
168 80, 25, 16);
169 // Now that we have the backend, construct the TApplication.
170 app1 = new DemoApplication(app1Backend);
171
172 /*
173 * The second panel is the same sequence, except that we also change
174 * the font from the default Terminus to JVM monospaced.
175 */
176 JPanel app2Panel = new JPanel();
177 SwingBackend app2Backend = new SwingBackend(app2Panel, null,
178 80, 25, 18);
179 app2 = new DemoApplication(app2Backend);
180 Font font = new Font(Font.MONOSPACED, Font.PLAIN, 18);
181 app2Backend.setFont(font);
182
183 /*
184 * Now that the applications are ready, spin them off on their
185 * threads.
186 */
187 (new Thread(app1)).start();
188 (new Thread(app2)).start();
189
190 /*
191 * The rest of this is standard Swing. Set up a frame, a split pane,
192 * put each of the panels on it, and make it visible.
193 */
194 JFrame frame = new JFrame();
195 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
196 frame.addWindowListener(this);
197 JSplitPane mainPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
198 app1Panel, app2Panel);
199 mainPane.setOneTouchExpandable(true);
200 mainPane.setDividerLocation(500);
201 mainPane.setDividerSize(6);
202 mainPane.setBorder(null);
203 frame.setContentPane(mainPane);
204
205 frame.setTitle(i18n.getString("frameTitle"));
206 frame.setSize(1000, 640);
207 frame.setVisible(true);
208 }
209
210 /**
211 * Main entry point.
212 *
213 * @param args Command line arguments
214 */
215 public static void main(final String [] args) {
216 try {
217 Demo5 demo = new Demo5();
218 demo.addApplications();
219 } catch (Exception e) {
220 e.printStackTrace();
221 }
222 }
223
224 }