【问题标题】:How to close an editor programatically in eclipse [closed]如何在eclipse中以编程方式关闭编辑器[关闭]
【发布时间】:2016-09-19 09:04:02
【问题描述】:

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

【问题讨论】:

    标签: eclipse eclipse-plugin eclipse-rcp eclipse-cdt


    【解决方案1】:

    您可以通过以下方式获取对页面上打开的编辑器的引用:

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

    【讨论】:

    • 也可以直接从editorReference获取IEditorInput(即editorRefs[index].getEditorInput())。这会阻止创建编辑器。
    • 感谢 greg 成功了。赫尔曼也感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2011-01-10
    • 2011-07-15
    • 2015-11-26
    相关资源
    最近更新 更多