Difference between revisions of "FAQ How do I open an editor on something that is not a file?"

From Eclipsepedia

Jump to: navigation, search
 
 
(4 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Most editors will accept as input either an <tt>IFileEditorInput</tt> or an
+
Since 3.3 you can use the new EFS support to open an text editor on a file store that's backed by any kind of EFS using <code>[http://help.eclipse.org/stable/nftopic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/ui/ide/IDE.html#openEditorOnFileStore(org.eclipse.ui.IWorkbenchPage,%20org.eclipse.core.filesystem.IFileStore) IDE.openEditorOnFileStore(IWorkbenchPage, IFileStore)]</code>.
<tt>IStorageEditorInput</tt>. The former can be used only for opening files in the  
+
workspace, but the latter can be used to open a stream of bytes from anywhere.
+
If you want to open a file on a database object, remote file, or  other data
+
source, <tt>IStorage</tt> is the way to go. The only downside is that this is
+
a read-only input type, so you can use it only for viewing a file, not editing it.
+
To use this approach, implement <tt>IStorage</tt> so that it returns the bytes
+
for the file you want to display. Here is an <tt>IStorage</tt> that returns the contents
+
of a string:
+
<pre>
+
  class StringStorage extends PlatformObject
+
    implements IStorage {
+
      private String string;
+
      StringStorage(String input) {this.string = input;}
+
      public InputStream getContents() throws CoreException {
+
        return new ByteArrayInputStream(string.getBytes());
+
      }
+
      public IPath getFullPath() {return null;}
+
      public String getName() {
+
        int len = Math.min(5, string.length());
+
        return string.substring(0, len).concat(&quot;...&quot;);
+
      }
+
      public boolean isReadOnly() {return true;}
+
  }
+
</pre>
+
The class extends <tt>PlatformObject</tt> to inherit the standard implementation of
+
<tt>IAdaptable</tt>, which <tt>IStorage</tt> extends.  The <tt>getName</tt>
+
and <tt>getFullPath</tt> methods can  return <tt>null</tt> if they are not
+
needed.  In this case, we&#146;ve  implemented <tt>getName</tt>
+
to return the first five characters of the string.
+
  
 +
Most editors will accept as input either an <tt>IFileEditorInput</tt> or an <tt>IStorageEditorInput</tt>.  The former can be used only for opening files in the workspace, but the latter can be used to open a stream of bytes from anywhere. If you want to open a file on a database object, remote file, or  other data source, <tt>IStorage</tt> is the way to go.  The only downside is that this is a read-only input type, so you can use it only for viewing a file, not editing it. To use this approach, implement <tt>IStorage</tt> so that it returns the bytes for the file you want to display.  Here is an <tt>IStorage</tt> that returns the contents of a string:
  
The next step is to create an <tt>IStorageEditorInput</tt> implementation  
+
<source lang="java">
that returns your <tt>IStorage</tt> object:
+
class StringStorage implements IStorage {
<pre>
+
  private String string;
   class StringInput extends PlatformObject
+
 
    implements IStorageEditorInput {
+
  StringStorage(String input) {
 +
    this.string = input;
 +
  }
 +
 
 +
  public InputStream getContents() throws CoreException {
 +
    return new ByteArrayInputStream(string.getBytes());
 +
  }
 +
 
 +
  public IPath getFullPath() {
 +
    return null;
 +
  }
 +
 
 +
  public Object getAdapter(Class adapter) {
 +
    return null;
 +
  }
 +
 
 +
  public String getName() {
 +
    int len = Math.min(5, string.length());
 +
    return string.substring(0, len).concat("..."); //$NON-NLS-1$
 +
  }
 +
 
 +
  public boolean isReadOnly() {
 +
    return true;
 +
  }
 +
}
 +
</source>
 +
 
 +
The class extends <tt>PlatformObject</tt> to inherit the standard implementation of <tt>IAdaptable</tt>, which <tt>IStorage</tt> extends.  The <tt>getName</tt> and <tt>getFullPath</tt> methods can  return <tt>null</tt> if they are not needed.  In this case, we've implemented <tt>getName</tt> to return the first five characters of the string.
 +
 
 +
The next step is to create an <tt>IStorageEditorInput</tt> implementation that returns your <tt>IStorage</tt> object:
 +
 
 +
<source lang="java">
 +
   class StringInput implements IStorageEditorInput {
 
       private IStorage storage;
 
       private IStorage storage;
 
       StringInput(IStorage storage) {this.storage = storage;}
 
       StringInput(IStorage storage) {this.storage = storage;}
Line 49: Line 53:
 
       public String getToolTipText() {
 
       public String getToolTipText() {
 
         return "String-based file: " + storage.getName();
 
         return "String-based file: " + storage.getName();
 +
      }
 +
      public Object getAdapter(Class adapter) {
 +
        return null;
 
       }
 
       }
 
   }
 
   }
</pre>
+
</source>
  
 +
Again, many of the methods here are optional.  The <tt>getPersistable</tt> method is used for implementing persistence of your editor input, so the platform can automatically restore your editor on start-up.  Here, we've implemented the bare essentials: the editor name, and a tool tip.
  
Again, many of the methods here are optional.  The <tt>getPersistable</tt> method
+
The final step is to open an editor with this input.  This snippet opens the platform's default text editor on a given string:
is used for implementing persistence of your editor input, so the platform can
+
automatically restore your editor on start-up.  Here, we&#146;ve
+
implemented the
+
bare essentials: the editor name, and a tool tip.
+
  
 
+
<source lang="java">
The final step is to open an editor with this input.  This snippet opens
+
the platform&#146;s default text editor on a given string:
+
<pre>
+
 
   IWorkbenchWindow window = ...;
 
   IWorkbenchWindow window = ...;
 
   String string = "This is the text file contents";
 
   String string = "This is the text file contents";
Line 71: Line 72:
 
   if (page != null)
 
   if (page != null)
 
       page.openEditor(input, "org.eclipse.ui.DefaultTextEditor");
 
       page.openEditor(input, "org.eclipse.ui.DefaultTextEditor");
</pre>
+
</source>
 +
 
 +
== See Also: ==
 +
*[[FAQ How do I open an editor programmatically?]]
 +
*[[FAQ How do I open an editor on a file outside the workspace?]]
  
<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>
+
{{Template:FAQ_Tagline}}

Latest revision as of 13:20, 19 January 2010

Since 3.3 you can use the new EFS support to open an text editor on a file store that's backed by any kind of EFS using IDE.openEditorOnFileStore(IWorkbenchPage, IFileStore).

Most editors will accept as input either an IFileEditorInput or an IStorageEditorInput. The former can be used only for opening files in the workspace, but the latter can be used to open a stream of bytes from anywhere. If you want to open a file on a database object, remote file, or other data source, IStorage is the way to go. The only downside is that this is a read-only input type, so you can use it only for viewing a file, not editing it. To use this approach, implement IStorage so that it returns the bytes for the file you want to display. Here is an IStorage that returns the contents of a string:

class StringStorage implements IStorage {
  private String string;
 
  StringStorage(String input) {
    this.string = input;
  }
 
  public InputStream getContents() throws CoreException {
    return new ByteArrayInputStream(string.getBytes());
  }
 
  public IPath getFullPath() {
    return null;
  }
 
  public Object getAdapter(Class adapter) {
    return null;
  }
 
  public String getName() {
    int len = Math.min(5, string.length());
    return string.substring(0, len).concat("..."); //$NON-NLS-1$
  }
 
  public boolean isReadOnly() {
    return true;
  }
}

The class extends PlatformObject to inherit the standard implementation of IAdaptable, which IStorage extends. The getName and getFullPath methods can return null if they are not needed. In this case, we've implemented getName to return the first five characters of the string.

The next step is to create an IStorageEditorInput implementation that returns your IStorage object:

   class StringInput implements IStorageEditorInput {
      private IStorage storage;
      StringInput(IStorage storage) {this.storage = storage;}
      public boolean exists() {return true;}
      public ImageDescriptor getImageDescriptor() {return null;}
      public String getName() {
         return storage.getName();
      }
      public IPersistableElement getPersistable() {return null;}
      public IStorage getStorage() {
         return storage;
      }
      public String getToolTipText() {
         return "String-based file: " + storage.getName();
      }
      public Object getAdapter(Class adapter) {
        return null;
      }
   }

Again, many of the methods here are optional. The getPersistable method is used for implementing persistence of your editor input, so the platform can automatically restore your editor on start-up. Here, we've implemented the bare essentials: the editor name, and a tool tip.

The final step is to open an editor with this input. This snippet opens the platform's default text editor on a given string:

   IWorkbenchWindow window = ...;
   String string = "This is the text file contents";
   IStorage storage = new StringStorage(string);
   IStorageEditorInput input = new StringInput(storage);
   IWorkbenchPage page = window.getActivePage();
   if (page != null)
      page.openEditor(input, "org.eclipse.ui.DefaultTextEditor");

[edit] 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.