首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

eclipse plugin 获取编辑器内容,看起来挺简单,只是报ClassCastException~

2013-10-04 
eclipse plugin 获取编辑器内容,看上去挺简单,只是报ClassCastException~!公司要我写个eclipse插件获取编

eclipse plugin 获取编辑器内容,看上去挺简单,只是报ClassCastException~!
公司要我写个eclipse插件获取编辑器的输入,我是这么做的:新建hello world的plugin-in project,重写SampleAction的run方法:
public void run(IAction action) {

//取得工作台
IWorkbench workbench = PlatformUI.getWorkbench();
//取得工作台窗口
IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
//取得工作台页面
IWorkbenchPage page = window.getActivePage();
//取得当前处于活动状态的编辑器窗口
IEditorPart part = page.getActiveEditor();
IEditorInput input = part.getEditorInput();  

IDocument document;
       document = ((ITextEditor)part).getDocumentProvider().getDocument(input);  
}

上述代码在最后一行报异常:
java.lang.ClassCastException: org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor cannot be cast to org.eclipse.ui.texteditor.ITextEditor
at t1.actions.SampleAction.run(SampleAction.java:57)

我查看了文档,CompilationUnitEditor是实现了ITextEditor接口的,那是哪里出错了?! ps:本人做c的,初识java一周...

大家帮我看看啊 再搞不出要被骂了...  eclipse插件 异常
[解决办法]
IEditorReference[] refers=PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
for(IEditorReference refer:refers){
IEditorPart part=refer.getEditor(false);
if(part instanceof ITextEditor){
\\...
}
}

取IEditorPart的方式改下试试

热点排行