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
.event
.TKeypressEvent
;
37 import jexer
.event
.TMouseEvent
;
38 import static jexer
.TKeypress
.kbDown
;
39 import static jexer
.TKeypress
.kbEnd
;
40 import static jexer
.TKeypress
.kbHome
;
41 import static jexer
.TKeypress
.kbLeft
;
42 import static jexer
.TKeypress
.kbPgDn
;
43 import static jexer
.TKeypress
.kbPgUp
;
44 import static jexer
.TKeypress
.kbRight
;
45 import static jexer
.TKeypress
.kbUp
;
48 * TText implements a simple scrollable text area. It reflows automatically on
51 public class TText
extends TScrollableWidget
{
53 // ------------------------------------------------------------------------
54 // Constants --------------------------------------------------------------
55 // ------------------------------------------------------------------------
58 * Available text justifications.
60 public enum Justification
{
63 * Not justified at all, use spacing as provided by the client.
68 * Left-justified text.
78 * Right-justified text.
83 * Fully-justified text.
88 // ------------------------------------------------------------------------
89 // Variables --------------------------------------------------------------
90 // ------------------------------------------------------------------------
93 * How to justify the text.
95 private Justification justification
= Justification
.LEFT
;
103 * Text converted to lines.
105 private List
<String
> lines
;
110 private String colorKey
;
113 * Maximum width of a single line.
115 private int maxLineWidth
;
118 * Number of lines between each paragraph.
120 private int lineSpacing
= 1;
122 // ------------------------------------------------------------------------
123 // Constructors -----------------------------------------------------------
124 // ------------------------------------------------------------------------
127 * Public constructor.
129 * @param parent parent widget
130 * @param text text on the screen
131 * @param x column relative to parent
132 * @param y row relative to parent
133 * @param width width of text area
134 * @param height height of text area
136 public TText(final TWidget parent
, final String text
, final int x
,
137 final int y
, final int width
, final int height
) {
139 this(parent
, text
, x
, y
, width
, height
, "ttext");
143 * Public constructor.
145 * @param parent parent widget
146 * @param text text on the screen
147 * @param x column relative to parent
148 * @param y row relative to parent
149 * @param width width of text area
150 * @param height height of text area
151 * @param colorKey ColorTheme key color to use for foreground
152 * text. Default is "ttext".
154 public TText(final TWidget parent
, final String text
, final int x
,
155 final int y
, final int width
, final int height
,
156 final String colorKey
) {
158 // Set parent and window
159 super(parent
, x
, y
, width
, height
);
162 this.colorKey
= colorKey
;
164 lines
= new LinkedList
<String
>();
166 vScroller
= new TVScroller(this, getWidth() - 1, 0, getHeight() - 1);
167 hScroller
= new THScroller(this, 0, getHeight() - 1, getWidth() - 1);
171 // ------------------------------------------------------------------------
172 // TScrollableWidget ------------------------------------------------------
173 // ------------------------------------------------------------------------
181 CellAttributes color
= getTheme().getColor(colorKey
);
183 int begin
= vScroller
.getValue();
185 for (int i
= begin
; i
< lines
.size(); i
++) {
186 String line
= lines
.get(i
);
187 if (hScroller
.getValue() < line
.length()) {
188 line
= line
.substring(hScroller
.getValue());
192 String formatString
= "%-" + Integer
.toString(getWidth() - 1) + "s";
193 putStringXY(0, topY
, String
.format(formatString
, line
), color
);
196 if (topY
>= (getHeight() - 1)) {
201 // Pad the rest with blank lines
202 for (int i
= topY
; i
< (getHeight() - 1); i
++) {
203 hLineXY(0, i
, getWidth() - 1, ' ', color
);
209 * Handle mouse press events.
211 * @param mouse mouse button press event
214 public void onMouseDown(final TMouseEvent mouse
) {
215 if (mouse
.isMouseWheelUp()) {
216 vScroller
.decrement();
219 if (mouse
.isMouseWheelDown()) {
220 vScroller
.increment();
225 super.onMouseDown(mouse
);
231 * @param keypress keystroke event
234 public void onKeypress(final TKeypressEvent keypress
) {
235 if (keypress
.equals(kbLeft
)) {
236 hScroller
.decrement();
237 } else if (keypress
.equals(kbRight
)) {
238 hScroller
.increment();
239 } else if (keypress
.equals(kbUp
)) {
240 vScroller
.decrement();
241 } else if (keypress
.equals(kbDown
)) {
242 vScroller
.increment();
243 } else if (keypress
.equals(kbPgUp
)) {
244 vScroller
.bigDecrement();
245 } else if (keypress
.equals(kbPgDn
)) {
246 vScroller
.bigIncrement();
247 } else if (keypress
.equals(kbHome
)) {
249 } else if (keypress
.equals(kbEnd
)) {
250 vScroller
.toBottom();
252 // Pass other keys (tab etc.) on
253 super.onKeypress(keypress
);
258 * Resize text and scrollbars for a new width/height.
261 public void reflowData() {
265 // Break up text into paragraphs
266 String
[] paragraphs
= text
.split("\n\n");
267 for (String p
: paragraphs
) {
268 switch (justification
) {
270 lines
.addAll(Arrays
.asList(p
.split("\n")));
273 lines
.addAll(jexer
.bits
.StringUtils
.left(p
,
277 lines
.addAll(jexer
.bits
.StringUtils
.center(p
,
281 lines
.addAll(jexer
.bits
.StringUtils
.right(p
,
285 lines
.addAll(jexer
.bits
.StringUtils
.full(p
,
290 for (int i
= 0; i
< lineSpacing
; i
++) {
297 // ------------------------------------------------------------------------
298 // TText ------------------------------------------------------------------
299 // ------------------------------------------------------------------------
304 * @param text new text to display
306 public void setText(final String text
) {
316 public String
getText() {
321 * Convenience method used by TWindowLoggerOutput.
323 * @param line new line to add
325 public void addLine(final String line
) {
326 if (text
.length() == 0) {
336 * Recompute the bounds for the scrollbars.
338 private void computeBounds() {
340 for (String line
: lines
) {
341 if (line
.length() > maxLineWidth
) {
342 maxLineWidth
= line
.length();
346 vScroller
.setTopValue(0);
347 vScroller
.setBottomValue((lines
.size() - getHeight()) + 1);
348 if (vScroller
.getBottomValue() < 0) {
349 vScroller
.setBottomValue(0);
351 if (vScroller
.getValue() > vScroller
.getBottomValue()) {
352 vScroller
.setValue(vScroller
.getBottomValue());
355 hScroller
.setLeftValue(0);
356 hScroller
.setRightValue((maxLineWidth
- getWidth()) + 1);
357 if (hScroller
.getRightValue() < 0) {
358 hScroller
.setRightValue(0);
360 if (hScroller
.getValue() > hScroller
.getRightValue()) {
361 hScroller
.setValue(hScroller
.getRightValue());
368 * @param justification LEFT, CENTER, RIGHT, or FULL
370 public void setJustification(final Justification justification
) {
371 this.justification
= justification
;
376 * Left-justify the text.
378 public void leftJustify() {
379 justification
= Justification
.LEFT
;
384 * Center-justify the text.
386 public void centerJustify() {
387 justification
= Justification
.CENTER
;
392 * Right-justify the text.
394 public void rightJustify() {
395 justification
= Justification
.RIGHT
;
400 * Fully-justify the text.
402 public void fullJustify() {
403 justification
= Justification
.FULL
;