MultiBackend and MultiScreen
[nikiroo-utils.git] / src / jexer / backend / MultiBackend.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.backend;
30
31 import java.util.LinkedList;
32 import java.util.List;
33
34 import jexer.event.TInputEvent;
35
36 /**
37 * MultiBackend mirrors its I/O to several backends.
38 */
39 public class MultiBackend implements Backend {
40
41 /**
42 * The screen to use.
43 */
44 private MultiScreen multiScreen;
45
46 /**
47 * The list of backends to use.
48 */
49 private List<Backend> backends = new LinkedList<Backend>();
50
51 /**
52 * Public constructor requires one backend. Note that this backend's
53 * screen will be replaced with a MultiScreen.
54 *
55 * @param backend the backend to add
56 */
57 public MultiBackend(final Backend backend) {
58 backends.add(backend);
59 multiScreen = new MultiScreen(backend.getScreen());
60 }
61
62 /**
63 * Add a backend to the list.
64 *
65 * @param backend the backend to add
66 */
67 public void addBackend(final Backend backend) {
68 backends.add(backend);
69 multiScreen.addScreen(backend.getScreen());
70 }
71
72 /**
73 * Remove a backend from the list.
74 *
75 * @param backend the backend to remove
76 */
77 public void removeBackend(final Backend backend) {
78 if (backends.size() > 1) {
79 multiScreen.removeScreen(backend.getScreen());
80 backends.remove(backend);
81 }
82 }
83
84 /**
85 * Getter for sessionInfo.
86 *
87 * @return the SessionInfo
88 */
89 public final SessionInfo getSessionInfo() {
90 return backends.get(0).getSessionInfo();
91 }
92
93 /**
94 * Getter for screen.
95 *
96 * @return the Screen
97 */
98 public final Screen getScreen() {
99 return multiScreen;
100 }
101
102 /**
103 * Subclasses must provide an implementation that syncs the logical
104 * screen to the physical device.
105 */
106 public void flushScreen() {
107 for (Backend backend: backends) {
108 backend.flushScreen();
109 }
110 }
111
112 /**
113 * Subclasses must provide an implementation to get keyboard, mouse, and
114 * screen resize events.
115 *
116 * @param queue list to append new events to
117 */
118 public void getEvents(List<TInputEvent> queue) {
119 for (Backend backend: backends) {
120 backend.getEvents(queue);
121 }
122 }
123
124 /**
125 * Subclasses must provide an implementation that closes sockets,
126 * restores console, etc.
127 */
128 public void shutdown() {
129 for (Backend backend: backends) {
130 backend.shutdown();
131 }
132 }
133
134 /**
135 * Subclasses must provide an implementation that sets the window title.
136 *
137 * @param title the new title
138 */
139 public void setTitle(final String title) {
140 for (Backend backend: backends) {
141 backend.setTitle(title);
142 }
143 }
144
145 /**
146 * Set listener to a different Object.
147 *
148 * @param listener the new listening object that run() wakes up on new
149 * input
150 */
151 public void setListener(final Object listener) {
152 for (Backend backend: backends) {
153 backend.setListener(listener);
154 }
155 }
156
157 }