Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
128e5be1 KL |
2 | * Jexer - Java Text User Interface |
3 | * | |
e16dda65 | 4 | * The MIT License (MIT) |
128e5be1 | 5 | * |
a69ed767 | 6 | * Copyright (C) 2019 Kevin Lamonte |
128e5be1 | 7 | * |
e16dda65 KL |
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: | |
128e5be1 | 14 | * |
e16dda65 KL |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
128e5be1 | 17 | * |
e16dda65 KL |
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. | |
128e5be1 KL |
25 | * |
26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
27 | * @version 1 | |
28 | */ | |
29 | package jexer; | |
30 | ||
31 | import jexer.bits.CellAttributes; | |
32 | import jexer.bits.GraphicsChars; | |
58c6a100 | 33 | import jexer.bits.StringUtils; |
128e5be1 KL |
34 | import jexer.event.TKeypressEvent; |
35 | import jexer.event.TMouseEvent; | |
36 | import static jexer.TKeypress.*; | |
37 | ||
38 | /** | |
87a17f3c | 39 | * TField implements an editable text field. |
128e5be1 | 40 | */ |
87a17f3c | 41 | public class TField extends TWidget { |
128e5be1 | 42 | |
d36057df KL |
43 | // ------------------------------------------------------------------------ |
44 | // Variables -------------------------------------------------------------- | |
45 | // ------------------------------------------------------------------------ | |
46 | ||
1dac6b8d KL |
47 | /** |
48 | * Background character for unfilled-in text. | |
49 | */ | |
afdec5e9 | 50 | protected int backgroundChar = GraphicsChars.HATCH; |
1dac6b8d | 51 | |
128e5be1 KL |
52 | /** |
53 | * Field text. | |
54 | */ | |
87a17f3c | 55 | protected String text = ""; |
128e5be1 | 56 | |
128e5be1 KL |
57 | /** |
58 | * If true, only allow enough characters that will fit in the width. If | |
59 | * false, allow the field to scroll to the right. | |
60 | */ | |
87a17f3c | 61 | protected boolean fixed = false; |
128e5be1 KL |
62 | |
63 | /** | |
64 | * Current editing position within text. | |
65 | */ | |
87a17f3c | 66 | protected int position = 0; |
128e5be1 | 67 | |
58c6a100 KL |
68 | /** |
69 | * Current editing position screen column number. | |
70 | */ | |
71 | protected int screenPosition = 0; | |
72 | ||
128e5be1 KL |
73 | /** |
74 | * Beginning of visible portion. | |
75 | */ | |
87a17f3c | 76 | protected int windowStart = 0; |
128e5be1 KL |
77 | |
78 | /** | |
79 | * If true, new characters are inserted at position. | |
80 | */ | |
87a17f3c | 81 | protected boolean insertMode = true; |
128e5be1 KL |
82 | |
83 | /** | |
84 | * Remember mouse state. | |
85 | */ | |
87a17f3c | 86 | protected TMouseEvent mouse; |
128e5be1 KL |
87 | |
88 | /** | |
89 | * The action to perform when the user presses enter. | |
90 | */ | |
87a17f3c | 91 | protected TAction enterAction; |
128e5be1 KL |
92 | |
93 | /** | |
94 | * The action to perform when the text is updated. | |
95 | */ | |
87a17f3c | 96 | protected TAction updateAction; |
128e5be1 | 97 | |
1dac6b8d KL |
98 | /** |
99 | * The color to use when this field is active. | |
100 | */ | |
101 | private String activeColorKey = "tfield.active"; | |
102 | ||
103 | /** | |
104 | * The color to use when this field is not active. | |
105 | */ | |
106 | private String inactiveColorKey = "tfield.inactive"; | |
107 | ||
d36057df KL |
108 | // ------------------------------------------------------------------------ |
109 | // Constructors ----------------------------------------------------------- | |
110 | // ------------------------------------------------------------------------ | |
111 | ||
128e5be1 KL |
112 | /** |
113 | * Public constructor. | |
114 | * | |
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 | |
120 | */ | |
121 | public TField(final TWidget parent, final int x, final int y, | |
122 | final int width, final boolean fixed) { | |
123 | ||
124 | this(parent, x, y, width, fixed, "", null, null); | |
125 | } | |
126 | ||
127 | /** | |
128 | * Public constructor. | |
129 | * | |
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 | |
136 | */ | |
137 | public TField(final TWidget parent, final int x, final int y, | |
138 | final int width, final boolean fixed, final String text) { | |
139 | ||
140 | this(parent, x, y, width, fixed, text, null, null); | |
141 | } | |
142 | ||
143 | /** | |
144 | * Public constructor. | |
145 | * | |
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 | |
154 | */ | |
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) { | |
158 | ||
159 | // Set parent and window | |
a83fea2b | 160 | super(parent, x, y, width, 1); |
128e5be1 | 161 | |
7c870d89 | 162 | setCursorVisible(true); |
128e5be1 KL |
163 | this.fixed = fixed; |
164 | this.text = text; | |
165 | this.enterAction = enterAction; | |
166 | this.updateAction = updateAction; | |
167 | } | |
168 | ||
d36057df KL |
169 | // ------------------------------------------------------------------------ |
170 | // Event handlers --------------------------------------------------------- | |
171 | // ------------------------------------------------------------------------ | |
172 | ||
128e5be1 KL |
173 | /** |
174 | * Returns true if the mouse is currently on the field. | |
175 | * | |
176 | * @return if true the mouse is currently on the field | |
177 | */ | |
87a17f3c | 178 | protected boolean mouseOnField() { |
128e5be1 KL |
179 | int rightEdge = getWidth() - 1; |
180 | if ((mouse != null) | |
181 | && (mouse.getY() == 0) | |
182 | && (mouse.getX() >= 0) | |
183 | && (mouse.getX() <= rightEdge) | |
184 | ) { | |
185 | return true; | |
186 | } | |
187 | return false; | |
188 | } | |
189 | ||
128e5be1 KL |
190 | /** |
191 | * Handle mouse button presses. | |
192 | * | |
193 | * @param mouse mouse button event | |
194 | */ | |
195 | @Override | |
196 | public void onMouseDown(final TMouseEvent mouse) { | |
197 | this.mouse = mouse; | |
198 | ||
7c870d89 | 199 | if ((mouseOnField()) && (mouse.isMouse1())) { |
128e5be1 KL |
200 | // Move cursor |
201 | int deltaX = mouse.getX() - getCursorX(); | |
58c6a100 KL |
202 | screenPosition += deltaX; |
203 | if (screenPosition > StringUtils.width(text)) { | |
204 | screenPosition = StringUtils.width(text); | |
128e5be1 | 205 | } |
58c6a100 | 206 | position = screenToTextPosition(screenPosition); |
128e5be1 KL |
207 | updateCursor(); |
208 | return; | |
209 | } | |
210 | } | |
211 | ||
212 | /** | |
213 | * Handle keystrokes. | |
214 | * | |
215 | * @param keypress keystroke event | |
216 | */ | |
217 | @Override | |
218 | public void onKeypress(final TKeypressEvent keypress) { | |
219 | ||
220 | if (keypress.equals(kbLeft)) { | |
221 | if (position > 0) { | |
6b2633ac KL |
222 | if (position < codePointLength(text)) { |
223 | screenPosition -= StringUtils.width(text.codePointBefore(position)); | |
224 | position -= Character.charCount(text.codePointBefore(position)); | |
58c6a100 KL |
225 | } else { |
226 | screenPosition--; | |
6b2633ac | 227 | position--; |
58c6a100 | 228 | } |
128e5be1 KL |
229 | } |
230 | if (fixed == false) { | |
58c6a100 | 231 | if ((screenPosition == windowStart) && (windowStart > 0)) { |
84a34fdf | 232 | windowStart -= StringUtils.width(text.codePointAt( |
58c6a100 | 233 | screenToTextPosition(windowStart))); |
128e5be1 KL |
234 | } |
235 | } | |
24489803 | 236 | normalizeWindowStart(); |
128e5be1 KL |
237 | return; |
238 | } | |
239 | ||
240 | if (keypress.equals(kbRight)) { | |
6b2633ac | 241 | if (position < codePointLength(text)) { |
84a34fdf | 242 | screenPosition += StringUtils.width(text.codePointAt(position)); |
6b2633ac | 243 | position += Character.charCount(text.codePointAt(position)); |
128e5be1 | 244 | if (fixed == true) { |
58c6a100 KL |
245 | if (screenPosition == getWidth()) { |
246 | screenPosition--; | |
6b2633ac | 247 | position -= Character.charCount(text.codePointAt(position)); |
128e5be1 KL |
248 | } |
249 | } else { | |
58c6a100 | 250 | if ((screenPosition - windowStart) >= getWidth()) { |
84a34fdf | 251 | windowStart += StringUtils.width(text.codePointAt( |
58c6a100 | 252 | screenToTextPosition(windowStart))); |
128e5be1 KL |
253 | } |
254 | } | |
255 | } | |
256 | return; | |
257 | } | |
258 | ||
259 | if (keypress.equals(kbEnter)) { | |
260 | dispatch(true); | |
261 | return; | |
262 | } | |
263 | ||
264 | if (keypress.equals(kbIns)) { | |
265 | insertMode = !insertMode; | |
266 | return; | |
267 | } | |
268 | if (keypress.equals(kbHome)) { | |
1b120607 | 269 | home(); |
128e5be1 KL |
270 | return; |
271 | } | |
272 | ||
273 | if (keypress.equals(kbEnd)) { | |
1b120607 | 274 | end(); |
128e5be1 KL |
275 | return; |
276 | } | |
277 | ||
278 | if (keypress.equals(kbDel)) { | |
6b2633ac | 279 | if ((codePointLength(text) > 0) && (position < codePointLength(text))) { |
128e5be1 KL |
280 | text = text.substring(0, position) |
281 | + text.substring(position + 1); | |
58c6a100 | 282 | screenPosition = StringUtils.width(text.substring(0, position)); |
128e5be1 | 283 | } |
24489803 | 284 | dispatch(false); |
128e5be1 KL |
285 | return; |
286 | } | |
287 | ||
288 | if (keypress.equals(kbBackspace) || keypress.equals(kbBackspaceDel)) { | |
289 | if (position > 0) { | |
6b2633ac | 290 | position -= Character.charCount(text.codePointBefore(position)); |
128e5be1 KL |
291 | text = text.substring(0, position) |
292 | + text.substring(position + 1); | |
58c6a100 | 293 | screenPosition = StringUtils.width(text.substring(0, position)); |
128e5be1 KL |
294 | } |
295 | if (fixed == false) { | |
58c6a100 | 296 | if ((screenPosition >= windowStart) |
128e5be1 KL |
297 | && (windowStart > 0) |
298 | ) { | |
84a34fdf | 299 | windowStart -= StringUtils.width(text.codePointAt( |
58c6a100 | 300 | screenToTextPosition(windowStart))); |
128e5be1 KL |
301 | } |
302 | } | |
303 | dispatch(false); | |
24489803 | 304 | normalizeWindowStart(); |
128e5be1 KL |
305 | return; |
306 | } | |
307 | ||
7c870d89 KL |
308 | if (!keypress.getKey().isFnKey() |
309 | && !keypress.getKey().isAlt() | |
310 | && !keypress.getKey().isCtrl() | |
128e5be1 KL |
311 | ) { |
312 | // Plain old keystroke, process it | |
6b2633ac | 313 | if ((position == codePointLength(text)) |
58c6a100 | 314 | && (StringUtils.width(text) < getWidth())) { |
128e5be1 KL |
315 | |
316 | // Append case | |
7c870d89 | 317 | appendChar(keypress.getKey().getChar()); |
6b2633ac | 318 | } else if ((position < codePointLength(text)) |
58c6a100 | 319 | && (StringUtils.width(text) < getWidth())) { |
128e5be1 KL |
320 | |
321 | // Overwrite or insert a character | |
322 | if (insertMode == false) { | |
323 | // Replace character | |
324 | text = text.substring(0, position) | |
84a34fdf | 325 | + codePointString(keypress.getKey().getChar()) |
128e5be1 | 326 | + text.substring(position + 1); |
84a34fdf KL |
327 | screenPosition += StringUtils.width(text.codePointAt(position)); |
328 | position += Character.charCount(keypress.getKey().getChar()); | |
128e5be1 KL |
329 | } else { |
330 | // Insert character | |
7c870d89 | 331 | insertChar(keypress.getKey().getChar()); |
128e5be1 | 332 | } |
6b2633ac | 333 | } else if ((position < codePointLength(text)) |
58c6a100 | 334 | && (StringUtils.width(text) >= getWidth())) { |
128e5be1 KL |
335 | |
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) | |
84a34fdf | 342 | + codePointString(keypress.getKey().getChar()) |
128e5be1 | 343 | + text.substring(position + 1); |
58c6a100 | 344 | if (screenPosition < getWidth() - 1) { |
84a34fdf KL |
345 | screenPosition += StringUtils.width(text.codePointAt(position)); |
346 | position += Character.charCount(keypress.getKey().getChar()); | |
128e5be1 KL |
347 | } |
348 | } else if ((fixed == false) && (insertMode == false)) { | |
349 | // Overwrite the last character, definitely move position | |
350 | text = text.substring(0, position) | |
84a34fdf | 351 | + codePointString(keypress.getKey().getChar()) |
128e5be1 | 352 | + text.substring(position + 1); |
84a34fdf KL |
353 | screenPosition += StringUtils.width(text.codePointAt(position)); |
354 | position += Character.charCount(keypress.getKey().getChar()); | |
128e5be1 | 355 | } else { |
6b2633ac | 356 | if (position == codePointLength(text)) { |
128e5be1 | 357 | // Append this character |
7c870d89 | 358 | appendChar(keypress.getKey().getChar()); |
128e5be1 KL |
359 | } else { |
360 | // Insert this character | |
7c870d89 | 361 | insertChar(keypress.getKey().getChar()); |
128e5be1 KL |
362 | } |
363 | } | |
364 | } else { | |
365 | assert (!fixed); | |
366 | ||
367 | // Append this character | |
7c870d89 | 368 | appendChar(keypress.getKey().getChar()); |
128e5be1 KL |
369 | } |
370 | dispatch(false); | |
371 | return; | |
372 | } | |
373 | ||
374 | // Pass to parent for the things we don't care about. | |
375 | super.onKeypress(keypress); | |
376 | } | |
377 | ||
d36057df KL |
378 | // ------------------------------------------------------------------------ |
379 | // TWidget ---------------------------------------------------------------- | |
380 | // ------------------------------------------------------------------------ | |
381 | ||
382 | /** | |
383 | * Draw the text field. | |
384 | */ | |
385 | @Override | |
386 | public void draw() { | |
387 | CellAttributes fieldColor; | |
388 | ||
389 | if (isAbsoluteActive()) { | |
1dac6b8d | 390 | fieldColor = getTheme().getColor(activeColorKey); |
d36057df | 391 | } else { |
1dac6b8d | 392 | fieldColor = getTheme().getColor(inactiveColorKey); |
d36057df KL |
393 | } |
394 | ||
395 | int end = windowStart + getWidth(); | |
58c6a100 KL |
396 | if (end > StringUtils.width(text)) { |
397 | end = StringUtils.width(text); | |
d36057df | 398 | } |
1dac6b8d | 399 | hLineXY(0, 0, getWidth(), backgroundChar, fieldColor); |
58c6a100 KL |
400 | putStringXY(0, 0, text.substring(screenToTextPosition(windowStart), |
401 | screenToTextPosition(end)), fieldColor); | |
d36057df KL |
402 | |
403 | // Fix the cursor, it will be rendered by TApplication.drawAll(). | |
404 | updateCursor(); | |
405 | } | |
406 | ||
407 | // ------------------------------------------------------------------------ | |
408 | // TField ----------------------------------------------------------------- | |
409 | // ------------------------------------------------------------------------ | |
410 | ||
84a34fdf KL |
411 | /** |
412 | * Convert a char (codepoint) to a string. | |
413 | * | |
414 | * @param ch the char | |
415 | * @return the string | |
416 | */ | |
417 | private String codePointString(final int ch) { | |
418 | StringBuilder sb = new StringBuilder(1); | |
419 | sb.append(Character.toChars(ch)); | |
420 | return sb.toString(); | |
421 | } | |
422 | ||
6b2633ac KL |
423 | /** |
424 | * Get the number of codepoints in a string. | |
425 | * | |
426 | * @param str the string | |
427 | * @return the number of codepoints | |
428 | */ | |
429 | private int codePointLength(final String str) { | |
430 | return str.codePointCount(0, str.length()); | |
431 | } | |
432 | ||
1dac6b8d KL |
433 | /** |
434 | * Get field background character. | |
435 | * | |
436 | * @return background character | |
437 | */ | |
afdec5e9 | 438 | public final int getBackgroundChar() { |
1dac6b8d KL |
439 | return backgroundChar; |
440 | } | |
441 | ||
442 | /** | |
443 | * Set field background character. | |
444 | * | |
445 | * @param backgroundChar the background character | |
446 | */ | |
afdec5e9 | 447 | public void setBackgroundChar(final int backgroundChar) { |
1dac6b8d KL |
448 | this.backgroundChar = backgroundChar; |
449 | } | |
450 | ||
d36057df KL |
451 | /** |
452 | * Get field text. | |
453 | * | |
454 | * @return field text | |
455 | */ | |
456 | public final String getText() { | |
457 | return text; | |
458 | } | |
459 | ||
460 | /** | |
461 | * Set field text. | |
462 | * | |
463 | * @param text the new field text | |
464 | */ | |
051e2913 | 465 | public void setText(final String text) { |
a69ed767 | 466 | assert (text != null); |
d36057df KL |
467 | this.text = text; |
468 | position = 0; | |
469 | windowStart = 0; | |
470 | } | |
471 | ||
472 | /** | |
473 | * Dispatch to the action function. | |
474 | * | |
475 | * @param enter if true, the user pressed Enter, else this was an update | |
476 | * to the text. | |
477 | */ | |
478 | protected void dispatch(final boolean enter) { | |
479 | if (enter) { | |
480 | if (enterAction != null) { | |
481 | enterAction.DO(); | |
482 | } | |
483 | } else { | |
484 | if (updateAction != null) { | |
485 | updateAction.DO(); | |
486 | } | |
487 | } | |
488 | } | |
489 | ||
58c6a100 KL |
490 | /** |
491 | * Determine string position from screen position. | |
492 | * | |
493 | * @param screenPosition the position on screen | |
494 | * @return the equivalent position in text | |
495 | */ | |
496 | protected int screenToTextPosition(final int screenPosition) { | |
497 | if (screenPosition == 0) { | |
498 | return 0; | |
499 | } | |
500 | ||
501 | int n = 0; | |
6b2633ac | 502 | for (int i = 0; i < codePointLength(text); i++) { |
84a34fdf | 503 | n += StringUtils.width(text.codePointAt(i)); |
58c6a100 KL |
504 | if (n >= screenPosition) { |
505 | return i + 1; | |
506 | } | |
507 | } | |
508 | // screenPosition exceeds the available text length. | |
509 | throw new IndexOutOfBoundsException("screenPosition " + screenPosition + | |
6b2633ac | 510 | " exceeds available text length " + codePointLength(text)); |
58c6a100 KL |
511 | } |
512 | ||
d36057df KL |
513 | /** |
514 | * Update the visible cursor position to match the location of position | |
515 | * and windowStart. | |
516 | */ | |
517 | protected void updateCursor() { | |
58c6a100 | 518 | if ((screenPosition > getWidth()) && fixed) { |
d36057df | 519 | setCursorX(getWidth()); |
58c6a100 | 520 | } else if ((screenPosition - windowStart >= getWidth()) && !fixed) { |
d36057df KL |
521 | setCursorX(getWidth() - 1); |
522 | } else { | |
58c6a100 | 523 | setCursorX(screenPosition - windowStart); |
d36057df KL |
524 | } |
525 | } | |
526 | ||
527 | /** | |
528 | * Normalize windowStart such that most of the field data if visible. | |
529 | */ | |
530 | protected void normalizeWindowStart() { | |
531 | if (fixed) { | |
532 | // windowStart had better be zero, there is nothing to do here. | |
533 | assert (windowStart == 0); | |
534 | return; | |
535 | } | |
58c6a100 | 536 | windowStart = screenPosition - (getWidth() - 1); |
d36057df KL |
537 | if (windowStart < 0) { |
538 | windowStart = 0; | |
539 | } | |
540 | ||
541 | updateCursor(); | |
542 | } | |
543 | ||
128e5be1 KL |
544 | /** |
545 | * Append char to the end of the field. | |
546 | * | |
afdec5e9 | 547 | * @param ch char to append |
128e5be1 | 548 | */ |
afdec5e9 | 549 | protected void appendChar(final int ch) { |
128e5be1 | 550 | // Append the LAST character |
84a34fdf KL |
551 | text += codePointString(ch); |
552 | position += Character.charCount(ch); | |
58c6a100 | 553 | screenPosition += StringUtils.width(ch); |
128e5be1 | 554 | |
6b2633ac | 555 | assert (position == codePointLength(text)); |
128e5be1 KL |
556 | |
557 | if (fixed) { | |
58c6a100 | 558 | if (screenPosition >= getWidth()) { |
84a34fdf | 559 | position -= Character.charCount(ch); |
58c6a100 | 560 | screenPosition -= StringUtils.width(ch); |
128e5be1 KL |
561 | } |
562 | } else { | |
58c6a100 | 563 | if ((screenPosition - windowStart) >= getWidth()) { |
128e5be1 KL |
564 | windowStart++; |
565 | } | |
566 | } | |
567 | } | |
568 | ||
569 | /** | |
570 | * Insert char somewhere in the middle of the field. | |
571 | * | |
572 | * @param ch char to append | |
573 | */ | |
afdec5e9 | 574 | protected void insertChar(final int ch) { |
84a34fdf KL |
575 | text = text.substring(0, position) + codePointString(ch) |
576 | + text.substring(position); | |
577 | position += Character.charCount(ch); | |
58c6a100 KL |
578 | screenPosition += StringUtils.width(ch); |
579 | if ((screenPosition - windowStart) == getWidth()) { | |
128e5be1 KL |
580 | assert (!fixed); |
581 | windowStart++; | |
582 | } | |
583 | } | |
584 | ||
1b120607 KL |
585 | /** |
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. | |
588 | */ | |
589 | public void home() { | |
590 | position = 0; | |
58c6a100 | 591 | screenPosition = 0; |
1b120607 KL |
592 | windowStart = 0; |
593 | } | |
594 | ||
595 | /** | |
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. | |
598 | */ | |
599 | public void end() { | |
6b2633ac | 600 | position = codePointLength(text); |
58c6a100 | 601 | screenPosition = StringUtils.width(text); |
1b120607 | 602 | if (fixed == true) { |
58c6a100 | 603 | if (screenPosition >= getWidth()) { |
6b2633ac | 604 | position = codePointLength(text) - 1; |
58c6a100 KL |
605 | screenPosition = StringUtils.width(text) - 1; |
606 | } | |
1b120607 | 607 | } else { |
58c6a100 | 608 | windowStart = StringUtils.width(text) - getWidth() + 1; |
1b120607 KL |
609 | if (windowStart < 0) { |
610 | windowStart = 0; | |
611 | } | |
612 | } | |
613 | } | |
614 | ||
615 | /** | |
616 | * Set the editing position. The field may adjust the window start to | |
617 | * show as much of the field as possible. | |
618 | * | |
619 | * @param position the new position | |
620 | * @throws IndexOutOfBoundsException if position is outside the range of | |
621 | * the available text | |
622 | */ | |
623 | public void setPosition(final int position) { | |
6b2633ac | 624 | if ((position < 0) || (position >= codePointLength(text))) { |
1b120607 | 625 | throw new IndexOutOfBoundsException("Max length is " + |
6b2633ac | 626 | codePointLength(text) + ", requested position " + position); |
1b120607 KL |
627 | } |
628 | this.position = position; | |
629 | normalizeWindowStart(); | |
630 | } | |
631 | ||
1dac6b8d KL |
632 | /** |
633 | * Set the active color key. | |
634 | * | |
635 | * @param activeColorKey ColorTheme key color to use when this field is | |
636 | * active | |
637 | */ | |
638 | public void setActiveColorKey(final String activeColorKey) { | |
639 | this.activeColorKey = activeColorKey; | |
640 | } | |
641 | ||
642 | /** | |
643 | * Set the inactive color key. | |
644 | * | |
645 | * @param inactiveColorKey ColorTheme key color to use when this field is | |
646 | * inactive | |
647 | */ | |
648 | public void setInactiveColorKey(final String inactiveColorKey) { | |
649 | this.inactiveColorKey = inactiveColorKey; | |
650 | } | |
651 | ||
2b427404 KL |
652 | /** |
653 | * Set the action to perform when the user presses enter. | |
654 | * | |
655 | * @param action the action to perform when the user presses enter | |
656 | */ | |
657 | public void setEnterAction(final TAction action) { | |
658 | enterAction = action; | |
659 | } | |
660 | ||
661 | /** | |
662 | * Set the action to perform when the field is updated. | |
663 | * | |
664 | * @param action the action to perform when the field is updated | |
665 | */ | |
666 | public void setUpdateAction(final TAction action) { | |
667 | updateAction = action; | |
668 | } | |
1dac6b8d | 669 | |
128e5be1 | 670 | } |