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