我正在开发一个XTEXT2.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);
}阅读XText2.0文档时,我还看到有一个EStructuralFeatureNode。我不太了解这种类型的节点是什么以及如何使用它。你能解释一下EStructuralFeatureNode是用来做什么的吗?
非常感谢
发布于 2012-04-13 21:09:24
你的代码有几个问题:
this.myContainerNode:不能保证您的提供者是原型;有人可以将实例配置为单例。因此,请避免使用实例字段。
这个问题有两种解决方案:
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 );
}https://stackoverflow.com/questions/6929456
复制相似问题