如何在Eclipse插件中编写撤消操作的脚本?

时间:2011-08-03 10:45:26

标签: java eclipse plugins undo

我正在为Eclipse创建一个插件,每当用户按下与插件关联的撤消按钮时,我都希望利用内置的Eclipse'Undo'操作(org.eclipse.core.commands.operations)英寸

理想情况下,它只会重现按CTRL + Z时会发生什么,但我没有模拟按键工作。

我已尝试过这些代码段:

在工作台中执行撤消:

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

在工作区中执行撤消:

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

我正在寻找的是类似的,但这不起作用:

在编辑/文档上执行撤消:

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 个答案:

答案 0 :(得分:1)

如果您的编辑器上有一个查看器(例如TextViewer,SourceViewer,ProjectionViewer),那么您可以在查看器上添加一个撤消操作来调用撤消操作,例如。

Action undoAction = new Action()
{
  @Override
  public void run()
  {
    getViewer().doOperation( ITextOperationTarget.UNDO );
  }
}; 

答案 1 :(得分:0)

我不确定我是否理解正确,但我认为这可能适合您:http://www.eclipsezone.com/eclipse/forums/t80577.html#92048329

这有点过时,但这个想法仍然不错。