Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将IUndoableOperation添加到我的EditorPart时出现上下文问题_Java_Eclipse_Eclipse Plugin_Eclipse Rcp - Fatal编程技术网

Java 将IUndoableOperation添加到我的EditorPart时出现上下文问题

Java 将IUndoableOperation添加到我的EditorPart时出现上下文问题,java,eclipse,eclipse-plugin,eclipse-rcp,Java,Eclipse,Eclipse Plugin,Eclipse Rcp,我遵循中描述的示例 在我的EditorPart /** * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite, * org.eclipse.ui.IEditorInput) */ @Override public void init(IEditorSite site,IEditorInput input) throws PartInitExcepti

我遵循中描述的示例

在我的
EditorPart

    /**
     * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite,
     * org.eclipse.ui.IEditorInput)
     */
    @Override
    public void init(IEditorSite site,IEditorInput input)
    throws PartInitException {
        ...
        IWorkbench workbench = getSite().getWorkbenchWindow().getWorkbench();
        myUndoContext= workbench.getOperationSupport().getUndoContext();


        UndoRedoActionGroup historyActionGroup = new UndoRedoActionGroup(site, myUndoContext, true);
        historyActionGroup.fillActionBars(site.getActionBars());
        }
        IUndoableOperation operation = new MyOperation("My Action");
        operation.addContext(myUndoContext);
        try {
            IStatus status = OperationHistoryFactory.getOperationHistory().execute(operation, null, null);
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
。。。我在我的
EditorPart

    /**
     * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite,
     * org.eclipse.ui.IEditorInput)
     */
    @Override
    public void init(IEditorSite site,IEditorInput input)
    throws PartInitException {
        ...
        IWorkbench workbench = getSite().getWorkbenchWindow().getWorkbench();
        myUndoContext= workbench.getOperationSupport().getUndoContext();


        UndoRedoActionGroup historyActionGroup = new UndoRedoActionGroup(site, myUndoContext, true);
        historyActionGroup.fillActionBars(site.getActionBars());
        }
        IUndoableOperation operation = new MyOperation("My Action");
        operation.addContext(myUndoContext);
        try {
            IStatus status = OperationHistoryFactory.getOperationHistory().execute(operation, null, null);
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

我可以添加撤销操作,但我注意到撤销操作被添加到我的自定义编辑器打开的所有文件中。当我对文件进行更改时,所有其他打开的文件都会在“编辑”菜单中添加“撤消”操作。如何使其仅适用于当前活动的文件?

我认为问题在于这一行:

  myUndoContext= workbench.getOperationSupport().getUndoContext();
这将为您提供一个用于整个工作台的撤销上下文。我认为您需要一个特定于编辑器的撤销上下文
org.eclipse.core.commands.operations.ObjectUndoContext
看起来很合适。这可能就足够了

  myUndoContext = new ObjectUndoContext(this);

尽管Eclipse编辑器中的许多用法都使用and document或undo manager作为对象。

我刚刚修复了类似的问题。我的问题如下:所有打开的编辑器都会出现撤消重做操作。原因是,为每个编辑器创建并添加了
UndoActionHandler
RedoActionHandler
,如下所示:

if (undoCmdHandler == null || !contains(undoCmdHandler)) {
    undoCmdHandler = new UndoActionHandler(site, undoContext);
    actionBars.getToolBarManager().add(undoCmdHandler);
}
redoCmdHandler.setContext(undoContext);
#contains()方法总是返回false(因为它有错误),所以总是创建处理程序。因此,我将其删除:)

在您的示例中,还始终为每个编辑器创建
UndoRedoActionGroup
,并用它填充操作栏。我建议您不要使用操作组,而是自己创建
UndoActionHandler
RedoActionHandler
,如上所示

如果您可以控制处理程序,那么解决方案是只创建
UndoActionHandler
RedoActionHandler
的一个实例,用它填充操作栏,但当您的上下文更改时(当用户在编辑器之间导航时(您应该侦听此事件)),只需更新处理程序上的上下文,如下所示:

if (undoCmdHandler == null || !contains(undoCmdHandler)) {
    undoCmdHandler = new UndoActionHandler(site, undoContext);
    actionBars.getToolBarManager().add(undoCmdHandler);
}
redoCmdHandler.setContext(undoContext);
所以,最终你会得到类似的结果:

public static RedoActionHandler getRedoActionHandler(IWorkbenchPartSite site, IActionBars actionBars, IUndoContext undoContext) {
        if (redoCmdHandler == null) {
            redoCmdHandler = new RedoActionHandler(site, undoContext);
            actionBars.getToolBarManager().add(redoCmdHandler);
        } else {
            redoCmdHandler.setContext(undoContext);
        }
        return redoCmdHandler;
    }

我尝试了你的建议,但一旦我离开编辑器,“撤消”按钮就会被禁用。这是大多数编辑器的正常行为,当编辑器获得焦点时,它应该再次启用。我的意思是,当我离开编辑器后重新访问编辑器时,它会被禁用。