How do I open an editor on something that is not a file?
2014-12-25 00:00
507 查看
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
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:
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:
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:
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 " );
相关文章推荐
- How do I open an editor on something that is not a file?
- How do I open an editor on a file outside the workspace?
- How do I open an editor on a file outside the workspace?
- file - How do I remove/delete a folder that is not empty with Python? - Stack Overflow
- Error 24 Could not delete file 'e:/debug/vc80.idb'. Make sure that the file is not open by another p
- Slow network performance when you open a file that is located in a shared folder on a remote network computer
- HBase中RegionServers报异常: No lease on /apps/hbase/data/oldWALs... File is not open for writing
- You do not have permissions to open this file on Excel Services
- 编程技巧之:VC6.0在打开ResourceView 时,无法打开,报This file is already open in an editor.怎么办? .
- This file is already open in an editor问题的解决方法
- This file is already open in an editor
- the file is already open in an editor 解决方法
- the file is already open in an editor 解决方法 .
- hive 异常 (Attempt to do update or delete on table terminal that does not use an )
- Android Studio添加应用作为依赖时报错Error:Dependency MonthText:xlistview:unspecified on project app resolves to an APK archive which is not supported as a compilation dependency. File: 及其解决方案
- 编程技巧之:VC6.0在打开ResourceView 时,无法打开,报This file is already open in an editor.怎么办?
- You do not have permissions to open this file on Excel Services
- vc中如何解决“This file is already open in an editor”
- How to run a user control assembly that is hosted on Internet Information Services (IIS) in Internet Explorer
- A 2007 Office system application does not load an add-in that is developed by using VSTO