Commit | Line | Data |
---|---|---|
a3b510ab NR |
1 | /* |
2 | * This file is part of lanterna (http://code.google.com/p/lanterna/). | |
3 | * | |
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. | |
8 | * | |
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. | |
13 | * | |
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/>. | |
16 | * | |
17 | * Copyright (C) 2010-2015 Martin | |
18 | */ | |
19 | package com.googlecode.lanterna.terminal.swing; | |
20 | ||
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; | |
29 | ||
30 | import java.awt.*; | |
31 | import java.io.IOException; | |
32 | import java.util.concurrent.TimeUnit; | |
33 | ||
34 | /** | |
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. | |
40 | * | |
41 | * <p>Please note that this is the AWT version and there is a Swing counterpart: {@link SwingTerminalFrame} | |
42 | * @see AWTTerminal | |
43 | * @see SwingTerminalFrame | |
44 | * @author martin | |
45 | */ | |
46 | @SuppressWarnings("serial") | |
47 | public class AWTTerminalFrame extends Frame implements IOSafeTerminal { | |
48 | private final AWTTerminal awtTerminal; | |
49 | private TerminalEmulatorAutoCloseTrigger autoCloseTrigger; | |
50 | private boolean disposed; | |
51 | ||
52 | /** | |
53 | * Creates a new AWTTerminalFrame that doesn't automatically close. | |
54 | */ | |
55 | public AWTTerminalFrame() throws HeadlessException { | |
56 | this(TerminalEmulatorAutoCloseTrigger.DoNotAutoClose); | |
57 | } | |
58 | ||
59 | /** | |
60 | * Creates a new AWTTerminalFrame with a specified auto-close behaviour | |
61 | * @param autoCloseTrigger What to trigger automatic disposal of the Frame | |
62 | */ | |
63 | @SuppressWarnings({"SameParameterValue", "WeakerAccess"}) | |
64 | public AWTTerminalFrame(TerminalEmulatorAutoCloseTrigger autoCloseTrigger) { | |
65 | this("AwtTerminalFrame", autoCloseTrigger); | |
66 | } | |
67 | ||
68 | /** | |
69 | * Creates a new AWTTerminalFrame with a given title and no automatic closing. | |
70 | * @param title Title to use for the window | |
71 | */ | |
72 | public AWTTerminalFrame(String title) throws HeadlessException { | |
73 | this(title, TerminalEmulatorAutoCloseTrigger.DoNotAutoClose); | |
74 | } | |
75 | ||
76 | /** | |
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 | |
80 | */ | |
81 | @SuppressWarnings("WeakerAccess") | |
82 | public AWTTerminalFrame(String title, TerminalEmulatorAutoCloseTrigger autoCloseTrigger) throws HeadlessException { | |
83 | this(title, new AWTTerminal(), autoCloseTrigger); | |
84 | } | |
85 | ||
86 | /** | |
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 | |
92 | */ | |
93 | public AWTTerminalFrame(String title, | |
94 | TerminalEmulatorDeviceConfiguration deviceConfiguration, | |
95 | AWTTerminalFontConfiguration fontConfiguration, | |
96 | TerminalEmulatorColorConfiguration colorConfiguration) { | |
97 | this(title, deviceConfiguration, fontConfiguration, colorConfiguration, TerminalEmulatorAutoCloseTrigger.DoNotAutoClose); | |
98 | } | |
99 | ||
100 | /** | |
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 | |
107 | */ | |
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); | |
114 | } | |
115 | ||
116 | /** | |
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 | |
124 | */ | |
125 | public AWTTerminalFrame(String title, | |
126 | TerminalSize terminalSize, | |
127 | TerminalEmulatorDeviceConfiguration deviceConfiguration, | |
128 | AWTTerminalFontConfiguration fontConfiguration, | |
129 | TerminalEmulatorColorConfiguration colorConfiguration, | |
130 | TerminalEmulatorAutoCloseTrigger autoCloseTrigger) { | |
131 | this(title, | |
132 | new AWTTerminal(terminalSize, deviceConfiguration, fontConfiguration, colorConfiguration), | |
133 | autoCloseTrigger); | |
134 | } | |
135 | ||
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; | |
141 | ||
142 | setLayout(new BorderLayout()); | |
143 | add(awtTerminal, BorderLayout.CENTER); | |
144 | setBackground(Color.BLACK); //This will reduce white flicker when resizing the window | |
145 | pack(); | |
146 | ||
147 | //Put input focus on the terminal component by default | |
148 | awtTerminal.requestFocusInWindow(); | |
149 | } | |
150 | ||
151 | /** | |
152 | * Returns the auto-close trigger used by the AWTTerminalFrame | |
153 | * @return Current auto-close trigger | |
154 | */ | |
155 | public TerminalEmulatorAutoCloseTrigger getAutoCloseTrigger() { | |
156 | return autoCloseTrigger; | |
157 | } | |
158 | ||
159 | /** | |
160 | * Changes the current auto-close trigger used by this AWTTerminalFrame | |
161 | * @param autoCloseTrigger New auto-close trigger to use | |
162 | */ | |
163 | public void setAutoCloseTrigger(TerminalEmulatorAutoCloseTrigger autoCloseTrigger) { | |
164 | this.autoCloseTrigger = autoCloseTrigger; | |
165 | } | |
166 | ||
167 | @Override | |
168 | public void dispose() { | |
169 | super.dispose(); | |
170 | disposed = true; | |
171 | } | |
172 | ||
173 | /////////// | |
174 | // Delegate all Terminal interface implementations to AWTTerminal | |
175 | /////////// | |
176 | @Override | |
177 | public KeyStroke pollInput() { | |
178 | if(disposed) { | |
179 | return new KeyStroke(KeyType.EOF); | |
180 | } | |
181 | KeyStroke keyStroke = awtTerminal.pollInput(); | |
182 | if(autoCloseTrigger == TerminalEmulatorAutoCloseTrigger.CloseOnEscape && | |
183 | keyStroke != null && | |
184 | keyStroke.getKeyType() == KeyType.Escape) { | |
185 | dispose(); | |
186 | } | |
187 | return keyStroke; | |
188 | } | |
189 | ||
190 | @Override | |
191 | public KeyStroke readInput() throws IOException { | |
192 | return awtTerminal.readInput(); | |
193 | } | |
194 | ||
195 | @Override | |
196 | public void enterPrivateMode() { | |
197 | awtTerminal.enterPrivateMode(); | |
198 | } | |
199 | ||
200 | @Override | |
201 | public void exitPrivateMode() { | |
202 | awtTerminal.exitPrivateMode(); | |
203 | if(autoCloseTrigger == TerminalEmulatorAutoCloseTrigger.CloseOnExitPrivateMode) { | |
204 | dispose(); | |
205 | } | |
206 | } | |
207 | ||
208 | @Override | |
209 | public void clearScreen() { | |
210 | awtTerminal.clearScreen(); | |
211 | } | |
212 | ||
213 | @Override | |
214 | public void setCursorPosition(int x, int y) { | |
215 | awtTerminal.setCursorPosition(x, y); | |
216 | } | |
217 | ||
218 | @Override | |
219 | public void setCursorVisible(boolean visible) { | |
220 | awtTerminal.setCursorVisible(visible); | |
221 | } | |
222 | ||
223 | @Override | |
224 | public void putCharacter(char c) { | |
225 | awtTerminal.putCharacter(c); | |
226 | } | |
227 | ||
228 | @Override | |
229 | public TextGraphics newTextGraphics() throws IOException { | |
230 | return awtTerminal.newTextGraphics(); | |
231 | } | |
232 | ||
233 | @Override | |
234 | public void enableSGR(SGR sgr) { | |
235 | awtTerminal.enableSGR(sgr); | |
236 | } | |
237 | ||
238 | @Override | |
239 | public void disableSGR(SGR sgr) { | |
240 | awtTerminal.disableSGR(sgr); | |
241 | } | |
242 | ||
243 | @Override | |
244 | public void resetColorAndSGR() { | |
245 | awtTerminal.resetColorAndSGR(); | |
246 | } | |
247 | ||
248 | @Override | |
249 | public void setForegroundColor(TextColor color) { | |
250 | awtTerminal.setForegroundColor(color); | |
251 | } | |
252 | ||
253 | @Override | |
254 | public void setBackgroundColor(TextColor color) { | |
255 | awtTerminal.setBackgroundColor(color); | |
256 | } | |
257 | ||
258 | @Override | |
259 | public TerminalSize getTerminalSize() { | |
260 | return awtTerminal.getTerminalSize(); | |
261 | } | |
262 | ||
263 | @Override | |
264 | public byte[] enquireTerminal(int timeout, TimeUnit timeoutUnit) { | |
265 | return awtTerminal.enquireTerminal(timeout, timeoutUnit); | |
266 | } | |
267 | ||
268 | @Override | |
269 | public void flush() { | |
270 | awtTerminal.flush(); | |
271 | } | |
272 | ||
273 | @Override | |
274 | public void addResizeListener(ResizeListener listener) { | |
275 | awtTerminal.addResizeListener(listener); | |
276 | } | |
277 | ||
278 | @Override | |
279 | public void removeResizeListener(ResizeListener listener) { | |
280 | awtTerminal.removeResizeListener(listener); | |
281 | } | |
282 | } |