#16 Refactor Swing backend, demo of multiple TApplications in one Swing frame
[nikiroo-utils.git] / src / jexer / demos / Demo5.java
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 */
29 package jexer.demos;
30
31 import java.awt.event.WindowEvent;
32 import java.awt.event.WindowListener;
33 import javax.swing.JFrame;
34 import javax.swing.JPanel;
35 import javax.swing.JSplitPane;
36
37 import jexer.backend.SwingBackend;
38
39 /**
40 * This class is the main driver for a simple demonstration of Jexer's
41 * capabilities. It shows two Swing demo applications running in the same
42 * Swing UI.
43 */
44 public class Demo5 implements WindowListener {
45
46 /**
47 * The first demo application instance.
48 */
49 DemoApplication app1 = null;
50
51 /**
52 * The second demo application instance.
53 */
54 DemoApplication app2 = null;
55
56 /**
57 * Pass window events into the event queue.
58 *
59 * @param event window event received
60 */
61 public void windowActivated(final WindowEvent event) {
62 // Ignore
63 }
64
65 /**
66 * Pass window events into the event queue.
67 *
68 * @param event window event received
69 */
70 public void windowClosed(final WindowEvent event) {
71 // Ignore
72 }
73
74 /**
75 * Pass window events into the event queue.
76 *
77 * @param event window event received
78 */
79 public void windowClosing(final WindowEvent event) {
80 if (app1 != null) {
81 app1.exit();
82 }
83 if (app2 != null) {
84 app2.exit();
85 }
86 }
87
88 /**
89 * Pass window events into the event queue.
90 *
91 * @param event window event received
92 */
93 public void windowDeactivated(final WindowEvent event) {
94 // Ignore
95 }
96
97 /**
98 * Pass window events into the event queue.
99 *
100 * @param event window event received
101 */
102 public void windowDeiconified(final WindowEvent event) {
103 // Ignore
104 }
105
106 /**
107 * Pass window events into the event queue.
108 *
109 * @param event window event received
110 */
111 public void windowIconified(final WindowEvent event) {
112 // Ignore
113 }
114
115 /**
116 * Pass window events into the event queue.
117 *
118 * @param event window event received
119 */
120 public void windowOpened(final WindowEvent event) {
121 // Ignore
122 }
123
124 /**
125 * Run two demo applications in separate panes.
126 */
127 private void addApplications() {
128
129 // Spin up the frame
130 JFrame frame = new JFrame();
131 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
132 frame.addWindowListener(this);
133
134 // Create two panels with two applications, each with a different
135 // font size.
136 JPanel app1Panel = new JPanel();
137 SwingBackend app1Backend = new SwingBackend(app1Panel, new Object(),
138 80, 25, 16);
139 app1 = new DemoApplication(app1Backend);
140 app1Backend.setListener(app1);
141
142 JPanel app2Panel = new JPanel();
143 SwingBackend app2Backend = new SwingBackend(app2Panel, new Object(),
144 80, 25, 18);
145 app2 = new DemoApplication(app2Backend);
146 app1Backend.setListener(app2);
147 (new Thread(app1)).start();
148 (new Thread(app2)).start();
149
150 JSplitPane mainPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
151 app1Panel, app2Panel);
152 mainPane.setOneTouchExpandable(true);
153 mainPane.setDividerLocation(500);
154 mainPane.setDividerSize(6);
155 mainPane.setBorder(null);
156 frame.setContentPane(mainPane);
157
158 frame.setTitle("Two Jexer Apps In One Swing UI");
159 frame.setSize(1000, 640);
160 frame.setVisible(true);
161 }
162
163 /**
164 * Main entry point.
165 *
166 * @param args Command line arguments
167 */
168 public static void main(final String [] args) {
169 try {
170 Demo5 demo = new Demo5();
171 demo.addApplications();
172 } catch (Exception e) {
173 e.printStackTrace();
174 }
175 }
176
177 }