From: Kevin Lamonte Date: Fri, 18 Aug 2017 20:05:17 +0000 (-0400) Subject: #19 expose home/end for TField X-Git-Url: http://git.nikiroo.be/?a=commitdiff_plain;h=1b1206070bb94d706c14400b8332a81646b77a25;p=fanfix.git #19 expose home/end for TField --- diff --git a/src/jexer/TField.java b/src/jexer/TField.java index 785a276..c93cecb 100644 --- a/src/jexer/TField.java +++ b/src/jexer/TField.java @@ -316,23 +316,12 @@ public class TField extends TWidget { return; } if (keypress.equals(kbHome)) { - position = 0; - windowStart = 0; + home(); return; } if (keypress.equals(kbEnd)) { - position = text.length(); - if (fixed == true) { - if (position >= getWidth()) { - position = text.length() - 1; - } - } else { - windowStart = text.length() - getWidth() + 1; - if (windowStart < 0) { - windowStart = 0; - } - } + end(); return; } @@ -467,4 +456,48 @@ public class TField extends TWidget { } } + /** + * Position the cursor at the first column. The field may adjust the + * window start to show as much of the field as possible. + */ + public void home() { + position = 0; + windowStart = 0; + } + + /** + * Set the editing position to the last filled character. The field may + * adjust the window start to show as much of the field as possible. + */ + public void end() { + position = text.length(); + if (fixed == true) { + if (position >= getWidth()) { + position = text.length() - 1; + } + } else { + windowStart = text.length() - getWidth() + 1; + if (windowStart < 0) { + windowStart = 0; + } + } + } + + /** + * Set the editing position. The field may adjust the window start to + * show as much of the field as possible. + * + * @param position the new position + * @throws IndexOutOfBoundsException if position is outside the range of + * the available text + */ + public void setPosition(final int position) { + if ((position < 0) || (position >= text.length())) { + throw new IndexOutOfBoundsException("Max length is " + + text.length() + ", requested position " + position); + } + this.position = position; + normalizeWindowStart(); + } + }