Richtextfx: Question: How to stop and resume the undomanager

Created on 23 Apr 2018  路  3Comments  路  Source: FXMisc/RichTextFX

My application is running some commands on ctrl+space. When the user presses Ctrl+space, it begins to search if the last word typed at the caret position is a command and replaces it with a template. My problem is that the undomanager is saving the commands. eg. "pk" is replaced by "primary key" and "pk" shows up again on ctrl+z / ctrl+y. How do i force the undomanager to ignore this replaceText(...).

question

Most helpful comment

For those who are wondering about the magic "Recreate UndoUtils#defaultUndoManager" line in the answer above, use the code below to create/set the new manager:

area.setUndoManager(UndoManagerFactory.unlimitedHistoryFactory().createMultiChangeUM(area.multiRichChanges().conditionOn(allowChange), TextChange::invert, UndoUtils.applyMultiRichTextChange(area), TextChange::mergeWith, TextChange::isIdentity));

All 3 comments

You'll need to create your own custom EventStream and then rebuild the default UndoManager using methods in the UndoUtils class. Something like this should work:
```java EventStream<PlainTextChange> regularStream = area.plainTextChanges(); SuspendableYes allowChange = new SuspendableYes(); EventStream<PlainTextChange> stream = regularStream.conditionOn(allowChange); // Recreate UndoUtils#defaultUndoManager // but pass instream` as the EventStream
UndoManager um = // creation code

// when want to do CTRL+Space in some other part of your code
if (userPressedCtrlSpace) {
allowChange.suspendWhile(() -> {
area.replaceText();
// area will emit a change but that change
// will be ignored because allowChange is now 'false' during its suspension
}
}
````

I'm assuming this answers your question. Reopen if it's still unanswered.

For those who are wondering about the magic "Recreate UndoUtils#defaultUndoManager" line in the answer above, use the code below to create/set the new manager:

area.setUndoManager(UndoManagerFactory.unlimitedHistoryFactory().createMultiChangeUM(area.multiRichChanges().conditionOn(allowChange), TextChange::invert, UndoUtils.applyMultiRichTextChange(area), TextChange::mergeWith, TextChange::isIdentity));
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Kim4444 picture Kim4444  路  5Comments

garybentley picture garybentley  路  11Comments

oowekyala picture oowekyala  路  10Comments

RobertBColton picture RobertBColton  路  5Comments

RobertBColton picture RobertBColton  路  8Comments