博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How do I open an editor on something that is not a file?
阅读量:5984 次
发布时间:2019-06-20

本文共 2917 字,大约阅读时间需要 9 分钟。

  hot3.png

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

转载于:https://my.oschina.net/dollyn/blog/360634

你可能感兴趣的文章
Android adb.exe 启动失败
查看>>
我的友情链接
查看>>
使用JavaMail完成邮件的编写
查看>>
Xcode8修改或者新建的XIB文件 xcode7上报错问题
查看>>
MSSQL获取昨天,本周,本月。。。
查看>>
记录:我的大学的最后时光(大三下学期 _11
查看>>
标签使用 struts2
查看>>
windows下如何查看80端口被哪个服务占用了
查看>>
LinuxCast 视频教程笔记 - Ubuntu
查看>>
python 常用模块
查看>>
confluence使用问题总结
查看>>
文章分享:Android四大组件详解
查看>>
Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp
查看>>
自定义ActionBar高分辨率下左边缺一块的解决方案
查看>>
OSPF、EIGRP路由汇总
查看>>
ISCSI安装配置
查看>>
京东金融大数据竞赛猪脸识别(2)- 图像特征提取之一
查看>>
jstl 一些表达式的用法
查看>>
Zookeeper实战之单机模式
查看>>
同步,异步,并行和串行之间区别
查看>>