459722275b77c334e8ff53bb4908dc33070ffb6b
[jvcard.git] / src / com / googlecode / lanterna / terminal / swing / SwingTerminal.java
1 package com.googlecode.lanterna.terminal.swing;
2
3 import com.googlecode.lanterna.SGR;
4 import com.googlecode.lanterna.TerminalSize;
5 import com.googlecode.lanterna.TextColor;
6 import com.googlecode.lanterna.graphics.TextGraphics;
7 import com.googlecode.lanterna.input.*;
8 import com.googlecode.lanterna.input.KeyStroke;
9 import com.googlecode.lanterna.terminal.IOSafeTerminal;
10 import com.googlecode.lanterna.terminal.ResizeListener;
11
12 import javax.swing.*;
13 import java.awt.*;
14 import java.io.IOException;
15 import java.util.concurrent.TimeUnit;
16
17 /**
18 * This class provides an Swing implementation of the {@link com.googlecode.lanterna.terminal.Terminal} interface that
19 * is an embeddable component you can put into a Swing container. The class has static helper methods for opening a new
20 * frame with a {@link SwingTerminal} as its content, similar to how the SwingTerminal used to work in earlier versions
21 * of lanterna. This version supports private mode and non-private mode with a scrollback history. You can customize
22 * many of the properties by supplying device configuration, font configuration and color configuration when you
23 * construct the object.
24 * @author martin
25 */
26 public class SwingTerminal extends JComponent implements IOSafeTerminal {
27
28 private final SwingTerminalImplementation terminalImplementation;
29
30 /**
31 * Creates a new SwingTerminal with all the defaults set and no scroll controller connected.
32 */
33 public SwingTerminal() {
34 this(new TerminalScrollController.Null());
35 }
36
37
38 /**
39 * Creates a new SwingTerminal with a particular scrolling controller that will be notified when the terminals
40 * history size grows and will be called when this class needs to figure out the current scrolling position.
41 * @param scrollController Controller for scrolling the terminal history
42 */
43 @SuppressWarnings("WeakerAccess")
44 public SwingTerminal(TerminalScrollController scrollController) {
45 this(TerminalEmulatorDeviceConfiguration.getDefault(),
46 SwingTerminalFontConfiguration.getDefault(),
47 TerminalEmulatorColorConfiguration.getDefault(),
48 scrollController);
49 }
50
51 /**
52 * Creates a new SwingTerminal component using custom settings and no scroll controller.
53 * @param deviceConfiguration Device configuration to use for this SwingTerminal
54 * @param fontConfiguration Font configuration to use for this SwingTerminal
55 * @param colorConfiguration Color configuration to use for this SwingTerminal
56 */
57 public SwingTerminal(
58 TerminalEmulatorDeviceConfiguration deviceConfiguration,
59 SwingTerminalFontConfiguration fontConfiguration,
60 TerminalEmulatorColorConfiguration colorConfiguration) {
61
62 this(null, deviceConfiguration, fontConfiguration, colorConfiguration);
63 }
64
65 /**
66 * Creates a new SwingTerminal component using custom settings and no scroll controller.
67 * @param initialTerminalSize Initial size of the terminal, which will be used when calculating the preferred size
68 * of the component. If null, it will default to 80x25. If the AWT layout manager forces
69 * the component to a different size, the value of this parameter won't have any meaning
70 * @param deviceConfiguration Device configuration to use for this SwingTerminal
71 * @param fontConfiguration Font configuration to use for this SwingTerminal
72 * @param colorConfiguration Color configuration to use for this SwingTerminal
73 */
74 public SwingTerminal(
75 TerminalSize initialTerminalSize,
76 TerminalEmulatorDeviceConfiguration deviceConfiguration,
77 SwingTerminalFontConfiguration fontConfiguration,
78 TerminalEmulatorColorConfiguration colorConfiguration) {
79
80 this(initialTerminalSize,
81 deviceConfiguration,
82 fontConfiguration,
83 colorConfiguration,
84 new TerminalScrollController.Null());
85 }
86
87 /**
88 * Creates a new SwingTerminal component using custom settings and a custom scroll controller. The scrolling
89 * controller will be notified when the terminal's history size grows and will be called when this class needs to
90 * figure out the current scrolling position.
91 * @param deviceConfiguration Device configuration to use for this SwingTerminal
92 * @param fontConfiguration Font configuration to use for this SwingTerminal
93 * @param colorConfiguration Color configuration to use for this SwingTerminal
94 * @param scrollController Controller to use for scrolling, the object passed in will be notified whenever the
95 * scrollable area has changed
96 */
97 public SwingTerminal(
98 TerminalEmulatorDeviceConfiguration deviceConfiguration,
99 SwingTerminalFontConfiguration fontConfiguration,
100 TerminalEmulatorColorConfiguration colorConfiguration,
101 TerminalScrollController scrollController) {
102
103 this(null, deviceConfiguration, fontConfiguration, colorConfiguration, scrollController);
104 }
105
106
107
108 /**
109 * Creates a new SwingTerminal component using custom settings and a custom scroll controller. The scrolling
110 * controller will be notified when the terminal's history size grows and will be called when this class needs to
111 * figure out the current scrolling position.
112 * @param initialTerminalSize Initial size of the terminal, which will be used when calculating the preferred size
113 * of the component. If null, it will default to 80x25. If the AWT layout manager forces
114 * the component to a different size, the value of this parameter won't have any meaning
115 * @param deviceConfiguration Device configuration to use for this SwingTerminal
116 * @param fontConfiguration Font configuration to use for this SwingTerminal
117 * @param colorConfiguration Color configuration to use for this SwingTerminal
118 * @param scrollController Controller to use for scrolling, the object passed in will be notified whenever the
119 * scrollable area has changed
120 */
121 public SwingTerminal(
122 TerminalSize initialTerminalSize,
123 TerminalEmulatorDeviceConfiguration deviceConfiguration,
124 SwingTerminalFontConfiguration fontConfiguration,
125 TerminalEmulatorColorConfiguration colorConfiguration,
126 TerminalScrollController scrollController) {
127
128 //Enforce valid values on the input parameters
129 if(deviceConfiguration == null) {
130 deviceConfiguration = TerminalEmulatorDeviceConfiguration.getDefault();
131 }
132 if(fontConfiguration == null) {
133 fontConfiguration = SwingTerminalFontConfiguration.getDefault();
134 }
135 if(colorConfiguration == null) {
136 colorConfiguration = TerminalEmulatorColorConfiguration.getDefault();
137 }
138
139 terminalImplementation = new SwingTerminalImplementation(
140 this,
141 fontConfiguration,
142 initialTerminalSize,
143 deviceConfiguration,
144 colorConfiguration,
145 scrollController);
146 }
147
148 @Override
149 public synchronized Dimension getPreferredSize() {
150 return terminalImplementation.getPreferredSize();
151 }
152
153 @Override
154 protected synchronized void paintComponent(Graphics componentGraphics) {
155 terminalImplementation.paintComponent(componentGraphics);
156 }
157
158 // Terminal methods below here, just forward to the implementation
159
160 @Override
161 public void enterPrivateMode() {
162 terminalImplementation.enterPrivateMode();
163 }
164
165 @Override
166 public void exitPrivateMode() {
167 terminalImplementation.exitPrivateMode();
168 }
169
170 @Override
171 public void clearScreen() {
172 terminalImplementation.clearScreen();
173 }
174
175 @Override
176 public void setCursorPosition(int x, int y) {
177 terminalImplementation.setCursorPosition(x, y);
178 }
179
180 @Override
181 public void setCursorVisible(boolean visible) {
182 terminalImplementation.setCursorVisible(visible);
183 }
184
185 @Override
186 public void putCharacter(char c) {
187 terminalImplementation.putCharacter(c);
188 }
189
190 @Override
191 public void enableSGR(SGR sgr) {
192 terminalImplementation.enableSGR(sgr);
193 }
194
195 @Override
196 public void disableSGR(SGR sgr) {
197 terminalImplementation.disableSGR(sgr);
198 }
199
200 @Override
201 public void resetColorAndSGR() {
202 terminalImplementation.resetColorAndSGR();
203 }
204
205 @Override
206 public void setForegroundColor(TextColor color) {
207 terminalImplementation.setForegroundColor(color);
208 }
209
210 @Override
211 public void setBackgroundColor(TextColor color) {
212 terminalImplementation.setBackgroundColor(color);
213 }
214
215 @Override
216 public TerminalSize getTerminalSize() {
217 return terminalImplementation.getTerminalSize();
218 }
219
220 @Override
221 public byte[] enquireTerminal(int timeout, TimeUnit timeoutUnit) {
222 return terminalImplementation.enquireTerminal(timeout, timeoutUnit);
223 }
224
225 @Override
226 public void flush() {
227 terminalImplementation.flush();
228 }
229
230 @Override
231 public KeyStroke pollInput() {
232 return terminalImplementation.pollInput();
233 }
234
235 @Override
236 public KeyStroke readInput() throws IOException {
237 return terminalImplementation.readInput();
238 }
239
240 @Override
241 public TextGraphics newTextGraphics() throws IOException {
242 return terminalImplementation.newTextGraphics();
243 }
244
245 @Override
246 public void addResizeListener(ResizeListener listener) {
247 terminalImplementation.addResizeListener(listener);
248 }
249
250 @Override
251 public void removeResizeListener(ResizeListener listener) {
252 terminalImplementation.removeResizeListener(listener);
253 }
254 }