Commit | Line | Data |
---|---|---|
a3b510ab NR |
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 | ||
bcb54330 NR |
148 | /** |
149 | * Overridden method from Swing's {@code JComponent} class that returns the preferred size of the terminal (in | |
150 | * pixels) | |
151 | * @return The terminal's preferred size in pixels | |
152 | */ | |
a3b510ab NR |
153 | @Override |
154 | public synchronized Dimension getPreferredSize() { | |
155 | return terminalImplementation.getPreferredSize(); | |
156 | } | |
157 | ||
bcb54330 NR |
158 | /** |
159 | * Overridden method from Swing's {@code JComponent} class that is called by OS window system when the component | |
160 | * needs to be redrawn | |
161 | * @param {@code Graphics} object to use when drawing the component | |
162 | */ | |
a3b510ab NR |
163 | @Override |
164 | protected synchronized void paintComponent(Graphics componentGraphics) { | |
165 | terminalImplementation.paintComponent(componentGraphics); | |
166 | } | |
167 | ||
bcb54330 | 168 | //////////////////////////////////////////////////////////////////////////////// |
a3b510ab NR |
169 | // Terminal methods below here, just forward to the implementation |
170 | ||
171 | @Override | |
172 | public void enterPrivateMode() { | |
173 | terminalImplementation.enterPrivateMode(); | |
174 | } | |
175 | ||
176 | @Override | |
177 | public void exitPrivateMode() { | |
178 | terminalImplementation.exitPrivateMode(); | |
179 | } | |
180 | ||
181 | @Override | |
182 | public void clearScreen() { | |
183 | terminalImplementation.clearScreen(); | |
184 | } | |
185 | ||
186 | @Override | |
187 | public void setCursorPosition(int x, int y) { | |
188 | terminalImplementation.setCursorPosition(x, y); | |
189 | } | |
190 | ||
191 | @Override | |
192 | public void setCursorVisible(boolean visible) { | |
193 | terminalImplementation.setCursorVisible(visible); | |
194 | } | |
195 | ||
196 | @Override | |
197 | public void putCharacter(char c) { | |
198 | terminalImplementation.putCharacter(c); | |
199 | } | |
200 | ||
201 | @Override | |
202 | public void enableSGR(SGR sgr) { | |
203 | terminalImplementation.enableSGR(sgr); | |
204 | } | |
205 | ||
206 | @Override | |
207 | public void disableSGR(SGR sgr) { | |
208 | terminalImplementation.disableSGR(sgr); | |
209 | } | |
210 | ||
211 | @Override | |
212 | public void resetColorAndSGR() { | |
213 | terminalImplementation.resetColorAndSGR(); | |
214 | } | |
215 | ||
216 | @Override | |
217 | public void setForegroundColor(TextColor color) { | |
218 | terminalImplementation.setForegroundColor(color); | |
219 | } | |
220 | ||
221 | @Override | |
222 | public void setBackgroundColor(TextColor color) { | |
223 | terminalImplementation.setBackgroundColor(color); | |
224 | } | |
225 | ||
226 | @Override | |
227 | public TerminalSize getTerminalSize() { | |
228 | return terminalImplementation.getTerminalSize(); | |
229 | } | |
230 | ||
231 | @Override | |
232 | public byte[] enquireTerminal(int timeout, TimeUnit timeoutUnit) { | |
233 | return terminalImplementation.enquireTerminal(timeout, timeoutUnit); | |
234 | } | |
235 | ||
236 | @Override | |
237 | public void flush() { | |
238 | terminalImplementation.flush(); | |
239 | } | |
240 | ||
241 | @Override | |
242 | public KeyStroke pollInput() { | |
243 | return terminalImplementation.pollInput(); | |
244 | } | |
245 | ||
246 | @Override | |
247 | public KeyStroke readInput() throws IOException { | |
248 | return terminalImplementation.readInput(); | |
249 | } | |
250 | ||
251 | @Override | |
252 | public TextGraphics newTextGraphics() throws IOException { | |
253 | return terminalImplementation.newTextGraphics(); | |
254 | } | |
255 | ||
256 | @Override | |
257 | public void addResizeListener(ResizeListener listener) { | |
258 | terminalImplementation.addResizeListener(listener); | |
259 | } | |
260 | ||
261 | @Override | |
262 | public void removeResizeListener(ResizeListener listener) { | |
263 | terminalImplementation.removeResizeListener(listener); | |
264 | } | |
265 | } |