007ff2d5a7d95fbe0ee1ab95cedd4a8422dcc8a3
2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2016 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
31 import static jexer
.TKeypress
.kbDown
;
32 import static jexer
.TKeypress
.kbEnd
;
33 import static jexer
.TKeypress
.kbHome
;
34 import static jexer
.TKeypress
.kbLeft
;
35 import static jexer
.TKeypress
.kbPgDn
;
36 import static jexer
.TKeypress
.kbPgUp
;
37 import static jexer
.TKeypress
.kbRight
;
38 import static jexer
.TKeypress
.kbUp
;
40 import java
.util
.LinkedList
;
41 import java
.util
.List
;
43 import jexer
.bits
.CellAttributes
;
44 import jexer
.event
.TKeypressEvent
;
45 import jexer
.event
.TMouseEvent
;
48 * TText implements a simple scrollable text area. It reflows automatically on
51 public final class TText
extends TWidget
{
59 * Text converted to lines.
61 private List
<String
> lines
;
66 private String colorKey
;
71 private TVScroller vScroller
;
74 * Horizontal scrollbar.
76 private THScroller hScroller
;
79 * Maximum width of a single line.
81 private int maxLineWidth
;
84 * Number of lines between each paragraph.
86 private int lineSpacing
= 1;
89 * Convenience method used by TWindowLoggerOutput.
91 * @param line new line to add
93 public void addLine(final String line
) {
94 if (text
.length() == 0) {
104 * Recompute the bounds for the scrollbars.
106 private void computeBounds() {
108 for (String line
: lines
) {
109 if (line
.length() > maxLineWidth
) {
110 maxLineWidth
= line
.length();
114 vScroller
.setBottomValue((lines
.size() - getHeight()) + 1);
115 if (vScroller
.getBottomValue() < 0) {
116 vScroller
.setBottomValue(0);
118 if (vScroller
.getValue() > vScroller
.getBottomValue()) {
119 vScroller
.setValue(vScroller
.getBottomValue());
122 hScroller
.setRightValue((maxLineWidth
- getWidth()) + 1);
123 if (hScroller
.getRightValue() < 0) {
124 hScroller
.setRightValue(0);
126 if (hScroller
.getValue() > hScroller
.getRightValue()) {
127 hScroller
.setValue(hScroller
.getRightValue());
132 * Insert newlines into a string to wrap it to a maximum column. Terminate
133 * the final string with a newline. Note that interior newlines are
134 * converted to spaces.
136 * @param str the string
137 * @param n the maximum number of characters in a line
138 * @return the wrapped string
140 private String
wrap(final String str
, final int n
) {
143 StringBuilder sb
= new StringBuilder();
144 StringBuilder word
= new StringBuilder();
146 for (int i
= 0; i
< str
.length(); i
++) {
147 char ch
= str
.charAt(i
);
152 sb
.append(word
.toString());
154 if (word
.length() >= (n
- 1)) {
158 word
= new StringBuilder();
164 if (col
>= (n
- 1)) {
169 sb
.append(word
.toString());
171 return sb
.toString();
175 * Resize text and scrollbars for a new width/height.
177 public void reflow() {
181 // Break up text into paragraphs
182 String
[] paragraphs
= text
.split("\n\n");
183 for (String p
: paragraphs
) {
184 String paragraph
= wrap(p
, getWidth() - 1);
185 for (String line
: paragraph
.split("\n")) {
188 for (int i
= 0; i
< lineSpacing
; i
++) {
194 if (vScroller
== null) {
195 vScroller
= new TVScroller(this, getWidth() - 1, 0, getHeight() - 1);
196 vScroller
.setTopValue(0);
197 vScroller
.setValue(0);
199 vScroller
.setX(getWidth() - 1);
200 vScroller
.setHeight(getHeight() - 1);
202 vScroller
.setBigChange(getHeight() - 1);
205 if (hScroller
== null) {
206 hScroller
= new THScroller(this, 0, getHeight() - 1, getWidth() - 1);
207 hScroller
.setLeftValue(0);
208 hScroller
.setValue(0);
210 hScroller
.setY(getHeight() - 1);
211 hScroller
.setWidth(getWidth() - 1);
213 hScroller
.setBigChange(getWidth() - 1);
219 * Public constructor.
221 * @param parent parent widget
222 * @param text text on the screen
223 * @param x column relative to parent
224 * @param y row relative to parent
225 * @param width width of text area
226 * @param height height of text area
228 public TText(final TWidget parent
, final String text
, final int x
,
229 final int y
, final int width
, final int height
) {
231 this(parent
, text
, x
, y
, width
, height
, "ttext");
235 * Public constructor.
237 * @param parent parent widget
238 * @param text text on the screen
239 * @param x column relative to parent
240 * @param y row relative to parent
241 * @param width width of text area
242 * @param height height of text area
243 * @param colorKey ColorTheme key color to use for foreground
244 * text. Default is "ttext".
246 public TText(final TWidget parent
, final String text
, final int x
,
247 final int y
, final int width
, final int height
,
248 final String colorKey
) {
250 // Set parent and window
251 super(parent
, x
, y
, width
, height
);
254 this.colorKey
= colorKey
;
256 lines
= new LinkedList
<String
>();
267 CellAttributes color
= getTheme().getColor(colorKey
);
269 int begin
= vScroller
.getValue();
271 for (int i
= begin
; i
< lines
.size(); i
++) {
272 String line
= lines
.get(i
);
273 if (hScroller
.getValue() < line
.length()) {
274 line
= line
.substring(hScroller
.getValue());
278 String formatString
= "%-" + Integer
.toString(getWidth() - 1) + "s";
279 getScreen().putStringXY(0, topY
, String
.format(formatString
, line
),
283 if (topY
>= (getHeight() - 1)) {
288 // Pad the rest with blank lines
289 for (int i
= topY
; i
< (getHeight() - 1); i
++) {
290 getScreen().hLineXY(0, i
, getWidth() - 1, ' ', color
);
296 * Handle mouse press events.
298 * @param mouse mouse button press event
301 public void onMouseDown(final TMouseEvent mouse
) {
302 if (mouse
.isMouseWheelUp()) {
303 vScroller
.decrement();
306 if (mouse
.isMouseWheelDown()) {
307 vScroller
.increment();
312 super.onMouseDown(mouse
);
318 * @param keypress keystroke event
321 public void onKeypress(final TKeypressEvent keypress
) {
322 if (keypress
.equals(kbLeft
)) {
323 hScroller
.decrement();
324 } else if (keypress
.equals(kbRight
)) {
325 hScroller
.increment();
326 } else if (keypress
.equals(kbUp
)) {
327 vScroller
.decrement();
328 } else if (keypress
.equals(kbDown
)) {
329 vScroller
.increment();
330 } else if (keypress
.equals(kbPgUp
)) {
331 vScroller
.bigDecrement();
332 } else if (keypress
.equals(kbPgDn
)) {
333 vScroller
.bigIncrement();
334 } else if (keypress
.equals(kbHome
)) {
336 } else if (keypress
.equals(kbEnd
)) {
337 vScroller
.toBottom();
339 // Pass other keys (tab etc.) on
340 super.onKeypress(keypress
);