`

如何在JFace Text Editor中对非文件内容进行编辑

阅读更多

最近开发中碰到要在EMF中对某个模型的内容放到JFace Text Editor中去编辑,于是把Text Editor研究了一番,发现其默认的实现是基于文件的,而我希望对一段字符串进行编辑,看了半天代码没有找到突破口,曾经想过使用临时文件来处理这个问题,好在eclipse.org上找到一篇文章解决了我们的问题,其做法就是使用IStorageEditerInput 来作为Text Editor的输入。原文如下:

FROM:http://wiki.eclipse.org/index.php/FAQ_How_do_I_open_an_editor_on_something_that_is_not_a_file%3F

 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:

java 代码
  1. class StringStorage extends PlatformObject    
  2.    implements IStorage {   
  3.      private String string;   
  4.      StringStorage(String input) {this.string = input;}   
  5.      public InputStream getContents() throws CoreException {   
  6.         return new ByteArrayInputStream(string.getBytes());   
  7.      }   
  8.      public IPath getFullPath() {return null;}   
  9.      public String getName() {   
  10.         int len = Math.min(5, string.length());   
  11.         return string.substring(0, len).concat("...");   
  12.      }   
  13.      public boolean isReadOnly() {return true;}   
  14.   }   

  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: 

java 代码
  1. class StringInput extends PlatformObject    
  2.    implements IStorageEditorInput {   
  3.      private IStorage storage;   
  4.      StringInput(IStorage storage) {this.storage = storage;}   
  5.      public boolean exists() {return true;}   
  6.      public ImageDescriptor getImageDescriptor() {return null;}   
  7.      public String getName() {   
  8.         return storage.getName();   
  9.      }   
  10.      public IPersistableElement getPersistable() {return null;}   
  11.      public IStorage getStorage() {   
  12.         return storage;   
  13.      }   
  14.      public String getToolTipText() {   
  15.         return "String-based file: " + storage.getName();   
  16.      }   
  17.   }   

 

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:

java 代码
  1. IWorkbenchWindow window = ...;   
  2.    String string = "This is the text file contents";   
  3.    IStorage storage = new StringStorage(string);   
  4.    IStorageEditorInput input = new StringInput(storage);   
  5.    IWorkbenchPage page = window.getActivePage();   
  6.    if (page != null)   
  7.       page.openEditor(input, "org.eclipse.ui.DefaultTextEditor");  


分享到:
评论
2 楼 macrochen 2008-12-13  
我已经不错eclipse插件好久了, 所以没法帮助你了
1 楼 jjgoooooole 2008-12-11  
你好,我最近做的东西是eclipse rcp的,也想用IStorageEditorInput
看了文章后,做了个只有这个编辑器的实例,但是运行不起,其它的代码都一样,只是在一个视图中点击tableview的一列表打开编辑器,代码:PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().openEditor(new StringEditorInput("text to be edited"), "org.eclipse.ui.DefaultTextEditor");

但是没有达到效果,确认依赖包都有,不知道使IStorageEditorInput还有什么地方需要注意的,可否给个IStorageEditorInput能运行的简单例子,感激不尽!
jjgoooooole@126.com

相关推荐

Global site tag (gtag.js) - Google Analytics