【问题标题】:How do I script an undo operation in an Eclipse plug-in?如何在 Eclipse 插件中编写撤消操作脚本?
【发布时间】:2011-10-19 00:11:41
【问题描述】:

我正在为 Eclipse 制作一个插件,并且我想在用户按下与插件关联的撤消按钮时利用内置的 Eclipse“撤消”操作 (org.eclipse.core.commands.operations) -在。

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

我试过这些代码sn-ps:

在工作台中执行的撤消:

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);

【问题讨论】:

  • 我也在处理这个问题。我使用这个更新站点 (download.eclipse.org/eclipse/updates/4.2) 找到了这个示例功能 (org.eclipse.sdk.examples.feature),它有一个很好的实现工作得很好。我只需要弄清楚如何让我的视图部分参与 IUndoContext,以便撤消和重做操作显示在 Eclipse 撤消和重做菜单项中。奇怪的是,我的撤消重做操作仅在示例视图部分处于活动状态时才能正常工作,而不是在我的视图部分处于活动状态时。我想我还没有完全理解 IUndoContext。
  • 尝试使用“安装新软件”对话框安装 Eclipse SDK 示例时,请使用过滤器框并键入“示例”,但请务必取消选中“按类别分组项目”,否则您将看不到任何结果。
  • 要使这种方法发挥作用,您应该在编辑器中执行所有更改,方法是向操作堆栈添加可撤消的命令。

标签: java eclipse plugins undo


【解决方案1】:

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

有点过时了,但想法还不错。

【讨论】:

  • 我真的更喜欢使用 Eclipse 的内置包 (org.eclipse.core.commands.operations)。我在网上阅读了很多很多例子,声称这个包是实现这个功能的理想选择。我已经成功添加了一个实现 IUndoableOperation 并且能够执行()操作的类,但是我似乎无法使 undo()工作......?
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    相关资源
    最近更新 更多