从IEDitorPart(Eclipse)获取ITextViewer

从IEDitorPart(Eclipse)获取ITextViewer,eclipse,eclipse-rcp,Eclipse,Eclipse Rcp,Eclipse RCP问题 我打开文件时使用: IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); IEditorPart editorPart = IDE.openEditor(page, file); 我还可以通过以下方式获取文档: IDocument doc = ((ITextEditor)editorPart).getDocumentProvider().

Eclipse RCP问题

我打开文件时使用:

IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart editorPart = IDE.openEditor(page, file);
我还可以通过以下方式获取文档:

IDocument doc = ((ITextEditor)editorPart).getDocumentProvider().getDocument(editorPart.getEditorInput());
我需要访问该文档的文本查看器(用于创建LinkedModelUI),有什么方法可以做到这一点吗?

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

以下内容适用于我:

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