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; |
51e46b3e | 34 | import jexer.event.TCommandEvent; |
128e5be1 KL |
35 | import jexer.event.TKeypressEvent; |
36 | import jexer.event.TMouseEvent; | |
51e46b3e | 37 | import static jexer.TCommand.*; |
128e5be1 KL |
38 | import static jexer.TKeypress.*; |
39 | ||
40 | /** | |
87a17f3c | 41 | * TField implements an editable text field. |
128e5be1 | 42 | */ |
51e46b3e | 43 | public class TField extends TWidget implements EditMenuUser { |
128e5be1 | 44 | |
d36057df KL |
45 | // ------------------------------------------------------------------------ |
46 | // Variables -------------------------------------------------------------- | |
47 | // ------------------------------------------------------------------------ | |
48 | ||
1dac6b8d KL |
49 | /** |
50 | * Background character for unfilled-in text. | |
51 | */ | |
afdec5e9 | 52 | protected int backgroundChar = GraphicsChars.HATCH; |
1dac6b8d | 53 | |
128e5be1 KL |
54 | /** |
55 | * Field text. | |
56 | */ | |
87a17f3c | 57 | protected String text = ""; |
128e5be1 | 58 | |
128e5be1 KL |
59 | /** |
60 | * If true, only allow enough characters that will fit in the width. If | |
61 | * false, allow the field to scroll to the right. | |
62 | */ | |
87a17f3c | 63 | protected boolean fixed = false; |
128e5be1 KL |
64 | |
65 | /** | |
66 | * Current editing position within text. | |
67 | */ | |
87a17f3c | 68 | protected int position = 0; |
128e5be1 | 69 | |
58c6a100 KL |
70 | /** |
71 | * Current editing position screen column number. | |
72 | */ | |
73 | protected int screenPosition = 0; | |
74 | ||
128e5be1 KL |
75 | /** |
76 | * Beginning of visible portion. | |
77 | */ | |
87a17f3c | 78 | protected int windowStart = 0; |
128e5be1 KL |
79 | |
80 | /** | |
81 | * If true, new characters are inserted at position. | |
82 | */ | |
87a17f3c | 83 | protected boolean insertMode = true; |
128e5be1 KL |
84 | |
85 | /** | |
86 | * Remember mouse state. | |
87 | */ | |
87a17f3c | 88 | protected TMouseEvent mouse; |
128e5be1 KL |
89 | |
90 | /** | |
91 | * The action to perform when the user presses enter. | |
92 | */ | |
87a17f3c | 93 | protected TAction enterAction; |
128e5be1 KL |
94 | |
95 | /** | |
96 | * The action to perform when the text is updated. | |
97 | */ | |
87a17f3c | 98 | protected TAction updateAction; |
128e5be1 | 99 | |
1dac6b8d KL |
100 | /** |
101 | * The color to use when this field is active. | |
102 | */ | |
103 | private String activeColorKey = "tfield.active"; | |
104 | ||
105 | /** | |
106 | * The color to use when this field is not active. | |
107 | */ | |
108 | private String inactiveColorKey = "tfield.inactive"; | |
109 | ||
d36057df KL |
110 | // ------------------------------------------------------------------------ |
111 | // Constructors ----------------------------------------------------------- | |
112 | // ------------------------------------------------------------------------ | |
113 | ||
128e5be1 KL |
114 | /** |
115 | * Public constructor. | |
116 | * | |
117 | * @param parent parent widget | |
118 | * @param x column relative to parent | |
119 | * @param y row relative to parent | |
120 | * @param width visible text width | |
121 | * @param fixed if true, the text cannot exceed the display width | |
122 | */ | |
123 | public TField(final TWidget parent, final int x, final int y, | |
124 | final int width, final boolean fixed) { | |
125 | ||
126 | this(parent, x, y, width, fixed, "", null, null); | |
127 | } | |
128 | ||
129 | /** | |
130 | * Public constructor. | |
131 | * | |
132 | * @param parent parent widget | |
133 | * @param x column relative to parent | |
134 | * @param y row relative to parent | |
135 | * @param width visible text width | |
136 | * @param fixed if true, the text cannot exceed the display width | |
137 | * @param text initial text, default is empty string | |
138 | */ | |
139 | public TField(final TWidget parent, final int x, final int y, | |
140 | final int width, final boolean fixed, final String text) { | |
141 | ||
142 | this(parent, x, y, width, fixed, text, null, null); | |
143 | } | |
144 | ||
145 | /** | |
146 | * Public constructor. | |
147 | * | |
148 | * @param parent parent widget | |
149 | * @param x column relative to parent | |
150 | * @param y row relative to parent | |
151 | * @param width visible text width | |
152 | * @param fixed if true, the text cannot exceed the display width | |
153 | * @param text initial text, default is empty string | |
154 | * @param enterAction function to call when enter key is pressed | |
155 | * @param updateAction function to call when the text is updated | |
156 | */ | |
157 | public TField(final TWidget parent, final int x, final int y, | |
158 | final int width, final boolean fixed, final String text, | |
159 | final TAction enterAction, final TAction updateAction) { | |
160 | ||
161 | // Set parent and window | |
a83fea2b | 162 | super(parent, x, y, width, 1); |
128e5be1 | 163 | |
7c870d89 | 164 | setCursorVisible(true); |
128e5be1 KL |
165 | this.fixed = fixed; |
166 | this.text = text; | |
167 | this.enterAction = enterAction; | |
168 | this.updateAction = updateAction; | |
169 | } | |
170 | ||
d36057df KL |
171 | // ------------------------------------------------------------------------ |
172 | // Event handlers --------------------------------------------------------- | |
173 | // ------------------------------------------------------------------------ | |
174 | ||
128e5be1 KL |
175 | /** |
176 | * Returns true if the mouse is currently on the field. | |
177 | * | |
178 | * @return if true the mouse is currently on the field | |
179 | */ | |
87a17f3c | 180 | protected boolean mouseOnField() { |
128e5be1 KL |
181 | int rightEdge = getWidth() - 1; |
182 | if ((mouse != null) | |
183 | && (mouse.getY() == 0) | |
184 | && (mouse.getX() >= 0) | |
185 | && (mouse.getX() <= rightEdge) | |
186 | ) { | |
187 | return true; | |
188 | } | |
189 | return false; | |
190 | } | |
191 | ||
128e5be1 KL |
192 | /** |
193 | * Handle mouse button presses. | |
194 | * | |
195 | * @param mouse mouse button event | |
196 | */ | |
197 | @Override | |
198 | public void onMouseDown(final TMouseEvent mouse) { | |
199 | this.mouse = mouse; | |
200 | ||
7c870d89 | 201 | if ((mouseOnField()) && (mouse.isMouse1())) { |
128e5be1 KL |
202 | // Move cursor |
203 | int deltaX = mouse.getX() - getCursorX(); | |
58c6a100 KL |
204 | screenPosition += deltaX; |
205 | if (screenPosition > StringUtils.width(text)) { | |
206 | screenPosition = StringUtils.width(text); | |
128e5be1 | 207 | } |
58c6a100 | 208 | position = screenToTextPosition(screenPosition); |
128e5be1 KL |
209 | updateCursor(); |
210 | return; | |
211 | } | |
212 | } | |
213 | ||
214 | /** | |
215 | * Handle keystrokes. | |
216 | * | |
217 | * @param keypress keystroke event | |
218 | */ | |
219 | @Override | |
220 | public void onKeypress(final TKeypressEvent keypress) { | |
221 | ||
222 | if (keypress.equals(kbLeft)) { | |
223 | if (position > 0) { | |
ebb9a1e4 KL |
224 | screenPosition -= StringUtils.width(text.codePointBefore(position)); |
225 | position -= Character.charCount(text.codePointBefore(position)); | |
128e5be1 KL |
226 | } |
227 | if (fixed == false) { | |
58c6a100 | 228 | if ((screenPosition == windowStart) && (windowStart > 0)) { |
84a34fdf | 229 | windowStart -= StringUtils.width(text.codePointAt( |
58c6a100 | 230 | screenToTextPosition(windowStart))); |
128e5be1 KL |
231 | } |
232 | } | |
24489803 | 233 | normalizeWindowStart(); |
128e5be1 KL |
234 | return; |
235 | } | |
236 | ||
237 | if (keypress.equals(kbRight)) { | |
ebb9a1e4 | 238 | if (position < text.length()) { |
51e46b3e | 239 | int lastPosition = position; |
84a34fdf | 240 | screenPosition += StringUtils.width(text.codePointAt(position)); |
6b2633ac | 241 | position += Character.charCount(text.codePointAt(position)); |
128e5be1 | 242 | if (fixed == true) { |
58c6a100 KL |
243 | if (screenPosition == getWidth()) { |
244 | screenPosition--; | |
51e46b3e | 245 | position -= Character.charCount(text.codePointAt(lastPosition)); |
128e5be1 KL |
246 | } |
247 | } else { | |
ebb9a1e4 KL |
248 | while ((screenPosition - windowStart + |
249 | StringUtils.width(text.codePointAt(text.length() - 1))) | |
250 | > getWidth() | |
251 | ) { | |
84a34fdf | 252 | windowStart += StringUtils.width(text.codePointAt( |
58c6a100 | 253 | screenToTextPosition(windowStart))); |
128e5be1 KL |
254 | } |
255 | } | |
256 | } | |
ebb9a1e4 | 257 | assert (position <= text.length()); |
128e5be1 KL |
258 | return; |
259 | } | |
260 | ||
261 | if (keypress.equals(kbEnter)) { | |
262 | dispatch(true); | |
263 | return; | |
264 | } | |
265 | ||
266 | if (keypress.equals(kbIns)) { | |
267 | insertMode = !insertMode; | |
268 | return; | |
269 | } | |
270 | if (keypress.equals(kbHome)) { | |
1b120607 | 271 | home(); |
128e5be1 KL |
272 | return; |
273 | } | |
274 | ||
275 | if (keypress.equals(kbEnd)) { | |
1b120607 | 276 | end(); |
128e5be1 KL |
277 | return; |
278 | } | |
279 | ||
280 | if (keypress.equals(kbDel)) { | |
ebb9a1e4 | 281 | if ((text.length() > 0) && (position < text.length())) { |
128e5be1 KL |
282 | text = text.substring(0, position) |
283 | + text.substring(position + 1); | |
58c6a100 | 284 | screenPosition = StringUtils.width(text.substring(0, position)); |
128e5be1 | 285 | } |
24489803 | 286 | dispatch(false); |
128e5be1 KL |
287 | return; |
288 | } | |
289 | ||
290 | if (keypress.equals(kbBackspace) || keypress.equals(kbBackspaceDel)) { | |
291 | if (position > 0) { | |
6b2633ac | 292 | position -= Character.charCount(text.codePointBefore(position)); |
128e5be1 KL |
293 | text = text.substring(0, position) |
294 | + text.substring(position + 1); | |
58c6a100 | 295 | screenPosition = StringUtils.width(text.substring(0, position)); |
128e5be1 KL |
296 | } |
297 | if (fixed == false) { | |
58c6a100 | 298 | if ((screenPosition >= windowStart) |
128e5be1 KL |
299 | && (windowStart > 0) |
300 | ) { | |
84a34fdf | 301 | windowStart -= StringUtils.width(text.codePointAt( |
58c6a100 | 302 | screenToTextPosition(windowStart))); |
128e5be1 KL |
303 | } |
304 | } | |
305 | dispatch(false); | |
24489803 | 306 | normalizeWindowStart(); |
128e5be1 KL |
307 | return; |
308 | } | |
309 | ||
7c870d89 KL |
310 | if (!keypress.getKey().isFnKey() |
311 | && !keypress.getKey().isAlt() | |
312 | && !keypress.getKey().isCtrl() | |
128e5be1 KL |
313 | ) { |
314 | // Plain old keystroke, process it | |
ebb9a1e4 | 315 | if ((position == text.length()) |
58c6a100 | 316 | && (StringUtils.width(text) < getWidth())) { |
128e5be1 KL |
317 | |
318 | // Append case | |
7c870d89 | 319 | appendChar(keypress.getKey().getChar()); |
ebb9a1e4 | 320 | } else if ((position < text.length()) |
58c6a100 | 321 | && (StringUtils.width(text) < getWidth())) { |
128e5be1 KL |
322 | |
323 | // Overwrite or insert a character | |
324 | if (insertMode == false) { | |
325 | // Replace character | |
326 | text = text.substring(0, position) | |
84a34fdf | 327 | + codePointString(keypress.getKey().getChar()) |
128e5be1 | 328 | + text.substring(position + 1); |
84a34fdf KL |
329 | screenPosition += StringUtils.width(text.codePointAt(position)); |
330 | position += Character.charCount(keypress.getKey().getChar()); | |
128e5be1 KL |
331 | } else { |
332 | // Insert character | |
7c870d89 | 333 | insertChar(keypress.getKey().getChar()); |
128e5be1 | 334 | } |
ebb9a1e4 | 335 | } else if ((position < text.length()) |
58c6a100 | 336 | && (StringUtils.width(text) >= getWidth())) { |
128e5be1 KL |
337 | |
338 | // Multiple cases here | |
339 | if ((fixed == true) && (insertMode == true)) { | |
340 | // Buffer is full, do nothing | |
341 | } else if ((fixed == true) && (insertMode == false)) { | |
342 | // Overwrite the last character, maybe move position | |
343 | text = text.substring(0, position) | |
84a34fdf | 344 | + codePointString(keypress.getKey().getChar()) |
128e5be1 | 345 | + text.substring(position + 1); |
58c6a100 | 346 | if (screenPosition < getWidth() - 1) { |
84a34fdf KL |
347 | screenPosition += StringUtils.width(text.codePointAt(position)); |
348 | position += Character.charCount(keypress.getKey().getChar()); | |
128e5be1 KL |
349 | } |
350 | } else if ((fixed == false) && (insertMode == false)) { | |
351 | // Overwrite the last character, definitely move position | |
352 | text = text.substring(0, position) | |
84a34fdf | 353 | + codePointString(keypress.getKey().getChar()) |
128e5be1 | 354 | + text.substring(position + 1); |
84a34fdf KL |
355 | screenPosition += StringUtils.width(text.codePointAt(position)); |
356 | position += Character.charCount(keypress.getKey().getChar()); | |
128e5be1 | 357 | } else { |
ebb9a1e4 | 358 | if (position == text.length()) { |
128e5be1 | 359 | // Append this character |
7c870d89 | 360 | appendChar(keypress.getKey().getChar()); |
128e5be1 KL |
361 | } else { |
362 | // Insert this character | |
7c870d89 | 363 | insertChar(keypress.getKey().getChar()); |
128e5be1 KL |
364 | } |
365 | } | |
366 | } else { | |
367 | assert (!fixed); | |
368 | ||
369 | // Append this character | |
7c870d89 | 370 | appendChar(keypress.getKey().getChar()); |
128e5be1 KL |
371 | } |
372 | dispatch(false); | |
373 | return; | |
374 | } | |
375 | ||
376 | // Pass to parent for the things we don't care about. | |
377 | super.onKeypress(keypress); | |
378 | } | |
f8b30e4b | 379 | |
51e46b3e KL |
380 | /** |
381 | * Handle posted command events. | |
382 | * | |
383 | * @param command command event | |
384 | */ | |
385 | @Override | |
386 | public void onCommand(final TCommandEvent command) { | |
387 | if (command.equals(cmCut)) { | |
388 | // Copy text to clipboard, and then remove it. | |
389 | getClipboard().copyText(text); | |
390 | setText(""); | |
391 | return; | |
392 | } | |
393 | ||
394 | if (command.equals(cmCopy)) { | |
395 | // Copy text to clipboard. | |
396 | getClipboard().copyText(text); | |
397 | return; | |
398 | } | |
399 | ||
400 | if (command.equals(cmPaste)) { | |
401 | // Paste text from clipboard. | |
402 | String newText = getClipboard().pasteText(); | |
403 | if (newText != null) { | |
404 | setText(newText); | |
405 | } | |
406 | return; | |
407 | } | |
408 | ||
409 | if (command.equals(cmClear)) { | |
410 | // Remove text. | |
411 | setText(""); | |
412 | return; | |
413 | } | |
414 | ||
415 | } | |
128e5be1 | 416 | |
d36057df KL |
417 | // ------------------------------------------------------------------------ |
418 | // TWidget ---------------------------------------------------------------- | |
419 | // ------------------------------------------------------------------------ | |
420 | ||
d8dc8aea KL |
421 | /** |
422 | * Override TWidget's height: we can only set height at construction | |
423 | * time. | |
424 | * | |
425 | * @param height new widget height (ignored) | |
426 | */ | |
427 | @Override | |
428 | public void setHeight(final int height) { | |
429 | // Do nothing | |
430 | } | |
431 | ||
d36057df KL |
432 | /** |
433 | * Draw the text field. | |
434 | */ | |
435 | @Override | |
436 | public void draw() { | |
437 | CellAttributes fieldColor; | |
438 | ||
439 | if (isAbsoluteActive()) { | |
1dac6b8d | 440 | fieldColor = getTheme().getColor(activeColorKey); |
d36057df | 441 | } else { |
1dac6b8d | 442 | fieldColor = getTheme().getColor(inactiveColorKey); |
d36057df KL |
443 | } |
444 | ||
445 | int end = windowStart + getWidth(); | |
58c6a100 KL |
446 | if (end > StringUtils.width(text)) { |
447 | end = StringUtils.width(text); | |
d36057df | 448 | } |
1dac6b8d | 449 | hLineXY(0, 0, getWidth(), backgroundChar, fieldColor); |
58c6a100 KL |
450 | putStringXY(0, 0, text.substring(screenToTextPosition(windowStart), |
451 | screenToTextPosition(end)), fieldColor); | |
d36057df KL |
452 | |
453 | // Fix the cursor, it will be rendered by TApplication.drawAll(). | |
454 | updateCursor(); | |
455 | } | |
456 | ||
457 | // ------------------------------------------------------------------------ | |
458 | // TField ----------------------------------------------------------------- | |
459 | // ------------------------------------------------------------------------ | |
460 | ||
84a34fdf KL |
461 | /** |
462 | * Convert a char (codepoint) to a string. | |
463 | * | |
464 | * @param ch the char | |
465 | * @return the string | |
466 | */ | |
467 | private String codePointString(final int ch) { | |
468 | StringBuilder sb = new StringBuilder(1); | |
469 | sb.append(Character.toChars(ch)); | |
ebb9a1e4 | 470 | assert (Character.charCount(ch) == sb.length()); |
84a34fdf KL |
471 | return sb.toString(); |
472 | } | |
473 | ||
1dac6b8d KL |
474 | /** |
475 | * Get field background character. | |
476 | * | |
477 | * @return background character | |
478 | */ | |
afdec5e9 | 479 | public final int getBackgroundChar() { |
1dac6b8d KL |
480 | return backgroundChar; |
481 | } | |
482 | ||
483 | /** | |
484 | * Set field background character. | |
485 | * | |
486 | * @param backgroundChar the background character | |
487 | */ | |
afdec5e9 | 488 | public void setBackgroundChar(final int backgroundChar) { |
1dac6b8d KL |
489 | this.backgroundChar = backgroundChar; |
490 | } | |
491 | ||
d36057df KL |
492 | /** |
493 | * Get field text. | |
494 | * | |
495 | * @return field text | |
496 | */ | |
497 | public final String getText() { | |
498 | return text; | |
499 | } | |
500 | ||
501 | /** | |
502 | * Set field text. | |
503 | * | |
504 | * @param text the new field text | |
505 | */ | |
051e2913 | 506 | public void setText(final String text) { |
a69ed767 | 507 | assert (text != null); |
d36057df KL |
508 | this.text = text; |
509 | position = 0; | |
51e46b3e | 510 | screenPosition = 0; |
d36057df | 511 | windowStart = 0; |
51e46b3e KL |
512 | if ((fixed == true) && (this.text.length() > getWidth())) { |
513 | this.text = this.text.substring(0, getWidth()); | |
514 | } | |
d36057df KL |
515 | } |
516 | ||
517 | /** | |
518 | * Dispatch to the action function. | |
519 | * | |
520 | * @param enter if true, the user pressed Enter, else this was an update | |
521 | * to the text. | |
522 | */ | |
523 | protected void dispatch(final boolean enter) { | |
524 | if (enter) { | |
525 | if (enterAction != null) { | |
a524aa2e | 526 | enterAction.DO(this); |
d36057df KL |
527 | } |
528 | } else { | |
529 | if (updateAction != null) { | |
a524aa2e | 530 | updateAction.DO(this); |
d36057df KL |
531 | } |
532 | } | |
533 | } | |
534 | ||
58c6a100 KL |
535 | /** |
536 | * Determine string position from screen position. | |
537 | * | |
538 | * @param screenPosition the position on screen | |
539 | * @return the equivalent position in text | |
540 | */ | |
541 | protected int screenToTextPosition(final int screenPosition) { | |
542 | if (screenPosition == 0) { | |
543 | return 0; | |
544 | } | |
545 | ||
546 | int n = 0; | |
ebb9a1e4 | 547 | for (int i = 0; i < text.length(); i++) { |
84a34fdf | 548 | n += StringUtils.width(text.codePointAt(i)); |
58c6a100 KL |
549 | if (n >= screenPosition) { |
550 | return i + 1; | |
551 | } | |
552 | } | |
553 | // screenPosition exceeds the available text length. | |
554 | throw new IndexOutOfBoundsException("screenPosition " + screenPosition + | |
ebb9a1e4 | 555 | " exceeds available text length " + text.length()); |
58c6a100 KL |
556 | } |
557 | ||
d36057df KL |
558 | /** |
559 | * Update the visible cursor position to match the location of position | |
560 | * and windowStart. | |
561 | */ | |
562 | protected void updateCursor() { | |
58c6a100 | 563 | if ((screenPosition > getWidth()) && fixed) { |
d36057df | 564 | setCursorX(getWidth()); |
58c6a100 | 565 | } else if ((screenPosition - windowStart >= getWidth()) && !fixed) { |
d36057df KL |
566 | setCursorX(getWidth() - 1); |
567 | } else { | |
58c6a100 | 568 | setCursorX(screenPosition - windowStart); |
d36057df KL |
569 | } |
570 | } | |
571 | ||
572 | /** | |
573 | * Normalize windowStart such that most of the field data if visible. | |
574 | */ | |
575 | protected void normalizeWindowStart() { | |
576 | if (fixed) { | |
577 | // windowStart had better be zero, there is nothing to do here. | |
578 | assert (windowStart == 0); | |
579 | return; | |
580 | } | |
58c6a100 | 581 | windowStart = screenPosition - (getWidth() - 1); |
d36057df KL |
582 | if (windowStart < 0) { |
583 | windowStart = 0; | |
584 | } | |
585 | ||
586 | updateCursor(); | |
587 | } | |
588 | ||
128e5be1 KL |
589 | /** |
590 | * Append char to the end of the field. | |
591 | * | |
afdec5e9 | 592 | * @param ch char to append |
128e5be1 | 593 | */ |
afdec5e9 | 594 | protected void appendChar(final int ch) { |
128e5be1 | 595 | // Append the LAST character |
84a34fdf KL |
596 | text += codePointString(ch); |
597 | position += Character.charCount(ch); | |
58c6a100 | 598 | screenPosition += StringUtils.width(ch); |
128e5be1 | 599 | |
ebb9a1e4 | 600 | assert (position == text.length()); |
128e5be1 KL |
601 | |
602 | if (fixed) { | |
58c6a100 | 603 | if (screenPosition >= getWidth()) { |
84a34fdf | 604 | position -= Character.charCount(ch); |
58c6a100 | 605 | screenPosition -= StringUtils.width(ch); |
128e5be1 KL |
606 | } |
607 | } else { | |
58c6a100 | 608 | if ((screenPosition - windowStart) >= getWidth()) { |
128e5be1 KL |
609 | windowStart++; |
610 | } | |
611 | } | |
612 | } | |
613 | ||
614 | /** | |
615 | * Insert char somewhere in the middle of the field. | |
616 | * | |
617 | * @param ch char to append | |
618 | */ | |
afdec5e9 | 619 | protected void insertChar(final int ch) { |
84a34fdf KL |
620 | text = text.substring(0, position) + codePointString(ch) |
621 | + text.substring(position); | |
622 | position += Character.charCount(ch); | |
58c6a100 KL |
623 | screenPosition += StringUtils.width(ch); |
624 | if ((screenPosition - windowStart) == getWidth()) { | |
128e5be1 KL |
625 | assert (!fixed); |
626 | windowStart++; | |
627 | } | |
628 | } | |
629 | ||
1b120607 KL |
630 | /** |
631 | * Position the cursor at the first column. The field may adjust the | |
632 | * window start to show as much of the field as possible. | |
633 | */ | |
634 | public void home() { | |
635 | position = 0; | |
58c6a100 | 636 | screenPosition = 0; |
1b120607 KL |
637 | windowStart = 0; |
638 | } | |
639 | ||
640 | /** | |
641 | * Set the editing position to the last filled character. The field may | |
642 | * adjust the window start to show as much of the field as possible. | |
643 | */ | |
644 | public void end() { | |
ebb9a1e4 | 645 | position = text.length(); |
58c6a100 | 646 | screenPosition = StringUtils.width(text); |
1b120607 | 647 | if (fixed == true) { |
58c6a100 | 648 | if (screenPosition >= getWidth()) { |
ebb9a1e4 | 649 | position -= Character.charCount(text.codePointBefore(position)); |
58c6a100 KL |
650 | screenPosition = StringUtils.width(text) - 1; |
651 | } | |
1b120607 | 652 | } else { |
58c6a100 | 653 | windowStart = StringUtils.width(text) - getWidth() + 1; |
1b120607 KL |
654 | if (windowStart < 0) { |
655 | windowStart = 0; | |
656 | } | |
657 | } | |
658 | } | |
659 | ||
660 | /** | |
661 | * Set the editing position. The field may adjust the window start to | |
662 | * show as much of the field as possible. | |
663 | * | |
664 | * @param position the new position | |
665 | * @throws IndexOutOfBoundsException if position is outside the range of | |
666 | * the available text | |
667 | */ | |
668 | public void setPosition(final int position) { | |
ebb9a1e4 | 669 | if ((position < 0) || (position >= text.length())) { |
1b120607 | 670 | throw new IndexOutOfBoundsException("Max length is " + |
ebb9a1e4 | 671 | text.length() + ", requested position " + position); |
1b120607 KL |
672 | } |
673 | this.position = position; | |
674 | normalizeWindowStart(); | |
675 | } | |
676 | ||
1dac6b8d KL |
677 | /** |
678 | * Set the active color key. | |
679 | * | |
680 | * @param activeColorKey ColorTheme key color to use when this field is | |
681 | * active | |
682 | */ | |
683 | public void setActiveColorKey(final String activeColorKey) { | |
684 | this.activeColorKey = activeColorKey; | |
685 | } | |
686 | ||
687 | /** | |
688 | * Set the inactive color key. | |
689 | * | |
690 | * @param inactiveColorKey ColorTheme key color to use when this field is | |
691 | * inactive | |
692 | */ | |
693 | public void setInactiveColorKey(final String inactiveColorKey) { | |
694 | this.inactiveColorKey = inactiveColorKey; | |
695 | } | |
696 | ||
2b427404 KL |
697 | /** |
698 | * Set the action to perform when the user presses enter. | |
699 | * | |
700 | * @param action the action to perform when the user presses enter | |
701 | */ | |
702 | public void setEnterAction(final TAction action) { | |
703 | enterAction = action; | |
704 | } | |
705 | ||
706 | /** | |
707 | * Set the action to perform when the field is updated. | |
708 | * | |
709 | * @param action the action to perform when the field is updated | |
710 | */ | |
711 | public void setUpdateAction(final TAction action) { | |
712 | updateAction = action; | |
713 | } | |
1dac6b8d | 714 | |
51e46b3e KL |
715 | // ------------------------------------------------------------------------ |
716 | // EditMenuUser ----------------------------------------------------------- | |
717 | // ------------------------------------------------------------------------ | |
718 | ||
719 | /** | |
720 | * Check if the cut menu item should be enabled. | |
721 | * | |
722 | * @return true if the cut menu item should be enabled | |
723 | */ | |
724 | public boolean isEditMenuCut() { | |
725 | return true; | |
726 | } | |
727 | ||
728 | /** | |
729 | * Check if the copy menu item should be enabled. | |
730 | * | |
731 | * @return true if the copy menu item should be enabled | |
732 | */ | |
733 | public boolean isEditMenuCopy() { | |
734 | return true; | |
735 | } | |
736 | ||
737 | /** | |
738 | * Check if the paste menu item should be enabled. | |
739 | * | |
740 | * @return true if the paste menu item should be enabled | |
741 | */ | |
742 | public boolean isEditMenuPaste() { | |
743 | return true; | |
744 | } | |
745 | ||
746 | /** | |
747 | * Check if the clear menu item should be enabled. | |
748 | * | |
749 | * @return true if the clear menu item should be enabled | |
750 | */ | |
751 | public boolean isEditMenuClear() { | |
752 | return true; | |
753 | } | |
754 | ||
128e5be1 | 755 | } |