【问题标题】:How do I implement an IUndoableOperation in my RCP application using my own IUndoContext如何使用我自己的 IUndoContext 在我的 RCP 应用程序中实现 IUndoableOperation
【发布时间】:2013-08-09 01:48:18
【问题描述】:

我已经实现了我的操作类,它扩展了 org.eclipse.core.commands.operations.AbstractOperation,所以它实现了方法 execute()、redo() 和 undo()。起初,我只能调用 execute() 方法,但我永远无法在我的 Eclipse 撤消重做按钮上看到撤消或重做命令可用。

然后我在我的 RCP 应用程序 (org.eclipse.ui.examples.undo) 中安装了一个示例插件,神奇的事情开始发生了。

使用示例插件附带的“撤消历史视图”视图,我可以看到我的自定义撤消和重做项目被添加到要撤消和重做的事物堆栈中。另外,当“撤消历史视图”处于活动状态时,我的自定义撤消重做操作会显示在 Eclipse 撤消重做按钮中。所以,它工作正常,但有一个问题。

当我选择我的视图部分时,项目会从 Eclipse 撤消重做按钮中消失。我的假设是我没有正确使用 IUndoContext。

就操作执行而言,这是我所拥有的。我的 InventoryLabelOperation 类扩展了 org.eclipse.core.commands.operations.AbstractOperation。 activePart 字段是我希望与 IUndoContext 正确播放的 View Part 的一个实例,但事实并非如此。

InventoryLabelOperation operation = new InventoryLabelOperation(message + name);
IOperationHistory operationHistory = OperationHistoryFactory.getOperationHistory();
IWorkbench workbench = activePart.getSite().getWorkbenchWindow().getWorkbench();
IUndoContext undoContext = workbench.getOperationSupport().getUndoContext();
operation.addContext(undoContext);
operationHistory.execute(operation, new NullProgressMonitor(), null);

有人看到我在这里缺少什么吗?

【问题讨论】:

    标签: eclipse eclipse-plugin eclipse-rcp


    【解决方案1】:

    事实证明,我在操作创建方面所做的一切都是正确的。事实证明,我正在开发的视图部分扩展了 ViewPart 的 CommonNavigator 类(因为我的视图正在利用 Common Navigator 框架)。我没有意识到 CommonNavigator 正在设置自己的上下文,因此导致我使用工作台上下文的撤消操作无法正确显示。

    一旦我进行了操作,就使用从该调用返回的上下文...

    undoContext = 
        (IUndoContext) ResourcesPlugin.getWorkspace().getAdapter(IUndoContext.class);
    

    事情开始按预期显示在 UI 中。

    【讨论】:

      猜你喜欢
      • 2014-04-08
      • 1970-01-01
      • 2014-11-28
      • 2011-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      相关资源
      最近更新 更多