|
8 | 8 | import static org.fxmisc.wellbehaved.event.template.InputMapTemplate.*; |
9 | 9 | import static org.reactfx.EventStreams.*; |
10 | 10 |
|
11 | | -import java.util.Arrays; |
12 | 11 | import java.util.function.Predicate; |
13 | 12 |
|
14 | 13 | import javafx.event.Event; |
@@ -90,7 +89,9 @@ class GenericStyledAreaBehavior { |
90 | 89 | consume(keyPressed(SHORTCUT_Z), (b, e) -> b.view.undo()), |
91 | 90 | consume( |
92 | 91 | anyOf(keyPressed(SHORTCUT_Y), keyPressed(SHORTCUT_SHIFT_Z)), |
93 | | - (b, e) -> b.view.redo()) |
| 92 | + (b, e) -> b.view.redo()), |
| 93 | + // insert/overwrite |
| 94 | + consume(keyPressed(INSERT), GenericStyledAreaBehavior::toggelOverwriteMode) |
94 | 95 | ); |
95 | 96 | InputMapTemplate<GenericStyledAreaBehavior, KeyEvent> edits = when(b -> b.view.isEditable(), editsBase); |
96 | 97 |
|
@@ -299,6 +300,11 @@ private enum DragState { |
299 | 300 |
|
300 | 301 | private final GenericStyledArea<?, ?, ?> view; |
301 | 302 |
|
| 303 | + /** |
| 304 | + * Indicates weather the area is in overwrite or insert mode. |
| 305 | + */ |
| 306 | + private boolean overwriteMode = false; |
| 307 | + |
302 | 308 | /** |
303 | 309 | * Indicates whether an existing selection is being dragged by the user. |
304 | 310 | */ |
@@ -353,7 +359,19 @@ private void keyTyped(KeyEvent event) { |
353 | 359 | return; |
354 | 360 | } |
355 | 361 |
|
356 | | - view.replaceSelection(text); |
| 362 | + IndexRange range = view.getSelection(); |
| 363 | + int start = range.getStart(); |
| 364 | + int end = range.getEnd(); |
| 365 | + |
| 366 | + if (overwriteMode && start == end) { |
| 367 | + end = Math.min(end+1, view.getLength()); |
| 368 | + } |
| 369 | + |
| 370 | + view.replaceText(start, end, text); |
| 371 | + } |
| 372 | + |
| 373 | + private void toggelOverwriteMode(KeyEvent ignore) { |
| 374 | + overwriteMode = !overwriteMode; |
357 | 375 | } |
358 | 376 |
|
359 | 377 | private void deleteBackward(KeyEvent ignore) { |
|
0 commit comments