X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTText.java;h=0edb290a8a74f7de3e94bd8890961233844a8495;hb=0525b2ed026e0d510fdf23f6d8f4cb4562a17e0b;hp=21c2f1f1445ed826106061bb47e25e14c3251664;hpb=7657ad8c9c48bdde0c7d693859e942bd2186b1f7;p=nikiroo-utils.git diff --git a/src/jexer/TText.java b/src/jexer/TText.java index 21c2f1f..0edb290 100644 --- a/src/jexer/TText.java +++ b/src/jexer/TText.java @@ -28,27 +28,19 @@ */ package jexer; -import static jexer.TKeypress.kbDown; -import static jexer.TKeypress.kbEnd; -import static jexer.TKeypress.kbHome; -import static jexer.TKeypress.kbLeft; -import static jexer.TKeypress.kbPgDn; -import static jexer.TKeypress.kbPgUp; -import static jexer.TKeypress.kbRight; -import static jexer.TKeypress.kbUp; - import java.util.LinkedList; import java.util.List; import jexer.bits.CellAttributes; import jexer.event.TKeypressEvent; import jexer.event.TMouseEvent; +import static jexer.TKeypress.*; /** * TText implements a simple scrollable text area. It reflows automatically on * resize. */ -public final class TText extends TWidget { +public class TText extends TScrollableWidget { /** * Available text justifications. @@ -96,24 +88,33 @@ public final class TText extends TWidget { private String colorKey; /** - * Vertical scrollbar. + * Maximum width of a single line. */ - private TVScroller vScroller; + private int maxLineWidth; /** - * Horizontal scrollbar. + * Number of lines between each paragraph. */ - private THScroller hScroller; + private int lineSpacing = 1; /** - * Maximum width of a single line. + * Set the text. + * + * @param text new text to display */ - private int maxLineWidth; + public void setText(final String text) { + this.text = text; + reflowData(); + } /** - * Number of lines between each paragraph. + * Get the text. + * + * @return the text */ - private int lineSpacing = 1; + public String getText() { + return text; + } /** * Convenience method used by TWindowLoggerOutput. @@ -127,7 +128,7 @@ public final class TText extends TWidget { text += "\n\n"; text += line; } - reflow(); + reflowData(); } /** @@ -141,6 +142,7 @@ public final class TText extends TWidget { } } + vScroller.setTopValue(0); vScroller.setBottomValue((lines.size() - getHeight()) + 1); if (vScroller.getBottomValue() < 0) { vScroller.setBottomValue(0); @@ -149,6 +151,7 @@ public final class TText extends TWidget { vScroller.setValue(vScroller.getBottomValue()); } + hScroller.setLeftValue(0); hScroller.setRightValue((maxLineWidth - getWidth()) + 1); if (hScroller.getRightValue() < 0) { hScroller.setRightValue(0); @@ -165,7 +168,7 @@ public final class TText extends TWidget { */ public void setJustification(final Justification justification) { this.justification = justification; - reflow(); + reflowData(); } /** @@ -173,7 +176,7 @@ public final class TText extends TWidget { */ public void leftJustify() { justification = Justification.LEFT; - reflow(); + reflowData(); } /** @@ -181,7 +184,7 @@ public final class TText extends TWidget { */ public void centerJustify() { justification = Justification.CENTER; - reflow(); + reflowData(); } /** @@ -189,7 +192,7 @@ public final class TText extends TWidget { */ public void rightJustify() { justification = Justification.RIGHT; - reflow(); + reflowData(); } /** @@ -197,13 +200,14 @@ public final class TText extends TWidget { */ public void fullJustify() { justification = Justification.FULL; - reflow(); + reflowData(); } /** * Resize text and scrollbars for a new width/height. */ - public void reflow() { + @Override + public void reflowData() { // Reset the lines lines.clear(); @@ -212,19 +216,19 @@ public final class TText extends TWidget { for (String p : paragraphs) { switch (justification) { case LEFT: - lines.addAll(jexer.bits.StringJustifier.left(p, + lines.addAll(jexer.bits.StringUtils.left(p, getWidth() - 1)); break; case CENTER: - lines.addAll(jexer.bits.StringJustifier.center(p, + lines.addAll(jexer.bits.StringUtils.center(p, getWidth() - 1)); break; case RIGHT: - lines.addAll(jexer.bits.StringJustifier.right(p, + lines.addAll(jexer.bits.StringUtils.right(p, getWidth() - 1)); break; case FULL: - lines.addAll(jexer.bits.StringJustifier.full(p, + lines.addAll(jexer.bits.StringUtils.full(p, getWidth() - 1)); break; } @@ -233,29 +237,6 @@ public final class TText extends TWidget { lines.add(""); } } - - // Start at the top - if (vScroller == null) { - vScroller = new TVScroller(this, getWidth() - 1, 0, getHeight() - 1); - vScroller.setTopValue(0); - vScroller.setValue(0); - } else { - vScroller.setX(getWidth() - 1); - vScroller.setHeight(getHeight() - 1); - } - vScroller.setBigChange(getHeight() - 1); - - // Start at the left - if (hScroller == null) { - hScroller = new THScroller(this, 0, getHeight() - 1, getWidth() - 1); - hScroller.setLeftValue(0); - hScroller.setValue(0); - } else { - hScroller.setY(getHeight() - 1); - hScroller.setWidth(getWidth() - 1); - } - hScroller.setBigChange(getWidth() - 1); - computeBounds(); } @@ -299,7 +280,9 @@ public final class TText extends TWidget { lines = new LinkedList(); - reflow(); + vScroller = new TVScroller(this, getWidth() - 1, 0, getHeight() - 1); + hScroller = new THScroller(this, 0, getHeight() - 1, getWidth() - 1); + reflowData(); } /**