Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

FAQ How do I open an editor on a file in the workspace?

Revision as of 16:32, 14 March 2006 by Claffra (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

In Eclipse 2.1, use the openEditor methods on IWorkbenchPage to open files in the workspace. In 3.0, this API was moved to the IDE class in order to remove the dependency between the generic workbench and the workspace. If you use openEditor(IFile), the platform will guess the appropriate editor to use, based on the file extension.


To open an editor to a particular position, you can create a marker in the file and then use openEditor(IMarker). Be sure to get rid of the marker when you’re done. You can specify what editor to open by setting the EDITOR_ID_ATTR on the marker. If you don’t do this, the workbench will guess what kind of editor to open from the file extension. The following code snippet opens the default text editor to line 5, using a marker:

   IFile file = <choose the file to open>;
   IWorkbenchPage page = <the page to open the editor in>;
   HashMap map = new HashMap();
   map.put(IMarker.LINE_NUMBER, new Integer(5));
   map.put(IWorkbenchPage.EDITOR_ID_ATTR, 
      "org.eclipse.ui.DefaultTextEditor");
   IMarker marker = file.createMarker(IMarker.TEXT);
   marker.setAttributes(map);
   //page.openEditor(marker); //2.1 API
   IDE.openEditor(marker); //3.0 API
   marker.delete();

See Also:

FAQ_How_do_I_open_an_editor_programmatically?

FAQ_How_do_I_open_an_editor_on_a_file_outside_the_workspace?

FAQ_How_do_I_open_an_editor_on_something_that_is_not_a_file?


This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.

Copyright © Eclipse Foundation, Inc. All Rights Reserved.