1f1359fb01cd2e40afec8165616e0d1e1df32ba2
2 * Jexer - Java Text User Interface
4 * License: LGPLv3 or later
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
10 * Copyright (C) 2015 Kevin Lamonte
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
33 import java
.util
.LinkedList
;
34 import java
.util
.List
;
36 import jexer
.bits
.CellAttributes
;
37 import jexer
.event
.TKeypressEvent
;
38 import jexer
.event
.TMouseEvent
;
39 import static jexer
.TKeypress
.*;
42 * TText implements a simple text windget.
44 public final class TText
extends TWidget
{
52 * Text converted to lines.
54 private List
<String
> lines
;
59 private String colorKey
;
64 private TVScroller vScroller
;
67 * Horizontal scrollbar.
69 private THScroller hScroller
;
72 * Maximum width of a single line.
74 private int maxLineWidth
;
77 * Number of lines between each paragraph.
79 private int lineSpacing
= 1;
82 * Convenience method used by TWindowLoggerOutput.
84 * @param line new line to add
86 public void addLine(final String line
) {
87 if (text
.length() == 0) {
97 * Recompute the bounds for the scrollbars.
99 private void computeBounds() {
101 for (String line
: lines
) {
102 if (line
.length() > maxLineWidth
) {
103 maxLineWidth
= line
.length();
107 vScroller
.setBottomValue(lines
.size() - getHeight() + 1);
108 if (vScroller
.getBottomValue() < 0) {
109 vScroller
.setBottomValue(0);
111 if (vScroller
.getValue() > vScroller
.getBottomValue()) {
112 vScroller
.setValue(vScroller
.getBottomValue());
115 hScroller
.setRightValue(maxLineWidth
- getWidth() + 1);
116 if (hScroller
.getRightValue() < 0) {
117 hScroller
.setRightValue(0);
119 if (hScroller
.getValue() > hScroller
.getRightValue()) {
120 hScroller
.setValue(hScroller
.getRightValue());
125 * Insert newlines into a string to wrap it to a maximum column.
126 * Terminate the final string with a newline. Note that interior
127 * newlines are converted to spaces.
129 * @param str the string
130 * @param n the maximum number of characters in a line
131 * @return the wrapped string
133 private String
wrap(final String str
, final int n
) {
136 StringBuilder sb
= new StringBuilder();
137 StringBuilder word
= new StringBuilder();
139 for (int i
= 0; i
< str
.length(); i
++) {
140 char ch
= str
.charAt(i
);
145 sb
.append(word
.toString());
147 if (word
.length() >= n
- 1) {
151 word
= new StringBuilder();
162 sb
.append(word
.toString());
164 return sb
.toString();
169 * Resize text and scrollbars for a new width/height.
171 public void reflow() {
175 // Break up text into paragraphs
176 String
[] paragraphs
= text
.split("\n\n");
177 for (String p
: paragraphs
) {
178 String paragraph
= wrap(p
, getWidth() - 1);
179 for (String line
: paragraph
.split("\n")) {
182 for (int i
= 0; i
< lineSpacing
; i
++) {
188 if (vScroller
== null) {
189 vScroller
= new TVScroller(this, getWidth() - 1, 0,
191 vScroller
.setTopValue(0);
192 vScroller
.setValue(0);
194 vScroller
.setX(getWidth() - 1);
195 vScroller
.setHeight(getHeight() - 1);
197 vScroller
.setBigChange(getHeight() - 1);
200 if (hScroller
== null) {
201 hScroller
= new THScroller(this, 0, getHeight() - 1,
203 hScroller
.setLeftValue(0);
204 hScroller
.setValue(0);
206 hScroller
.setY(getHeight() - 1);
207 hScroller
.setWidth(getWidth() - 1);
209 hScroller
.setBigChange(getWidth() - 1);
215 * Public constructor.
217 * @param parent parent widget
218 * @param text text on the screen
219 * @param x column relative to parent
220 * @param y row relative to parent
221 * @param width width of text area
222 * @param height height of text area
224 public TText(final TWidget parent
, final String text
, final int x
,
225 final int y
, final int width
, final int height
) {
227 this(parent
, text
, x
, y
, width
, height
, "ttext");
231 * Public constructor.
233 * @param parent parent widget
234 * @param text text on the screen
235 * @param x column relative to parent
236 * @param y row relative to parent
237 * @param width width of text area
238 * @param height height of text area
239 * @param colorKey ColorTheme key color to use for foreground text.
242 public TText(final TWidget parent
, final String text
, final int x
,
243 final int y
, final int width
, final int height
, final String colorKey
) {
245 // Set parent and window
253 this.colorKey
= colorKey
;
255 lines
= new LinkedList
<String
>();
266 CellAttributes color
= getTheme().getColor(colorKey
);
268 int begin
= vScroller
.getValue();
270 for (int i
= begin
; i
< lines
.size(); i
++) {
271 String line
= lines
.get(i
);
272 if (hScroller
.getValue() < line
.length()) {
273 line
= line
.substring(hScroller
.getValue());
277 String formatString
= "%-" + Integer
.toString(getWidth() - 1) + "s";
278 getScreen().putStrXY(0, topY
, String
.format(formatString
, line
),
282 if (topY
>= getHeight() - 1) {
287 // Pad the rest with blank lines
288 for (int i
= topY
; i
< getHeight() - 1; i
++) {
289 getScreen().hLineXY(0, i
, getWidth() - 1, ' ', color
);
295 * Handle mouse press events.
297 * @param mouse mouse button press event
300 public void onMouseDown(final TMouseEvent mouse
) {
301 if (mouse
.getMouseWheelUp()) {
302 vScroller
.decrement();
305 if (mouse
.getMouseWheelDown()) {
306 vScroller
.increment();
311 super.onMouseDown(mouse
);
317 * @param keypress keystroke event
320 public void onKeypress(final TKeypressEvent keypress
) {
321 if (keypress
.equals(kbLeft
)) {
322 hScroller
.decrement();
323 } else if (keypress
.equals(kbRight
)) {
324 hScroller
.increment();
325 } else if (keypress
.equals(kbUp
)) {
326 vScroller
.decrement();
327 } else if (keypress
.equals(kbDown
)) {
328 vScroller
.increment();
329 } else if (keypress
.equals(kbPgUp
)) {
330 vScroller
.bigDecrement();
331 } else if (keypress
.equals(kbPgDn
)) {
332 vScroller
.bigIncrement();
333 } else if (keypress
.equals(kbHome
)) {
335 } else if (keypress
.equals(kbEnd
)) {
336 vScroller
.toBottom();
338 // Pass other keys (tab etc.) on
339 super.onKeypress(keypress
);