2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 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 java
.util
.Arrays
;
32 import java
.util
.LinkedList
;
33 import java
.util
.List
;
35 import jexer
.bits
.CellAttributes
;
36 import jexer
.bits
.StringUtils
;
37 import jexer
.event
.TKeypressEvent
;
38 import jexer
.event
.TMouseEvent
;
39 import static jexer
.TKeypress
.kbDown
;
40 import static jexer
.TKeypress
.kbEnd
;
41 import static jexer
.TKeypress
.kbHome
;
42 import static jexer
.TKeypress
.kbLeft
;
43 import static jexer
.TKeypress
.kbPgDn
;
44 import static jexer
.TKeypress
.kbPgUp
;
45 import static jexer
.TKeypress
.kbRight
;
46 import static jexer
.TKeypress
.kbUp
;
49 * TText implements a simple scrollable text area. It reflows automatically on
52 public class TText
extends TScrollableWidget
{
54 // ------------------------------------------------------------------------
55 // Constants --------------------------------------------------------------
56 // ------------------------------------------------------------------------
59 * Available text justifications.
61 public enum Justification
{
64 * Not justified at all, use spacing as provided by the client.
69 * Left-justified text.
79 * Right-justified text.
84 * Fully-justified text.
89 // ------------------------------------------------------------------------
90 // Variables --------------------------------------------------------------
91 // ------------------------------------------------------------------------
94 * How to justify the text.
96 private Justification justification
= Justification
.LEFT
;
104 * Text converted to lines.
106 private List
<String
> lines
;
111 private String colorKey
;
114 * Maximum width of a single line.
116 private int maxLineWidth
;
119 * Number of lines between each paragraph.
121 private int lineSpacing
= 1;
123 // ------------------------------------------------------------------------
124 // Constructors -----------------------------------------------------------
125 // ------------------------------------------------------------------------
128 * Public constructor.
130 * @param parent parent widget
131 * @param text text on the screen
132 * @param x column relative to parent
133 * @param y row relative to parent
134 * @param width width of text area
135 * @param height height of text area
137 public TText(final TWidget parent
, final String text
, final int x
,
138 final int y
, final int width
, final int height
) {
140 this(parent
, text
, x
, y
, width
, height
, "ttext");
144 * Public constructor.
146 * @param parent parent widget
147 * @param text text on the screen
148 * @param x column relative to parent
149 * @param y row relative to parent
150 * @param width width of text area
151 * @param height height of text area
152 * @param colorKey ColorTheme key color to use for foreground
153 * text. Default is "ttext".
155 public TText(final TWidget parent
, final String text
, final int x
,
156 final int y
, final int width
, final int height
,
157 final String colorKey
) {
159 // Set parent and window
160 super(parent
, x
, y
, width
, height
);
163 this.colorKey
= colorKey
;
165 lines
= new LinkedList
<String
>();
167 vScroller
= new TVScroller(this, getWidth() - 1, 0, getHeight() - 1);
168 hScroller
= new THScroller(this, 0, getHeight() - 1, getWidth() - 1);
172 // ------------------------------------------------------------------------
173 // TScrollableWidget ------------------------------------------------------
174 // ------------------------------------------------------------------------
182 CellAttributes color
= getTheme().getColor(colorKey
);
184 int begin
= vScroller
.getValue();
186 for (int i
= begin
; i
< lines
.size(); i
++) {
187 String line
= lines
.get(i
);
188 if (hScroller
.getValue() < StringUtils
.width(line
)) {
189 line
= line
.substring(hScroller
.getValue());
193 String formatString
= "%-" + Integer
.toString(getWidth() - 1) + "s";
194 putStringXY(0, topY
, String
.format(formatString
, line
), color
);
197 if (topY
>= (getHeight() - 1)) {
202 // Pad the rest with blank lines
203 for (int i
= topY
; i
< (getHeight() - 1); i
++) {
204 hLineXY(0, i
, getWidth() - 1, ' ', color
);
210 * Handle mouse press events.
212 * @param mouse mouse button press event
215 public void onMouseDown(final TMouseEvent mouse
) {
216 if (mouse
.isMouseWheelUp()) {
217 vScroller
.decrement();
220 if (mouse
.isMouseWheelDown()) {
221 vScroller
.increment();
226 super.onMouseDown(mouse
);
232 * @param keypress keystroke event
235 public void onKeypress(final TKeypressEvent keypress
) {
236 if (keypress
.equals(kbLeft
)) {
237 hScroller
.decrement();
238 } else if (keypress
.equals(kbRight
)) {
239 hScroller
.increment();
240 } else if (keypress
.equals(kbUp
)) {
241 vScroller
.decrement();
242 } else if (keypress
.equals(kbDown
)) {
243 vScroller
.increment();
244 } else if (keypress
.equals(kbPgUp
)) {
245 vScroller
.bigDecrement();
246 } else if (keypress
.equals(kbPgDn
)) {
247 vScroller
.bigIncrement();
248 } else if (keypress
.equals(kbHome
)) {
250 } else if (keypress
.equals(kbEnd
)) {
251 vScroller
.toBottom();
253 // Pass other keys (tab etc.) on
254 super.onKeypress(keypress
);
259 * Resize text and scrollbars for a new width/height.
262 public void reflowData() {
266 // Break up text into paragraphs
267 String
[] paragraphs
= text
.split("\n\n");
268 for (String p
: paragraphs
) {
269 switch (justification
) {
271 lines
.addAll(Arrays
.asList(p
.split("\n")));
274 lines
.addAll(jexer
.bits
.StringUtils
.left(p
,
278 lines
.addAll(jexer
.bits
.StringUtils
.center(p
,
282 lines
.addAll(jexer
.bits
.StringUtils
.right(p
,
286 lines
.addAll(jexer
.bits
.StringUtils
.full(p
,
291 for (int i
= 0; i
< lineSpacing
; i
++) {
298 // ------------------------------------------------------------------------
299 // TText ------------------------------------------------------------------
300 // ------------------------------------------------------------------------
305 * @param text new text to display
307 public void setText(final String text
) {
317 public String
getText() {
322 * Convenience method used by TWindowLoggerOutput.
324 * @param line new line to add
326 public void addLine(final String line
) {
327 if (StringUtils
.width(text
) == 0) {
337 * Recompute the bounds for the scrollbars.
339 private void computeBounds() {
341 for (String line
: lines
) {
342 if (StringUtils
.width(line
) > maxLineWidth
) {
343 maxLineWidth
= StringUtils
.width(line
);
347 vScroller
.setTopValue(0);
348 vScroller
.setBottomValue((lines
.size() - getHeight()) + 1);
349 if (vScroller
.getBottomValue() < 0) {
350 vScroller
.setBottomValue(0);
352 if (vScroller
.getValue() > vScroller
.getBottomValue()) {
353 vScroller
.setValue(vScroller
.getBottomValue());
356 hScroller
.setLeftValue(0);
357 hScroller
.setRightValue((maxLineWidth
- getWidth()) + 1);
358 if (hScroller
.getRightValue() < 0) {
359 hScroller
.setRightValue(0);
361 if (hScroller
.getValue() > hScroller
.getRightValue()) {
362 hScroller
.setValue(hScroller
.getRightValue());
369 * @param justification LEFT, CENTER, RIGHT, or FULL
371 public void setJustification(final Justification justification
) {
372 this.justification
= justification
;
377 * Left-justify the text.
379 public void leftJustify() {
380 justification
= Justification
.LEFT
;
385 * Center-justify the text.
387 public void centerJustify() {
388 justification
= Justification
.CENTER
;
393 * Right-justify the text.
395 public void rightJustify() {
396 justification
= Justification
.RIGHT
;
401 * Fully-justify the text.
403 public void fullJustify() {
404 justification
= Justification
.FULL
;