简体   繁体   English

eclipse插件如何通过代码在IDE中打开文件

[英]eclipse plugin how to open file in IDE by code

I am working on eclipse plugin in which i have to open a file from project explorer. 我正在研究eclipse插件,我必须从项目资源管理器中打开一个文件。 Suppose i have a project ABC in project explorer. 假设我在项目资源管理器中有一个项目ABC。 after right click on project i got a option to run my plugin tool. 右键单击项目后,我有一个选项来运行我的插件工具。 after processing i got some result like Check file xyz.java. 在处理之后我得到了一些结果,比如检查文件xyz.java。

Now i want to open this file in IDE by code 现在我想通过代码在IDE中打开此文件

i am using this 我正在使用这个

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

here it's create a new file named decider in c drive which means it's getting a wrong path. 这里它在c盘中创建一个名为decider的新文件,这意味着它的路径错误。

but when i use path code in some independent java file as a normal JAVA project it's getting the correct path. 但是当我在一些独立的java文件中使用路径代码作为普通的JAVA项目时,它正在获得正确的路径。

For a file in the workspace you should use IFile . 对于工作空间中的文件,您应该使用IFile If you have a selection from Project Explorer or another view that should already be an IFile or can be adapted to an IFile . 如果您从Project Explorer或其他视图中选择了应该已经是IFile或可以适应IFile

If you just have a workspace relative path use ResourcesPlugin.getWorkspace().getRoot().getFile(path) (path would include a project). 如果您只有一个工作空间相对路径,请使用ResourcesPlugin.getWorkspace().getRoot().getFile(path) (路径将包含一个项目)。

To open the default editor for the file contents use 要打开文件内容的默认编辑器,请使用

IDE.openEditor(page, file, true);

to open a specific editor use 打开特定的编辑器使用

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM