Eclipse 4.7.1a - 在 code/programatically 的编辑器中打开文件

Eclipse 4.7.1a - open file in editor from code/programatically

我想为我的 eclipse 插件添加功能,以便在新的 Eclipse 编辑器中打开指定的文件。我找到了基于 org.eclipse.ui.ide.IDEimport org.eclipse.core.filesystem.EFS:

的解决方案
import java.io.File;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;

File fileToOpen = new File("externalfile.xml");

if (fileToOpen.exists() && fileToOpen.isFile()) {
    IFileStore fileStore = EFS.getLocalFileSystem().getStore(fileToOpen.toURI());
    IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();

    try {
        IDE.openEditorOnFileStore( page, fileStore );
    } catch ( PartInitException e ) {
        //Put your exception handler here if you wish to
    }
} else {
    //Do something if the file does not exist
}

不幸的是,该解决方案在第 4 版 Eclipse 中不再受支持,并且仅适用于较旧的 IDE。

是否有可能在 Eclipse 4.x.x 中以编程方式在指定文件上打开编辑器或使 workaround/deal 具有兼容性?

Eclipse 4 仍然支持 3.x 风格的代码(3.x 兼容模式)所以这应该仍然有效,除非你正在编写一个纯 'e4' 风格的应用程序。

解决方案非常简单 - 当我想导入前面列出的 类 时,Eclipse 无法识别这些导入,所以一开始我认为它根本不兼容。我又做了一项研究,在快速修复菜单中找到 Fix project setup... 选项。该选项只是将所需的依赖项添加到我的功能项目中,所以现在它们是可见的,并且没有更多的错误。