对轮廓节点进行分组

发布于 2024-11-27 18:05:17 字数 686 浏览 3 评论 0原文

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

非常感谢

I'm developing an XTEXT 2.0 plugin. I'd like to group some nodes inside my outline in a "virtual" node. Which is the right way to achieve this result?

Currently if i want to group nodes of type "A", in my OutlineTreeProvider I define the following method

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);
}

Reading the Xtext 2.0 documentation i saw also that there is a EStructuralFeatureNode. I didn't understand exactly what this type of node is and how to use it. Can you explain what EStructuralFeatureNode is used for?

Many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只为守护你 2024-12-04 18:05:17

您的代码存在一些问题:

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 );
}

There are a couple of problems with your code:

this.myContainerNode: There is no guarantee that your provider is a prototype; someone could configure the instance as singleton. Therefore, avoid instance fields.

There are two solutions to this problem:

  1. Search the parent node for your container node whenever you need it (slow but simple)
  2. Add a cache to your instance (see How do I attach some cached information to an Eclipse editor or resource?)

super._createNode(): Don't call the methods with _, always call the plain version (super.createNode()). That method will figure out which overloaded _create* method to call for you. But in your case, you can't call any of these methods because you'd get a loop. Call createEObjectNode() instead.

Lastely, you don't need to create an instance of A (S3DFactoryImpl.eINSTANCE.createA()). Nodes can be backed by model elements but that's optional.

For grouping, I use this class:

public class VirtualOutlineNode extends AbstractOutlineNode {
    protected VirtualOutlineNode( IOutlineNode parent, Image image, Object text, boolean isLeaf ) {
        super( parent, image, text, isLeaf );
    }
}

In your case, the code would look like so:

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 );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文