2 * This file is part of lanterna (http://code.google.com/p/lanterna/).
4 * lanterna is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 * Copyright (C) 2010-2015 Martin
19 package com
.googlecode
.lanterna
.terminal
;
21 import com
.googlecode
.lanterna
.SGR
;
22 import com
.googlecode
.lanterna
.TerminalSize
;
23 import com
.googlecode
.lanterna
.TextColor
;
24 import com
.googlecode
.lanterna
.graphics
.TextGraphics
;
25 import com
.googlecode
.lanterna
.input
.KeyStroke
;
26 import java
.io
.IOException
;
27 import java
.util
.concurrent
.TimeUnit
;
30 * This class exposes methods for converting a terminal into an IOSafeTerminal. There are two options available, either
31 * one that will convert any IOException to a RuntimeException (and re-throw it) or one that will silently swallow any
32 * IOException (and return null in those cases the method has a non-void return type).
35 public class IOSafeTerminalAdapter
implements IOSafeTerminal
{
36 private interface ExceptionHandler
{
37 void onException(IOException e
);
40 private static class ConvertToRuntimeException
implements ExceptionHandler
{
42 public void onException(IOException e
) {
43 throw new RuntimeException(e
);
47 private static class DoNothingAndOrReturnNull
implements ExceptionHandler
{
49 public void onException(IOException e
) { }
53 * Creates a wrapper around a Terminal that exposes it as a IOSafeTerminal. If any IOExceptions occur, they will be
54 * wrapped by a RuntimeException and re-thrown.
55 * @param terminal Terminal to wrap
56 * @return IOSafeTerminal wrapping the supplied terminal
58 public static IOSafeTerminal
createRuntimeExceptionConvertingAdapter(Terminal terminal
) {
59 if (terminal
instanceof ExtendedTerminal
) { // also handle Runtime-type:
60 return createRuntimeExceptionConvertingAdapter((ExtendedTerminal
)terminal
);
62 return new IOSafeTerminalAdapter(terminal
, new ConvertToRuntimeException());
67 * Creates a wrapper around an ExtendedTerminal that exposes it as a IOSafeExtendedTerminal.
68 * If any IOExceptions occur, they will be wrapped by a RuntimeException and re-thrown.
69 * @param terminal Terminal to wrap
70 * @return IOSafeTerminal wrapping the supplied terminal
72 public static IOSafeExtendedTerminal
createRuntimeExceptionConvertingAdapter(ExtendedTerminal terminal
) {
73 return new IOSafeTerminalAdapter
.Extended(terminal
, new ConvertToRuntimeException());
77 * Creates a wrapper around a Terminal that exposes it as a IOSafeTerminal. If any IOExceptions occur, they will be
78 * silently ignored and for those method with a non-void return type, null will be returned.
79 * @param terminal Terminal to wrap
80 * @return IOSafeTerminal wrapping the supplied terminal
82 public static IOSafeTerminal
createDoNothingOnExceptionAdapter(Terminal terminal
) {
83 if (terminal
instanceof ExtendedTerminal
) { // also handle Runtime-type:
84 return createDoNothingOnExceptionAdapter((ExtendedTerminal
)terminal
);
86 return new IOSafeTerminalAdapter(terminal
, new DoNothingAndOrReturnNull());
91 * Creates a wrapper around an ExtendedTerminal that exposes it as a IOSafeExtendedTerminal.
92 * If any IOExceptions occur, they will be silently ignored and for those method with a
93 * non-void return type, null will be returned.
94 * @param terminal Terminal to wrap
95 * @return IOSafeTerminal wrapping the supplied terminal
97 public static IOSafeExtendedTerminal
createDoNothingOnExceptionAdapter(ExtendedTerminal terminal
) {
98 return new IOSafeTerminalAdapter
.Extended(terminal
, new DoNothingAndOrReturnNull());
101 private final Terminal backend
;
102 final ExceptionHandler exceptionHandler
;
104 @SuppressWarnings("WeakerAccess")
105 public IOSafeTerminalAdapter(Terminal backend
, ExceptionHandler exceptionHandler
) {
106 this.backend
= backend
;
107 this.exceptionHandler
= exceptionHandler
;
111 public void enterPrivateMode() {
113 backend
.enterPrivateMode();
115 catch(IOException e
) {
116 exceptionHandler
.onException(e
);
121 public void exitPrivateMode() {
123 backend
.exitPrivateMode();
125 catch(IOException e
) {
126 exceptionHandler
.onException(e
);
131 public void clearScreen() {
133 backend
.clearScreen();
135 catch(IOException e
) {
136 exceptionHandler
.onException(e
);
141 public void setCursorPosition(int x
, int y
) {
143 backend
.setCursorPosition(x
, y
);
145 catch(IOException e
) {
146 exceptionHandler
.onException(e
);
151 public void setCursorVisible(boolean visible
) {
153 backend
.setCursorVisible(visible
);
155 catch(IOException e
) {
156 exceptionHandler
.onException(e
);
161 public void putCharacter(char c
) {
163 backend
.putCharacter(c
);
165 catch(IOException e
) {
166 exceptionHandler
.onException(e
);
171 public TextGraphics
newTextGraphics() throws IOException
{
172 return backend
.newTextGraphics();
176 public void enableSGR(SGR sgr
) {
178 backend
.enableSGR(sgr
);
180 catch(IOException e
) {
181 exceptionHandler
.onException(e
);
186 public void disableSGR(SGR sgr
) {
188 backend
.disableSGR(sgr
);
190 catch(IOException e
) {
191 exceptionHandler
.onException(e
);
196 public void resetColorAndSGR() {
198 backend
.resetColorAndSGR();
200 catch(IOException e
) {
201 exceptionHandler
.onException(e
);
206 public void setForegroundColor(TextColor color
) {
208 backend
.setForegroundColor(color
);
210 catch(IOException e
) {
211 exceptionHandler
.onException(e
);
216 public void setBackgroundColor(TextColor color
) {
218 backend
.setBackgroundColor(color
);
220 catch(IOException e
) {
221 exceptionHandler
.onException(e
);
226 public void addResizeListener(ResizeListener listener
) {
227 backend
.addResizeListener(listener
);
231 public void removeResizeListener(ResizeListener listener
) {
232 backend
.removeResizeListener(listener
);
236 public TerminalSize
getTerminalSize() {
238 return backend
.getTerminalSize();
240 catch(IOException e
) {
241 exceptionHandler
.onException(e
);
247 public byte[] enquireTerminal(int timeout
, TimeUnit timeoutUnit
) {
249 return backend
.enquireTerminal(timeout
, timeoutUnit
);
251 catch(IOException e
) {
252 exceptionHandler
.onException(e
);
258 public void flush() {
262 catch(IOException e
) {
263 exceptionHandler
.onException(e
);
268 public KeyStroke
pollInput() {
270 return backend
.pollInput();
272 catch(IOException e
) {
273 exceptionHandler
.onException(e
);
279 public KeyStroke
readInput() {
281 return backend
.readInput();
283 catch(IOException e
) {
284 exceptionHandler
.onException(e
);
290 * This class exposes methods for converting an extended terminal into an IOSafeExtendedTerminal.
292 public static class Extended
extends IOSafeTerminalAdapter
implements IOSafeExtendedTerminal
{
293 private final ExtendedTerminal backend
;
295 public Extended(ExtendedTerminal backend
, ExceptionHandler exceptionHandler
) {
296 super(backend
, exceptionHandler
);
297 this.backend
= backend
;
301 public void setTerminalSize(int columns
, int rows
) {
303 backend
.setTerminalSize(columns
, rows
);
305 catch(IOException e
) {
306 exceptionHandler
.onException(e
);
311 public void setTitle(String title
) {
313 backend
.setTitle(title
);
315 catch(IOException e
) {
316 exceptionHandler
.onException(e
);
321 public void pushTitle() {
325 catch(IOException e
) {
326 exceptionHandler
.onException(e
);
331 public void popTitle() {
335 catch(IOException e
) {
336 exceptionHandler
.onException(e
);
341 public void iconify() {
345 catch(IOException e
) {
346 exceptionHandler
.onException(e
);
351 public void deiconify() {
355 catch(IOException e
) {
356 exceptionHandler
.onException(e
);
361 public void maximize() {
365 catch(IOException e
) {
366 exceptionHandler
.onException(e
);
371 public void unmaximize() {
373 backend
.unmaximize();
375 catch(IOException e
) {
376 exceptionHandler
.onException(e
);
381 public void setMouseCaptureMode(MouseCaptureMode mouseCaptureMode
) {
383 backend
.setMouseCaptureMode(mouseCaptureMode
);
385 catch(IOException e
) {
386 exceptionHandler
.onException(e
);
391 public void scrollLines(int firstLine
, int lastLine
, int distance
) {
393 backend
.scrollLines(firstLine
, lastLine
, distance
);
395 catch(IOException e
) {
396 exceptionHandler
.onException(e
);