9

我正在开发 eclipse 插件,我必须从项目资源管理器中打开一个文件。假设我在项目资源管理器中有一个项目 ABC。右键单击项目后,我可以选择运行我的插件工具。处理后我得到了一些结果,比如检查文件 xyz.java。

现在我想通过代码在IDE中打开这个文件

我正在使用这个

File absolute = new File("/Decider.java");  
File file = new File("/Decider.java");
IFileStore fileOnLocalDisk = EFS.getLocalFileSystem().getStore(absolute.toURI() );

FileStoreEditorInput editorInput = new FileStoreEditorInput(fileOnLocalDisk);

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

 try {  
    page.openEditor(editorInput, "org.eclipse.ui.DefaultTextEditor");         

    page.openEditor(editorInput, "MyEditor.editor");          

        IFileStore fileStore = EFS.getLocalFileSystem().getStore(absolute.toURI() );
        IDE.openEditorOnFileStore( page, fileStore );

      } catch (PartInitException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    try {
        System.out.println(file.getCanonicalPath());
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }


    IPath path = new Path(" /DirectoryReader.java");
    IFile sampleFile =  ResourcesPlugin.getWorkspace().getRoot().getFile(path);

    IEditorInput editorInput1 = new FileEditorInput(sampleFile);
    IWorkbenchWindow window1=PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    IWorkbenchPage page1 = window1.getActivePage();
    try {
        page1.openEditor(editorInput1, "org.eclipse.ui.DefaultTextEdtior");
    } catch (PartInitException e1) {

        e1.printStackTrace();
    } 

在这里,它在 C 驱动器中创建了一个名为 decisioner 的新文件,这意味着它的路径错误。

但是当我在一些独立的java文件中使用路径代码作为普通的JAVA项目时,它得到了正确的路径。

4

1 回答 1

8

对于工作区中的文件,您应该使用IFile. 如果您从 Project Explorer 或其他应该已经是IFile或可以适应IFile.

如果您只有一个工作区相对路径使用ResourcesPlugin.getWorkspace().getRoot().getFile(path)(路径将包括一个项目)。

要打开文件内容的默认编辑器,请使用

IDE.openEditor(page, file, true);

打开特定的编辑器使用

IDE.openEditor(page, file, "editor id");

IDE 是 org.eclipse.ui.ide.IDE。

于 2013-10-08T07:10:25.710 回答