Java Eclipse编辑器插件:";“错误”;在项目外部打开文件时
我正在为eclipse开发一个编辑器插件。它在eclipse项目中的文件上运行良好,但是当通过“文件->打开文件”菜单打开一个外部文件(该菜单适用于文件,例如Java文件)时,我会看到一个页面,除了一条水平的蓝线和一个“ERROR”字外,什么也没有显示。eclipse的错误日志是空的,.metadata目录中的日志文件也是空的 这是什么原因造成的?当我没有错误消息告诉我去哪里查找时,如何诊断错误?似乎没有办法从eclipse获得更详细的日志记录 编辑:Java Eclipse编辑器插件:";“错误”;在项目外部打开文件时,java,eclipse-plugin,eclipse-pde,Java,Eclipse Plugin,Eclipse Pde,我正在为eclipse开发一个编辑器插件。它在eclipse项目中的文件上运行良好,但是当通过“文件->打开文件”菜单打开一个外部文件(该菜单适用于文件,例如Java文件)时,我会看到一个页面,除了一条水平的蓝线和一个“ERROR”字外,什么也没有显示。eclipse的错误日志是空的,.metadata目录中的日志文件也是空的 这是什么原因造成的?当我没有错误消息告诉我去哪里查找时,如何诊断错误?似乎没有办法从eclipse获得更详细的日志记录 编辑: 我发现问题的根源与jamesh提到的很接近
我发现问题的根源与jamesh提到的很接近,但不是ClassCastException-文本查看器根本没有要显示的
IDocument
实例,因为StorageDocumentProvider.createDocument()
返回空值。原因是它只知道如何为org.eclipse.ui.IStorageEditorInput
的实例创建文档,但在本例中,它会获得一个org.eclipse.ui.ide.FileStoreEditorInput
的实例,该实例不实现该接口,但实际上实现了org.eclipse.ui.IURIEditorInput您是否尝试在工作区之外使用编辑器创建JAVA文件
当使用文件路径调用编辑器时,在文件路径的开头加上“file://”。例如:如果路径是C://temp//Sample.java,则将其修改为file://C://temp//Sample.java。我现在离源代码有点远,尽管我怀疑问题是
类castexception
:
- 对于工作区文件,
是IEditorInput
org.eclipse.ui.IFileEditorInput
- 对于本地非工作区文件,
是IEditorInput
org.eclipse.ui.IStorageEditorInput
IEditorInput
获取内容。JDT执行显式的instanceof
检查以进行切换
我不认为getAdapter(Class clazz)
会返回java.io.InputStream
,如果您提供它的话
我不太明白他们为什么这样做,但感觉很难看
编辑:
关于调试eclipse应用程序的一个更一般的观点——尝试将所有日志组装到一个地方(即控制台)是非常有用的
为此,请确保使用命令行选项
-console
和-consoleLog
。后者帮助节省了无数个小时的时间。如果您还没有,请学习有关如何使用控制台的最基本的知识(ss
和start
是我最常用的)。这将为诊断某类问题节省更多的时间。我也遇到了同样的问题,最终找到了适合我的解决方案。
您必须提供两个不同的文档提供程序—第一个扩展FileDocumentProvider用于工作台内的文件,第二个扩展TextFileDocumentProvider用于工作区外的其他资源。然后根据编辑器中的输入注册正确的提供者,如下所示:
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);
}
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;
}
然后在新的文档提供程序(扩展TextFileDocumentProvider)中插入如下内容:
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);
}
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;
}
这对我很有用:)最后我必须提到,我不是那么聪明,我从project Amateras(eclipse的开源HTML编辑器插件)复制了这个解决方案。我根本没有调用编辑器,我是通过eclipse的菜单打开一个文件。它是一种特定的文件类型,其扩展名导致它在我的编辑器插件中打开。我只是提到Java作为一个编辑器的例子,它可以工作。这听起来肯定是原因——我会在我可以的时候检查(我现在也不知道代码了:)谢谢!对于编辑器插件示例(Eclipse3.7)不能与非工作区文件一起开箱即用,我感到非常沮丧。仅供参考,在3.7中,此代码中的类型检查无法正常工作。如果您改为执行以下操作,它将起作用:
IFileEdit或Input的实例=>XMLFileDocumentProvider,else
=>XMLTextDocumentProvider。此外,如果在这两种情况下都只使用XMLTextDocumentProvider(即TextFileDocumentProvider),它也可以工作。更多信息。我不确定扩展FileDocumentProvider的原因。