private static class EditorGuiImpl.UndoableEditor
extends java.lang.Object
Two states need to be tracked:
Key.VK_CTRL_DEL
, Keyboard.KA_DELETE_CHARACTER
,
Keyboard.KA_BACKSPACE
or Keyboard.KA_CUT
.
When a selected text is deleted by a printable key,
recordDeletedText(int, com.goldencode.p2j.ui.client.NativePoint, java.lang.String)
is called as if DELETE was pressed.
printable key
is pressed.
The undo data is reset if the inserted or deleted text is not contiguous.
On CTRL-Z, the deletedText
and insertedText
are being switched, with the shown text being already selected.
Modifier and Type | Field and Description |
---|---|
private java.lang.String |
deletedText
The deleted text (all contiguous).
|
private EditorGuiImpl |
editor
The editor instance managed by this class.
|
private java.lang.String |
insertedText
The inserted text (all contiguous).
|
private int |
lastKey
The last processed key by this class (not pressed).
|
private int |
lastResetOffset
The caret/cursor offset when the last reset was performed (this is the offset where
CTRL-Z will append the deleted text).
|
private int |
prevCaretOffset
The previous caret/cursor offset.
|
private boolean |
showDelete
Flag indicating which text to show on CTRL-Z (
deletedText or
insertedText ). |
private boolean |
wasDelete
Flag indicating if previous operation was a character removal key.
|
Constructor and Description |
---|
UndoableEditor(EditorGuiImpl editor)
Create a new instance to process undo for the given editor.
|
Modifier and Type | Method and Description |
---|---|
private java.lang.String |
getChars(int startOffset,
int endOffset)
Get the editor chars from the given interval.
|
void |
processKey(int key)
Process the given key.
|
private void |
processUndo()
Process CTRL-Z by showing the
deletedText or insertedText . |
private void |
recordDeletedText(int key,
int cursorOffset)
Record the deleted text by saving it into
deletedText . |
private void |
recordDeletedText(int key,
int cursorOffset,
java.lang.String text)
Record the deleted text by saving it into
deletedText . |
private void |
recordDeletedText(int key,
NativePoint start,
java.lang.String text)
Record the deleted text by saving it into
deletedText . |
private void |
recordInsertedText(int key,
int cursorOffset)
Record the inserted text by saving it into
insertedText . |
private void |
reset(int key)
Reset the undo data and save the processed key.
|
private final EditorGuiImpl editor
private int prevCaretOffset
private int lastResetOffset
private int lastKey
private java.lang.String deletedText
private java.lang.String insertedText
private boolean showDelete
deletedText
or
insertedText
).private boolean wasDelete
public UndoableEditor(EditorGuiImpl editor)
editor
- The editor.public void processKey(int key)
key
- The key to process.private void processUndo()
deletedText
or insertedText
.private java.lang.String getChars(int startOffset, int endOffset)
startOffset
- The 1-based start offset.endOffset
- The 1-based end offset.private void reset(int key)
key
- The key code which triggered the reset.private void recordInsertedText(int key, int cursorOffset)
insertedText
.
This is called BEFORE the key is processed by the editor.
key
- The key code.cursorOffset
- The cursor offset where the key will be inserted.private void recordDeletedText(int key, NativePoint start, java.lang.String text)
deletedText
.
This is called BEFORE the key is processed by the editor, in cases when a selected text needs to be removed.
key
- The key code.start
- The position where the text is located in the editor.text
- The text to be removed.private void recordDeletedText(int key, int cursorOffset)
deletedText
.
This is called BEFORE the key is processed by the editor.
key
- The key code.cursorOffset
- The cursor offset from where the text is deleted.private void recordDeletedText(int key, int cursorOffset, java.lang.String text)
deletedText
.
This is called BEFORE the key is processed by the editor.
key
- The key code.cursorOffset
- The cursor offset from where the character is removed.text
- If non-null, this overrides the deleted character (is assumed the entire text
is being removed).