Eclipse 如何使用自己的IUndoContext在RCP应用程序中实现IUndoableOperation

Eclipse 如何使用自己的IUndoContext在RCP应用程序中实现IUndoableOperation,eclipse,eclipse-plugin,eclipse-rcp,Eclipse,Eclipse Plugin,Eclipse Rcp,我已经实现了我的operation类,它扩展了org.eclipse.core.commands.operations.AbstractOperation,因此它实现了execute()、redo()和undo()方法。起初,我只能调用execute()方法,但我从来没有看到我的EclipseUndo-redo按钮上可以使用undo或redo命令 然后,我在我的RCP应用程序(org.eclipse.ui.examples.undo)中安装了一个示例插件,魔术开始了 使用示例插件附带的视图“Un

我已经实现了我的operation类,它扩展了org.eclipse.core.commands.operations.AbstractOperation,因此它实现了execute()、redo()和undo()方法。起初,我只能调用execute()方法,但我从来没有看到我的EclipseUndo-redo按钮上可以使用undo或redo命令

然后,我在我的RCP应用程序(org.eclipse.ui.examples.undo)中安装了一个示例插件,魔术开始了

使用示例插件附带的视图“Undo History view”,我可以看到我的自定义Undo和redo项被添加到要撤消和重做的内容堆栈中。另外,当“撤消历史视图”处于活动状态时,我的自定义撤消重做操作将显示在Eclipse撤消重做按钮中。这是可行的,但有个问题

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

以下是我所掌握的有关执行该操作的信息。我的InventoryLabelOperation类扩展了org.eclipse.core.commands.operations.AbstractOperation。activePart字段是我的视图部件的一个实例,我想让它在IUndoContext中正确运行,但事实并非如此

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

有人看到我在这里遗漏了什么吗?

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

一旦我进行了操作,请使用此调用返回的上下文

undoContext = 
    (IUndoContext) ResourcesPlugin.getWorkspace().getAdapter(IUndoContext.class);
UI中开始出现预期的情况