How do I script an undo operation in an Eclipse plug-in?
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.
I开发者_StackOverflow社区deally, 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 );
}
};
I'm not sure if I understood you correctly, but I think this may be something for you: http://www.eclipsezone.com/eclipse/forums/t80577.html#92048329
It is a bit outdated but the idea is still not bad.
精彩评论