problem with TriggeredOperations (undo doesn't work??) [message #663415] |
Mon, 04 April 2011 22:42 |
Carlo Salinari Messages: 66 Registered: October 2010 |
Member |
|
|
Hi,
I have an application in which a parent-operation may trigger N
child-operations.
I want the parent-operation to appear in the operations-history, and if
I call undo on the parent-operation, I want it to undo all the
child-operations it triggered.
I think this is, more or less, the use case for ICompositeOperation and
in particular for TriggeredOperations.
The problem is, I can't figure out how to make it work correctly.
Please, see my test class below.
In this class I instantiate 3 operations:
1. triggerOp
2. childOp1
3. childOp2
(they are dummy ops that just print their name and invoked method)
then I create a TriggeredOperations batch as follows:
TriggeredOperations batch = new TriggeredOperations(triggerOp, history);
and I intercept the child-ops by executing them inside a
history.operationOpen/historyOperationClose block.
So far, so good. ChildOp1 and ChildOp2 end up as children of batch.
The problem arises if I try to call history.undo: only triggerOp.undo is
invoked, while I would have expected to see
childOp1.undo
childOp2.undo
called! Is it a bug, or am I missing something here?
I dump the code here. Dependencies are:
org.eclipse.core.commands
org.eclipse.core.runtime
org.eclipse.equinox.common
----- cut here -----
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.operations.AbstractOperation;
import org.eclipse.core.commands.operations.DefaultOperationHistory ;
import org.eclipse.core.commands.operations.IOperationHistory;
import org.eclipse.core.commands.operations.IUndoContext;
import org.eclipse.core.commands.operations.IUndoableOperation;
import org.eclipse.core.commands.operations.ObjectUndoContext;
import org.eclipse.core.commands.operations.TriggeredOperations;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
public class TriggeredOperationsTest {
static class MyOperation extends AbstractOperation {
public MyOperation(String label) {
super(label);
}
@Override
public IStatus execute(IProgressMonitor monitor, IAdaptable info)
throws ExecutionException {
System.out.println(this.getLabel() + "->execute");
return Status.OK_STATUS;
}
@Override
public IStatus redo(IProgressMonitor monitor, IAdaptable info)
throws ExecutionException {
System.out.println(this.getLabel() + "->redo");
return Status.OK_STATUS;
}
@Override
public IStatus undo(IProgressMonitor monitor, IAdaptable info)
throws ExecutionException {
System.out.println(this.getLabel() + "->undo");
return Status.OK_STATUS;
}
}
public static void main(String[] args) {
IOperationHistory history = new DefaultOperationHistory();
IUndoContext myContext = new ObjectUndoContext("MyContext");
// used only as a marker (in my -erroneous- understanding)
IUndoableOperation triggerOp = new MyOperation("TriggerOp");
triggerOp.addContext(myContext);
// this are two actual operations
IUndoableOperation childOp1 = new MyOperation("ChildOp1");
childOp1.addContext(myContext);
IUndoableOperation childOp2 = new MyOperation("ChildOp2");
childOp2.addContext(myContext);
// the composite operation is marked by triggerOp
TriggeredOperations batch = new TriggeredOperations(triggerOp, history);
// from now on operations executed by history will be added to "batch"
history.openOperation(batch, IOperationHistory.EXECUTE);
try {
history.execute(childOp1, null, null);
history.execute(childOp2, null, null);
} catch (ExecutionException e) {
e.printStackTrace();
}
// the composite operation is closed and added to the history
history.closeOperation(true, true, IOperationHistory.EXECUTE);
// history contains only the
System.out.println();
System.out.println("history:");
for (IUndoableOperation i : history.getUndoHistory(myContext)) {
// type is printed too: the history contains only "batch"
System.out.println(i.getClass().getSimpleName() + " " + i.getLabel());
}
// I expected child operations be undone too, but thry are not
System.out.println();
System.out.println("undo:");
try {
history.undo(myContext, null, null);
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.07285 seconds