Difference between revisions of "FAQ How do I open an editor programmatically?"

From Eclipsepedia

Jump to: navigation, search
Line 1: Line 1:
First of all, this works on eclipse 3.3.0. I haven't tried the codes on the other versions of eclipse.
+
Use the <tt>openEditor</tt> methods on <tt>org.eclipse.ui.IWorkbenchPage</tt> to open an editor on a given input. The <tt>openEditor</tt> methods require you to supply the ID of the editor to open.  You can use the editor registry to find out what editor ID is appropriate for a given file name, using the <tt>getDefaultEditor</tt> method on <tt>IEditorRegistry</tt>. In Eclipse 3.0, the editor opening methods that were specific to <tt>IFile</tt> were moved to the <tt>IDE</tt> class.
 
+
 
+
  
 
<pre>
 
<pre>
            import java.io.File;
+
  IWorkbenchPage page = ...;
 
+
  IFile file = ...;
            import org.eclipse.core.filesystem.EFS;
+
  IEditorDescriptor desc = PlatformUI.getWorkbench().
            import org.eclipse.core.filesystem.IFileStore;
+
      getEditorRegistry().getDefaultEditor(file.getName());
 
+
  page.openEditor(
            import org.eclipse.ui.PartInitException;
+
      new FileEditorInput(file),
            import org.eclipse.ui.IWorkbenchPage;
+
      desc.getId());
            import org.eclipse.ui.PlatformUI;
+
            import org.eclipse.ui.ide.IDE;
+
 
+
            ...
+
 
+
 
+
 
+
            File fileToOpen = new File( textField.getText() );
+
 
+
            if ( fileToOpen.exists() && fileToOpen.isFile() )
+
            {
+
                IFileStore fileStore = EFS.getLocalFileSystem().getStore( fileToOpen.toURI() );
+
 
+
                IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
+
                try
+
                {
+
                    IDE.openEditorOnFileStore( page, fileStore );
+
 
+
                }
+
                catch ( PartInitException e )
+
                {
+
                    //Create your error handler codes here
+
                }
+
            }
+
            else
+
            {
+
                //You may do something if the file is not a file and the file does not exist
+
            }
+
 
</pre>
 
</pre>
  
 
+
This code needs to run on the UI thread and result from getActiveWorkbenchWindow() and getActivePage() need to be checked against null.
 
+
 
== See Also: ==
 
== See Also: ==
 
*[[FAQ Is Eclipse 3.0 going to break all of my old plug-ins?]]
 
*[[FAQ Is Eclipse 3.0 going to break all of my old plug-ins?]]

Revision as of 00:08, 25 February 2008

Use the openEditor methods on org.eclipse.ui.IWorkbenchPage to open an editor on a given input. The openEditor methods require you to supply the ID of the editor to open. You can use the editor registry to find out what editor ID is appropriate for a given file name, using the getDefaultEditor method on IEditorRegistry. In Eclipse 3.0, the editor opening methods that were specific to IFile were moved to the IDE class.

   IWorkbenchPage page = ...;
   IFile file = ...;
   IEditorDescriptor desc = PlatformUI.getWorkbench().
      getEditorRegistry().getDefaultEditor(file.getName());
   page.openEditor(
      new FileEditorInput(file),
      desc.getId());

This code needs to run on the UI thread and result from getActiveWorkbenchWindow() and getActivePage() need to be checked against null.

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.