老十的专栏

下自己的蛋,让别人说去吧!

老十ID:dollyn
6931次访问,排名14168(4)好友23人,关注者78
a
dollyn的文章
原创 15 篇
翻译 0 篇
转载 15 篇
评论 1 篇
老十的公告
  • 主要关注Eclipse插件开发,RCP,GEF,EMF及其他相关技术。
  • 关注:Java脚本引擎。
  • 本博客文章如无特别说明,可以随意转载,但请注明作者和出处。
  • 所有文章仅代表个人观点。
  • 本博客所转载的文章版权归原作者所有。
  • 最近评论
    algcfx:Wow gold
    文章分类
    收藏
      相册
      QQ表情
      Test
      链接
      EclipseWorld(中国Eclipse社区)
      八进制的博客
      疯狂的波菜
      存档
      软件项目交易
      订阅我的博客
      XML聚合  FeedSky

      转载 How do I open an editor on something that is not a file?收藏

      新一篇: eclipse.ini说明 | 旧一篇: How do I open an editor on a file outside the workspace?

      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(page, fileStore).

      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 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("...");
            }

            
      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 extends PlatformObject 
          
      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();
            }

         }



      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");


       

      发表于 @ 2008年03月03日 10:57:00|评论(loading...)|编辑

      新一篇: eclipse.ini说明 | 旧一篇: How do I open an editor on a file outside the workspace?

      评论:没有评论。

      发表评论  


      当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
      Csdn Blog version 3.1a
      Copyright © 老十