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
142 super(parent
, x
, y
, width
, 1);
147 this.enterAction
= enterAction
;
148 this.updateAction
= updateAction
;
152 * Returns true if the mouse is currently on the field.
154 * @return if true the mouse is currently on the field
156 protected boolean mouseOnField() {
157 int rightEdge
= getWidth() - 1;
159 && (mouse
.getY() == 0)
160 && (mouse
.getX() >= 0)
161 && (mouse
.getX() <= rightEdge
)
169 * Dispatch to the action function.
171 * @param enter if true, the user pressed Enter, else this was an update
174 protected void dispatch(final boolean enter
) {
176 if (enterAction
!= null) {
180 if (updateAction
!= null) {
187 * Draw the text field.
191 CellAttributes fieldColor
;
193 if (getAbsoluteActive()) {
194 fieldColor
= getTheme().getColor("tfield.active");
196 fieldColor
= getTheme().getColor("tfield.inactive");
199 int end
= windowStart
+ getWidth();
200 if (end
> text
.length()) {
203 getScreen().hLineXY(0, 0, getWidth(), GraphicsChars
.HATCH
, fieldColor
);
204 getScreen().putStrXY(0, 0, text
.substring(windowStart
, end
),
207 // Fix the cursor, it will be rendered by TApplication.drawAll().
212 * Update the cursor position.
214 protected void updateCursor() {
215 if ((position
> getWidth()) && fixed
) {
216 setCursorX(getWidth());
217 } else if ((position
- windowStart
== getWidth()) && !fixed
) {
218 setCursorX(getWidth() - 1);
220 setCursorX(position
- windowStart
);
225 * Handle mouse button presses.
227 * @param mouse mouse button event
230 public void onMouseDown(final TMouseEvent mouse
) {
233 if ((mouseOnField()) && (mouse
.getMouse1())) {
235 int deltaX
= mouse
.getX() - getCursorX();
237 if (position
> text
.length()) {
238 position
= text
.length();
248 * @param keypress keystroke event
251 public void onKeypress(final TKeypressEvent keypress
) {
253 if (keypress
.equals(kbLeft
)) {
257 if (fixed
== false) {
258 if ((position
== windowStart
) && (windowStart
> 0)) {
265 if (keypress
.equals(kbRight
)) {
266 if (position
< text
.length()) {
269 if (position
== getWidth()) {
273 if ((position
- windowStart
) == getWidth()) {
281 if (keypress
.equals(kbEnter
)) {
286 if (keypress
.equals(kbIns
)) {
287 insertMode
= !insertMode
;
290 if (keypress
.equals(kbHome
)) {
296 if (keypress
.equals(kbEnd
)) {
297 position
= text
.length();
299 if (position
>= getWidth()) {
300 position
= text
.length() - 1;
303 windowStart
= text
.length() - getWidth() + 1;
304 if (windowStart
< 0) {
311 if (keypress
.equals(kbDel
)) {
312 if ((text
.length() > 0) && (position
< text
.length())) {
313 text
= text
.substring(0, position
)
314 + text
.substring(position
+ 1);
319 if (keypress
.equals(kbBackspace
) || keypress
.equals(kbBackspaceDel
)) {
322 text
= text
.substring(0, position
)
323 + text
.substring(position
+ 1);
325 if (fixed
== false) {
326 if ((position
== windowStart
)
336 if (!keypress
.getKey().getIsKey()
337 && !keypress
.getKey().getAlt()
338 && !keypress
.getKey().getCtrl()
340 // Plain old keystroke, process it
341 if ((position
== text
.length())
342 && (text
.length() < getWidth())) {
345 appendChar(keypress
.getKey().getCh());
346 } else if ((position
< text
.length())
347 && (text
.length() < getWidth())) {
349 // Overwrite or insert a character
350 if (insertMode
== false) {
352 text
= text
.substring(0, position
)
353 + keypress
.getKey().getCh()
354 + text
.substring(position
+ 1);
358 insertChar(keypress
.getKey().getCh());
360 } else if ((position
< text
.length())
361 && (text
.length() >= getWidth())) {
363 // Multiple cases here
364 if ((fixed
== true) && (insertMode
== true)) {
365 // Buffer is full, do nothing
366 } else if ((fixed
== true) && (insertMode
== false)) {
367 // Overwrite the last character, maybe move position
368 text
= text
.substring(0, position
)
369 + keypress
.getKey().getCh()
370 + text
.substring(position
+ 1);
371 if (position
< getWidth() - 1) {
374 } else if ((fixed
== false) && (insertMode
== false)) {
375 // Overwrite the last character, definitely move position
376 text
= text
.substring(0, position
)
377 + keypress
.getKey().getCh()
378 + text
.substring(position
+ 1);
381 if (position
== text
.length()) {
382 // Append this character
383 appendChar(keypress
.getKey().getCh());
385 // Insert this character
386 insertChar(keypress
.getKey().getCh());
392 // Append this character
393 appendChar(keypress
.getKey().getCh());
399 // Pass to parent for the things we don't care about.
400 super.onKeypress(keypress
);
404 * Append char to the end of the field.
406 * @param ch = char to append
408 protected void appendChar(final char ch
) {
409 // Append the LAST character
413 assert (position
== text
.length());
416 if (position
== getWidth()) {
420 if ((position
- windowStart
) == getWidth()) {
427 * Insert char somewhere in the middle of the field.
429 * @param ch char to append
431 protected void insertChar(final char ch
) {
432 text
= text
.substring(0, position
) + ch
+ text
.substring(position
);
434 if ((position
- windowStart
) == getWidth()) {