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 cursor position.
223 protected void updateCursor() {
224 if ((position
> getWidth()) && fixed
) {
225 setCursorX(getWidth());
226 } else if ((position
- windowStart
== getWidth()) && !fixed
) {
227 setCursorX(getWidth() - 1);
229 setCursorX(position
- windowStart
);
234 * Handle mouse button presses.
236 * @param mouse mouse button event
239 public void onMouseDown(final TMouseEvent mouse
) {
242 if ((mouseOnField()) && (mouse
.isMouse1())) {
244 int deltaX
= mouse
.getX() - getCursorX();
246 if (position
> text
.length()) {
247 position
= text
.length();
257 * @param keypress keystroke event
260 public void onKeypress(final TKeypressEvent keypress
) {
262 if (keypress
.equals(kbLeft
)) {
266 if (fixed
== false) {
267 if ((position
== windowStart
) && (windowStart
> 0)) {
274 if (keypress
.equals(kbRight
)) {
275 if (position
< text
.length()) {
278 if (position
== getWidth()) {
282 if ((position
- windowStart
) == getWidth()) {
290 if (keypress
.equals(kbEnter
)) {
295 if (keypress
.equals(kbIns
)) {
296 insertMode
= !insertMode
;
299 if (keypress
.equals(kbHome
)) {
305 if (keypress
.equals(kbEnd
)) {
306 position
= text
.length();
308 if (position
>= getWidth()) {
309 position
= text
.length() - 1;
312 windowStart
= text
.length() - getWidth() + 1;
313 if (windowStart
< 0) {
320 if (keypress
.equals(kbDel
)) {
321 if ((text
.length() > 0) && (position
< text
.length())) {
322 text
= text
.substring(0, position
)
323 + text
.substring(position
+ 1);
328 if (keypress
.equals(kbBackspace
) || keypress
.equals(kbBackspaceDel
)) {
331 text
= text
.substring(0, position
)
332 + text
.substring(position
+ 1);
334 if (fixed
== false) {
335 if ((position
== windowStart
)
345 if (!keypress
.getKey().isFnKey()
346 && !keypress
.getKey().isAlt()
347 && !keypress
.getKey().isCtrl()
349 // Plain old keystroke, process it
350 if ((position
== text
.length())
351 && (text
.length() < getWidth())) {
354 appendChar(keypress
.getKey().getChar());
355 } else if ((position
< text
.length())
356 && (text
.length() < getWidth())) {
358 // Overwrite or insert a character
359 if (insertMode
== false) {
361 text
= text
.substring(0, position
)
362 + keypress
.getKey().getChar()
363 + text
.substring(position
+ 1);
367 insertChar(keypress
.getKey().getChar());
369 } else if ((position
< text
.length())
370 && (text
.length() >= getWidth())) {
372 // Multiple cases here
373 if ((fixed
== true) && (insertMode
== true)) {
374 // Buffer is full, do nothing
375 } else if ((fixed
== true) && (insertMode
== false)) {
376 // Overwrite the last character, maybe move position
377 text
= text
.substring(0, position
)
378 + keypress
.getKey().getChar()
379 + text
.substring(position
+ 1);
380 if (position
< getWidth() - 1) {
383 } else if ((fixed
== false) && (insertMode
== false)) {
384 // Overwrite the last character, definitely move position
385 text
= text
.substring(0, position
)
386 + keypress
.getKey().getChar()
387 + text
.substring(position
+ 1);
390 if (position
== text
.length()) {
391 // Append this character
392 appendChar(keypress
.getKey().getChar());
394 // Insert this character
395 insertChar(keypress
.getKey().getChar());
401 // Append this character
402 appendChar(keypress
.getKey().getChar());
408 // Pass to parent for the things we don't care about.
409 super.onKeypress(keypress
);
413 * Append char to the end of the field.
415 * @param ch = char to append
417 protected void appendChar(final char ch
) {
418 // Append the LAST character
422 assert (position
== text
.length());
425 if (position
== getWidth()) {
429 if ((position
- windowStart
) == getWidth()) {
436 * Insert char somewhere in the middle of the field.
438 * @param ch char to append
440 protected void insertChar(final char ch
) {
441 text
= text
.substring(0, position
) + ch
+ text
.substring(position
);
443 if ((position
- windowStart
) == getWidth()) {