2 * Jexer - Java Text User Interface
4 * License: LGPLv3 or later
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
10 * Copyright (C) 2015 Kevin Lamonte
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
33 import jexer
.bits
.CellAttributes
;
34 import jexer
.bits
.GraphicsChars
;
35 import jexer
.event
.TKeypressEvent
;
36 import jexer
.event
.TMouseEvent
;
37 import static jexer
.TKeypress
.*;
40 * TField implements an editable text field.
42 public class TField
extends TWidget
{
47 protected String text
= "";
54 public final String
getText() {
59 * If true, only allow enough characters that will fit in the width. If
60 * false, allow the field to scroll to the right.
62 protected boolean fixed
= false;
65 * Current editing position within text.
67 protected int position
= 0;
70 * Beginning of visible portion.
72 protected int windowStart
= 0;
75 * If true, new characters are inserted at position.
77 protected boolean insertMode
= true;
80 * Remember mouse state.
82 protected TMouseEvent mouse
;
85 * The action to perform when the user presses enter.
87 protected TAction enterAction
;
90 * The action to perform when the text is updated.
92 protected TAction updateAction
;
97 * @param parent parent widget
98 * @param x column relative to parent
99 * @param y row relative to parent
100 * @param width visible text width
101 * @param fixed if true, the text cannot exceed the display width
103 public TField(final TWidget parent
, final int x
, final int y
,
104 final int width
, final boolean fixed
) {
106 this(parent
, x
, y
, width
, fixed
, "", null, null);
110 * Public constructor.
112 * @param parent parent widget
113 * @param x column relative to parent
114 * @param y row relative to parent
115 * @param width visible text width
116 * @param fixed if true, the text cannot exceed the display width
117 * @param text initial text, default is empty string
119 public TField(final TWidget parent
, final int x
, final int y
,
120 final int width
, final boolean fixed
, final String text
) {
122 this(parent
, x
, y
, width
, fixed
, text
, null, null);
126 * Public constructor.
128 * @param parent parent widget
129 * @param x column relative to parent
130 * @param y row relative to parent
131 * @param width visible text width
132 * @param fixed if true, the text cannot exceed the display width
133 * @param text initial text, default is empty string
134 * @param enterAction function to call when enter key is pressed
135 * @param updateAction function to call when the text is updated
137 public TField(final TWidget parent
, final int x
, final int y
,
138 final int width
, final boolean fixed
, final String text
,
139 final TAction enterAction
, final TAction updateAction
) {
141 // Set parent and window
151 this.enterAction
= enterAction
;
152 this.updateAction
= updateAction
;
156 * Returns true if the mouse is currently on the field.
158 * @return if true the mouse is currently on the field
160 protected boolean mouseOnField() {
161 int rightEdge
= getWidth() - 1;
163 && (mouse
.getY() == 0)
164 && (mouse
.getX() >= 0)
165 && (mouse
.getX() <= rightEdge
)
173 * Dispatch to the action function.
175 * @param enter if true, the user pressed Enter, else this was an update
178 protected void dispatch(final boolean enter
) {
180 if (enterAction
!= null) {
184 if (updateAction
!= null) {
191 * Draw the text field.
195 CellAttributes fieldColor
;
197 if (getAbsoluteActive()) {
198 fieldColor
= getTheme().getColor("tfield.active");
200 fieldColor
= getTheme().getColor("tfield.inactive");
203 int end
= windowStart
+ getWidth();
204 if (end
> text
.length()) {
207 getScreen().hLineXY(0, 0, getWidth(), GraphicsChars
.HATCH
, fieldColor
);
208 getScreen().putStrXY(0, 0, text
.substring(windowStart
, end
),
211 // Fix the cursor, it will be rendered by TApplication.drawAll().
216 * Update the cursor position.
218 protected void updateCursor() {
219 if ((position
> getWidth()) && fixed
) {
220 setCursorX(getWidth());
221 } else if ((position
- windowStart
== getWidth()) && !fixed
) {
222 setCursorX(getWidth() - 1);
224 setCursorX(position
- windowStart
);
229 * Handle mouse button presses.
231 * @param mouse mouse button event
234 public void onMouseDown(final TMouseEvent mouse
) {
237 if ((mouseOnField()) && (mouse
.getMouse1())) {
239 int deltaX
= mouse
.getX() - getCursorX();
241 if (position
> text
.length()) {
242 position
= text
.length();
252 * @param keypress keystroke event
255 public void onKeypress(final TKeypressEvent keypress
) {
257 if (keypress
.equals(kbLeft
)) {
261 if (fixed
== false) {
262 if ((position
== windowStart
) && (windowStart
> 0)) {
269 if (keypress
.equals(kbRight
)) {
270 if (position
< text
.length()) {
273 if (position
== getWidth()) {
277 if ((position
- windowStart
) == getWidth()) {
285 if (keypress
.equals(kbEnter
)) {
290 if (keypress
.equals(kbIns
)) {
291 insertMode
= !insertMode
;
294 if (keypress
.equals(kbHome
)) {
300 if (keypress
.equals(kbEnd
)) {
301 position
= text
.length();
303 if (position
>= getWidth()) {
304 position
= text
.length() - 1;
307 windowStart
= text
.length() - getWidth() + 1;
308 if (windowStart
< 0) {
315 if (keypress
.equals(kbDel
)) {
316 if ((text
.length() > 0) && (position
< text
.length())) {
317 text
= text
.substring(0, position
)
318 + text
.substring(position
+ 1);
323 if (keypress
.equals(kbBackspace
) || keypress
.equals(kbBackspaceDel
)) {
326 text
= text
.substring(0, position
)
327 + text
.substring(position
+ 1);
329 if (fixed
== false) {
330 if ((position
== windowStart
)
340 if (!keypress
.getKey().getIsKey()
341 && !keypress
.getKey().getAlt()
342 && !keypress
.getKey().getCtrl()
344 // Plain old keystroke, process it
345 if ((position
== text
.length())
346 && (text
.length() < getWidth())) {
349 appendChar(keypress
.getKey().getCh());
350 } else if ((position
< text
.length())
351 && (text
.length() < getWidth())) {
353 // Overwrite or insert a character
354 if (insertMode
== false) {
356 text
= text
.substring(0, position
)
357 + keypress
.getKey().getCh()
358 + text
.substring(position
+ 1);
362 insertChar(keypress
.getKey().getCh());
364 } else if ((position
< text
.length())
365 && (text
.length() >= getWidth())) {
367 // Multiple cases here
368 if ((fixed
== true) && (insertMode
== true)) {
369 // Buffer is full, do nothing
370 } else if ((fixed
== true) && (insertMode
== false)) {
371 // Overwrite the last character, maybe move position
372 text
= text
.substring(0, position
)
373 + keypress
.getKey().getCh()
374 + text
.substring(position
+ 1);
375 if (position
< getWidth() - 1) {
378 } else if ((fixed
== false) && (insertMode
== false)) {
379 // Overwrite the last character, definitely move position
380 text
= text
.substring(0, position
)
381 + keypress
.getKey().getCh()
382 + text
.substring(position
+ 1);
385 if (position
== text
.length()) {
386 // Append this character
387 appendChar(keypress
.getKey().getCh());
389 // Insert this character
390 insertChar(keypress
.getKey().getCh());
396 // Append this character
397 appendChar(keypress
.getKey().getCh());
403 // Pass to parent for the things we don't care about.
404 super.onKeypress(keypress
);
408 * Append char to the end of the field.
410 * @param ch = char to append
412 protected void appendChar(final char ch
) {
413 // Append the LAST character
417 assert (position
== text
.length());
420 if (position
== getWidth()) {
424 if ((position
- windowStart
) == getWidth()) {
431 * Insert char somewhere in the middle of the field.
433 * @param ch char to append
435 protected void insertChar(final char ch
) {
436 text
= text
.substring(0, position
) + ch
+ text
.substring(position
);
438 if ((position
- windowStart
) == getWidth()) {