d01b9442c0526cc9ce2a67af0be5e8f1a2a514c7
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
.ArrayList
;
32 import java
.util
.List
;
34 import jexer
.event
.TCommandEvent
;
35 import jexer
.event
.TInputEvent
;
36 import static jexer
.TCommand
.*;
39 * MultiBackend mirrors its I/O to several backends.
41 public class MultiBackend
implements Backend
{
43 // ------------------------------------------------------------------------
44 // Variables --------------------------------------------------------------
45 // ------------------------------------------------------------------------
50 private MultiScreen multiScreen
;
53 * The list of backends to use.
55 private List
<Backend
> backends
= new ArrayList
<Backend
>();
58 * The SessionInfo to return.
60 private SessionInfo sessionInfo
;
62 // ------------------------------------------------------------------------
63 // Constructors -----------------------------------------------------------
64 // ------------------------------------------------------------------------
67 * Public constructor requires one backend. Note that this backend's
68 * screen will be replaced with a MultiScreen.
70 * @param backend the backend to add
72 public MultiBackend(final Backend backend
) {
73 backends
.add(backend
);
74 if (backend
instanceof TWindowBackend
) {
75 multiScreen
= new MultiScreen(((TWindowBackend
) backend
).getOtherScreen());
77 multiScreen
= new MultiScreen(backend
.getScreen());
79 if (backend
instanceof GenericBackend
) {
80 ((GenericBackend
) backend
).abortOnDisconnect
= false;
82 sessionInfo
= backend
.getSessionInfo();
85 // ------------------------------------------------------------------------
86 // Backend ----------------------------------------------------------------
87 // ------------------------------------------------------------------------
90 * Getter for sessionInfo.
92 * @return the SessionInfo
94 public SessionInfo
getSessionInfo() {
103 public Screen
getScreen() {
108 * Subclasses must provide an implementation that syncs the logical
109 * screen to the physical device.
111 public void flushScreen() {
112 for (Backend backend
: backends
) {
113 backend
.flushScreen();
118 * Check if there are events in the queue.
120 * @return if true, getEvents() has something to return to the application
122 public boolean hasEvents() {
123 if (backends
.size() == 0) {
126 for (Backend backend
: backends
) {
127 if (backend
.hasEvents()) {
135 * Subclasses must provide an implementation to get keyboard, mouse, and
136 * screen resize events.
138 * @param queue list to append new events to
140 public void getEvents(List
<TInputEvent
> queue
) {
141 List
<Backend
> backendsToRemove
= null;
142 for (Backend backend
: backends
) {
143 if (backend
.hasEvents()) {
144 backend
.getEvents(queue
);
146 // This default backend assumes a single user, and if that
147 // user becomes disconnected we should terminate the
149 if (queue
.size() > 0) {
150 TInputEvent event
= queue
.get(queue
.size() - 1);
151 if (event
instanceof TCommandEvent
) {
152 TCommandEvent command
= (TCommandEvent
) event
;
153 if (command
.equals(cmBackendDisconnect
)) {
154 if (backendsToRemove
== null) {
155 backendsToRemove
= new ArrayList
<Backend
>();
157 backendsToRemove
.add(backend
);
163 if (backendsToRemove
!= null) {
164 for (Backend backend
: backendsToRemove
) {
165 multiScreen
.removeScreen(backend
.getScreen());
166 backends
.remove(backend
);
170 if (backends
.size() == 0) {
171 queue
.add(new TCommandEvent(cmAbort
));
176 * Subclasses must provide an implementation that closes sockets,
177 * restores console, etc.
179 public void shutdown() {
180 for (Backend backend
: backends
) {
186 * Subclasses must provide an implementation that sets the window title.
188 * @param title the new title
190 public void setTitle(final String title
) {
191 for (Backend backend
: backends
) {
192 backend
.setTitle(title
);
197 * Set listener to a different Object.
199 * @param listener the new listening object that run() wakes up on new
202 public void setListener(final Object listener
) {
203 for (Backend backend
: backends
) {
204 backend
.setListener(listener
);
209 * Reload backend options from System properties.
211 public void reloadOptions() {
212 for (Backend backend
: backends
) {
213 backend
.reloadOptions();
217 // ------------------------------------------------------------------------
218 // MultiBackend -----------------------------------------------------------
219 // ------------------------------------------------------------------------
222 * Add a backend to the list.
224 * @param backend the backend to add
226 public void addBackend(final Backend backend
) {
227 backends
.add(backend
);
228 if (backend
instanceof TWindowBackend
) {
229 multiScreen
.addScreen(((TWindowBackend
) backend
).getOtherScreen());
231 multiScreen
.addScreen(backend
.getScreen());
233 if (backend
instanceof GenericBackend
) {
234 ((GenericBackend
) backend
).abortOnDisconnect
= false;
239 * Remove a backend from the list.
241 * @param backend the backend to remove
243 public void removeBackend(final Backend backend
) {
244 if (backends
.size() > 1) {
245 if (backend
instanceof TWindowBackend
) {
246 multiScreen
.removeScreen(((TWindowBackend
) backend
).getOtherScreen());
248 multiScreen
.removeScreen(backend
.getScreen());
250 backends
.remove(backend
);