如何在 Eclipse 中以编程方式关闭编辑器

How to close an editor programatically in eclipse

我们想在删除一个项目(IProject)时关闭它在eclipse中所有打开的文件。我可以通过项目的成员方法访问 IFiles。我想关闭已删除项目的所有文件。

您可以在页面上获取对打开的编辑器的引用:

IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchPage page = workbenchWindow.getActivePage();

IEditorReference[] editorRefs = page.getEditorReferences();

从参考中获取实际的编辑器部分:

IEditorPart editor = editorRefs[index].getEditor(true);

获取编辑器输入:

IEditorInput input = editor.getEditorInput();

获取编辑器正在编辑的文件:

IFile file = (IFile)input.getAdapter(IFile.class);

关闭编辑器:

page.closeEditor(editor, true);