Thanks Hussein.  Based on your example, I appear to be using the begin/end 
batch editing calls correctly.

Do you forsee any future support for adding a rollbackBatchEditing, that:
1) Undoes the pending changes without any thing being added to the undo/redo 
stack
2) Ends the block editing event

Cheers,
Mike

-----Original Message-----
From: Hussein Shafie [mailto:[email protected]]
Sent: Mon 11/19/2007 9:53 AM
To: Santy, Michael
Cc: xmleditor-support at xmlmind.com
Subject: Re: [XXE] batch editing rollback
 
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();
}






-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.xmlmind.com/pipermail/xmleditor-support/attachments/20071119/0d248bab/attachment.htm
 

Reply via email to