How do I open an editor on something that is not a file?

自3.3版本起,可通过新的EFS支持在任何类型的EFS上使用IDE.openEditorOnFileStore打开文本编辑器。此方法适用于数据库对象、远程文件等数据源,但仅限于查看而非编辑。实现步骤包括创建自定义IStorage和IStorageEditorInput,并使用平台默认文本编辑器打开。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:

 
classStringStorageextendsPlatformObject
implementsIStorage...{
privateStringstring;
StringStorage(Stringinput)
...{this.string=input;}
publicInputStreamgetContents()throwsCoreException...{
returnnewByteArrayInputStream(string.getBytes());
}

publicIPathgetFullPath()...{returnnull;}
publicStringgetName()...{
intlen=Math.min(5,string.length());
returnstring.substring(0,len).concat("...");
}

publicbooleanisReadOnly()...{returntrue;}
}



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:

classStringInputextendsPlatformObject
implementsIStorageEditorInput...{
privateIStoragestorage;
StringInput(IStoragestorage)
...{this.storage=storage;}
publicbooleanexists()...{returntrue;}
publicImageDescriptorgetImageDescriptor()...{returnnull;}
publicStringgetName()...{
returnstorage.getName();
}

publicIPersistableElementgetPersistable()...{returnnull;}
publicIStoragegetStorage()...{
returnstorage;
}

publicStringgetToolTipText()...{
return"String-basedfile:"+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:

IWorkbenchWindowwindow=...;
Stringstring
="Thisisthetextfilecontents";
IStoragestorage
=newStringStorage(string);
IStorageEditorInputinput
=newStringInput(storage);
IWorkbenchPagepage
=window.getActivePage();
if(page!=null)
page.openEditor(input,
"org.eclipse.ui.DefaultTextEditor");


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值