从IEDitorPart(Eclipse)获取ITextViewer

时间:2009-05-28 21:39:47

标签: eclipse eclipse-rcp

Eclipse RCP问题

我打开文件:

IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart editorPart = IDE.openEditor(page, file);

我也得到了以下文件:

IDocument doc = ((ITextEditor)editorPart).getDocumentProvider().getDocument(editorPart.getEditorInput());

我需要访问该文档的文本查看器(用于创建LinkedModeUI),有没有办法做到这一点?

2 个答案:

答案 0 :(得分:6)

以下对我有用:

IEditorPart editorPart = getSite().getPage().getActiveEditor();
if (editorPart != null) {
    ITextOperationTarget target =
            (ITextOperationTarget)editorPart.getAdapter(ITextOperationTarget.class);
    if (target instanceof ITextViewer) {
        ITextViewer textViewer = (ITextViewer)target;
        // ...
    } 
}

答案 1 :(得分:1)

1)可以使用多个编辑器打开一个文档。您必须迭代所有编辑器才能查找文件的编辑器。 2)查看器封装在编辑器中。我认为唯一的方法是扩展编辑器类来添加getter。如果继承人无法访问查看器,请重新定义它。

相关问题