2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2017 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
{
54 * Available text justifications.
56 public enum Justification
{
58 * Left-justified text.
68 * Right-justified text.
73 * Fully-justified text.
79 * How to justify the text.
81 private Justification justification
= Justification
.LEFT
;
89 * Text converted to lines.
91 private List
<String
> lines
;
96 private String colorKey
;
101 private TVScroller vScroller
;
104 * Horizontal scrollbar.
106 private THScroller hScroller
;
109 * Maximum width of a single line.
111 private int maxLineWidth
;
114 * Number of lines between each paragraph.
116 private int lineSpacing
= 1;
119 * Convenience method used by TWindowLoggerOutput.
121 * @param line new line to add
123 public void addLine(final String line
) {
124 if (text
.length() == 0) {
134 * Recompute the bounds for the scrollbars.
136 private void computeBounds() {
138 for (String line
: lines
) {
139 if (line
.length() > maxLineWidth
) {
140 maxLineWidth
= line
.length();
144 vScroller
.setBottomValue((lines
.size() - getHeight()) + 1);
145 if (vScroller
.getBottomValue() < 0) {
146 vScroller
.setBottomValue(0);
148 if (vScroller
.getValue() > vScroller
.getBottomValue()) {
149 vScroller
.setValue(vScroller
.getBottomValue());
152 hScroller
.setRightValue((maxLineWidth
- getWidth()) + 1);
153 if (hScroller
.getRightValue() < 0) {
154 hScroller
.setRightValue(0);
156 if (hScroller
.getValue() > hScroller
.getRightValue()) {
157 hScroller
.setValue(hScroller
.getRightValue());
164 * @param justification LEFT, CENTER, RIGHT, or FULL
166 public void setJustification(final Justification justification
) {
167 this.justification
= justification
;
172 * Left-justify the text.
174 public void leftJustify() {
175 justification
= Justification
.LEFT
;
180 * Center-justify the text.
182 public void centerJustify() {
183 justification
= Justification
.CENTER
;
188 * Right-justify the text.
190 public void rightJustify() {
191 justification
= Justification
.RIGHT
;
196 * Fully-justify the text.
198 public void fullJustify() {
199 justification
= Justification
.FULL
;
204 * Resize text and scrollbars for a new width/height.
206 public void reflow() {
210 // Break up text into paragraphs
211 String
[] paragraphs
= text
.split("\n\n");
212 for (String p
: paragraphs
) {
213 switch (justification
) {
215 lines
.addAll(jexer
.bits
.StringJustifier
.left(p
,
219 lines
.addAll(jexer
.bits
.StringJustifier
.center(p
,
223 lines
.addAll(jexer
.bits
.StringJustifier
.right(p
,
227 lines
.addAll(jexer
.bits
.StringJustifier
.full(p
,
232 for (int i
= 0; i
< lineSpacing
; i
++) {
238 if (vScroller
== null) {
239 vScroller
= new TVScroller(this, getWidth() - 1, 0, getHeight() - 1);
240 vScroller
.setTopValue(0);
241 vScroller
.setValue(0);
243 vScroller
.setX(getWidth() - 1);
244 vScroller
.setHeight(getHeight() - 1);
246 vScroller
.setBigChange(getHeight() - 1);
249 if (hScroller
== null) {
250 hScroller
= new THScroller(this, 0, getHeight() - 1, getWidth() - 1);
251 hScroller
.setLeftValue(0);
252 hScroller
.setValue(0);
254 hScroller
.setY(getHeight() - 1);
255 hScroller
.setWidth(getWidth() - 1);
257 hScroller
.setBigChange(getWidth() - 1);
263 * Public constructor.
265 * @param parent parent widget
266 * @param text text on the screen
267 * @param x column relative to parent
268 * @param y row relative to parent
269 * @param width width of text area
270 * @param height height of text area
272 public TText(final TWidget parent
, final String text
, final int x
,
273 final int y
, final int width
, final int height
) {
275 this(parent
, text
, x
, y
, width
, height
, "ttext");
279 * Public constructor.
281 * @param parent parent widget
282 * @param text text on the screen
283 * @param x column relative to parent
284 * @param y row relative to parent
285 * @param width width of text area
286 * @param height height of text area
287 * @param colorKey ColorTheme key color to use for foreground
288 * text. Default is "ttext".
290 public TText(final TWidget parent
, final String text
, final int x
,
291 final int y
, final int width
, final int height
,
292 final String colorKey
) {
294 // Set parent and window
295 super(parent
, x
, y
, width
, height
);
298 this.colorKey
= colorKey
;
300 lines
= new LinkedList
<String
>();
311 CellAttributes color
= getTheme().getColor(colorKey
);
313 int begin
= vScroller
.getValue();
315 for (int i
= begin
; i
< lines
.size(); i
++) {
316 String line
= lines
.get(i
);
317 if (hScroller
.getValue() < line
.length()) {
318 line
= line
.substring(hScroller
.getValue());
322 String formatString
= "%-" + Integer
.toString(getWidth() - 1) + "s";
323 getScreen().putStringXY(0, topY
, String
.format(formatString
, line
),
327 if (topY
>= (getHeight() - 1)) {
332 // Pad the rest with blank lines
333 for (int i
= topY
; i
< (getHeight() - 1); i
++) {
334 getScreen().hLineXY(0, i
, getWidth() - 1, ' ', color
);
340 * Handle mouse press events.
342 * @param mouse mouse button press event
345 public void onMouseDown(final TMouseEvent mouse
) {
346 if (mouse
.isMouseWheelUp()) {
347 vScroller
.decrement();
350 if (mouse
.isMouseWheelDown()) {
351 vScroller
.increment();
356 super.onMouseDown(mouse
);
362 * @param keypress keystroke event
365 public void onKeypress(final TKeypressEvent keypress
) {
366 if (keypress
.equals(kbLeft
)) {
367 hScroller
.decrement();
368 } else if (keypress
.equals(kbRight
)) {
369 hScroller
.increment();
370 } else if (keypress
.equals(kbUp
)) {
371 vScroller
.decrement();
372 } else if (keypress
.equals(kbDown
)) {
373 vScroller
.increment();
374 } else if (keypress
.equals(kbPgUp
)) {
375 vScroller
.bigDecrement();
376 } else if (keypress
.equals(kbPgDn
)) {
377 vScroller
.bigIncrement();
378 } else if (keypress
.equals(kbHome
)) {
380 } else if (keypress
.equals(kbEnd
)) {
381 vScroller
.toBottom();
383 // Pass other keys (tab etc.) on
384 super.onKeypress(keypress
);