1 package com
.googlecode
.lanterna
.terminal
.swing
;
3 import com
.googlecode
.lanterna
.TerminalSize
;
4 import com
.googlecode
.lanterna
.TextCharacter
;
8 import java
.awt
.event
.HierarchyEvent
;
9 import java
.awt
.event
.HierarchyListener
;
10 import java
.awt
.event
.MouseAdapter
;
11 import java
.awt
.event
.MouseEvent
;
12 import java
.io
.IOException
;
13 import java
.util
.Collections
;
16 * Concrete implementation of {@link GraphicalTerminalImplementation} that adapts it to Swing
18 class SwingTerminalImplementation
extends GraphicalTerminalImplementation
{
20 private final JComponent component
;
21 private final SwingTerminalFontConfiguration fontConfiguration
;
24 * Creates a new {@code SwingTerminalImplementation}
25 * @param component JComponent that is the Swing terminal surface
26 * @param fontConfiguration Font configuration to use
27 * @param initialTerminalSize Initial size of the terminal
28 * @param deviceConfiguration Device configuration
29 * @param colorConfiguration Color configuration
30 * @param scrollController Controller to be used when inspecting scroll status
32 SwingTerminalImplementation(
34 SwingTerminalFontConfiguration fontConfiguration
,
35 TerminalSize initialTerminalSize
,
36 TerminalEmulatorDeviceConfiguration deviceConfiguration
,
37 TerminalEmulatorColorConfiguration colorConfiguration
,
38 TerminalScrollController scrollController
) {
40 super(initialTerminalSize
, deviceConfiguration
, colorConfiguration
, scrollController
);
41 this.component
= component
;
42 this.fontConfiguration
= fontConfiguration
;
44 //Prevent us from shrinking beyond one character
45 component
.setMinimumSize(new Dimension(fontConfiguration
.getFontWidth(), fontConfiguration
.getFontHeight()));
47 //noinspection unchecked
48 component
.setFocusTraversalKeys(KeyboardFocusManager
.FORWARD_TRAVERSAL_KEYS
, Collections
.<AWTKeyStroke
>emptySet());
49 //noinspection unchecked
50 component
.setFocusTraversalKeys(KeyboardFocusManager
.BACKWARD_TRAVERSAL_KEYS
, Collections
.<AWTKeyStroke
>emptySet());
52 //Make sure the component is double-buffered to prevent flickering
53 component
.setDoubleBuffered(true);
55 component
.addKeyListener(new TerminalInputListener());
56 component
.addMouseListener(new MouseAdapter() {
58 public void mouseClicked(MouseEvent e
) {
59 SwingTerminalImplementation
.this.component
.requestFocusInWindow();
62 component
.addHierarchyListener(new HierarchyListener() {
64 public void hierarchyChanged(HierarchyEvent e
) {
65 if(e
.getChangeFlags() == HierarchyEvent
.DISPLAYABILITY_CHANGED
) {
66 if(e
.getChanged().isDisplayable()) {
79 * Returns the current font configuration. Note that it is immutable and cannot be changed.
80 * @return This SwingTerminal's current font configuration
82 public SwingTerminalFontConfiguration
getFontConfiguration() {
83 return fontConfiguration
;
87 protected int getFontHeight() {
88 return fontConfiguration
.getFontHeight();
92 protected int getFontWidth() {
93 return fontConfiguration
.getFontWidth();
97 protected int getHeight() {
98 return component
.getHeight();
102 protected int getWidth() {
103 return component
.getWidth();
107 protected Font
getFontForCharacter(TextCharacter character
) {
108 return fontConfiguration
.getFontForCharacter(character
);
112 protected boolean isTextAntiAliased() {
113 return fontConfiguration
.isAntiAliased();
117 protected void repaint() {
118 if(SwingUtilities
.isEventDispatchThread()) {
122 SwingUtilities
.invokeLater(new Runnable() {
132 public com
.googlecode
.lanterna
.input
.KeyStroke
readInput() throws IOException
{
133 if(SwingUtilities
.isEventDispatchThread()) {
134 throw new UnsupportedOperationException("Cannot call SwingTerminal.readInput() on the AWT thread");
136 return super.readInput();