我已经实现了一个 View ,它将自身注册为 XText 编辑器和相关大纲中更改的监听器。为此,我添加了这一行
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().addSelectionListener(this);
在 View 的createPartControl方法中(它实现了ISelectionListener接口(interface))。因此,在 SelectionChanged 方法中,我检查选择是否是 ITextSelection(它来自 XTextEditor)或 IStructuredSelection(它来自 Outline)。
问题是,这样做时,当我启动 Eclipse 时,大纲被称为“不可用”。如果我单击大纲,它会刷新,显示内容并正确更新收听 View 。 我做错了什么以及我应该做什么来避免大纲最初的“不可用”?
最佳答案
我最近遇到了这个问题,并通过在扩展 ViewPart 的类中实现 IPartListener2 来解决它,然后在 createPartcontrol 方法中添加一个部件监听器,如下所示:
getSite().getWorkbenchWindow().getPartService().addPartListener(this);
现在,通过在您的partOpened方法中使用类似的方法(必须在您实际使用部件监听器之前实现该方法,您将首先获得可用的 View 内容:
public void partOpened(IWorkbenchPartReference partRef) {
if(partRef.getPage().getActiveEditor() instanceof XtextEditor) {
somepart=partRef.getPage().getActiveEditor();
final XtextEditor editor = (XtextEditor)somepart;
final IXtextDocument document = editor.getDocument();
document.readOnly(new IUnitOfWork.Void<XtextResource>(){
public void process (XtextResource resource) throws Exception {
IParseResult parseResult = resource.getParseResult();
if(parseResult ==null)
return;
CompositeNode rootNode=(CompositeNode) parseResult.getRootNode();
LeafNode node = (LeafNode)NodeModelUtils.findLeafNodeAtOffset(rootNode, 0);
EObject object =NodeModelUtils.findActualSemanticObjectFor(node);
view.setInput(object);
}
});
}
}
这将使您正在实现的 View 在您激活 XtextEditor(特定于您的 DSL)时获取其内容。 为了使 View 在编辑器中更改 Activity 文件中的任何内容时实时更改内容,您应该实现 IDocumentListener 并重写 DocumentChanged 方法。如果您这样做,您将不再依赖 SelectionListener,因为当文档中发生更改时 View 应该自动更新
希望这有帮助!
关于java - Eclipse (XText) SelectionListener 注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16858699/