When you click on a file in the Package Explorer or Navigator, the file will open the associated editor. If you are looking for a way to do the same action through code, this tip is for you.



There are three things you need to open an editor:
(*) The IEditorInput for the resource you want to open
(*) A IWorkbenchPage, in which you want to open the editor
(*) The id of the editor, which you want to open.

For this tip, we assume that you want to open a file. So we can use FileEditorInput.

IEditorInput editorInput = new FileEditorInput(fileToBeOpened);

;

We will open the editor in the active page of the active window.

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


Open the TextEditor in that page.

page.openEditor(editorInput, "org.eclipse.ui.DefaultTextEdtior");



So here goes the entire snippet:

IFile fileToBeOpened = ...;
IEditorInput editorInput = new FileEditorInput(fileToBeOpened);
IWorkbenchWindow window=PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchPage page = window.getActivePage();
page.openEditor(editorInput, "org.eclipse.ui.DefaultTextEdtior");

 

 

More articles :

» Contributing Workbench Wizards thru Commands

You must have been used this JDT toolbar quite often.The New Class tool item is contributed as an Action in (id "org.eclipse.jdt.ui.actions.NewTypeDropDown"), and then the Menu on the Action is contributed via . In this tip, let us see how to do...

» Selection Dialogs in Eclipse

If you are an Eclipse Plug-in developer, you must have used the MessageDialog. There are many other Dialogs provided by Eclipse Platform are reusable and part of the API. I'll try to explain the various selection dialogs that I know of. In case I've...

» A short tutorial on Intro/Welcome

 Welcome page in Eclipse provides you a unique way to introduce your product to the new users. Its very flexible and can be extended by other plugins. You can create the entire Welcome view by yourself by extending the org.eclipse.ui.intro and...

» Creating the defacto RCP Mail application

  In many of the RCP related tips in this website and other sites, the starting point would be this one liner "Create the RCP Mail application". Instead of repeatedly this step in those tips, I've put the steps in this item. Step 1: Open the "New"...

» How to open an external file in Eclipse

Background Based on work in SWT () and the native launcher (), the Eclipse IDE now supports opening a file from the command line, or opening a file that has been associated with the Eclipse executable ().

Subscribe To

Unless stated, all the text contents of this site is available under Eclipse Public License