How do I script an undo operation in an Eclipse plug-in?

Viewed 1218

I'm making a plug-in for Eclipse and I want to leverage the built-in Eclipse 'Undo' action (org.eclipse.core.commands.operations) whenever a user presses the undo button associated with the plug-in.

Ideally, it would just reproduce what happens when you press CTRL+Z, but I didn't get simulating keypresses working.

I've tried these code snippets:

Undo performed in a workbench:

IWorkbenchOperationSupport operationSupport = PlatformUI.getWorkbench().getOperationSupport();
IUndoContext context = operationSupport.getUndoContext();
IOperationHistory operationHistory = operationSupport.getOperationHistory();    
IStatus status = operationHistory.undo(context, null, null);

Undo performed in a workspace:

IWorkbenchOperationSupport operationSupport = PlatformUI.getWorkbench().getOperationSupport();
IUndoContext context= (IUndoContext)ResourcesPlugin.getWorkspace().getAdapter(IUndoContext.class);
IOperationHistory operationHistory = operationSupport.getOperationHistory();
IStatus status = operationHistory.undo(context, null, null);

What I am then looking for, analogously, is this, but it doesn't work:

Undo performed on editor/document:

IWorkbenchOperationSupport operationSupport = PlatformUI.getWorkbench().getOperationSupport();
IEditorPart currentEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
IUndoContext context = (IUndoContext) currentEditor.getAdapter(IUndoContext.class);
IOperationHistory operationHistory = operationSupport.getOperationHistory();
IStatus status = operationHistory.undo(context, null, null);
2 Answers
Related