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.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; | |
28 | ||
29 | import java.awt.*; | |
30 | import java.io.IOException; | |
31 | import java.util.concurrent.TimeUnit; | |
32 | ||
33 | ||
34 | /** | |
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. | |
40 | * @author martin | |
41 | */ | |
42 | @SuppressWarnings("serial") | |
43 | public class AWTTerminal extends Panel implements IOSafeTerminal { | |
44 | ||
45 | private final AWTTerminalImplementation terminalImplementation; | |
46 | ||
47 | /** | |
48 | * Creates a new AWTTerminal with all the defaults set and no scroll controller connected. | |
49 | */ | |
50 | public AWTTerminal() { | |
51 | this(new TerminalScrollController.Null()); | |
52 | } | |
53 | ||
54 | ||
55 | /** | |
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 | |
59 | */ | |
60 | @SuppressWarnings("WeakerAccess") | |
61 | public AWTTerminal(TerminalScrollController scrollController) { | |
62 | this(TerminalEmulatorDeviceConfiguration.getDefault(), | |
63 | AWTTerminalFontConfiguration.getDefault(), | |
64 | TerminalEmulatorColorConfiguration.getDefault(), | |
65 | scrollController); | |
66 | } | |
67 | ||
68 | /** | |
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 | |
73 | */ | |
74 | public AWTTerminal( | |
75 | TerminalEmulatorDeviceConfiguration deviceConfiguration, | |
76 | AWTTerminalFontConfiguration fontConfiguration, | |
77 | TerminalEmulatorColorConfiguration colorConfiguration) { | |
78 | ||
79 | this(null, deviceConfiguration, fontConfiguration, colorConfiguration); | |
80 | } | |
81 | ||
82 | /** | |
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 | |
90 | */ | |
91 | public AWTTerminal( | |
92 | TerminalSize initialTerminalSize, | |
93 | TerminalEmulatorDeviceConfiguration deviceConfiguration, | |
94 | AWTTerminalFontConfiguration fontConfiguration, | |
95 | TerminalEmulatorColorConfiguration colorConfiguration) { | |
96 | ||
97 | this(initialTerminalSize, | |
98 | deviceConfiguration, | |
99 | fontConfiguration, | |
100 | colorConfiguration, | |
101 | new TerminalScrollController.Null()); | |
102 | } | |
103 | ||
104 | /** | |
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 | |
113 | */ | |
114 | public AWTTerminal( | |
115 | TerminalEmulatorDeviceConfiguration deviceConfiguration, | |
116 | AWTTerminalFontConfiguration fontConfiguration, | |
117 | TerminalEmulatorColorConfiguration colorConfiguration, | |
118 | TerminalScrollController scrollController) { | |
119 | ||
120 | this(null, deviceConfiguration, fontConfiguration, colorConfiguration, scrollController); | |
121 | } | |
122 | ||
123 | ||
124 | ||
125 | /** | |
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 | |
137 | */ | |
138 | public AWTTerminal( | |
139 | TerminalSize initialTerminalSize, | |
140 | TerminalEmulatorDeviceConfiguration deviceConfiguration, | |
141 | AWTTerminalFontConfiguration fontConfiguration, | |
142 | TerminalEmulatorColorConfiguration colorConfiguration, | |
143 | TerminalScrollController scrollController) { | |
144 | ||
145 | //Enforce valid values on the input parameters | |
146 | if(deviceConfiguration == null) { | |
147 | deviceConfiguration = TerminalEmulatorDeviceConfiguration.getDefault(); | |
148 | } | |
149 | if(fontConfiguration == null) { | |
150 | fontConfiguration = SwingTerminalFontConfiguration.getDefault(); | |
151 | } | |
152 | if(colorConfiguration == null) { | |
153 | colorConfiguration = TerminalEmulatorColorConfiguration.getDefault(); | |
154 | } | |
155 | ||
156 | terminalImplementation = new AWTTerminalImplementation( | |
157 | this, | |
158 | fontConfiguration, | |
159 | initialTerminalSize, | |
160 | deviceConfiguration, | |
161 | colorConfiguration, | |
162 | scrollController); | |
163 | } | |
164 | ||
165 | @Override | |
166 | public synchronized Dimension getPreferredSize() { | |
167 | return terminalImplementation.getPreferredSize(); | |
168 | } | |
169 | ||
170 | @Override | |
171 | public synchronized void paint(Graphics componentGraphics) { | |
172 | // Flicker-free AWT! | |
173 | // Extend Panel and do the drawing work in both update(..) and paint(..) | |
174 | terminalImplementation.paintComponent(componentGraphics); | |
175 | } | |
176 | ||
177 | @Override | |
178 | public synchronized void update(Graphics componentGraphics) { | |
179 | // Flicker-free AWT! | |
180 | // Extend Panel and do the drawing work in both update(..) and paint(..) | |
181 | terminalImplementation.paintComponent(componentGraphics); | |
182 | } | |
183 | ||
184 | // Terminal methods below here, just forward to the implementation | |
185 | ||
186 | @Override | |
187 | public void enterPrivateMode() { | |
188 | terminalImplementation.enterPrivateMode(); | |
189 | } | |
190 | ||
191 | @Override | |
192 | public void exitPrivateMode() { | |
193 | terminalImplementation.exitPrivateMode(); | |
194 | } | |
195 | ||
196 | @Override | |
197 | public void clearScreen() { | |
198 | terminalImplementation.clearScreen(); | |
199 | } | |
200 | ||
201 | @Override | |
202 | public void setCursorPosition(int x, int y) { | |
203 | terminalImplementation.setCursorPosition(x, y); | |
204 | } | |
205 | ||
206 | @Override | |
207 | public void setCursorVisible(boolean visible) { | |
208 | terminalImplementation.setCursorVisible(visible); | |
209 | } | |
210 | ||
211 | @Override | |
212 | public void putCharacter(char c) { | |
213 | terminalImplementation.putCharacter(c); | |
214 | } | |
215 | ||
216 | @Override | |
217 | public void enableSGR(SGR sgr) { | |
218 | terminalImplementation.enableSGR(sgr); | |
219 | } | |
220 | ||
221 | @Override | |
222 | public void disableSGR(SGR sgr) { | |
223 | terminalImplementation.disableSGR(sgr); | |
224 | } | |
225 | ||
226 | @Override | |
227 | public void resetColorAndSGR() { | |
228 | terminalImplementation.resetColorAndSGR(); | |
229 | } | |
230 | ||
231 | @Override | |
232 | public void setForegroundColor(TextColor color) { | |
233 | terminalImplementation.setForegroundColor(color); | |
234 | } | |
235 | ||
236 | @Override | |
237 | public void setBackgroundColor(TextColor color) { | |
238 | terminalImplementation.setBackgroundColor(color); | |
239 | } | |
240 | ||
241 | @Override | |
242 | public TerminalSize getTerminalSize() { | |
243 | return terminalImplementation.getTerminalSize(); | |
244 | } | |
245 | ||
246 | @Override | |
247 | public byte[] enquireTerminal(int timeout, TimeUnit timeoutUnit) { | |
248 | return terminalImplementation.enquireTerminal(timeout, timeoutUnit); | |
249 | } | |
250 | ||
251 | @Override | |
252 | public void flush() { | |
253 | terminalImplementation.flush(); | |
254 | } | |
255 | ||
256 | @Override | |
257 | public KeyStroke pollInput() { | |
258 | return terminalImplementation.pollInput(); | |
259 | } | |
260 | ||
261 | @Override | |
262 | public KeyStroke readInput() throws IOException { | |
263 | return terminalImplementation.readInput(); | |
264 | } | |
265 | ||
266 | @Override | |
267 | public TextGraphics newTextGraphics() throws IOException { | |
268 | return terminalImplementation.newTextGraphics(); | |
269 | } | |
270 | ||
271 | @Override | |
272 | public void addResizeListener(ResizeListener listener) { | |
273 | terminalImplementation.addResizeListener(listener); | |
274 | } | |
275 | ||
276 | @Override | |
277 | public void removeResizeListener(ResizeListener listener) { | |
278 | terminalImplementation.removeResizeListener(listener); | |
279 | } | |
280 | } |