2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2017 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
{
45 protected String text
= "";
52 public final String
getText() {
59 * @param text the new field text
61 public final void setText(String text
) {
68 * If true, only allow enough characters that will fit in the width. If
69 * false, allow the field to scroll to the right.
71 protected boolean fixed
= false;
74 * Current editing position within text.
76 protected int position
= 0;
79 * Beginning of visible portion.
81 protected int windowStart
= 0;
84 * If true, new characters are inserted at position.
86 protected boolean insertMode
= true;
89 * Remember mouse state.
91 protected TMouseEvent mouse
;
94 * The action to perform when the user presses enter.
96 protected TAction enterAction
;
99 * The action to perform when the text is updated.
101 protected TAction updateAction
;
104 * Public constructor.
106 * @param parent parent widget
107 * @param x column relative to parent
108 * @param y row relative to parent
109 * @param width visible text width
110 * @param fixed if true, the text cannot exceed the display width
112 public TField(final TWidget parent
, final int x
, final int y
,
113 final int width
, final boolean fixed
) {
115 this(parent
, x
, y
, width
, fixed
, "", null, null);
119 * Public constructor.
121 * @param parent parent widget
122 * @param x column relative to parent
123 * @param y row relative to parent
124 * @param width visible text width
125 * @param fixed if true, the text cannot exceed the display width
126 * @param text initial text, default is empty string
128 public TField(final TWidget parent
, final int x
, final int y
,
129 final int width
, final boolean fixed
, final String text
) {
131 this(parent
, x
, y
, width
, fixed
, text
, null, null);
135 * Public constructor.
137 * @param parent parent widget
138 * @param x column relative to parent
139 * @param y row relative to parent
140 * @param width visible text width
141 * @param fixed if true, the text cannot exceed the display width
142 * @param text initial text, default is empty string
143 * @param enterAction function to call when enter key is pressed
144 * @param updateAction function to call when the text is updated
146 public TField(final TWidget parent
, final int x
, final int y
,
147 final int width
, final boolean fixed
, final String text
,
148 final TAction enterAction
, final TAction updateAction
) {
150 // Set parent and window
151 super(parent
, x
, y
, width
, 1);
153 setCursorVisible(true);
156 this.enterAction
= enterAction
;
157 this.updateAction
= updateAction
;
161 * Returns true if the mouse is currently on the field.
163 * @return if true the mouse is currently on the field
165 protected boolean mouseOnField() {
166 int rightEdge
= getWidth() - 1;
168 && (mouse
.getY() == 0)
169 && (mouse
.getX() >= 0)
170 && (mouse
.getX() <= rightEdge
)
178 * Dispatch to the action function.
180 * @param enter if true, the user pressed Enter, else this was an update
183 protected void dispatch(final boolean enter
) {
185 if (enterAction
!= null) {
189 if (updateAction
!= null) {
196 * Draw the text field.
200 CellAttributes fieldColor
;
202 if (isAbsoluteActive()) {
203 fieldColor
= getTheme().getColor("tfield.active");
205 fieldColor
= getTheme().getColor("tfield.inactive");
208 int end
= windowStart
+ getWidth();
209 if (end
> text
.length()) {
212 getScreen().hLineXY(0, 0, getWidth(), GraphicsChars
.HATCH
, fieldColor
);
213 getScreen().putStringXY(0, 0, text
.substring(windowStart
, end
),
216 // Fix the cursor, it will be rendered by TApplication.drawAll().
221 * Update the visible cursor position to match the location of position
224 protected void updateCursor() {
225 if ((position
> getWidth()) && fixed
) {
226 setCursorX(getWidth());
227 } else if ((position
- windowStart
== getWidth()) && !fixed
) {
228 setCursorX(getWidth() - 1);
230 setCursorX(position
- windowStart
);
235 * Normalize windowStart such that most of the field data if visible.
237 protected void normalizeWindowStart() {
239 // windowStart had better be zero, there is nothing to do here.
240 assert (windowStart
== 0);
243 windowStart
= position
- (getWidth() - 1);
244 if (windowStart
< 0) {
252 * Handle mouse button presses.
254 * @param mouse mouse button event
257 public void onMouseDown(final TMouseEvent mouse
) {
260 if ((mouseOnField()) && (mouse
.isMouse1())) {
262 int deltaX
= mouse
.getX() - getCursorX();
264 if (position
> text
.length()) {
265 position
= text
.length();
275 * @param keypress keystroke event
278 public void onKeypress(final TKeypressEvent keypress
) {
280 if (keypress
.equals(kbLeft
)) {
284 if (fixed
== false) {
285 if ((position
== windowStart
) && (windowStart
> 0)) {
289 normalizeWindowStart();
293 if (keypress
.equals(kbRight
)) {
294 if (position
< text
.length()) {
297 if (position
== getWidth()) {
301 if ((position
- windowStart
) == getWidth()) {
309 if (keypress
.equals(kbEnter
)) {
314 if (keypress
.equals(kbIns
)) {
315 insertMode
= !insertMode
;
318 if (keypress
.equals(kbHome
)) {
324 if (keypress
.equals(kbEnd
)) {
325 position
= text
.length();
327 if (position
>= getWidth()) {
328 position
= text
.length() - 1;
331 windowStart
= text
.length() - getWidth() + 1;
332 if (windowStart
< 0) {
339 if (keypress
.equals(kbDel
)) {
340 if ((text
.length() > 0) && (position
< text
.length())) {
341 text
= text
.substring(0, position
)
342 + text
.substring(position
+ 1);
348 if (keypress
.equals(kbBackspace
) || keypress
.equals(kbBackspaceDel
)) {
351 text
= text
.substring(0, position
)
352 + text
.substring(position
+ 1);
354 if (fixed
== false) {
355 if ((position
== windowStart
)
362 normalizeWindowStart();
366 if (!keypress
.getKey().isFnKey()
367 && !keypress
.getKey().isAlt()
368 && !keypress
.getKey().isCtrl()
370 // Plain old keystroke, process it
371 if ((position
== text
.length())
372 && (text
.length() < getWidth())) {
375 appendChar(keypress
.getKey().getChar());
376 } else if ((position
< text
.length())
377 && (text
.length() < getWidth())) {
379 // Overwrite or insert a character
380 if (insertMode
== false) {
382 text
= text
.substring(0, position
)
383 + keypress
.getKey().getChar()
384 + text
.substring(position
+ 1);
388 insertChar(keypress
.getKey().getChar());
390 } else if ((position
< text
.length())
391 && (text
.length() >= getWidth())) {
393 // Multiple cases here
394 if ((fixed
== true) && (insertMode
== true)) {
395 // Buffer is full, do nothing
396 } else if ((fixed
== true) && (insertMode
== false)) {
397 // Overwrite the last character, maybe move position
398 text
= text
.substring(0, position
)
399 + keypress
.getKey().getChar()
400 + text
.substring(position
+ 1);
401 if (position
< getWidth() - 1) {
404 } else if ((fixed
== false) && (insertMode
== false)) {
405 // Overwrite the last character, definitely move position
406 text
= text
.substring(0, position
)
407 + keypress
.getKey().getChar()
408 + text
.substring(position
+ 1);
411 if (position
== text
.length()) {
412 // Append this character
413 appendChar(keypress
.getKey().getChar());
415 // Insert this character
416 insertChar(keypress
.getKey().getChar());
422 // Append this character
423 appendChar(keypress
.getKey().getChar());
429 // Pass to parent for the things we don't care about.
430 super.onKeypress(keypress
);
434 * Append char to the end of the field.
436 * @param ch = char to append
438 protected void appendChar(final char ch
) {
439 // Append the LAST character
443 assert (position
== text
.length());
446 if (position
== getWidth()) {
450 if ((position
- windowStart
) == getWidth()) {
457 * Insert char somewhere in the middle of the field.
459 * @param ch char to append
461 protected void insertChar(final char ch
) {
462 text
= text
.substring(0, position
) + ch
+ text
.substring(position
);
464 if ((position
- windowStart
) == getWidth()) {