1 package com
.googlecode
.lanterna
.terminal
.swing
;
3 import com
.googlecode
.lanterna
.SGR
;
4 import com
.googlecode
.lanterna
.TerminalSize
;
5 import com
.googlecode
.lanterna
.TextColor
;
6 import com
.googlecode
.lanterna
.graphics
.TextGraphics
;
7 import com
.googlecode
.lanterna
.input
.*;
8 import com
.googlecode
.lanterna
.input
.KeyStroke
;
9 import com
.googlecode
.lanterna
.terminal
.IOSafeTerminal
;
10 import com
.googlecode
.lanterna
.terminal
.ResizeListener
;
14 import java
.io
.IOException
;
15 import java
.util
.concurrent
.TimeUnit
;
18 * This class provides an Swing implementation of the {@link com.googlecode.lanterna.terminal.Terminal} interface that
19 * is an embeddable component you can put into a Swing container. The class has static helper methods for opening a new
20 * frame with a {@link SwingTerminal} as its content, similar to how the SwingTerminal used to work in earlier versions
21 * of lanterna. This version supports private mode and non-private mode with a scrollback history. You can customize
22 * many of the properties by supplying device configuration, font configuration and color configuration when you
23 * construct the object.
26 public class SwingTerminal
extends JComponent
implements IOSafeTerminal
{
28 private final SwingTerminalImplementation terminalImplementation
;
31 * Creates a new SwingTerminal with all the defaults set and no scroll controller connected.
33 public SwingTerminal() {
34 this(new TerminalScrollController
.Null());
39 * Creates a new SwingTerminal with a particular scrolling controller that will be notified when the terminals
40 * history size grows and will be called when this class needs to figure out the current scrolling position.
41 * @param scrollController Controller for scrolling the terminal history
43 @SuppressWarnings("WeakerAccess")
44 public SwingTerminal(TerminalScrollController scrollController
) {
45 this(TerminalEmulatorDeviceConfiguration
.getDefault(),
46 SwingTerminalFontConfiguration
.getDefault(),
47 TerminalEmulatorColorConfiguration
.getDefault(),
52 * Creates a new SwingTerminal component using custom settings and no scroll controller.
53 * @param deviceConfiguration Device configuration to use for this SwingTerminal
54 * @param fontConfiguration Font configuration to use for this SwingTerminal
55 * @param colorConfiguration Color configuration to use for this SwingTerminal
58 TerminalEmulatorDeviceConfiguration deviceConfiguration
,
59 SwingTerminalFontConfiguration fontConfiguration
,
60 TerminalEmulatorColorConfiguration colorConfiguration
) {
62 this(null, deviceConfiguration
, fontConfiguration
, colorConfiguration
);
66 * Creates a new SwingTerminal component using custom settings and no scroll controller.
67 * @param initialTerminalSize Initial size of the terminal, which will be used when calculating the preferred size
68 * of the component. If null, it will default to 80x25. If the AWT layout manager forces
69 * the component to a different size, the value of this parameter won't have any meaning
70 * @param deviceConfiguration Device configuration to use for this SwingTerminal
71 * @param fontConfiguration Font configuration to use for this SwingTerminal
72 * @param colorConfiguration Color configuration to use for this SwingTerminal
75 TerminalSize initialTerminalSize
,
76 TerminalEmulatorDeviceConfiguration deviceConfiguration
,
77 SwingTerminalFontConfiguration fontConfiguration
,
78 TerminalEmulatorColorConfiguration colorConfiguration
) {
80 this(initialTerminalSize
,
84 new TerminalScrollController
.Null());
88 * Creates a new SwingTerminal component using custom settings and a custom scroll controller. The scrolling
89 * controller will be notified when the terminal's history size grows and will be called when this class needs to
90 * figure out the current scrolling position.
91 * @param deviceConfiguration Device configuration to use for this SwingTerminal
92 * @param fontConfiguration Font configuration to use for this SwingTerminal
93 * @param colorConfiguration Color configuration to use for this SwingTerminal
94 * @param scrollController Controller to use for scrolling, the object passed in will be notified whenever the
95 * scrollable area has changed
98 TerminalEmulatorDeviceConfiguration deviceConfiguration
,
99 SwingTerminalFontConfiguration fontConfiguration
,
100 TerminalEmulatorColorConfiguration colorConfiguration
,
101 TerminalScrollController scrollController
) {
103 this(null, deviceConfiguration
, fontConfiguration
, colorConfiguration
, scrollController
);
109 * Creates a new SwingTerminal component using custom settings and a custom scroll controller. The scrolling
110 * controller will be notified when the terminal's history size grows and will be called when this class needs to
111 * figure out the current scrolling position.
112 * @param initialTerminalSize Initial size of the terminal, which will be used when calculating the preferred size
113 * of the component. If null, it will default to 80x25. If the AWT layout manager forces
114 * the component to a different size, the value of this parameter won't have any meaning
115 * @param deviceConfiguration Device configuration to use for this SwingTerminal
116 * @param fontConfiguration Font configuration to use for this SwingTerminal
117 * @param colorConfiguration Color configuration to use for this SwingTerminal
118 * @param scrollController Controller to use for scrolling, the object passed in will be notified whenever the
119 * scrollable area has changed
121 public SwingTerminal(
122 TerminalSize initialTerminalSize
,
123 TerminalEmulatorDeviceConfiguration deviceConfiguration
,
124 SwingTerminalFontConfiguration fontConfiguration
,
125 TerminalEmulatorColorConfiguration colorConfiguration
,
126 TerminalScrollController scrollController
) {
128 //Enforce valid values on the input parameters
129 if(deviceConfiguration
== null) {
130 deviceConfiguration
= TerminalEmulatorDeviceConfiguration
.getDefault();
132 if(fontConfiguration
== null) {
133 fontConfiguration
= SwingTerminalFontConfiguration
.getDefault();
135 if(colorConfiguration
== null) {
136 colorConfiguration
= TerminalEmulatorColorConfiguration
.getDefault();
139 terminalImplementation
= new SwingTerminalImplementation(
149 * Overridden method from Swing's {@code JComponent} class that returns the preferred size of the terminal (in
151 * @return The terminal's preferred size in pixels
154 public synchronized Dimension
getPreferredSize() {
155 return terminalImplementation
.getPreferredSize();
159 * Overridden method from Swing's {@code JComponent} class that is called by OS window system when the component
160 * needs to be redrawn
161 * @param {@code Graphics} object to use when drawing the component
164 protected synchronized void paintComponent(Graphics componentGraphics
) {
165 terminalImplementation
.paintComponent(componentGraphics
);
168 ////////////////////////////////////////////////////////////////////////////////
169 // Terminal methods below here, just forward to the implementation
172 public void enterPrivateMode() {
173 terminalImplementation
.enterPrivateMode();
177 public void exitPrivateMode() {
178 terminalImplementation
.exitPrivateMode();
182 public void clearScreen() {
183 terminalImplementation
.clearScreen();
187 public void setCursorPosition(int x
, int y
) {
188 terminalImplementation
.setCursorPosition(x
, y
);
192 public void setCursorVisible(boolean visible
) {
193 terminalImplementation
.setCursorVisible(visible
);
197 public void putCharacter(char c
) {
198 terminalImplementation
.putCharacter(c
);
202 public void enableSGR(SGR sgr
) {
203 terminalImplementation
.enableSGR(sgr
);
207 public void disableSGR(SGR sgr
) {
208 terminalImplementation
.disableSGR(sgr
);
212 public void resetColorAndSGR() {
213 terminalImplementation
.resetColorAndSGR();
217 public void setForegroundColor(TextColor color
) {
218 terminalImplementation
.setForegroundColor(color
);
222 public void setBackgroundColor(TextColor color
) {
223 terminalImplementation
.setBackgroundColor(color
);
227 public TerminalSize
getTerminalSize() {
228 return terminalImplementation
.getTerminalSize();
232 public byte[] enquireTerminal(int timeout
, TimeUnit timeoutUnit
) {
233 return terminalImplementation
.enquireTerminal(timeout
, timeoutUnit
);
237 public void flush() {
238 terminalImplementation
.flush();
242 public KeyStroke
pollInput() {
243 return terminalImplementation
.pollInput();
247 public KeyStroke
readInput() throws IOException
{
248 return terminalImplementation
.readInput();
252 public TextGraphics
newTextGraphics() throws IOException
{
253 return terminalImplementation
.newTextGraphics();
257 public void addResizeListener(ResizeListener listener
) {
258 terminalImplementation
.addResizeListener(listener
);
262 public void removeResizeListener(ResizeListener listener
) {
263 terminalImplementation
.removeResizeListener(listener
);