Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. Suppose i have a project ABC in project explorer. 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.

Now i want to open this file in IDE by code

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.

but when i use path code in some independent java file as a normal JAVA project it's getting the correct path.

like image 696
user2379020 Avatar asked Oct 08 '13 04:10

user2379020


People also ask

How do I browse code in Eclipse?

Select the type you are interested in and click OK. Eclipse will open up an editor showing the selected type. If source code is not available for the selected type it will use the Class File editor to show the byte code of the selected type.

How do I edit a file in Eclipse?

Editing a text file To edit a file, double click it in the Project explorer or Package explorer view. Whenever you open a text file, change it, or the editor receives the focus, the file is checked for errors. If there are errors (or warnings), they are displayed in the editor as shown below.

How do I open a package editor in Eclipse?

To view the project explorer, click on Window menu then, click on Show View and select Project Explorer. There is simpler way to open project explorer, when you are in the editor press alt + shift + w and select project explorer.


1 Answers

For a file in the workspace you should use 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.

If you just have a workspace relative path use ResourcesPlugin.getWorkspace().getRoot().getFile(path) (path would include a project).

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.

like image 92
greg-449 Avatar answered Sep 20 '22 10:09

greg-449