Change build scripts
[jvcard.git] / src / com / googlecode / lanterna / terminal / swing / SwingTerminalFrame.java
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 import javax.swing.*;
34
35 /**
36 * This class is similar to what SwingTerminal used to be before Lanterna 3.0; a JFrame that contains a terminal
37 * emulator. In Lanterna 3, this class is just a JFrame containing a SwingTerminal component, but it also implements
38 * the Terminal interface and delegates all calls to the internal SwingTerminal. You can tweak the class a bit to have
39 * special behaviours when exiting private mode or when the user presses ESC key.
40 * @author martin
41 */
42 @SuppressWarnings("serial")
43 public class SwingTerminalFrame extends JFrame implements IOSafeTerminal {
44 private final SwingTerminal swingTerminal;
45 private TerminalEmulatorAutoCloseTrigger autoCloseTrigger;
46 private boolean disposed;
47
48 /**
49 * Creates a new SwingTerminalFrame that doesn't automatically close.
50 */
51 public SwingTerminalFrame() throws HeadlessException {
52 this(TerminalEmulatorAutoCloseTrigger.DoNotAutoClose);
53 }
54
55 /**
56 * Creates a new SwingTerminalFrame with a specified auto-close behaviour
57 * @param autoCloseTrigger What to trigger automatic disposal of the JFrame
58 */
59 @SuppressWarnings({"SameParameterValue", "WeakerAccess"})
60 public SwingTerminalFrame(TerminalEmulatorAutoCloseTrigger autoCloseTrigger) {
61 this("SwingTerminalFrame", autoCloseTrigger);
62 }
63
64 /**
65 * Creates a new SwingTerminalFrame with a given title and no automatic closing.
66 * @param title Title to use for the window
67 */
68 public SwingTerminalFrame(String title) throws HeadlessException {
69 this(title, TerminalEmulatorAutoCloseTrigger.DoNotAutoClose);
70 }
71
72 /**
73 * Creates a new SwingTerminalFrame with a specified auto-close behaviour and specific title
74 * @param title Title to use for the window
75 * @param autoCloseTrigger What to trigger automatic disposal of the JFrame
76 */
77 @SuppressWarnings("WeakerAccess")
78 public SwingTerminalFrame(String title, TerminalEmulatorAutoCloseTrigger autoCloseTrigger) throws HeadlessException {
79 this(title, new SwingTerminal(), autoCloseTrigger);
80 }
81
82 /**
83 * Creates a new SwingTerminalFrame using a specified title and a series of swing terminal configuration objects
84 * @param title What title to use for the window
85 * @param deviceConfiguration Device configuration for the embedded SwingTerminal
86 * @param fontConfiguration Font configuration for the embedded SwingTerminal
87 * @param colorConfiguration Color configuration for the embedded SwingTerminal
88 */
89 public SwingTerminalFrame(String title,
90 TerminalEmulatorDeviceConfiguration deviceConfiguration,
91 SwingTerminalFontConfiguration fontConfiguration,
92 TerminalEmulatorColorConfiguration colorConfiguration) {
93 this(title, deviceConfiguration, fontConfiguration, colorConfiguration, TerminalEmulatorAutoCloseTrigger.DoNotAutoClose);
94 }
95
96 /**
97 * Creates a new SwingTerminalFrame using a specified title and a series of swing terminal configuration objects
98 * @param title What title to use for the window
99 * @param deviceConfiguration Device configuration for the embedded SwingTerminal
100 * @param fontConfiguration Font configuration for the embedded SwingTerminal
101 * @param colorConfiguration Color configuration for the embedded SwingTerminal
102 * @param autoCloseTrigger What to trigger automatic disposal of the JFrame
103 */
104 public SwingTerminalFrame(String title,
105 TerminalEmulatorDeviceConfiguration deviceConfiguration,
106 SwingTerminalFontConfiguration fontConfiguration,
107 TerminalEmulatorColorConfiguration colorConfiguration,
108 TerminalEmulatorAutoCloseTrigger autoCloseTrigger) {
109 this(title, null, deviceConfiguration, fontConfiguration, colorConfiguration, autoCloseTrigger);
110 }
111
112 /**
113 * Creates a new SwingTerminalFrame using a specified title and a series of swing terminal configuration objects
114 * @param title What title to use for the window
115 * @param terminalSize Initial size of the terminal, in rows and columns. If null, it will default to 80x25.
116 * @param deviceConfiguration Device configuration for the embedded SwingTerminal
117 * @param fontConfiguration Font configuration for the embedded SwingTerminal
118 * @param colorConfiguration Color configuration for the embedded SwingTerminal
119 * @param autoCloseTrigger What to trigger automatic disposal of the JFrame
120 */
121 public SwingTerminalFrame(String title,
122 TerminalSize terminalSize,
123 TerminalEmulatorDeviceConfiguration deviceConfiguration,
124 SwingTerminalFontConfiguration fontConfiguration,
125 TerminalEmulatorColorConfiguration colorConfiguration,
126 TerminalEmulatorAutoCloseTrigger autoCloseTrigger) {
127 this(title,
128 new SwingTerminal(terminalSize, deviceConfiguration, fontConfiguration, colorConfiguration),
129 autoCloseTrigger);
130 }
131
132 private SwingTerminalFrame(String title, SwingTerminal swingTerminal, TerminalEmulatorAutoCloseTrigger autoCloseTrigger) {
133 super(title != null ? title : "SwingTerminalFrame");
134 this.swingTerminal = swingTerminal;
135 this.autoCloseTrigger = autoCloseTrigger;
136 this.disposed = false;
137
138 getContentPane().setLayout(new BorderLayout());
139 getContentPane().add(swingTerminal, BorderLayout.CENTER);
140 setBackground(Color.BLACK); //This will reduce white flicker when resizing the window
141 pack();
142
143 //Put input focus on the terminal component by default
144 swingTerminal.requestFocusInWindow();
145 }
146
147 /**
148 * Returns the auto-close trigger used by the SwingTerminalFrame
149 * @return Current auto-close trigger
150 */
151 public TerminalEmulatorAutoCloseTrigger getAutoCloseTrigger() {
152 return autoCloseTrigger;
153 }
154
155 /**
156 * Changes the current auto-close trigger used by this SwingTerminalFrame
157 * @param autoCloseTrigger New auto-close trigger to use
158 */
159 public void setAutoCloseTrigger(TerminalEmulatorAutoCloseTrigger autoCloseTrigger) {
160 this.autoCloseTrigger = autoCloseTrigger;
161 }
162
163 @Override
164 public void dispose() {
165 super.dispose();
166 disposed = true;
167 }
168
169 ///////////
170 // Delegate all Terminal interface implementations to SwingTerminal
171 ///////////
172 @Override
173 public KeyStroke pollInput() {
174 if(disposed) {
175 return new KeyStroke(KeyType.EOF);
176 }
177 KeyStroke keyStroke = swingTerminal.pollInput();
178 if(autoCloseTrigger == TerminalEmulatorAutoCloseTrigger.CloseOnEscape &&
179 keyStroke != null &&
180 keyStroke.getKeyType() == KeyType.Escape) {
181 dispose();
182 }
183 return keyStroke;
184 }
185
186 @Override
187 public KeyStroke readInput() throws IOException {
188 return swingTerminal.readInput();
189 }
190
191 @Override
192 public void enterPrivateMode() {
193 swingTerminal.enterPrivateMode();
194 }
195
196 @Override
197 public void exitPrivateMode() {
198 swingTerminal.exitPrivateMode();
199 if(autoCloseTrigger == TerminalEmulatorAutoCloseTrigger.CloseOnExitPrivateMode) {
200 dispose();
201 }
202 }
203
204 @Override
205 public void clearScreen() {
206 swingTerminal.clearScreen();
207 }
208
209 @Override
210 public void setCursorPosition(int x, int y) {
211 swingTerminal.setCursorPosition(x, y);
212 }
213
214 @Override
215 public void setCursorVisible(boolean visible) {
216 swingTerminal.setCursorVisible(visible);
217 }
218
219 @Override
220 public void putCharacter(char c) {
221 swingTerminal.putCharacter(c);
222 }
223
224 @Override
225 public TextGraphics newTextGraphics() throws IOException {
226 return swingTerminal.newTextGraphics();
227 }
228
229 @Override
230 public void enableSGR(SGR sgr) {
231 swingTerminal.enableSGR(sgr);
232 }
233
234 @Override
235 public void disableSGR(SGR sgr) {
236 swingTerminal.disableSGR(sgr);
237 }
238
239 @Override
240 public void resetColorAndSGR() {
241 swingTerminal.resetColorAndSGR();
242 }
243
244 @Override
245 public void setForegroundColor(TextColor color) {
246 swingTerminal.setForegroundColor(color);
247 }
248
249 @Override
250 public void setBackgroundColor(TextColor color) {
251 swingTerminal.setBackgroundColor(color);
252 }
253
254 @Override
255 public TerminalSize getTerminalSize() {
256 return swingTerminal.getTerminalSize();
257 }
258
259 @Override
260 public byte[] enquireTerminal(int timeout, TimeUnit timeoutUnit) {
261 return swingTerminal.enquireTerminal(timeout, timeoutUnit);
262 }
263
264 @Override
265 public void flush() {
266 swingTerminal.flush();
267 }
268
269 @Override
270 public void addResizeListener(ResizeListener listener) {
271 swingTerminal.addResizeListener(listener);
272 }
273
274 @Override
275 public void removeResizeListener(ResizeListener listener) {
276 swingTerminal.removeResizeListener(listener);
277 }
278 }