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, boolean, 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 boolean |
lastPrintable
Flag indicating if
lastKey is printable. |
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(KeyInput ke)
Process the given key.
|
private void |
processUndo()
Process CTRL-Z by showing the
deletedText or insertedText . |
private void |
recordDeletedText(int key,
boolean printable,
int cursorOffset)
Record the deleted text by saving it into
deletedText . |
private void |
recordDeletedText(int key,
boolean printable,
int cursorOffset,
java.lang.String text)
Record the deleted text by saving it into
deletedText . |
private void |
recordDeletedText(int key,
boolean printable,
NativePoint start,
java.lang.String text)
Record the deleted text by saving it into
deletedText . |
private void |
recordInsertedText(int key,
boolean printable,
int cursorOffset)
Record the inserted text by saving it into
insertedText . |
private void |
recordPastedText(int cursorOffset,
java.lang.String text)
Record the inserted text by saving it into
insertedText . |
private void |
reset(int key,
boolean printable)
Reset the undo data and save the processed key.
|
private final EditorGuiImpl editor
private int prevCaretOffset
private int lastResetOffset
private int lastKey
private boolean lastPrintable
lastKey
is printable.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(KeyInput ke)
ke
- The key event 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, boolean printable)
key
- The key code which triggered the reset.printable
- Flag indicating if this is a printable key.private void recordInsertedText(int key, boolean printable, int cursorOffset)
insertedText
.
This is called BEFORE the key is processed by the editor.
key
- The key code.printable
- Flag indicating if this is a printable key.cursorOffset
- The cursor offset where the key will be inserted.private void recordPastedText(int cursorOffset, java.lang.String text)
insertedText
.cursorOffset
- The cursor offset where the key will be inserted.text
- The text to be inserted.private void recordDeletedText(int key, boolean printable, 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.printable
- Flag indicating if this is a printable key.start
- The position where the text is located in the editor.text
- The text to be removed.private void recordDeletedText(int key, boolean printable, int cursorOffset)
deletedText
.
This is called BEFORE the key is processed by the editor.
key
- The key code.printable
- Flag indicating if this is a printable key.cursorOffset
- The cursor offset from where the text is deleted.private void recordDeletedText(int key, boolean printable, 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.printable
- Flag indicating if this is a printable key.text
- If non-null, this overrides the deleted character (is assumed the entire text
is being removed).