Santy, Michael wrote:
>
> I have a situation that I'm wrapping some document processing in a batch
> editing scope to hide the intermediate document state from the view. If
> the processing runs into problems, I would like to revert the document
> state to that before the processing (analogous to a database rollback).
> I cannot figure out how to rollback the document state in XXE, so I'm
> using the undo manager as shown below:
>
> try {
> app.getActiveDocument().beginBatchEditing();
> .. do stuff that might throw an exception
> if(successful) {
> app.getActiveDocument().endBatchEditing();
> }
> }
> catch(FooException e) {
> .. error handling
> }
> catch(BarException e) {
> .. different kind of error handling
> }
> finally {
> if(!successful) {
> //
> // manually rollback the changes
> //
> app.getActiveDocument.endBatchEditing();
> app.getActiveDocumentView.getUndoManager().undo();
> }
> }
>
> While this accomplishes what I want, it adds an entry onto the undo/redo
> stack that can be redone. What I would like to do is something like this:
>
> try {
> app.getActiveDocument().beginBatchEditing();
> .. do stuff that might throw an exception
> if(successful) {
> app.getActiveDocument().endBatchEditing();
> }
> }
> catch(FooException e) {
> .. error handling
> }
> catch(BarException e) {
> .. different kind of error handling
> }
> finally {
> if(!successful) {
> app.getActiveDocument.rollbackBatchEditing();
> }
> }
>
> After rollback, it is like the processing never happened. Is there a
> way to accomplish this in XXE?
It's:
boolean success = false;
doc.beginBatchEditing();
try {
...
success = true;
} catch(FooException e) {
...
} finally {
doc.endBatchEditing();
}
if (!success) {
undoManager.undo();
}
It is *mandatory* to always invoke endBatchEditing after a
beginBatchEditing, whether the sequence of editing operations has
succeeded or not (otherwise you corrupt the internal state of XXE).
Now, if you don't want XXE to allow you to redo the broken editing
operation, there is no other way than invoking undoManager.reset(). See
http://www.xmlmind.com/xmleditor/_distrib/doc/api/com/xmlmind/xmledit/edit/UndoManager.html#reset()
Which gives:
boolean success = false;
doc.beginBatchEditing();
try {
...
success = true;
} catch(FooException e) {
...
} finally {
doc.endBatchEditing();
}
if (!success) {
undoManager.undo();
undoManager.reset();
}