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 TScrollableWidget
{
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
;
99 * Maximum width of a single line.
101 private int maxLineWidth
;
104 * Number of lines between each paragraph.
106 private int lineSpacing
= 1;
111 * @param text new text to display
113 public void setText(final String text
) {
123 public String
getText() {
128 * Convenience method used by TWindowLoggerOutput.
130 * @param line new line to add
132 public void addLine(final String line
) {
133 if (text
.length() == 0) {
143 * Recompute the bounds for the scrollbars.
145 private void computeBounds() {
147 for (String line
: lines
) {
148 if (line
.length() > maxLineWidth
) {
149 maxLineWidth
= line
.length();
153 vScroller
.setTopValue(0);
154 vScroller
.setBottomValue((lines
.size() - getHeight()) + 1);
155 if (vScroller
.getBottomValue() < 0) {
156 vScroller
.setBottomValue(0);
158 if (vScroller
.getValue() > vScroller
.getBottomValue()) {
159 vScroller
.setValue(vScroller
.getBottomValue());
162 hScroller
.setLeftValue(0);
163 hScroller
.setRightValue((maxLineWidth
- getWidth()) + 1);
164 if (hScroller
.getRightValue() < 0) {
165 hScroller
.setRightValue(0);
167 if (hScroller
.getValue() > hScroller
.getRightValue()) {
168 hScroller
.setValue(hScroller
.getRightValue());
175 * @param justification LEFT, CENTER, RIGHT, or FULL
177 public void setJustification(final Justification justification
) {
178 this.justification
= justification
;
183 * Left-justify the text.
185 public void leftJustify() {
186 justification
= Justification
.LEFT
;
191 * Center-justify the text.
193 public void centerJustify() {
194 justification
= Justification
.CENTER
;
199 * Right-justify the text.
201 public void rightJustify() {
202 justification
= Justification
.RIGHT
;
207 * Fully-justify the text.
209 public void fullJustify() {
210 justification
= Justification
.FULL
;
215 * Resize text and scrollbars for a new width/height.
218 public void reflowData() {
222 // Break up text into paragraphs
223 String
[] paragraphs
= text
.split("\n\n");
224 for (String p
: paragraphs
) {
225 switch (justification
) {
227 lines
.addAll(jexer
.bits
.StringJustifier
.left(p
,
231 lines
.addAll(jexer
.bits
.StringJustifier
.center(p
,
235 lines
.addAll(jexer
.bits
.StringJustifier
.right(p
,
239 lines
.addAll(jexer
.bits
.StringJustifier
.full(p
,
244 for (int i
= 0; i
< lineSpacing
; i
++) {
252 * Public constructor.
254 * @param parent parent widget
255 * @param text text on the screen
256 * @param x column relative to parent
257 * @param y row relative to parent
258 * @param width width of text area
259 * @param height height of text area
261 public TText(final TWidget parent
, final String text
, final int x
,
262 final int y
, final int width
, final int height
) {
264 this(parent
, text
, x
, y
, width
, height
, "ttext");
268 * Public constructor.
270 * @param parent parent widget
271 * @param text text on the screen
272 * @param x column relative to parent
273 * @param y row relative to parent
274 * @param width width of text area
275 * @param height height of text area
276 * @param colorKey ColorTheme key color to use for foreground
277 * text. Default is "ttext".
279 public TText(final TWidget parent
, final String text
, final int x
,
280 final int y
, final int width
, final int height
,
281 final String colorKey
) {
283 // Set parent and window
284 super(parent
, x
, y
, width
, height
);
287 this.colorKey
= colorKey
;
289 lines
= new LinkedList
<String
>();
291 vScroller
= new TVScroller(this, getWidth() - 1, 0, getHeight() - 1);
292 hScroller
= new THScroller(this, 0, getHeight() - 1, getWidth() - 1);
302 CellAttributes color
= getTheme().getColor(colorKey
);
304 int begin
= vScroller
.getValue();
306 for (int i
= begin
; i
< lines
.size(); i
++) {
307 String line
= lines
.get(i
);
308 if (hScroller
.getValue() < line
.length()) {
309 line
= line
.substring(hScroller
.getValue());
313 String formatString
= "%-" + Integer
.toString(getWidth() - 1) + "s";
314 getScreen().putStringXY(0, topY
, String
.format(formatString
, line
),
318 if (topY
>= (getHeight() - 1)) {
323 // Pad the rest with blank lines
324 for (int i
= topY
; i
< (getHeight() - 1); i
++) {
325 getScreen().hLineXY(0, i
, getWidth() - 1, ' ', color
);
331 * Handle mouse press events.
333 * @param mouse mouse button press event
336 public void onMouseDown(final TMouseEvent mouse
) {
337 if (mouse
.isMouseWheelUp()) {
338 vScroller
.decrement();
341 if (mouse
.isMouseWheelDown()) {
342 vScroller
.increment();
347 super.onMouseDown(mouse
);
353 * @param keypress keystroke event
356 public void onKeypress(final TKeypressEvent keypress
) {
357 if (keypress
.equals(kbLeft
)) {
358 hScroller
.decrement();
359 } else if (keypress
.equals(kbRight
)) {
360 hScroller
.increment();
361 } else if (keypress
.equals(kbUp
)) {
362 vScroller
.decrement();
363 } else if (keypress
.equals(kbDown
)) {
364 vScroller
.increment();
365 } else if (keypress
.equals(kbPgUp
)) {
366 vScroller
.bigDecrement();
367 } else if (keypress
.equals(kbPgDn
)) {
368 vScroller
.bigIncrement();
369 } else if (keypress
.equals(kbHome
)) {
371 } else if (keypress
.equals(kbEnd
)) {
372 vScroller
.toBottom();
374 // Pass other keys (tab etc.) on
375 super.onKeypress(keypress
);