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
.swing
;
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 com
.googlecode
.lanterna
.terminal
.IOSafeTerminal
;
27 import com
.googlecode
.lanterna
.terminal
.ResizeListener
;
30 import java
.io
.IOException
;
31 import java
.util
.concurrent
.TimeUnit
;
35 * This class provides an AWT implementation of the Terminal interface that is an embeddable component you can put into
36 * an AWT container. The class has static helper methods for opening a new frame with an AWTTerminal as its content,
37 * similar to how the SwingTerminal used to work in earlier versions of lanterna. This version supports private mode and
38 * non-private mode with a scrollback history. You can customize many of the properties by supplying device
39 * configuration, font configuration and color configuration when you construct the object.
42 @SuppressWarnings("serial")
43 public class AWTTerminal
extends Panel
implements IOSafeTerminal
{
45 private final AWTTerminalImplementation terminalImplementation
;
48 * Creates a new AWTTerminal with all the defaults set and no scroll controller connected.
50 public AWTTerminal() {
51 this(new TerminalScrollController
.Null());
56 * Creates a new AWTTerminal with a particular scrolling controller that will be notified when the terminals
57 * history size grows and will be called when this class needs to figure out the current scrolling position.
58 * @param scrollController Controller for scrolling the terminal history
60 @SuppressWarnings("WeakerAccess")
61 public AWTTerminal(TerminalScrollController scrollController
) {
62 this(TerminalEmulatorDeviceConfiguration
.getDefault(),
63 AWTTerminalFontConfiguration
.getDefault(),
64 TerminalEmulatorColorConfiguration
.getDefault(),
69 * Creates a new AWTTerminal component using custom settings and no scroll controller.
70 * @param deviceConfiguration Device configuration to use for this AWTTerminal
71 * @param fontConfiguration Font configuration to use for this AWTTerminal
72 * @param colorConfiguration Color configuration to use for this AWTTerminal
75 TerminalEmulatorDeviceConfiguration deviceConfiguration
,
76 AWTTerminalFontConfiguration fontConfiguration
,
77 TerminalEmulatorColorConfiguration colorConfiguration
) {
79 this(null, deviceConfiguration
, fontConfiguration
, colorConfiguration
);
83 * Creates a new AWTTerminal component using custom settings and no scroll controller.
84 * @param initialTerminalSize Initial size of the terminal, which will be used when calculating the preferred size
85 * of the component. If null, it will default to 80x25. If the AWT layout manager forces
86 * the component to a different size, the value of this parameter won't have any meaning
87 * @param deviceConfiguration Device configuration to use for this AWTTerminal
88 * @param fontConfiguration Font configuration to use for this AWTTerminal
89 * @param colorConfiguration Color configuration to use for this AWTTerminal
92 TerminalSize initialTerminalSize
,
93 TerminalEmulatorDeviceConfiguration deviceConfiguration
,
94 AWTTerminalFontConfiguration fontConfiguration
,
95 TerminalEmulatorColorConfiguration colorConfiguration
) {
97 this(initialTerminalSize
,
101 new TerminalScrollController
.Null());
105 * Creates a new AWTTerminal component using custom settings and a custom scroll controller. The scrolling
106 * controller will be notified when the terminal's history size grows and will be called when this class needs to
107 * figure out the current scrolling position.
108 * @param deviceConfiguration Device configuration to use for this AWTTerminal
109 * @param fontConfiguration Font configuration to use for this AWTTerminal
110 * @param colorConfiguration Color configuration to use for this AWTTerminal
111 * @param scrollController Controller to use for scrolling, the object passed in will be notified whenever the
112 * scrollable area has changed
115 TerminalEmulatorDeviceConfiguration deviceConfiguration
,
116 AWTTerminalFontConfiguration fontConfiguration
,
117 TerminalEmulatorColorConfiguration colorConfiguration
,
118 TerminalScrollController scrollController
) {
120 this(null, deviceConfiguration
, fontConfiguration
, colorConfiguration
, scrollController
);
126 * Creates a new AWTTerminal component using custom settings and a custom scroll controller. The scrolling
127 * controller will be notified when the terminal's history size grows and will be called when this class needs to
128 * figure out the current scrolling position.
129 * @param initialTerminalSize Initial size of the terminal, which will be used when calculating the preferred size
130 * of the component. If null, it will default to 80x25. If the AWT layout manager forces
131 * the component to a different size, the value of this parameter won't have any meaning
132 * @param deviceConfiguration Device configuration to use for this AWTTerminal
133 * @param fontConfiguration Font configuration to use for this AWTTerminal
134 * @param colorConfiguration Color configuration to use for this AWTTerminal
135 * @param scrollController Controller to use for scrolling, the object passed in will be notified whenever the
136 * scrollable area has changed
139 TerminalSize initialTerminalSize
,
140 TerminalEmulatorDeviceConfiguration deviceConfiguration
,
141 AWTTerminalFontConfiguration fontConfiguration
,
142 TerminalEmulatorColorConfiguration colorConfiguration
,
143 TerminalScrollController scrollController
) {
145 //Enforce valid values on the input parameters
146 if(deviceConfiguration
== null) {
147 deviceConfiguration
= TerminalEmulatorDeviceConfiguration
.getDefault();
149 if(fontConfiguration
== null) {
150 fontConfiguration
= SwingTerminalFontConfiguration
.getDefault();
152 if(colorConfiguration
== null) {
153 colorConfiguration
= TerminalEmulatorColorConfiguration
.getDefault();
156 terminalImplementation
= new AWTTerminalImplementation(
166 * Overridden method from AWT's {@code Component} class that returns the preferred size of the terminal (in pixels)
167 * @return The terminal's preferred size in pixels
170 public synchronized Dimension
getPreferredSize() {
171 return terminalImplementation
.getPreferredSize();
175 * Overridden method from AWT's {@code Component} class that is called by OS window system when the component needs
177 * @param {@code Graphics} object to use when drawing the component
180 public synchronized void paint(Graphics componentGraphics
) {
182 // Extend Panel and do the drawing work in both update(..) and paint(..)
183 terminalImplementation
.paintComponent(componentGraphics
);
187 * Overridden method from AWT's {@code Component} class that is called by OS window system when the component needs
188 * to be updated (the size has changed) and redrawn
189 * @param {@code Graphics} object to use when drawing the component
192 public synchronized void update(Graphics componentGraphics
) {
194 // Extend Panel and do the drawing work in both update(..) and paint(..)
195 terminalImplementation
.paintComponent(componentGraphics
);
198 // Terminal methods below here, just forward to the implementation
201 public void enterPrivateMode() {
202 terminalImplementation
.enterPrivateMode();
206 public void exitPrivateMode() {
207 terminalImplementation
.exitPrivateMode();
211 public void clearScreen() {
212 terminalImplementation
.clearScreen();
216 public void setCursorPosition(int x
, int y
) {
217 terminalImplementation
.setCursorPosition(x
, y
);
221 public void setCursorVisible(boolean visible
) {
222 terminalImplementation
.setCursorVisible(visible
);
226 public void putCharacter(char c
) {
227 terminalImplementation
.putCharacter(c
);
231 public void enableSGR(SGR sgr
) {
232 terminalImplementation
.enableSGR(sgr
);
236 public void disableSGR(SGR sgr
) {
237 terminalImplementation
.disableSGR(sgr
);
241 public void resetColorAndSGR() {
242 terminalImplementation
.resetColorAndSGR();
246 public void setForegroundColor(TextColor color
) {
247 terminalImplementation
.setForegroundColor(color
);
251 public void setBackgroundColor(TextColor color
) {
252 terminalImplementation
.setBackgroundColor(color
);
256 public TerminalSize
getTerminalSize() {
257 return terminalImplementation
.getTerminalSize();
261 public byte[] enquireTerminal(int timeout
, TimeUnit timeoutUnit
) {
262 return terminalImplementation
.enquireTerminal(timeout
, timeoutUnit
);
266 public void flush() {
267 terminalImplementation
.flush();
271 public KeyStroke
pollInput() {
272 return terminalImplementation
.pollInput();
276 public KeyStroke
readInput() throws IOException
{
277 return terminalImplementation
.readInput();
281 public TextGraphics
newTextGraphics() throws IOException
{
282 return terminalImplementation
.newTextGraphics();
286 public void addResizeListener(ResizeListener listener
) {
287 terminalImplementation
.addResizeListener(listener
);
291 public void removeResizeListener(ResizeListener listener
) {
292 terminalImplementation
.removeResizeListener(listener
);