![](/img/trans.png)
[英]Opening custom editor when a file contains an specific text (Eclipse RCP)
[英]Eclipse editor plugin: “ERROR” when opening file outside project
我正在為eclipse開發一個編輯器插件。 它在eclipse項目中的文件上工作正常,但是當通過“文件 - >打開文件”菜單(用例如Java文件工作文件)打開外部文件時,我得到的頁面只顯示水平藍線和單詞“ERROR”。 eclipse的錯誤日志為空,與.metadata目錄中的日志文件一樣。
什么可能導致這個? 當我沒有錯誤消息告訴我在哪里查看時,如何診斷錯誤? 似乎沒有辦法從eclipse獲得更詳細的日志記錄。
編輯:
我發現問題的根源與jamesh提到的相近,但不是ClassCastException - 因為StorageDocumentProvider.createDocument()
返回null,因此沒有IDocument
實例供文本查看器顯示。 這樣做的原因是它只知道如何為org.eclipse.ui.IStorageEditorInput
實例創建文檔,但是在這種情況下它獲取了一個org.eclipse.ui.ide.FileStoreEditorInput
的實例,它沒有實現該接口,但是實現了org.eclipse.ui.IURIEditorInput
我遇到了同樣的問題,終於找到了適合我的解決方案。 您必須提供2個不同的文檔提供程序 - 首先為工作台中的文件擴展FileDocumentProvider ,然后為工作區外的其他資源擴展TextFileDocumentProvider 。 然后根據編輯器doSetInput方法中的輸入注冊正確的提供程序,如下所示:
private IDocumentProvider createDocumentProvider(IEditorInput input) {
if(input instanceof IFileEditorInput){
return new XMLTextDocumentProvider();
} else if(input instanceof IStorageEditorInput){
return new XMLFileDocumentProvider();
} else {
return new XMLTextDocumentProvider();
}
}
@Override
protected final void doSetInput(IEditorInput input) throws CoreException {
setDocumentProvider(createDocumentProvider(input));
super.doSetInput(input);
}
然后在你的新文檔提供程序(擴展TextFileDocumentProvider)中插入somethnig,如下所示:
protected FileInfo createFileInfo(Object element) throws CoreException {
FileInfo info = super.createFileInfo(element);
if(info==null){
info = createEmptyFileInfo();
}
IDocument document = info.fTextFileBuffer.getDocument();
if (document != null) {
/* register your partitioner and other things here
same way as in your fisrt document provider */
}
return info;
}
這對我有用:)最后我要提一下,我不是那么聰明,我從項目Amateras(eclipse的Opensource HTML編輯器插件)復制了這個解決方案
我目前稍微遠離源代碼,但我懷疑問題是ClassCastException
:
IEditorInput
是org.eclipse.ui.IFileEditorInput
。 IEditorInput
是org.eclipse.ui.IStorageEditorInput
不同之處在於如何從IEditorInput
獲取內容。 JDT進行顯式的檢查instanceof
以進行切換。
如果你提供它,我認為getAdapter(Class clazz)
不會返回java.io.InputStream
。
我不太明白為什么他們這樣做,但感覺很難看。
編輯:關於調試eclipse應用程序的更一般的觀點 - 嘗試將所有日志組裝到一個地方(即控制台)非常有用。
為此,請確保使用命令行選項-console
和-consoleLog
。 后者幫助節省了無數小時的時間。 如果你還沒有,請學習如何使用控制台的最基本的東西( ss
和start
是我最常用的)。 這將節省更多時間來診斷某類問題。
您是否嘗試使用工作區外的編輯器創建JAVA文件?
使用文件路徑調用編輯器時,在文件path.eg的開頭連接“file://”:如果路徑是C://temp//Sample.java,則將其修改為file:// C ://temp//Sample.java。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.