Change build scripts
[jvcard.git] / src / com / googlecode / lanterna / terminal / swing / AWTTerminal.java
CommitLineData
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 */
19package com.googlecode.lanterna.terminal.swing;
20
21import com.googlecode.lanterna.SGR;
22import com.googlecode.lanterna.TerminalSize;
23import com.googlecode.lanterna.TextColor;
24import com.googlecode.lanterna.graphics.TextGraphics;
25import com.googlecode.lanterna.input.KeyStroke;
26import com.googlecode.lanterna.terminal.IOSafeTerminal;
27import com.googlecode.lanterna.terminal.ResizeListener;
28
29import java.awt.*;
30import java.io.IOException;
31import 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")
43public 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
bcb54330
NR
165 /**
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
168 */
a3b510ab
NR
169 @Override
170 public synchronized Dimension getPreferredSize() {
171 return terminalImplementation.getPreferredSize();
172 }
173
bcb54330
NR
174 /**
175 * Overridden method from AWT's {@code Component} class that is called by OS window system when the component needs
176 * to be redrawn
177 * @param {@code Graphics} object to use when drawing the component
178 */
a3b510ab
NR
179 @Override
180 public synchronized void paint(Graphics componentGraphics) {
181 // Flicker-free AWT!
182 // Extend Panel and do the drawing work in both update(..) and paint(..)
183 terminalImplementation.paintComponent(componentGraphics);
184 }
185
bcb54330
NR
186 /**
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
190 */
a3b510ab
NR
191 @Override
192 public synchronized void update(Graphics componentGraphics) {
193 // Flicker-free AWT!
194 // Extend Panel and do the drawing work in both update(..) and paint(..)
195 terminalImplementation.paintComponent(componentGraphics);
196 }
197
198 // Terminal methods below here, just forward to the implementation
199
200 @Override
201 public void enterPrivateMode() {
202 terminalImplementation.enterPrivateMode();
203 }
204
205 @Override
206 public void exitPrivateMode() {
207 terminalImplementation.exitPrivateMode();
208 }
209
210 @Override
211 public void clearScreen() {
212 terminalImplementation.clearScreen();
213 }
214
215 @Override
216 public void setCursorPosition(int x, int y) {
217 terminalImplementation.setCursorPosition(x, y);
218 }
219
220 @Override
221 public void setCursorVisible(boolean visible) {
222 terminalImplementation.setCursorVisible(visible);
223 }
224
225 @Override
226 public void putCharacter(char c) {
227 terminalImplementation.putCharacter(c);
228 }
229
230 @Override
231 public void enableSGR(SGR sgr) {
232 terminalImplementation.enableSGR(sgr);
233 }
234
235 @Override
236 public void disableSGR(SGR sgr) {
237 terminalImplementation.disableSGR(sgr);
238 }
239
240 @Override
241 public void resetColorAndSGR() {
242 terminalImplementation.resetColorAndSGR();
243 }
244
245 @Override
246 public void setForegroundColor(TextColor color) {
247 terminalImplementation.setForegroundColor(color);
248 }
249
250 @Override
251 public void setBackgroundColor(TextColor color) {
252 terminalImplementation.setBackgroundColor(color);
253 }
254
255 @Override
256 public TerminalSize getTerminalSize() {
257 return terminalImplementation.getTerminalSize();
258 }
259
260 @Override
261 public byte[] enquireTerminal(int timeout, TimeUnit timeoutUnit) {
262 return terminalImplementation.enquireTerminal(timeout, timeoutUnit);
263 }
264
265 @Override
266 public void flush() {
267 terminalImplementation.flush();
268 }
269
270 @Override
271 public KeyStroke pollInput() {
272 return terminalImplementation.pollInput();
273 }
274
275 @Override
276 public KeyStroke readInput() throws IOException {
277 return terminalImplementation.readInput();
278 }
279
280 @Override
281 public TextGraphics newTextGraphics() throws IOException {
282 return terminalImplementation.newTextGraphics();
283 }
284
285 @Override
286 public void addResizeListener(ResizeListener listener) {
287 terminalImplementation.addResizeListener(listener);
288 }
289
290 @Override
291 public void removeResizeListener(ResizeListener listener) {
292 terminalImplementation.removeResizeListener(listener);
293 }
294}