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
;
36 * This class is similar to what SwingTerminal used to be before Lanterna 3.0; a JFrame that contains a terminal
37 * emulator. In Lanterna 3, this class is just a JFrame containing a SwingTerminal component, but it also implements
38 * the Terminal interface and delegates all calls to the internal SwingTerminal. You can tweak the class a bit to have
39 * special behaviours when exiting private mode or when the user presses ESC key.
42 @SuppressWarnings("serial")
43 public class SwingTerminalFrame
extends JFrame
implements IOSafeTerminal
{
44 private final SwingTerminal swingTerminal
;
45 private TerminalEmulatorAutoCloseTrigger autoCloseTrigger
;
46 private boolean disposed
;
49 * Creates a new SwingTerminalFrame that doesn't automatically close.
51 public SwingTerminalFrame() throws HeadlessException
{
52 this(TerminalEmulatorAutoCloseTrigger
.DoNotAutoClose
);
56 * Creates a new SwingTerminalFrame with a specified auto-close behaviour
57 * @param autoCloseTrigger What to trigger automatic disposal of the JFrame
59 @SuppressWarnings({"SameParameterValue", "WeakerAccess"})
60 public SwingTerminalFrame(TerminalEmulatorAutoCloseTrigger autoCloseTrigger
) {
61 this("SwingTerminalFrame", autoCloseTrigger
);
65 * Creates a new SwingTerminalFrame with a given title and no automatic closing.
66 * @param title Title to use for the window
68 public SwingTerminalFrame(String title
) throws HeadlessException
{
69 this(title
, TerminalEmulatorAutoCloseTrigger
.DoNotAutoClose
);
73 * Creates a new SwingTerminalFrame with a specified auto-close behaviour and specific title
74 * @param title Title to use for the window
75 * @param autoCloseTrigger What to trigger automatic disposal of the JFrame
77 @SuppressWarnings("WeakerAccess")
78 public SwingTerminalFrame(String title
, TerminalEmulatorAutoCloseTrigger autoCloseTrigger
) throws HeadlessException
{
79 this(title
, new SwingTerminal(), autoCloseTrigger
);
83 * Creates a new SwingTerminalFrame using a specified title and a series of swing terminal configuration objects
84 * @param title What title to use for the window
85 * @param deviceConfiguration Device configuration for the embedded SwingTerminal
86 * @param fontConfiguration Font configuration for the embedded SwingTerminal
87 * @param colorConfiguration Color configuration for the embedded SwingTerminal
89 public SwingTerminalFrame(String title
,
90 TerminalEmulatorDeviceConfiguration deviceConfiguration
,
91 SwingTerminalFontConfiguration fontConfiguration
,
92 TerminalEmulatorColorConfiguration colorConfiguration
) {
93 this(title
, deviceConfiguration
, fontConfiguration
, colorConfiguration
, TerminalEmulatorAutoCloseTrigger
.DoNotAutoClose
);
97 * Creates a new SwingTerminalFrame using a specified title and a series of swing terminal configuration objects
98 * @param title What title to use for the window
99 * @param deviceConfiguration Device configuration for the embedded SwingTerminal
100 * @param fontConfiguration Font configuration for the embedded SwingTerminal
101 * @param colorConfiguration Color configuration for the embedded SwingTerminal
102 * @param autoCloseTrigger What to trigger automatic disposal of the JFrame
104 public SwingTerminalFrame(String title
,
105 TerminalEmulatorDeviceConfiguration deviceConfiguration
,
106 SwingTerminalFontConfiguration fontConfiguration
,
107 TerminalEmulatorColorConfiguration colorConfiguration
,
108 TerminalEmulatorAutoCloseTrigger autoCloseTrigger
) {
109 this(title
, null, deviceConfiguration
, fontConfiguration
, colorConfiguration
, autoCloseTrigger
);
113 * Creates a new SwingTerminalFrame using a specified title and a series of swing terminal configuration objects
114 * @param title What title to use for the window
115 * @param terminalSize Initial size of the terminal, in rows and columns. If null, it will default to 80x25.
116 * @param deviceConfiguration Device configuration for the embedded SwingTerminal
117 * @param fontConfiguration Font configuration for the embedded SwingTerminal
118 * @param colorConfiguration Color configuration for the embedded SwingTerminal
119 * @param autoCloseTrigger What to trigger automatic disposal of the JFrame
121 public SwingTerminalFrame(String title
,
122 TerminalSize terminalSize
,
123 TerminalEmulatorDeviceConfiguration deviceConfiguration
,
124 SwingTerminalFontConfiguration fontConfiguration
,
125 TerminalEmulatorColorConfiguration colorConfiguration
,
126 TerminalEmulatorAutoCloseTrigger autoCloseTrigger
) {
128 new SwingTerminal(terminalSize
, deviceConfiguration
, fontConfiguration
, colorConfiguration
),
132 private SwingTerminalFrame(String title
, SwingTerminal swingTerminal
, TerminalEmulatorAutoCloseTrigger autoCloseTrigger
) {
133 super(title
!= null ? title
: "SwingTerminalFrame");
134 this.swingTerminal
= swingTerminal
;
135 this.autoCloseTrigger
= autoCloseTrigger
;
136 this.disposed
= false;
138 getContentPane().setLayout(new BorderLayout());
139 getContentPane().add(swingTerminal
, BorderLayout
.CENTER
);
140 setBackground(Color
.BLACK
); //This will reduce white flicker when resizing the window
143 //Put input focus on the terminal component by default
144 swingTerminal
.requestFocusInWindow();
148 * Returns the auto-close trigger used by the SwingTerminalFrame
149 * @return Current auto-close trigger
151 public TerminalEmulatorAutoCloseTrigger
getAutoCloseTrigger() {
152 return autoCloseTrigger
;
156 * Changes the current auto-close trigger used by this SwingTerminalFrame
157 * @param autoCloseTrigger New auto-close trigger to use
159 public void setAutoCloseTrigger(TerminalEmulatorAutoCloseTrigger autoCloseTrigger
) {
160 this.autoCloseTrigger
= autoCloseTrigger
;
164 public void dispose() {
170 // Delegate all Terminal interface implementations to SwingTerminal
173 public KeyStroke
pollInput() {
175 return new KeyStroke(KeyType
.EOF
);
177 KeyStroke keyStroke
= swingTerminal
.pollInput();
178 if(autoCloseTrigger
== TerminalEmulatorAutoCloseTrigger
.CloseOnEscape
&&
180 keyStroke
.getKeyType() == KeyType
.Escape
) {
187 public KeyStroke
readInput() throws IOException
{
188 return swingTerminal
.readInput();
192 public void enterPrivateMode() {
193 swingTerminal
.enterPrivateMode();
197 public void exitPrivateMode() {
198 swingTerminal
.exitPrivateMode();
199 if(autoCloseTrigger
== TerminalEmulatorAutoCloseTrigger
.CloseOnExitPrivateMode
) {
205 public void clearScreen() {
206 swingTerminal
.clearScreen();
210 public void setCursorPosition(int x
, int y
) {
211 swingTerminal
.setCursorPosition(x
, y
);
215 public void setCursorVisible(boolean visible
) {
216 swingTerminal
.setCursorVisible(visible
);
220 public void putCharacter(char c
) {
221 swingTerminal
.putCharacter(c
);
225 public TextGraphics
newTextGraphics() throws IOException
{
226 return swingTerminal
.newTextGraphics();
230 public void enableSGR(SGR sgr
) {
231 swingTerminal
.enableSGR(sgr
);
235 public void disableSGR(SGR sgr
) {
236 swingTerminal
.disableSGR(sgr
);
240 public void resetColorAndSGR() {
241 swingTerminal
.resetColorAndSGR();
245 public void setForegroundColor(TextColor color
) {
246 swingTerminal
.setForegroundColor(color
);
250 public void setBackgroundColor(TextColor color
) {
251 swingTerminal
.setBackgroundColor(color
);
255 public TerminalSize
getTerminalSize() {
256 return swingTerminal
.getTerminalSize();
260 public byte[] enquireTerminal(int timeout
, TimeUnit timeoutUnit
) {
261 return swingTerminal
.enquireTerminal(timeout
, timeoutUnit
);
265 public void flush() {
266 swingTerminal
.flush();
270 public void addResizeListener(ResizeListener listener
) {
271 swingTerminal
.addResizeListener(listener
);
275 public void removeResizeListener(ResizeListener listener
) {
276 swingTerminal
.removeResizeListener(listener
);