X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTText.java;h=44176a4efa8979143b77cb74788c308824ade5c1;hb=615a0d99fd0aa4437116dd083147f9150d5e6527;hp=c57e8846ace8b8ee160d250147333bd63723ab76;hpb=56661844475522242093c8858ceb20fb15589da5;p=fanfix.git diff --git a/src/jexer/TText.java b/src/jexer/TText.java index c57e884..44176a4 100644 --- a/src/jexer/TText.java +++ b/src/jexer/TText.java @@ -28,6 +28,12 @@ */ package jexer; +import java.util.LinkedList; +import java.util.List; + +import jexer.bits.CellAttributes; +import jexer.event.TKeypressEvent; +import jexer.event.TMouseEvent; import static jexer.TKeypress.kbDown; import static jexer.TKeypress.kbEnd; import static jexer.TKeypress.kbHome; @@ -37,18 +43,15 @@ 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; - /** * TText implements a simple scrollable text area. It reflows automatically on * resize. */ -public final class TText extends TScrollableWidget { +public class TText extends TScrollableWidget { + + // ------------------------------------------------------------------------ + // Constants -------------------------------------------------------------- + // ------------------------------------------------------------------------ /** * Available text justifications. @@ -75,6 +78,10 @@ public final class TText extends TScrollableWidget { FULL, } + // ------------------------------------------------------------------------ + // Variables -------------------------------------------------------------- + // ------------------------------------------------------------------------ + /** * How to justify the text. */ @@ -105,148 +112,9 @@ public final class TText extends TScrollableWidget { */ private int lineSpacing = 1; - /** - * Set the text. - * - * @param text new text to display - */ - public void setText(final String text) { - this.text = text; - reflowData(); - } - - /** - * Get the text. - * - * @return the text - */ - public String getText() { - return text; - } - - /** - * Convenience method used by TWindowLoggerOutput. - * - * @param line new line to add - */ - public void addLine(final String line) { - if (text.length() == 0) { - text = line; - } else { - text += "\n\n"; - text += line; - } - reflowData(); - } - - /** - * Recompute the bounds for the scrollbars. - */ - private void computeBounds() { - maxLineWidth = 0; - for (String line : lines) { - if (line.length() > maxLineWidth) { - maxLineWidth = line.length(); - } - } - - vScroller.setTopValue(0); - vScroller.setBottomValue((lines.size() - getHeight()) + 1); - if (vScroller.getBottomValue() < 0) { - vScroller.setBottomValue(0); - } - if (vScroller.getValue() > vScroller.getBottomValue()) { - vScroller.setValue(vScroller.getBottomValue()); - } - - hScroller.setLeftValue(0); - hScroller.setRightValue((maxLineWidth - getWidth()) + 1); - if (hScroller.getRightValue() < 0) { - hScroller.setRightValue(0); - } - if (hScroller.getValue() > hScroller.getRightValue()) { - hScroller.setValue(hScroller.getRightValue()); - } - } - - /** - * Set justification. - * - * @param justification LEFT, CENTER, RIGHT, or FULL - */ - public void setJustification(final Justification justification) { - this.justification = justification; - reflowData(); - } - - /** - * Left-justify the text. - */ - public void leftJustify() { - justification = Justification.LEFT; - reflowData(); - } - - /** - * Center-justify the text. - */ - public void centerJustify() { - justification = Justification.CENTER; - reflowData(); - } - - /** - * Right-justify the text. - */ - public void rightJustify() { - justification = Justification.RIGHT; - reflowData(); - } - - /** - * Fully-justify the text. - */ - public void fullJustify() { - justification = Justification.FULL; - reflowData(); - } - - /** - * Resize text and scrollbars for a new width/height. - */ - @Override - public void reflowData() { - // Reset the lines - lines.clear(); - - // Break up text into paragraphs - String[] paragraphs = text.split("\n\n"); - for (String p : paragraphs) { - switch (justification) { - case LEFT: - lines.addAll(jexer.bits.StringJustifier.left(p, - getWidth() - 1)); - break; - case CENTER: - lines.addAll(jexer.bits.StringJustifier.center(p, - getWidth() - 1)); - break; - case RIGHT: - lines.addAll(jexer.bits.StringJustifier.right(p, - getWidth() - 1)); - break; - case FULL: - lines.addAll(jexer.bits.StringJustifier.full(p, - getWidth() - 1)); - break; - } - - for (int i = 0; i < lineSpacing; i++) { - lines.add(""); - } - } - computeBounds(); - } + // ------------------------------------------------------------------------ + // Constructors ----------------------------------------------------------- + // ------------------------------------------------------------------------ /** * Public constructor. @@ -293,6 +161,10 @@ public final class TText extends TScrollableWidget { reflowData(); } + // ------------------------------------------------------------------------ + // TScrollableWidget ------------------------------------------------------ + // ------------------------------------------------------------------------ + /** * Draw the text box. */ @@ -376,4 +248,151 @@ public final class TText extends TScrollableWidget { } } + /** + * Resize text and scrollbars for a new width/height. + */ + @Override + public void reflowData() { + // Reset the lines + lines.clear(); + + // Break up text into paragraphs + String[] paragraphs = text.split("\n\n"); + for (String p : paragraphs) { + switch (justification) { + case LEFT: + lines.addAll(jexer.bits.StringUtils.left(p, + getWidth() - 1)); + break; + case CENTER: + lines.addAll(jexer.bits.StringUtils.center(p, + getWidth() - 1)); + break; + case RIGHT: + lines.addAll(jexer.bits.StringUtils.right(p, + getWidth() - 1)); + break; + case FULL: + lines.addAll(jexer.bits.StringUtils.full(p, + getWidth() - 1)); + break; + } + + for (int i = 0; i < lineSpacing; i++) { + lines.add(""); + } + } + computeBounds(); + } + + // ------------------------------------------------------------------------ + // TText ------------------------------------------------------------------ + // ------------------------------------------------------------------------ + + /** + * Set the text. + * + * @param text new text to display + */ + public void setText(final String text) { + this.text = text; + reflowData(); + } + + /** + * Get the text. + * + * @return the text + */ + public String getText() { + return text; + } + + /** + * Convenience method used by TWindowLoggerOutput. + * + * @param line new line to add + */ + public void addLine(final String line) { + if (text.length() == 0) { + text = line; + } else { + text += "\n\n"; + text += line; + } + reflowData(); + } + + /** + * Recompute the bounds for the scrollbars. + */ + private void computeBounds() { + maxLineWidth = 0; + for (String line : lines) { + if (line.length() > maxLineWidth) { + maxLineWidth = line.length(); + } + } + + vScroller.setTopValue(0); + vScroller.setBottomValue((lines.size() - getHeight()) + 1); + if (vScroller.getBottomValue() < 0) { + vScroller.setBottomValue(0); + } + if (vScroller.getValue() > vScroller.getBottomValue()) { + vScroller.setValue(vScroller.getBottomValue()); + } + + hScroller.setLeftValue(0); + hScroller.setRightValue((maxLineWidth - getWidth()) + 1); + if (hScroller.getRightValue() < 0) { + hScroller.setRightValue(0); + } + if (hScroller.getValue() > hScroller.getRightValue()) { + hScroller.setValue(hScroller.getRightValue()); + } + } + + /** + * Set justification. + * + * @param justification LEFT, CENTER, RIGHT, or FULL + */ + public void setJustification(final Justification justification) { + this.justification = justification; + reflowData(); + } + + /** + * Left-justify the text. + */ + public void leftJustify() { + justification = Justification.LEFT; + reflowData(); + } + + /** + * Center-justify the text. + */ + public void centerJustify() { + justification = Justification.CENTER; + reflowData(); + } + + /** + * Right-justify the text. + */ + public void rightJustify() { + justification = Justification.RIGHT; + reflowData(); + } + + /** + * Fully-justify the text. + */ + public void fullJustify() { + justification = Justification.FULL; + reflowData(); + } + }