2 * This file is part of lanterna (http://code.google.com/p/lanterna/).
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.
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.
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/>.
17 * Copyright (C) 2010-2015 Martin
19 package com
.googlecode
.lanterna
.terminal
.swing
;
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
.terminal
.IOSafeTerminal
;
25 import com
.googlecode
.lanterna
.terminal
.ResizeListener
;
26 import com
.googlecode
.lanterna
.TerminalSize
;
27 import com
.googlecode
.lanterna
.TextColor
;
28 import java
.awt
.BorderLayout
;
29 import java
.awt
.Container
;
30 import java
.awt
.Scrollbar
;
31 import java
.awt
.event
.AdjustmentEvent
;
32 import java
.awt
.event
.AdjustmentListener
;
33 import java
.io
.IOException
;
34 import java
.util
.concurrent
.TimeUnit
;
37 * This is a AWT Container that carries an {@link AWTTerminal} with a scrollbar, effectively implementing a
38 * pseudo-terminal with scrollback history. You can choose the same parameters are for {@link AWTTerminal}, they are
39 * forwarded, this class mostly deals with linking the {@link AWTTerminal} with the scrollbar and having them update
43 @SuppressWarnings("serial")
44 public class ScrollingAWTTerminal
extends Container
implements IOSafeTerminal
{
46 private final AWTTerminal awtTerminal
;
47 private final Scrollbar scrollBar
;
50 * Creates a new {@code ScrollingAWTTerminal} with all default options
52 public ScrollingAWTTerminal() {
53 this(TerminalEmulatorDeviceConfiguration
.getDefault(),
54 SwingTerminalFontConfiguration
.getDefault(),
55 TerminalEmulatorColorConfiguration
.getDefault());
59 * Creates a new {@code ScrollingAWTTerminal} with customizable settings.
60 * @param deviceConfiguration How to configure the terminal virtual device
61 * @param fontConfiguration What kind of fonts to use
62 * @param colorConfiguration Which color schema to use for ANSI colors
64 @SuppressWarnings({"SameParameterValue", "WeakerAccess"})
65 public ScrollingAWTTerminal(
66 TerminalEmulatorDeviceConfiguration deviceConfiguration
,
67 SwingTerminalFontConfiguration fontConfiguration
,
68 TerminalEmulatorColorConfiguration colorConfiguration
) {
70 this.scrollBar
= new Scrollbar(Scrollbar
.VERTICAL
);
71 this.awtTerminal
= new AWTTerminal(
75 new ScrollController());
77 setLayout(new BorderLayout());
78 add(awtTerminal
, BorderLayout
.CENTER
);
79 add(scrollBar
, BorderLayout
.EAST
);
80 this.scrollBar
.setMinimum(0);
81 this.scrollBar
.setMaximum(20);
82 this.scrollBar
.setValue(0);
83 this.scrollBar
.setVisibleAmount(20);
84 this.scrollBar
.addAdjustmentListener(new ScrollbarListener());
87 private class ScrollController
implements TerminalScrollController
{
89 public void updateModel(int totalSize
, int screenSize
) {
90 if(scrollBar
.getMaximum() != totalSize
) {
91 int lastMaximum
= scrollBar
.getMaximum();
92 scrollBar
.setMaximum(totalSize
);
93 if(lastMaximum
< totalSize
&&
94 lastMaximum
- scrollBar
.getVisibleAmount() - scrollBar
.getValue() == 0) {
95 int adjustedValue
= scrollBar
.getValue() + (totalSize
- lastMaximum
);
96 scrollBar
.setValue(adjustedValue
);
99 if(scrollBar
.getVisibleAmount() != screenSize
) {
100 if(scrollBar
.getValue() + screenSize
> scrollBar
.getMaximum()) {
101 scrollBar
.setValue(scrollBar
.getMaximum() - screenSize
);
103 scrollBar
.setVisibleAmount(screenSize
);
108 public int getScrollingOffset() {
109 return scrollBar
.getMaximum() - scrollBar
.getVisibleAmount() - scrollBar
.getValue();
113 private class ScrollbarListener
implements AdjustmentListener
{
115 public synchronized void adjustmentValueChanged(AdjustmentEvent e
) {
116 awtTerminal
.repaint();
121 // Delegate all Terminal interface implementations to SwingTerminal
124 public KeyStroke
pollInput() {
125 return awtTerminal
.pollInput();
129 public KeyStroke
readInput() throws IOException
{
130 return awtTerminal
.readInput();
134 public void enterPrivateMode() {
135 awtTerminal
.enterPrivateMode();
139 public void exitPrivateMode() {
140 awtTerminal
.exitPrivateMode();
144 public void clearScreen() {
145 awtTerminal
.clearScreen();
149 public void setCursorPosition(int x
, int y
) {
150 awtTerminal
.setCursorPosition(x
, y
);
154 public void setCursorVisible(boolean visible
) {
155 awtTerminal
.setCursorVisible(visible
);
159 public void putCharacter(char c
) {
160 awtTerminal
.putCharacter(c
);
164 public TextGraphics
newTextGraphics() throws IOException
{
165 return awtTerminal
.newTextGraphics();
169 public void enableSGR(SGR sgr
) {
170 awtTerminal
.enableSGR(sgr
);
174 public void disableSGR(SGR sgr
) {
175 awtTerminal
.disableSGR(sgr
);
179 public void resetColorAndSGR() {
180 awtTerminal
.resetColorAndSGR();
184 public void setForegroundColor(TextColor color
) {
185 awtTerminal
.setForegroundColor(color
);
189 public void setBackgroundColor(TextColor color
) {
190 awtTerminal
.setBackgroundColor(color
);
194 public TerminalSize
getTerminalSize() {
195 return awtTerminal
.getTerminalSize();
199 public byte[] enquireTerminal(int timeout
, TimeUnit timeoutUnit
) {
200 return awtTerminal
.enquireTerminal(timeout
, timeoutUnit
);
204 public void flush() {
209 public void addResizeListener(ResizeListener listener
) {
210 awtTerminal
.addResizeListener(listener
);
214 public void removeResizeListener(ResizeListener listener
) {
215 awtTerminal
.removeResizeListener(listener
);