Commit | Line | Data |
---|---|---|
a3b510ab NR |
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.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.event.AdjustmentEvent; | |
30 | import java.awt.event.AdjustmentListener; | |
31 | import java.io.IOException; | |
32 | import java.util.concurrent.TimeUnit; | |
33 | import javax.swing.JComponent; | |
34 | import javax.swing.JScrollBar; | |
35 | ||
36 | /** | |
37 | * This is a Swing JComponent that carries a {@link SwingTerminal} with a scrollbar, effectively implementing a | |
38 | * pseudo-terminal with scrollback history. You can choose the same parameters are for {@link SwingTerminal}, they are | |
39 | * forwarded, this class mostly deals with linking the {@link SwingTerminal} with the scrollbar and having them update | |
40 | * each other. | |
41 | * @author Martin | |
42 | */ | |
43 | @SuppressWarnings("serial") | |
44 | public class ScrollingSwingTerminal extends JComponent implements IOSafeTerminal { | |
45 | ||
46 | private final SwingTerminal swingTerminal; | |
47 | private final JScrollBar scrollBar; | |
48 | ||
49 | /** | |
50 | * Creates a new {@code ScrollingSwingTerminal} with all default options | |
51 | */ | |
52 | public ScrollingSwingTerminal() { | |
53 | this(TerminalEmulatorDeviceConfiguration.getDefault(), | |
54 | SwingTerminalFontConfiguration.getDefault(), | |
55 | TerminalEmulatorColorConfiguration.getDefault()); | |
56 | } | |
57 | ||
58 | /** | |
59 | * Creates a new {@code ScrollingSwingTerminal} 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 | |
63 | */ | |
64 | @SuppressWarnings({"SameParameterValue", "WeakerAccess"}) | |
65 | public ScrollingSwingTerminal( | |
66 | TerminalEmulatorDeviceConfiguration deviceConfiguration, | |
67 | SwingTerminalFontConfiguration fontConfiguration, | |
68 | TerminalEmulatorColorConfiguration colorConfiguration) { | |
69 | ||
70 | this.scrollBar = new JScrollBar(JScrollBar.VERTICAL); | |
71 | this.swingTerminal = new SwingTerminal( | |
72 | deviceConfiguration, | |
73 | fontConfiguration, | |
74 | colorConfiguration, | |
75 | new ScrollController()); | |
76 | ||
77 | setLayout(new BorderLayout()); | |
78 | add(swingTerminal, 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()); | |
85 | } | |
86 | ||
87 | private class ScrollController implements TerminalScrollController { | |
88 | @Override | |
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); | |
97 | } | |
98 | } | |
99 | if(scrollBar.getVisibleAmount() != screenSize) { | |
100 | if(scrollBar.getValue() + screenSize > scrollBar.getMaximum()) { | |
101 | scrollBar.setValue(scrollBar.getMaximum() - screenSize); | |
102 | } | |
103 | scrollBar.setVisibleAmount(screenSize); | |
104 | } | |
105 | } | |
106 | ||
107 | @Override | |
108 | public int getScrollingOffset() { | |
109 | return scrollBar.getMaximum() - scrollBar.getVisibleAmount() - scrollBar.getValue(); | |
110 | } | |
111 | } | |
112 | ||
113 | private class ScrollbarListener implements AdjustmentListener { | |
114 | @Override | |
115 | public synchronized void adjustmentValueChanged(AdjustmentEvent e) { | |
116 | swingTerminal.repaint(); | |
117 | } | |
118 | } | |
119 | ||
120 | /////////// | |
121 | // Delegate all Terminal interface implementations to SwingTerminal | |
122 | /////////// | |
123 | @Override | |
124 | public KeyStroke pollInput() { | |
125 | return swingTerminal.pollInput(); | |
126 | } | |
127 | ||
128 | @Override | |
129 | public KeyStroke readInput() throws IOException { | |
130 | return swingTerminal.readInput(); | |
131 | } | |
132 | ||
133 | @Override | |
134 | public void enterPrivateMode() { | |
135 | swingTerminal.enterPrivateMode(); | |
136 | } | |
137 | ||
138 | @Override | |
139 | public void exitPrivateMode() { | |
140 | swingTerminal.exitPrivateMode(); | |
141 | } | |
142 | ||
143 | @Override | |
144 | public void clearScreen() { | |
145 | swingTerminal.clearScreen(); | |
146 | } | |
147 | ||
148 | @Override | |
149 | public void setCursorPosition(int x, int y) { | |
150 | swingTerminal.setCursorPosition(x, y); | |
151 | } | |
152 | ||
153 | @Override | |
154 | public void setCursorVisible(boolean visible) { | |
155 | swingTerminal.setCursorVisible(visible); | |
156 | } | |
157 | ||
158 | @Override | |
159 | public void putCharacter(char c) { | |
160 | swingTerminal.putCharacter(c); | |
161 | } | |
162 | ||
163 | @Override | |
164 | public TextGraphics newTextGraphics() throws IOException { | |
165 | return swingTerminal.newTextGraphics(); | |
166 | } | |
167 | ||
168 | @Override | |
169 | public void enableSGR(SGR sgr) { | |
170 | swingTerminal.enableSGR(sgr); | |
171 | } | |
172 | ||
173 | @Override | |
174 | public void disableSGR(SGR sgr) { | |
175 | swingTerminal.disableSGR(sgr); | |
176 | } | |
177 | ||
178 | @Override | |
179 | public void resetColorAndSGR() { | |
180 | swingTerminal.resetColorAndSGR(); | |
181 | } | |
182 | ||
183 | @Override | |
184 | public void setForegroundColor(TextColor color) { | |
185 | swingTerminal.setForegroundColor(color); | |
186 | } | |
187 | ||
188 | @Override | |
189 | public void setBackgroundColor(TextColor color) { | |
190 | swingTerminal.setBackgroundColor(color); | |
191 | } | |
192 | ||
193 | @Override | |
194 | public TerminalSize getTerminalSize() { | |
195 | return swingTerminal.getTerminalSize(); | |
196 | } | |
197 | ||
198 | @Override | |
199 | public byte[] enquireTerminal(int timeout, TimeUnit timeoutUnit) { | |
200 | return swingTerminal.enquireTerminal(timeout, timeoutUnit); | |
201 | } | |
202 | ||
203 | @Override | |
204 | public void flush() { | |
205 | swingTerminal.flush(); | |
206 | } | |
207 | ||
208 | @Override | |
209 | public void addResizeListener(ResizeListener listener) { | |
210 | swingTerminal.addResizeListener(listener); | |
211 | } | |
212 | ||
213 | @Override | |
214 | public void removeResizeListener(ResizeListener listener) { | |
215 | swingTerminal.removeResizeListener(listener); | |
216 | } | |
217 | } |