4

我正在开发一个 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 的用途吗?

非常感谢

4

1 回答 1

2

您的代码有几个问题:

this.myContainerNode:无法保证您的提供者是原型;有人可以将实例配置为单例。因此,请避免使用实例字段。

这个问题有两种解决方案:

  1. 随时在父节点中搜索您的容器节点(缓慢但简单)
  2. 向您的实例添加缓存(请参阅如何将一些缓存信息附加到 Eclipse 编辑器或资源?

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 );
}
于 2012-04-13T13:09:24.100 回答