0

询问 Ecore-Object 的根元素的最佳方式可能是什么?我正在听取某些编辑的意见并从中获取IStructuredSelection信息。但我需要TreeViewers进一步过滤,因为并非我所听的所有内容都包含相同的元素。据我所见,没有直接的方法,它是由 EMF 本身生成的,它要求根元素。你能指出我正确的方向吗?对此表示感谢。

4

1 回答 1

1

If you have an EObject, you may get the desired result just by recursively checking the eContainer(). Such as:

public static EObject getRoot(EObject eo) {
    EObject parent = eo.eContainer();
    if (parent != null) {
        return getRoot(parent);
    }
    return eo;
}

For any EObject this should return the top-level EObject that contains it.

But instead of rolling your own like that, you may want to rely on getRootContainer() in the EcoreUtil class. Don't overlook EcoreUtil when working with EMF, it has helper methods at least some of which are bound to be useful in an EMF application.

于 2013-10-28T15:39:29.770 回答