2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 package jexer
.backend
;
31 import java
.util
.LinkedList
;
32 import java
.util
.List
;
34 import jexer
.event
.TInputEvent
;
37 * MultiBackend mirrors its I/O to several backends.
39 public class MultiBackend
implements Backend
{
41 // ------------------------------------------------------------------------
42 // Variables --------------------------------------------------------------
43 // ------------------------------------------------------------------------
48 private MultiScreen multiScreen
;
51 * The list of backends to use.
53 private List
<Backend
> backends
= new LinkedList
<Backend
>();
55 // ------------------------------------------------------------------------
56 // Constructors -----------------------------------------------------------
57 // ------------------------------------------------------------------------
60 * Public constructor requires one backend. Note that this backend's
61 * screen will be replaced with a MultiScreen.
63 * @param backend the backend to add
65 public MultiBackend(final Backend backend
) {
66 backends
.add(backend
);
67 if (backend
instanceof TWindowBackend
) {
68 multiScreen
= new MultiScreen(((TWindowBackend
) backend
).getOtherScreen());
70 multiScreen
= new MultiScreen(backend
.getScreen());
74 // ------------------------------------------------------------------------
75 // Backend ----------------------------------------------------------------
76 // ------------------------------------------------------------------------
79 * Getter for sessionInfo.
81 * @return the SessionInfo
83 public SessionInfo
getSessionInfo() {
84 return backends
.get(0).getSessionInfo();
92 public Screen
getScreen() {
97 * Subclasses must provide an implementation that syncs the logical
98 * screen to the physical device.
100 public void flushScreen() {
101 for (Backend backend
: backends
) {
102 backend
.flushScreen();
107 * Check if there are events in the queue.
109 * @return if true, getEvents() has something to return to the application
111 public boolean hasEvents() {
112 for (Backend backend
: backends
) {
113 if (backend
.hasEvents()) {
121 * Subclasses must provide an implementation to get keyboard, mouse, and
122 * screen resize events.
124 * @param queue list to append new events to
126 public void getEvents(List
<TInputEvent
> queue
) {
127 for (Backend backend
: backends
) {
128 backend
.getEvents(queue
);
133 * Subclasses must provide an implementation that closes sockets,
134 * restores console, etc.
136 public void shutdown() {
137 for (Backend backend
: backends
) {
143 * Subclasses must provide an implementation that sets the window title.
145 * @param title the new title
147 public void setTitle(final String title
) {
148 for (Backend backend
: backends
) {
149 backend
.setTitle(title
);
154 * Set listener to a different Object.
156 * @param listener the new listening object that run() wakes up on new
159 public void setListener(final Object listener
) {
160 for (Backend backend
: backends
) {
161 backend
.setListener(listener
);
166 * Reload backend options from System properties.
168 public void reloadOptions() {
169 for (Backend backend
: backends
) {
170 backend
.reloadOptions();
174 // ------------------------------------------------------------------------
175 // MultiBackend -----------------------------------------------------------
176 // ------------------------------------------------------------------------
179 * Add a backend to the list.
181 * @param backend the backend to add
183 public void addBackend(final Backend backend
) {
184 backends
.add(backend
);
185 if (backend
instanceof TWindowBackend
) {
186 multiScreen
.addScreen(((TWindowBackend
) backend
).getOtherScreen());
188 multiScreen
.addScreen(backend
.getScreen());
193 * Remove a backend from the list.
195 * @param backend the backend to remove
197 public void removeBackend(final Backend backend
) {
198 if (backends
.size() > 1) {
199 if (backend
instanceof TWindowBackend
) {
200 multiScreen
.removeScreen(((TWindowBackend
) backend
).getOtherScreen());
202 multiScreen
.removeScreen(backend
.getScreen());
204 backends
.remove(backend
);