2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
31 import jexer
.bits
.CellAttributes
;
32 import jexer
.bits
.GraphicsChars
;
33 import jexer
.bits
.StringUtils
;
34 import jexer
.event
.TKeypressEvent
;
35 import jexer
.event
.TMouseEvent
;
36 import static jexer
.TKeypress
.*;
39 * TField implements an editable text field.
41 public class TField
extends TWidget
{
43 // ------------------------------------------------------------------------
44 // Variables --------------------------------------------------------------
45 // ------------------------------------------------------------------------
48 * Background character for unfilled-in text.
50 protected int backgroundChar
= GraphicsChars
.HATCH
;
55 protected String text
= "";
58 * If true, only allow enough characters that will fit in the width. If
59 * false, allow the field to scroll to the right.
61 protected boolean fixed
= false;
64 * Current editing position within text.
66 protected int position
= 0;
69 * Current editing position screen column number.
71 protected int screenPosition
= 0;
74 * Beginning of visible portion.
76 protected int windowStart
= 0;
79 * If true, new characters are inserted at position.
81 protected boolean insertMode
= true;
84 * Remember mouse state.
86 protected TMouseEvent mouse
;
89 * The action to perform when the user presses enter.
91 protected TAction enterAction
;
94 * The action to perform when the text is updated.
96 protected TAction updateAction
;
99 * The color to use when this field is active.
101 private String activeColorKey
= "tfield.active";
104 * The color to use when this field is not active.
106 private String inactiveColorKey
= "tfield.inactive";
108 // ------------------------------------------------------------------------
109 // Constructors -----------------------------------------------------------
110 // ------------------------------------------------------------------------
113 * Public constructor.
115 * @param parent parent widget
116 * @param x column relative to parent
117 * @param y row relative to parent
118 * @param width visible text width
119 * @param fixed if true, the text cannot exceed the display width
121 public TField(final TWidget parent
, final int x
, final int y
,
122 final int width
, final boolean fixed
) {
124 this(parent
, x
, y
, width
, fixed
, "", null, null);
128 * Public constructor.
130 * @param parent parent widget
131 * @param x column relative to parent
132 * @param y row relative to parent
133 * @param width visible text width
134 * @param fixed if true, the text cannot exceed the display width
135 * @param text initial text, default is empty string
137 public TField(final TWidget parent
, final int x
, final int y
,
138 final int width
, final boolean fixed
, final String text
) {
140 this(parent
, x
, y
, width
, fixed
, text
, null, null);
144 * Public constructor.
146 * @param parent parent widget
147 * @param x column relative to parent
148 * @param y row relative to parent
149 * @param width visible text width
150 * @param fixed if true, the text cannot exceed the display width
151 * @param text initial text, default is empty string
152 * @param enterAction function to call when enter key is pressed
153 * @param updateAction function to call when the text is updated
155 public TField(final TWidget parent
, final int x
, final int y
,
156 final int width
, final boolean fixed
, final String text
,
157 final TAction enterAction
, final TAction updateAction
) {
159 // Set parent and window
160 super(parent
, x
, y
, width
, 1);
162 setCursorVisible(true);
165 this.enterAction
= enterAction
;
166 this.updateAction
= updateAction
;
169 // ------------------------------------------------------------------------
170 // Event handlers ---------------------------------------------------------
171 // ------------------------------------------------------------------------
174 * Returns true if the mouse is currently on the field.
176 * @return if true the mouse is currently on the field
178 protected boolean mouseOnField() {
179 int rightEdge
= getWidth() - 1;
181 && (mouse
.getY() == 0)
182 && (mouse
.getX() >= 0)
183 && (mouse
.getX() <= rightEdge
)
191 * Handle mouse button presses.
193 * @param mouse mouse button event
196 public void onMouseDown(final TMouseEvent mouse
) {
199 if ((mouseOnField()) && (mouse
.isMouse1())) {
201 int deltaX
= mouse
.getX() - getCursorX();
202 screenPosition
+= deltaX
;
203 if (screenPosition
> StringUtils
.width(text
)) {
204 screenPosition
= StringUtils
.width(text
);
206 position
= screenToTextPosition(screenPosition
);
215 * @param keypress keystroke event
218 public void onKeypress(final TKeypressEvent keypress
) {
220 if (keypress
.equals(kbLeft
)) {
222 if (position
< codePointLength(text
)) {
223 screenPosition
-= StringUtils
.width(text
.codePointBefore(position
));
224 position
-= Character
.charCount(text
.codePointBefore(position
));
230 if (fixed
== false) {
231 if ((screenPosition
== windowStart
) && (windowStart
> 0)) {
232 windowStart
-= StringUtils
.width(text
.codePointAt(
233 screenToTextPosition(windowStart
)));
236 normalizeWindowStart();
240 if (keypress
.equals(kbRight
)) {
241 if (position
< codePointLength(text
)) {
242 screenPosition
+= StringUtils
.width(text
.codePointAt(position
));
243 position
+= Character
.charCount(text
.codePointAt(position
));
245 if (screenPosition
== getWidth()) {
247 position
-= Character
.charCount(text
.codePointAt(position
));
250 if ((screenPosition
- windowStart
) >= getWidth()) {
251 windowStart
+= StringUtils
.width(text
.codePointAt(
252 screenToTextPosition(windowStart
)));
259 if (keypress
.equals(kbEnter
)) {
264 if (keypress
.equals(kbIns
)) {
265 insertMode
= !insertMode
;
268 if (keypress
.equals(kbHome
)) {
273 if (keypress
.equals(kbEnd
)) {
278 if (keypress
.equals(kbDel
)) {
279 if ((codePointLength(text
) > 0) && (position
< codePointLength(text
))) {
280 text
= text
.substring(0, position
)
281 + text
.substring(position
+ 1);
282 screenPosition
= StringUtils
.width(text
.substring(0, position
));
288 if (keypress
.equals(kbBackspace
) || keypress
.equals(kbBackspaceDel
)) {
290 position
-= Character
.charCount(text
.codePointBefore(position
));
291 text
= text
.substring(0, position
)
292 + text
.substring(position
+ 1);
293 screenPosition
= StringUtils
.width(text
.substring(0, position
));
295 if (fixed
== false) {
296 if ((screenPosition
>= windowStart
)
299 windowStart
-= StringUtils
.width(text
.codePointAt(
300 screenToTextPosition(windowStart
)));
304 normalizeWindowStart();
308 if (!keypress
.getKey().isFnKey()
309 && !keypress
.getKey().isAlt()
310 && !keypress
.getKey().isCtrl()
312 // Plain old keystroke, process it
313 if ((position
== codePointLength(text
))
314 && (StringUtils
.width(text
) < getWidth())) {
317 appendChar(keypress
.getKey().getChar());
318 } else if ((position
< codePointLength(text
))
319 && (StringUtils
.width(text
) < getWidth())) {
321 // Overwrite or insert a character
322 if (insertMode
== false) {
324 text
= text
.substring(0, position
)
325 + codePointString(keypress
.getKey().getChar())
326 + text
.substring(position
+ 1);
327 screenPosition
+= StringUtils
.width(text
.codePointAt(position
));
328 position
+= Character
.charCount(keypress
.getKey().getChar());
331 insertChar(keypress
.getKey().getChar());
333 } else if ((position
< codePointLength(text
))
334 && (StringUtils
.width(text
) >= getWidth())) {
336 // Multiple cases here
337 if ((fixed
== true) && (insertMode
== true)) {
338 // Buffer is full, do nothing
339 } else if ((fixed
== true) && (insertMode
== false)) {
340 // Overwrite the last character, maybe move position
341 text
= text
.substring(0, position
)
342 + codePointString(keypress
.getKey().getChar())
343 + text
.substring(position
+ 1);
344 if (screenPosition
< getWidth() - 1) {
345 screenPosition
+= StringUtils
.width(text
.codePointAt(position
));
346 position
+= Character
.charCount(keypress
.getKey().getChar());
348 } else if ((fixed
== false) && (insertMode
== false)) {
349 // Overwrite the last character, definitely move position
350 text
= text
.substring(0, position
)
351 + codePointString(keypress
.getKey().getChar())
352 + text
.substring(position
+ 1);
353 screenPosition
+= StringUtils
.width(text
.codePointAt(position
));
354 position
+= Character
.charCount(keypress
.getKey().getChar());
356 if (position
== codePointLength(text
)) {
357 // Append this character
358 appendChar(keypress
.getKey().getChar());
360 // Insert this character
361 insertChar(keypress
.getKey().getChar());
367 // Append this character
368 appendChar(keypress
.getKey().getChar());
374 // Pass to parent for the things we don't care about.
375 super.onKeypress(keypress
);
378 // ------------------------------------------------------------------------
379 // TWidget ----------------------------------------------------------------
380 // ------------------------------------------------------------------------
383 * Draw the text field.
387 CellAttributes fieldColor
;
389 if (isAbsoluteActive()) {
390 fieldColor
= getTheme().getColor(activeColorKey
);
392 fieldColor
= getTheme().getColor(inactiveColorKey
);
395 int end
= windowStart
+ getWidth();
396 if (end
> StringUtils
.width(text
)) {
397 end
= StringUtils
.width(text
);
399 hLineXY(0, 0, getWidth(), backgroundChar
, fieldColor
);
400 putStringXY(0, 0, text
.substring(screenToTextPosition(windowStart
),
401 screenToTextPosition(end
)), fieldColor
);
403 // Fix the cursor, it will be rendered by TApplication.drawAll().
407 // ------------------------------------------------------------------------
408 // TField -----------------------------------------------------------------
409 // ------------------------------------------------------------------------
412 * Convert a char (codepoint) to a string.
417 private String
codePointString(final int ch
) {
418 StringBuilder sb
= new StringBuilder(1);
419 sb
.append(Character
.toChars(ch
));
420 return sb
.toString();
424 * Get the number of codepoints in a string.
426 * @param str the string
427 * @return the number of codepoints
429 private int codePointLength(final String str
) {
430 return str
.codePointCount(0, str
.length());
434 * Get field background character.
436 * @return background character
438 public final int getBackgroundChar() {
439 return backgroundChar
;
443 * Set field background character.
445 * @param backgroundChar the background character
447 public void setBackgroundChar(final int backgroundChar
) {
448 this.backgroundChar
= backgroundChar
;
456 public final String
getText() {
463 * @param text the new field text
465 public void setText(final String text
) {
466 assert (text
!= null);
473 * Dispatch to the action function.
475 * @param enter if true, the user pressed Enter, else this was an update
478 protected void dispatch(final boolean enter
) {
480 if (enterAction
!= null) {
484 if (updateAction
!= null) {
491 * Determine string position from screen position.
493 * @param screenPosition the position on screen
494 * @return the equivalent position in text
496 protected int screenToTextPosition(final int screenPosition
) {
497 if (screenPosition
== 0) {
502 for (int i
= 0; i
< codePointLength(text
); i
++) {
503 n
+= StringUtils
.width(text
.codePointAt(i
));
504 if (n
>= screenPosition
) {
508 // screenPosition exceeds the available text length.
509 throw new IndexOutOfBoundsException("screenPosition " + screenPosition
+
510 " exceeds available text length " + codePointLength(text
));
514 * Update the visible cursor position to match the location of position
517 protected void updateCursor() {
518 if ((screenPosition
> getWidth()) && fixed
) {
519 setCursorX(getWidth());
520 } else if ((screenPosition
- windowStart
>= getWidth()) && !fixed
) {
521 setCursorX(getWidth() - 1);
523 setCursorX(screenPosition
- windowStart
);
528 * Normalize windowStart such that most of the field data if visible.
530 protected void normalizeWindowStart() {
532 // windowStart had better be zero, there is nothing to do here.
533 assert (windowStart
== 0);
536 windowStart
= screenPosition
- (getWidth() - 1);
537 if (windowStart
< 0) {
545 * Append char to the end of the field.
547 * @param ch char to append
549 protected void appendChar(final int ch
) {
550 // Append the LAST character
551 text
+= codePointString(ch
);
552 position
+= Character
.charCount(ch
);
553 screenPosition
+= StringUtils
.width(ch
);
555 assert (position
== codePointLength(text
));
558 if (screenPosition
>= getWidth()) {
559 position
-= Character
.charCount(ch
);
560 screenPosition
-= StringUtils
.width(ch
);
563 if ((screenPosition
- windowStart
) >= getWidth()) {
570 * Insert char somewhere in the middle of the field.
572 * @param ch char to append
574 protected void insertChar(final int ch
) {
575 text
= text
.substring(0, position
) + codePointString(ch
)
576 + text
.substring(position
);
577 position
+= Character
.charCount(ch
);
578 screenPosition
+= StringUtils
.width(ch
);
579 if ((screenPosition
- windowStart
) == getWidth()) {
586 * Position the cursor at the first column. The field may adjust the
587 * window start to show as much of the field as possible.
596 * Set the editing position to the last filled character. The field may
597 * adjust the window start to show as much of the field as possible.
600 position
= codePointLength(text
);
601 screenPosition
= StringUtils
.width(text
);
603 if (screenPosition
>= getWidth()) {
604 position
= codePointLength(text
) - 1;
605 screenPosition
= StringUtils
.width(text
) - 1;
608 windowStart
= StringUtils
.width(text
) - getWidth() + 1;
609 if (windowStart
< 0) {
616 * Set the editing position. The field may adjust the window start to
617 * show as much of the field as possible.
619 * @param position the new position
620 * @throws IndexOutOfBoundsException if position is outside the range of
623 public void setPosition(final int position
) {
624 if ((position
< 0) || (position
>= codePointLength(text
))) {
625 throw new IndexOutOfBoundsException("Max length is " +
626 codePointLength(text
) + ", requested position " + position
);
628 this.position
= position
;
629 normalizeWindowStart();
633 * Set the active color key.
635 * @param activeColorKey ColorTheme key color to use when this field is
638 public void setActiveColorKey(final String activeColorKey
) {
639 this.activeColorKey
= activeColorKey
;
643 * Set the inactive color key.
645 * @param inactiveColorKey ColorTheme key color to use when this field is
648 public void setInactiveColorKey(final String inactiveColorKey
) {
649 this.inactiveColorKey
= inactiveColorKey
;
653 * Set the action to perform when the user presses enter.
655 * @param action the action to perform when the user presses enter
657 public void setEnterAction(final TAction action
) {
658 enterAction
= action
;
662 * Set the action to perform when the field is updated.
664 * @param action the action to perform when the field is updated
666 public void setUpdateAction(final TAction action
) {
667 updateAction
= action
;