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
.event
.TKeypressEvent
;
34 import jexer
.event
.TMouseEvent
;
35 import static jexer
.TKeypress
.*;
38 * TField implements an editable text field.
40 public class TField
extends TWidget
{
42 // ------------------------------------------------------------------------
43 // Variables --------------------------------------------------------------
44 // ------------------------------------------------------------------------
47 * Background character for unfilled-in text.
49 protected char backgroundChar
= GraphicsChars
.HATCH
;
54 protected String text
= "";
57 * If true, only allow enough characters that will fit in the width. If
58 * false, allow the field to scroll to the right.
60 protected boolean fixed
= false;
63 * Current editing position within text.
65 protected int position
= 0;
68 * Beginning of visible portion.
70 protected int windowStart
= 0;
73 * If true, new characters are inserted at position.
75 protected boolean insertMode
= true;
78 * Remember mouse state.
80 protected TMouseEvent mouse
;
83 * The action to perform when the user presses enter.
85 protected TAction enterAction
;
88 * The action to perform when the text is updated.
90 protected TAction updateAction
;
93 * The color to use when this field is active.
95 private String activeColorKey
= "tfield.active";
98 * The color to use when this field is not active.
100 private String inactiveColorKey
= "tfield.inactive";
102 // ------------------------------------------------------------------------
103 // Constructors -----------------------------------------------------------
104 // ------------------------------------------------------------------------
107 * Public constructor.
109 * @param parent parent widget
110 * @param x column relative to parent
111 * @param y row relative to parent
112 * @param width visible text width
113 * @param fixed if true, the text cannot exceed the display width
115 public TField(final TWidget parent
, final int x
, final int y
,
116 final int width
, final boolean fixed
) {
118 this(parent
, x
, y
, width
, fixed
, "", null, null);
122 * Public constructor.
124 * @param parent parent widget
125 * @param x column relative to parent
126 * @param y row relative to parent
127 * @param width visible text width
128 * @param fixed if true, the text cannot exceed the display width
129 * @param text initial text, default is empty string
131 public TField(final TWidget parent
, final int x
, final int y
,
132 final int width
, final boolean fixed
, final String text
) {
134 this(parent
, x
, y
, width
, fixed
, text
, null, null);
138 * Public constructor.
140 * @param parent parent widget
141 * @param x column relative to parent
142 * @param y row relative to parent
143 * @param width visible text width
144 * @param fixed if true, the text cannot exceed the display width
145 * @param text initial text, default is empty string
146 * @param enterAction function to call when enter key is pressed
147 * @param updateAction function to call when the text is updated
149 public TField(final TWidget parent
, final int x
, final int y
,
150 final int width
, final boolean fixed
, final String text
,
151 final TAction enterAction
, final TAction updateAction
) {
153 // Set parent and window
154 super(parent
, x
, y
, width
, 1);
156 setCursorVisible(true);
159 this.enterAction
= enterAction
;
160 this.updateAction
= updateAction
;
163 // ------------------------------------------------------------------------
164 // Event handlers ---------------------------------------------------------
165 // ------------------------------------------------------------------------
168 * Returns true if the mouse is currently on the field.
170 * @return if true the mouse is currently on the field
172 protected boolean mouseOnField() {
173 int rightEdge
= getWidth() - 1;
175 && (mouse
.getY() == 0)
176 && (mouse
.getX() >= 0)
177 && (mouse
.getX() <= rightEdge
)
185 * Handle mouse button presses.
187 * @param mouse mouse button event
190 public void onMouseDown(final TMouseEvent mouse
) {
193 if ((mouseOnField()) && (mouse
.isMouse1())) {
195 int deltaX
= mouse
.getX() - getCursorX();
197 if (position
> text
.length()) {
198 position
= text
.length();
208 * @param keypress keystroke event
211 public void onKeypress(final TKeypressEvent keypress
) {
213 if (keypress
.equals(kbLeft
)) {
217 if (fixed
== false) {
218 if ((position
== windowStart
) && (windowStart
> 0)) {
222 normalizeWindowStart();
226 if (keypress
.equals(kbRight
)) {
227 if (position
< text
.length()) {
230 if (position
== getWidth()) {
234 if ((position
- windowStart
) == getWidth()) {
242 if (keypress
.equals(kbEnter
)) {
247 if (keypress
.equals(kbIns
)) {
248 insertMode
= !insertMode
;
251 if (keypress
.equals(kbHome
)) {
256 if (keypress
.equals(kbEnd
)) {
261 if (keypress
.equals(kbDel
)) {
262 if ((text
.length() > 0) && (position
< text
.length())) {
263 text
= text
.substring(0, position
)
264 + text
.substring(position
+ 1);
270 if (keypress
.equals(kbBackspace
) || keypress
.equals(kbBackspaceDel
)) {
273 text
= text
.substring(0, position
)
274 + text
.substring(position
+ 1);
276 if (fixed
== false) {
277 if ((position
== windowStart
)
284 normalizeWindowStart();
288 if (!keypress
.getKey().isFnKey()
289 && !keypress
.getKey().isAlt()
290 && !keypress
.getKey().isCtrl()
292 // Plain old keystroke, process it
293 if ((position
== text
.length())
294 && (text
.length() < getWidth())) {
297 appendChar(keypress
.getKey().getChar());
298 } else if ((position
< text
.length())
299 && (text
.length() < getWidth())) {
301 // Overwrite or insert a character
302 if (insertMode
== false) {
304 text
= text
.substring(0, position
)
305 + keypress
.getKey().getChar()
306 + text
.substring(position
+ 1);
310 insertChar(keypress
.getKey().getChar());
312 } else if ((position
< text
.length())
313 && (text
.length() >= getWidth())) {
315 // Multiple cases here
316 if ((fixed
== true) && (insertMode
== true)) {
317 // Buffer is full, do nothing
318 } else if ((fixed
== true) && (insertMode
== false)) {
319 // Overwrite the last character, maybe move position
320 text
= text
.substring(0, position
)
321 + keypress
.getKey().getChar()
322 + text
.substring(position
+ 1);
323 if (position
< getWidth() - 1) {
326 } else if ((fixed
== false) && (insertMode
== false)) {
327 // Overwrite the last character, definitely move position
328 text
= text
.substring(0, position
)
329 + keypress
.getKey().getChar()
330 + text
.substring(position
+ 1);
333 if (position
== text
.length()) {
334 // Append this character
335 appendChar(keypress
.getKey().getChar());
337 // Insert this character
338 insertChar(keypress
.getKey().getChar());
344 // Append this character
345 appendChar(keypress
.getKey().getChar());
351 // Pass to parent for the things we don't care about.
352 super.onKeypress(keypress
);
355 // ------------------------------------------------------------------------
356 // TWidget ----------------------------------------------------------------
357 // ------------------------------------------------------------------------
360 * Draw the text field.
364 CellAttributes fieldColor
;
366 if (isAbsoluteActive()) {
367 fieldColor
= getTheme().getColor(activeColorKey
);
369 fieldColor
= getTheme().getColor(inactiveColorKey
);
372 int end
= windowStart
+ getWidth();
373 if (end
> text
.length()) {
376 hLineXY(0, 0, getWidth(), backgroundChar
, fieldColor
);
377 putStringXY(0, 0, text
.substring(windowStart
, end
), fieldColor
);
379 // Fix the cursor, it will be rendered by TApplication.drawAll().
383 // ------------------------------------------------------------------------
384 // TField -----------------------------------------------------------------
385 // ------------------------------------------------------------------------
388 * Get field background character.
390 * @return background character
392 public final char getBackgroundChar() {
393 return backgroundChar
;
397 * Set field background character.
399 * @param backgroundChar the background character
401 public void setBackgroundChar(final char backgroundChar
) {
402 this.backgroundChar
= backgroundChar
;
410 public final String
getText() {
417 * @param text the new field text
419 public void setText(final String text
) {
420 assert (text
!= null);
427 * Dispatch to the action function.
429 * @param enter if true, the user pressed Enter, else this was an update
432 protected void dispatch(final boolean enter
) {
434 if (enterAction
!= null) {
438 if (updateAction
!= null) {
445 * Update the visible cursor position to match the location of position
448 protected void updateCursor() {
449 if ((position
> getWidth()) && fixed
) {
450 setCursorX(getWidth());
451 } else if ((position
- windowStart
== getWidth()) && !fixed
) {
452 setCursorX(getWidth() - 1);
454 setCursorX(position
- windowStart
);
459 * Normalize windowStart such that most of the field data if visible.
461 protected void normalizeWindowStart() {
463 // windowStart had better be zero, there is nothing to do here.
464 assert (windowStart
== 0);
467 windowStart
= position
- (getWidth() - 1);
468 if (windowStart
< 0) {
476 * Append char to the end of the field.
478 * @param ch = char to append
480 protected void appendChar(final char ch
) {
481 // Append the LAST character
485 assert (position
== text
.length());
488 if (position
== getWidth()) {
492 if ((position
- windowStart
) == getWidth()) {
499 * Insert char somewhere in the middle of the field.
501 * @param ch char to append
503 protected void insertChar(final char ch
) {
504 text
= text
.substring(0, position
) + ch
+ text
.substring(position
);
506 if ((position
- windowStart
) == getWidth()) {
513 * Position the cursor at the first column. The field may adjust the
514 * window start to show as much of the field as possible.
522 * Set the editing position to the last filled character. The field may
523 * adjust the window start to show as much of the field as possible.
526 position
= text
.length();
528 if (position
>= getWidth()) {
529 position
= text
.length() - 1;
532 windowStart
= text
.length() - getWidth() + 1;
533 if (windowStart
< 0) {
540 * Set the editing position. The field may adjust the window start to
541 * show as much of the field as possible.
543 * @param position the new position
544 * @throws IndexOutOfBoundsException if position is outside the range of
547 public void setPosition(final int position
) {
548 if ((position
< 0) || (position
>= text
.length())) {
549 throw new IndexOutOfBoundsException("Max length is " +
550 text
.length() + ", requested position " + position
);
552 this.position
= position
;
553 normalizeWindowStart();
557 * Set the active color key.
559 * @param activeColorKey ColorTheme key color to use when this field is
562 public void setActiveColorKey(final String activeColorKey
) {
563 this.activeColorKey
= activeColorKey
;
567 * Set the inactive color key.
569 * @param inactiveColorKey ColorTheme key color to use when this field is
572 public void setInactiveColorKey(final String inactiveColorKey
) {
573 this.inactiveColorKey
= inactiveColorKey
;