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 screenPosition
-= StringUtils
.width(text
.codePointBefore(position
));
223 position
-= Character
.charCount(text
.codePointBefore(position
));
225 if (fixed
== false) {
226 if ((screenPosition
== windowStart
) && (windowStart
> 0)) {
227 windowStart
-= StringUtils
.width(text
.codePointAt(
228 screenToTextPosition(windowStart
)));
231 normalizeWindowStart();
235 if (keypress
.equals(kbRight
)) {
236 if (position
< text
.length()) {
237 screenPosition
+= StringUtils
.width(text
.codePointAt(position
));
238 position
+= Character
.charCount(text
.codePointAt(position
));
240 if (screenPosition
== getWidth()) {
242 position
-= Character
.charCount(text
.codePointAt(position
));
245 while ((screenPosition
- windowStart
+
246 StringUtils
.width(text
.codePointAt(text
.length() - 1)))
249 windowStart
+= StringUtils
.width(text
.codePointAt(
250 screenToTextPosition(windowStart
)));
254 assert (position
<= text
.length());
258 if (keypress
.equals(kbEnter
)) {
263 if (keypress
.equals(kbIns
)) {
264 insertMode
= !insertMode
;
267 if (keypress
.equals(kbHome
)) {
272 if (keypress
.equals(kbEnd
)) {
277 if (keypress
.equals(kbDel
)) {
278 if ((text
.length() > 0) && (position
< text
.length())) {
279 text
= text
.substring(0, position
)
280 + text
.substring(position
+ 1);
281 screenPosition
= StringUtils
.width(text
.substring(0, position
));
287 if (keypress
.equals(kbBackspace
) || keypress
.equals(kbBackspaceDel
)) {
289 position
-= Character
.charCount(text
.codePointBefore(position
));
290 text
= text
.substring(0, position
)
291 + text
.substring(position
+ 1);
292 screenPosition
= StringUtils
.width(text
.substring(0, position
));
294 if (fixed
== false) {
295 if ((screenPosition
>= windowStart
)
298 windowStart
-= StringUtils
.width(text
.codePointAt(
299 screenToTextPosition(windowStart
)));
303 normalizeWindowStart();
307 if (!keypress
.getKey().isFnKey()
308 && !keypress
.getKey().isAlt()
309 && !keypress
.getKey().isCtrl()
311 // Plain old keystroke, process it
312 if ((position
== text
.length())
313 && (StringUtils
.width(text
) < getWidth())) {
316 appendChar(keypress
.getKey().getChar());
317 } else if ((position
< text
.length())
318 && (StringUtils
.width(text
) < getWidth())) {
320 // Overwrite or insert a character
321 if (insertMode
== false) {
323 text
= text
.substring(0, position
)
324 + codePointString(keypress
.getKey().getChar())
325 + text
.substring(position
+ 1);
326 screenPosition
+= StringUtils
.width(text
.codePointAt(position
));
327 position
+= Character
.charCount(keypress
.getKey().getChar());
330 insertChar(keypress
.getKey().getChar());
332 } else if ((position
< text
.length())
333 && (StringUtils
.width(text
) >= getWidth())) {
335 // Multiple cases here
336 if ((fixed
== true) && (insertMode
== true)) {
337 // Buffer is full, do nothing
338 } else if ((fixed
== true) && (insertMode
== false)) {
339 // Overwrite the last character, maybe move position
340 text
= text
.substring(0, position
)
341 + codePointString(keypress
.getKey().getChar())
342 + text
.substring(position
+ 1);
343 if (screenPosition
< getWidth() - 1) {
344 screenPosition
+= StringUtils
.width(text
.codePointAt(position
));
345 position
+= Character
.charCount(keypress
.getKey().getChar());
347 } else if ((fixed
== false) && (insertMode
== false)) {
348 // Overwrite the last character, definitely move position
349 text
= text
.substring(0, position
)
350 + codePointString(keypress
.getKey().getChar())
351 + text
.substring(position
+ 1);
352 screenPosition
+= StringUtils
.width(text
.codePointAt(position
));
353 position
+= Character
.charCount(keypress
.getKey().getChar());
355 if (position
== text
.length()) {
356 // Append this character
357 appendChar(keypress
.getKey().getChar());
359 // Insert this character
360 insertChar(keypress
.getKey().getChar());
366 // Append this character
367 appendChar(keypress
.getKey().getChar());
373 // Pass to parent for the things we don't care about.
374 super.onKeypress(keypress
);
377 // ------------------------------------------------------------------------
378 // TWidget ----------------------------------------------------------------
379 // ------------------------------------------------------------------------
382 * Override TWidget's height: we can only set height at construction
385 * @param height new widget height (ignored)
388 public void setHeight(final int height
) {
393 * Draw the text field.
397 CellAttributes fieldColor
;
399 if (isAbsoluteActive()) {
400 fieldColor
= getTheme().getColor(activeColorKey
);
402 fieldColor
= getTheme().getColor(inactiveColorKey
);
405 int end
= windowStart
+ getWidth();
406 if (end
> StringUtils
.width(text
)) {
407 end
= StringUtils
.width(text
);
409 hLineXY(0, 0, getWidth(), backgroundChar
, fieldColor
);
410 putStringXY(0, 0, text
.substring(screenToTextPosition(windowStart
),
411 screenToTextPosition(end
)), fieldColor
);
413 // Fix the cursor, it will be rendered by TApplication.drawAll().
417 // ------------------------------------------------------------------------
418 // TField -----------------------------------------------------------------
419 // ------------------------------------------------------------------------
422 * Convert a char (codepoint) to a string.
427 private String
codePointString(final int ch
) {
428 StringBuilder sb
= new StringBuilder(1);
429 sb
.append(Character
.toChars(ch
));
430 assert (Character
.charCount(ch
) == sb
.length());
431 return sb
.toString();
435 * Get field background character.
437 * @return background character
439 public final int getBackgroundChar() {
440 return backgroundChar
;
444 * Set field background character.
446 * @param backgroundChar the background character
448 public void setBackgroundChar(final int backgroundChar
) {
449 this.backgroundChar
= backgroundChar
;
457 public final String
getText() {
464 * @param text the new field text
466 public void setText(final String text
) {
467 assert (text
!= null);
474 * Dispatch to the action function.
476 * @param enter if true, the user pressed Enter, else this was an update
479 protected void dispatch(final boolean enter
) {
481 if (enterAction
!= null) {
482 enterAction
.DO(this);
485 if (updateAction
!= null) {
486 updateAction
.DO(this);
492 * Determine string position from screen position.
494 * @param screenPosition the position on screen
495 * @return the equivalent position in text
497 protected int screenToTextPosition(final int screenPosition
) {
498 if (screenPosition
== 0) {
503 for (int i
= 0; i
< text
.length(); i
++) {
504 n
+= StringUtils
.width(text
.codePointAt(i
));
505 if (n
>= screenPosition
) {
509 // screenPosition exceeds the available text length.
510 throw new IndexOutOfBoundsException("screenPosition " + screenPosition
+
511 " exceeds available text length " + text
.length());
515 * Update the visible cursor position to match the location of position
518 protected void updateCursor() {
519 if ((screenPosition
> getWidth()) && fixed
) {
520 setCursorX(getWidth());
521 } else if ((screenPosition
- windowStart
>= getWidth()) && !fixed
) {
522 setCursorX(getWidth() - 1);
524 setCursorX(screenPosition
- windowStart
);
529 * Normalize windowStart such that most of the field data if visible.
531 protected void normalizeWindowStart() {
533 // windowStart had better be zero, there is nothing to do here.
534 assert (windowStart
== 0);
537 windowStart
= screenPosition
- (getWidth() - 1);
538 if (windowStart
< 0) {
546 * Append char to the end of the field.
548 * @param ch char to append
550 protected void appendChar(final int ch
) {
551 // Append the LAST character
552 text
+= codePointString(ch
);
553 position
+= Character
.charCount(ch
);
554 screenPosition
+= StringUtils
.width(ch
);
556 assert (position
== text
.length());
559 if (screenPosition
>= getWidth()) {
560 position
-= Character
.charCount(ch
);
561 screenPosition
-= StringUtils
.width(ch
);
564 if ((screenPosition
- windowStart
) >= getWidth()) {
571 * Insert char somewhere in the middle of the field.
573 * @param ch char to append
575 protected void insertChar(final int ch
) {
576 text
= text
.substring(0, position
) + codePointString(ch
)
577 + text
.substring(position
);
578 position
+= Character
.charCount(ch
);
579 screenPosition
+= StringUtils
.width(ch
);
580 if ((screenPosition
- windowStart
) == getWidth()) {
587 * Position the cursor at the first column. The field may adjust the
588 * window start to show as much of the field as possible.
597 * Set the editing position to the last filled character. The field may
598 * adjust the window start to show as much of the field as possible.
601 position
= text
.length();
602 screenPosition
= StringUtils
.width(text
);
604 if (screenPosition
>= getWidth()) {
605 position
-= Character
.charCount(text
.codePointBefore(position
));
606 screenPosition
= StringUtils
.width(text
) - 1;
609 windowStart
= StringUtils
.width(text
) - getWidth() + 1;
610 if (windowStart
< 0) {
617 * Set the editing position. The field may adjust the window start to
618 * show as much of the field as possible.
620 * @param position the new position
621 * @throws IndexOutOfBoundsException if position is outside the range of
624 public void setPosition(final int position
) {
625 if ((position
< 0) || (position
>= text
.length())) {
626 throw new IndexOutOfBoundsException("Max length is " +
627 text
.length() + ", requested position " + position
);
629 this.position
= position
;
630 normalizeWindowStart();
634 * Set the active color key.
636 * @param activeColorKey ColorTheme key color to use when this field is
639 public void setActiveColorKey(final String activeColorKey
) {
640 this.activeColorKey
= activeColorKey
;
644 * Set the inactive color key.
646 * @param inactiveColorKey ColorTheme key color to use when this field is
649 public void setInactiveColorKey(final String inactiveColorKey
) {
650 this.inactiveColorKey
= inactiveColorKey
;
654 * Set the action to perform when the user presses enter.
656 * @param action the action to perform when the user presses enter
658 public void setEnterAction(final TAction action
) {
659 enterAction
= action
;
663 * Set the action to perform when the field is updated.
665 * @param action the action to perform when the field is updated
667 public void setUpdateAction(final TAction action
) {
668 updateAction
= action
;