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);
If your editor has a viewer on it (e.g. TextViewer, SourceViewer, ProjectionViewer), then you can add an undo action that calls the undo operation on the viewer, e.g.
Action undoAction = new Action()
{
@Override
public void run()
{
getViewer().doOperation( ITextOperationTarget.UNDO );
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With