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 java
.util
.LinkedList
;
32 import java
.util
.List
;
34 import jexer
.bits
.CellAttributes
;
35 import jexer
.event
.TKeypressEvent
;
36 import jexer
.event
.TMouseEvent
;
37 import static jexer
.TKeypress
.kbDown
;
38 import static jexer
.TKeypress
.kbEnd
;
39 import static jexer
.TKeypress
.kbHome
;
40 import static jexer
.TKeypress
.kbLeft
;
41 import static jexer
.TKeypress
.kbPgDn
;
42 import static jexer
.TKeypress
.kbPgUp
;
43 import static jexer
.TKeypress
.kbRight
;
44 import static jexer
.TKeypress
.kbUp
;
47 * TText implements a simple scrollable text area. It reflows automatically on
50 public class TText
extends TScrollableWidget
{
52 // ------------------------------------------------------------------------
53 // Constants --------------------------------------------------------------
54 // ------------------------------------------------------------------------
57 * Available text justifications.
59 public enum Justification
{
61 * Left-justified text.
71 * Right-justified text.
76 * Fully-justified text.
81 // ------------------------------------------------------------------------
82 // Variables --------------------------------------------------------------
83 // ------------------------------------------------------------------------
86 * How to justify the text.
88 private Justification justification
= Justification
.LEFT
;
96 * Text converted to lines.
98 private List
<String
> lines
;
103 private String colorKey
;
106 * Maximum width of a single line.
108 private int maxLineWidth
;
111 * Number of lines between each paragraph.
113 private int lineSpacing
= 1;
115 // ------------------------------------------------------------------------
116 // Constructors -----------------------------------------------------------
117 // ------------------------------------------------------------------------
120 * Public constructor.
122 * @param parent parent widget
123 * @param text text on the screen
124 * @param x column relative to parent
125 * @param y row relative to parent
126 * @param width width of text area
127 * @param height height of text area
129 public TText(final TWidget parent
, final String text
, final int x
,
130 final int y
, final int width
, final int height
) {
132 this(parent
, text
, x
, y
, width
, height
, "ttext");
136 * Public constructor.
138 * @param parent parent widget
139 * @param text text on the screen
140 * @param x column relative to parent
141 * @param y row relative to parent
142 * @param width width of text area
143 * @param height height of text area
144 * @param colorKey ColorTheme key color to use for foreground
145 * text. Default is "ttext".
147 public TText(final TWidget parent
, final String text
, final int x
,
148 final int y
, final int width
, final int height
,
149 final String colorKey
) {
151 // Set parent and window
152 super(parent
, x
, y
, width
, height
);
155 this.colorKey
= colorKey
;
157 lines
= new LinkedList
<String
>();
159 vScroller
= new TVScroller(this, getWidth() - 1, 0, getHeight() - 1);
160 hScroller
= new THScroller(this, 0, getHeight() - 1, getWidth() - 1);
164 // ------------------------------------------------------------------------
165 // TScrollableWidget ------------------------------------------------------
166 // ------------------------------------------------------------------------
174 CellAttributes color
= getTheme().getColor(colorKey
);
176 int begin
= vScroller
.getValue();
178 for (int i
= begin
; i
< lines
.size(); i
++) {
179 String line
= lines
.get(i
);
180 if (hScroller
.getValue() < line
.length()) {
181 line
= line
.substring(hScroller
.getValue());
185 String formatString
= "%-" + Integer
.toString(getWidth() - 1) + "s";
186 getScreen().putStringXY(0, topY
, String
.format(formatString
, line
),
190 if (topY
>= (getHeight() - 1)) {
195 // Pad the rest with blank lines
196 for (int i
= topY
; i
< (getHeight() - 1); i
++) {
197 getScreen().hLineXY(0, i
, getWidth() - 1, ' ', color
);
203 * Handle mouse press events.
205 * @param mouse mouse button press event
208 public void onMouseDown(final TMouseEvent mouse
) {
209 if (mouse
.isMouseWheelUp()) {
210 vScroller
.decrement();
213 if (mouse
.isMouseWheelDown()) {
214 vScroller
.increment();
219 super.onMouseDown(mouse
);
225 * @param keypress keystroke event
228 public void onKeypress(final TKeypressEvent keypress
) {
229 if (keypress
.equals(kbLeft
)) {
230 hScroller
.decrement();
231 } else if (keypress
.equals(kbRight
)) {
232 hScroller
.increment();
233 } else if (keypress
.equals(kbUp
)) {
234 vScroller
.decrement();
235 } else if (keypress
.equals(kbDown
)) {
236 vScroller
.increment();
237 } else if (keypress
.equals(kbPgUp
)) {
238 vScroller
.bigDecrement();
239 } else if (keypress
.equals(kbPgDn
)) {
240 vScroller
.bigIncrement();
241 } else if (keypress
.equals(kbHome
)) {
243 } else if (keypress
.equals(kbEnd
)) {
244 vScroller
.toBottom();
246 // Pass other keys (tab etc.) on
247 super.onKeypress(keypress
);
252 * Resize text and scrollbars for a new width/height.
255 public void reflowData() {
259 // Break up text into paragraphs
260 String
[] paragraphs
= text
.split("\n\n");
261 for (String p
: paragraphs
) {
262 switch (justification
) {
264 lines
.addAll(jexer
.bits
.StringUtils
.left(p
,
268 lines
.addAll(jexer
.bits
.StringUtils
.center(p
,
272 lines
.addAll(jexer
.bits
.StringUtils
.right(p
,
276 lines
.addAll(jexer
.bits
.StringUtils
.full(p
,
281 for (int i
= 0; i
< lineSpacing
; i
++) {
288 // ------------------------------------------------------------------------
289 // TText ------------------------------------------------------------------
290 // ------------------------------------------------------------------------
295 * @param text new text to display
297 public void setText(final String text
) {
307 public String
getText() {
312 * Convenience method used by TWindowLoggerOutput.
314 * @param line new line to add
316 public void addLine(final String line
) {
317 if (text
.length() == 0) {
327 * Recompute the bounds for the scrollbars.
329 private void computeBounds() {
331 for (String line
: lines
) {
332 if (line
.length() > maxLineWidth
) {
333 maxLineWidth
= line
.length();
337 vScroller
.setTopValue(0);
338 vScroller
.setBottomValue((lines
.size() - getHeight()) + 1);
339 if (vScroller
.getBottomValue() < 0) {
340 vScroller
.setBottomValue(0);
342 if (vScroller
.getValue() > vScroller
.getBottomValue()) {
343 vScroller
.setValue(vScroller
.getBottomValue());
346 hScroller
.setLeftValue(0);
347 hScroller
.setRightValue((maxLineWidth
- getWidth()) + 1);
348 if (hScroller
.getRightValue() < 0) {
349 hScroller
.setRightValue(0);
351 if (hScroller
.getValue() > hScroller
.getRightValue()) {
352 hScroller
.setValue(hScroller
.getRightValue());
359 * @param justification LEFT, CENTER, RIGHT, or FULL
361 public void setJustification(final Justification justification
) {
362 this.justification
= justification
;
367 * Left-justify the text.
369 public void leftJustify() {
370 justification
= Justification
.LEFT
;
375 * Center-justify the text.
377 public void centerJustify() {
378 justification
= Justification
.CENTER
;
383 * Right-justify the text.
385 public void rightJustify() {
386 justification
= Justification
.RIGHT
;
391 * Fully-justify the text.
393 public void fullJustify() {
394 justification
= Justification
.FULL
;