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

Difference between revisions of "FAQ How do I open an editor on a file in the workspace?"

 
m
 
Line 1: Line 1:
In Eclipse 2.1, use the <tt>openEditor</tt> methods on <tt>IWorkbenchPage</tt>
+
In Eclipse 2.1, use the <tt>openEditor</tt> methods on <tt>IWorkbenchPage</tt> 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 <tt>openEditor(IFile)</tt>, the platform will guess the appropriate editor to use, based on the file extension.
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 <tt>openEditor(IFile)</tt>, 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
+
To open an editor to a particular position, you can create a marker in the file and then use <tt>openEditor(IMarker)</tt>.  Be sure to get rid of the marker when you&#146;re done.  You can specify what editor to open by setting the <tt>EDITOR_ID_ATTR</tt> on the marker.  If you don&#146;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:
file and then use <tt>openEditor(IMarker)</tt>.  Be sure
+
 
to get rid of the marker when you&#146;re done.  You can specify what editor to
+
open by setting the <tt>EDITOR_ID_ATTR</tt> on the marker.  If you  
+
don&#146;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:
+
 
<pre>
 
<pre>
 
   IFile file = &lt;choose the file to open&gt;;
 
   IFile file = &lt;choose the file to open&gt;;
Line 28: Line 19:
  
 
== See Also: ==
 
== 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?]]
  
[[FAQ_How_do_I_open_an_editor_programmatically%3F]]
+
{{Template:FAQ_Tagline}}
 
+
[[FAQ_How_do_I_open_an_editor_on_a_file_outside_the_workspace%3F]]
+
 
+
[[FAQ_How_do_I_open_an_editor_on_something_that_is_not_a_file%3F]]
+
 
+
<hr><font size=-2>This FAQ was originally published in [http://www.eclipsefaq.org Official Eclipse 3.0 FAQs]. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the [http://www.eclipse.org/legal/epl-v10.html Eclipse Public License v1.0].</font>
+

Latest revision as of 20:00, 29 May 2006

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&#146;re done. You can specify what editor to open by setting the EDITOR_ID_ATTR on the marker. If you don&#146;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:


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.

Back to the top