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
.graphics
.TextGraphics
;
23 import com
.googlecode
.lanterna
.input
.KeyStroke
;
24 import com
.googlecode
.lanterna
.input
.KeyType
;
25 import com
.googlecode
.lanterna
.terminal
.IOSafeTerminal
;
26 import com
.googlecode
.lanterna
.terminal
.ResizeListener
;
27 import com
.googlecode
.lanterna
.TerminalSize
;
28 import com
.googlecode
.lanterna
.TextColor
;
31 import java
.io
.IOException
;
32 import java
.util
.concurrent
.TimeUnit
;
35 * This class is similar to what SwingTerminal used to be before Lanterna 3.0; a Frame that contains a terminal
36 * emulator. In Lanterna 3, this class is just an AWT Frame containing a {@link AWTTerminal} component, but it also
37 * implements the {@link com.googlecode.lanterna.terminal.Terminal} interface and delegates all calls to the internal
38 * {@link AWTTerminal}. You can tweak the class a bit to have special behaviours when exiting private mode or when the
39 * user presses ESC key.
41 * <p>Please note that this is the AWT version and there is a Swing counterpart: {@link SwingTerminalFrame}
43 * @see SwingTerminalFrame
46 @SuppressWarnings("serial")
47 public class AWTTerminalFrame
extends Frame
implements IOSafeTerminal
{
48 private final AWTTerminal awtTerminal
;
49 private TerminalEmulatorAutoCloseTrigger autoCloseTrigger
;
50 private boolean disposed
;
53 * Creates a new AWTTerminalFrame that doesn't automatically close.
55 public AWTTerminalFrame() throws HeadlessException
{
56 this(TerminalEmulatorAutoCloseTrigger
.DoNotAutoClose
);
60 * Creates a new AWTTerminalFrame with a specified auto-close behaviour
61 * @param autoCloseTrigger What to trigger automatic disposal of the Frame
63 @SuppressWarnings({"SameParameterValue", "WeakerAccess"})
64 public AWTTerminalFrame(TerminalEmulatorAutoCloseTrigger autoCloseTrigger
) {
65 this("AwtTerminalFrame", autoCloseTrigger
);
69 * Creates a new AWTTerminalFrame with a given title and no automatic closing.
70 * @param title Title to use for the window
72 public AWTTerminalFrame(String title
) throws HeadlessException
{
73 this(title
, TerminalEmulatorAutoCloseTrigger
.DoNotAutoClose
);
77 * Creates a new AWTTerminalFrame with a specified auto-close behaviour and specific title
78 * @param title Title to use for the window
79 * @param autoCloseTrigger What to trigger automatic disposal of the Frame
81 @SuppressWarnings("WeakerAccess")
82 public AWTTerminalFrame(String title
, TerminalEmulatorAutoCloseTrigger autoCloseTrigger
) throws HeadlessException
{
83 this(title
, new AWTTerminal(), autoCloseTrigger
);
87 * Creates a new AWTTerminalFrame using a specified title and a series of AWT terminal configuration objects
88 * @param title What title to use for the window
89 * @param deviceConfiguration Device configuration for the embedded AWTTerminal
90 * @param fontConfiguration Font configuration for the embedded AWTTerminal
91 * @param colorConfiguration Color configuration for the embedded AWTTerminal
93 public AWTTerminalFrame(String title
,
94 TerminalEmulatorDeviceConfiguration deviceConfiguration
,
95 AWTTerminalFontConfiguration fontConfiguration
,
96 TerminalEmulatorColorConfiguration colorConfiguration
) {
97 this(title
, deviceConfiguration
, fontConfiguration
, colorConfiguration
, TerminalEmulatorAutoCloseTrigger
.DoNotAutoClose
);
101 * Creates a new AWTTerminalFrame using a specified title and a series of AWT terminal configuration objects
102 * @param title What title to use for the window
103 * @param deviceConfiguration Device configuration for the embedded AWTTerminal
104 * @param fontConfiguration Font configuration for the embedded AWTTerminal
105 * @param colorConfiguration Color configuration for the embedded AWTTerminal
106 * @param autoCloseTrigger What to trigger automatic disposal of the Frame
108 public AWTTerminalFrame(String title
,
109 TerminalEmulatorDeviceConfiguration deviceConfiguration
,
110 AWTTerminalFontConfiguration fontConfiguration
,
111 TerminalEmulatorColorConfiguration colorConfiguration
,
112 TerminalEmulatorAutoCloseTrigger autoCloseTrigger
) {
113 this(title
, null, deviceConfiguration
, fontConfiguration
, colorConfiguration
, autoCloseTrigger
);
117 * Creates a new AWTTerminalFrame using a specified title and a series of AWT terminal configuration objects
118 * @param title What title to use for the window
119 * @param terminalSize Initial size of the terminal, in rows and columns. If null, it will default to 80x25.
120 * @param deviceConfiguration Device configuration for the embedded AWTTerminal
121 * @param fontConfiguration Font configuration for the embedded AWTTerminal
122 * @param colorConfiguration Color configuration for the embedded AWTTerminal
123 * @param autoCloseTrigger What to trigger automatic disposal of the Frame
125 public AWTTerminalFrame(String title
,
126 TerminalSize terminalSize
,
127 TerminalEmulatorDeviceConfiguration deviceConfiguration
,
128 AWTTerminalFontConfiguration fontConfiguration
,
129 TerminalEmulatorColorConfiguration colorConfiguration
,
130 TerminalEmulatorAutoCloseTrigger autoCloseTrigger
) {
132 new AWTTerminal(terminalSize
, deviceConfiguration
, fontConfiguration
, colorConfiguration
),
136 private AWTTerminalFrame(String title
, AWTTerminal awtTerminal
, TerminalEmulatorAutoCloseTrigger autoCloseTrigger
) {
137 super(title
!= null ? title
: "AWTTerminalFrame");
138 this.awtTerminal
= awtTerminal
;
139 this.autoCloseTrigger
= autoCloseTrigger
;
140 this.disposed
= false;
142 setLayout(new BorderLayout());
143 add(awtTerminal
, BorderLayout
.CENTER
);
144 setBackground(Color
.BLACK
); //This will reduce white flicker when resizing the window
147 //Put input focus on the terminal component by default
148 awtTerminal
.requestFocusInWindow();
152 * Returns the auto-close trigger used by the AWTTerminalFrame
153 * @return Current auto-close trigger
155 public TerminalEmulatorAutoCloseTrigger
getAutoCloseTrigger() {
156 return autoCloseTrigger
;
160 * Changes the current auto-close trigger used by this AWTTerminalFrame
161 * @param autoCloseTrigger New auto-close trigger to use
163 public void setAutoCloseTrigger(TerminalEmulatorAutoCloseTrigger autoCloseTrigger
) {
164 this.autoCloseTrigger
= autoCloseTrigger
;
168 public void dispose() {
174 // Delegate all Terminal interface implementations to AWTTerminal
177 public KeyStroke
pollInput() {
179 return new KeyStroke(KeyType
.EOF
);
181 KeyStroke keyStroke
= awtTerminal
.pollInput();
182 if(autoCloseTrigger
== TerminalEmulatorAutoCloseTrigger
.CloseOnEscape
&&
184 keyStroke
.getKeyType() == KeyType
.Escape
) {
191 public KeyStroke
readInput() throws IOException
{
192 return awtTerminal
.readInput();
196 public void enterPrivateMode() {
197 awtTerminal
.enterPrivateMode();
201 public void exitPrivateMode() {
202 awtTerminal
.exitPrivateMode();
203 if(autoCloseTrigger
== TerminalEmulatorAutoCloseTrigger
.CloseOnExitPrivateMode
) {
209 public void clearScreen() {
210 awtTerminal
.clearScreen();
214 public void setCursorPosition(int x
, int y
) {
215 awtTerminal
.setCursorPosition(x
, y
);
219 public void setCursorVisible(boolean visible
) {
220 awtTerminal
.setCursorVisible(visible
);
224 public void putCharacter(char c
) {
225 awtTerminal
.putCharacter(c
);
229 public TextGraphics
newTextGraphics() throws IOException
{
230 return awtTerminal
.newTextGraphics();
234 public void enableSGR(SGR sgr
) {
235 awtTerminal
.enableSGR(sgr
);
239 public void disableSGR(SGR sgr
) {
240 awtTerminal
.disableSGR(sgr
);
244 public void resetColorAndSGR() {
245 awtTerminal
.resetColorAndSGR();
249 public void setForegroundColor(TextColor color
) {
250 awtTerminal
.setForegroundColor(color
);
254 public void setBackgroundColor(TextColor color
) {
255 awtTerminal
.setBackgroundColor(color
);
259 public TerminalSize
getTerminalSize() {
260 return awtTerminal
.getTerminalSize();
264 public byte[] enquireTerminal(int timeout
, TimeUnit timeoutUnit
) {
265 return awtTerminal
.enquireTerminal(timeout
, timeoutUnit
);
269 public void flush() {
274 public void addResizeListener(ResizeListener listener
) {
275 awtTerminal
.addResizeListener(listener
);
279 public void removeResizeListener(ResizeListener listener
) {
280 awtTerminal
.removeResizeListener(listener
);