From the Eclipse plugin, how can I programmatically force Eclipse to edit a specific file?

I have a plugin that creates a new view. In my opinion, I show some information about some Java classes in the project. I want to allow the user to double-click on a class in my view, and when he / she does, I want to open this class for editing in the editor. Basically similar to what the Hierarchy view does: it displays the class tree and when the user double-clicks on one of them, it goes to the editor. How to do this if I have an object of type IType?

+3
source share
1 answer

I will demonstrate a high level implementation for your requirement. You must take the following steps.

  • first you need to make an entry in Plugin.xmlfor the class, which extends viewPartboth editorPartfor presentation and editor, respectively.
  • to represent the hierarchy, you need to load the tree structure information into viewPart.
  • In node, double click Listener, you need to open the editor.

To open a file in an editor, use the following code to open an editor.

 if (fileToOpen.exists() && fileToOpen.isFile()) {
     String path = //Path for that to file to open;
     IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
     URI fromString = org.eclipse.core.runtime.URIUtil.fromString("file://" + path);
     try {
         IEditorPart openEditor = IDE.openEditor(page, fromString, Editor.ID, true);
         IEditorInput editorInput = openEditor.getEditorInput();
         //editorInput.
     } catch ( PartInitException e ) {
         //Put your exception handler here if you wish to.
     }
 }
0
source

All Articles