Change build scripts
[jvcard.git] / src / com / googlecode / lanterna / terminal / swing / AWTTerminalImplementation.java
CommitLineData
a3b510ab
NR
1package com.googlecode.lanterna.terminal.swing;
2
3import com.googlecode.lanterna.TerminalSize;
4import com.googlecode.lanterna.TextCharacter;
5import com.googlecode.lanterna.input.KeyStroke;
6
7import java.awt.*;
8import java.awt.event.*;
9import java.io.IOException;
10import java.util.Collections;
11
12/**
13 * AWT implementation of {@link GraphicalTerminalImplementation} that contains all the overrides for AWT
14 * Created by martin on 08/02/16.
15 */
16class AWTTerminalImplementation extends GraphicalTerminalImplementation {
17 private final Component component;
18 private final AWTTerminalFontConfiguration fontConfiguration;
19
20 /**
21 * Creates a new {@code AWTTerminalImplementation}
22 * @param component Component that is the AWT terminal surface
23 * @param fontConfiguration Font configuration to use
24 * @param initialTerminalSize Initial size of the terminal
25 * @param deviceConfiguration Device configuration
26 * @param colorConfiguration Color configuration
27 * @param scrollController Controller to be used when inspecting scroll status
28 */
29 AWTTerminalImplementation(
30 Component component,
31 AWTTerminalFontConfiguration fontConfiguration,
32 TerminalSize initialTerminalSize,
33 TerminalEmulatorDeviceConfiguration deviceConfiguration,
34 TerminalEmulatorColorConfiguration colorConfiguration,
35 TerminalScrollController scrollController) {
36
37 super(initialTerminalSize, deviceConfiguration, colorConfiguration, scrollController);
38 this.component = component;
39 this.fontConfiguration = fontConfiguration;
40
41 //Prevent us from shrinking beyond one character
42 component.setMinimumSize(new Dimension(fontConfiguration.getFontWidth(), fontConfiguration.getFontHeight()));
43
44 //noinspection unchecked
45 component.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, Collections.<AWTKeyStroke>emptySet());
46 //noinspection unchecked
47 component.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, Collections.<AWTKeyStroke>emptySet());
48
49 component.addKeyListener(new TerminalInputListener());
50 component.addMouseListener(new MouseAdapter() {
51 @Override
52 public void mouseClicked(MouseEvent e) {
53 AWTTerminalImplementation.this.component.requestFocusInWindow();
54 }
55 });
56
57 component.addHierarchyListener(new HierarchyListener() {
58 @Override
59 public void hierarchyChanged(HierarchyEvent e) {
60 if(e.getChangeFlags() == HierarchyEvent.DISPLAYABILITY_CHANGED) {
61 if(e.getChanged().isDisplayable()) {
62 startBlinkTimer();
63 }
64 else {
65 stopBlinkTimer();
66 }
67 }
68 }
69 });
70 }
71
72
73 /**
74 * Returns the current font configuration. Note that it is immutable and cannot be changed.
75 * @return This {@link AWTTerminal}'s current font configuration
76 */
77 public AWTTerminalFontConfiguration getFontConfiguration() {
78 return fontConfiguration;
79 }
80
81 @Override
82 protected int getFontHeight() {
83 return fontConfiguration.getFontHeight();
84 }
85
86 @Override
87 protected int getFontWidth() {
88 return fontConfiguration.getFontWidth();
89 }
90
91 @Override
92 protected int getHeight() {
93 return component.getHeight();
94 }
95
96 @Override
97 protected int getWidth() {
98 return component.getWidth();
99 }
100
101 @Override
102 protected Font getFontForCharacter(TextCharacter character) {
103 return fontConfiguration.getFontForCharacter(character);
104 }
105
106 @Override
107 protected boolean isTextAntiAliased() {
108 return fontConfiguration.isAntiAliased();
109 }
110
111 @Override
112 protected void repaint() {
113 if(EventQueue.isDispatchThread()) {
114 component.repaint();
115 }
116 else {
117 EventQueue.invokeLater(new Runnable() {
118 @Override
119 public void run() {
120 component.repaint();
121 }
122 });
123 }
124 }
125
126 @Override
127 public KeyStroke readInput() throws IOException {
128 if(EventQueue.isDispatchThread()) {
129 throw new UnsupportedOperationException("Cannot call SwingTerminal.readInput() on the AWT thread");
130 }
131 return super.readInput();
132 }
133}