我正在开发一个 XTEXT 2.0 插件。我想在“虚拟”节点中将大纲内的一些节点分组。哪种方法是实现此结果的正确方法?
目前,如果我想对“A”类型的节点进行分组,在我的 OutlineTreeProvider 中我定义了以下方法
protected void _createNode(IOutlineNode parentNode, A node) {
if(this.myContainerNode == null){
A container = S3DFactoryImpl.eINSTANCE.createA();
super._createNode(parentNode, container);
List<IOutlineNode> children = parentNode.getChildren();
this.myContainerNode = children.get(children.size()-1);
}
super._createNode(this.myContainerNode, node);
}
阅读 Xtext 2.0 文档我还看到有一个 EStructuralFeatureNode。我不完全了解这种类型的节点是什么以及如何使用它。你能解释一下 EStructuralFeatureNode 的用途吗?
非常感谢
最佳答案
您的代码有几个问题:this.myContainerNode
:无法保证您的提供者是原型(prototype);有人可以将实例配置为单例。因此,请避免使用实例字段。
这个问题有两种解决方案:
super._createNode()
: 不要用 _
调用方法,总是调用普通版本( super.createNode()
)。该方法将找出哪个重载_create
* 为您调用的方法。但在你的情况下,你不能调用任何这些方法,因为你会得到一个循环。调用 createEObjectNode()
反而。最后,您不需要创建
A
的实例。 (S3DFactoryImpl.eINSTANCE.createA()
)。节点可以由模型元素支持,但这是可选的。对于分组,我使用这个类:
public class VirtualOutlineNode extends AbstractOutlineNode {
protected VirtualOutlineNode( IOutlineNode parent, Image image, Object text, boolean isLeaf ) {
super( parent, image, text, isLeaf );
}
}
在您的情况下,代码如下所示:
protected void _createNode(IOutlineNode parentNode, A node) {
VirtualOutlineNode group = findExistingNode();
if( null == group ) {
group = new VirtualOutlineNode( parentNode, null, "Group A", false );
}
// calling super._createNode() or super.createNode() would create a loop
createEObjectNode( group, node );
}
关于eclipse - 分组大纲节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6929456/