Question

What might be the best way to ask for the root-element of an Ecore-Object? I am listening to certain editors and get the IStructuredSelection from it. But I need to filter the TreeViewers further, since not all of them I am listening to contain the same elements. As far as I can see there is no direct method, which is generated by the EMF itself, that asks for the root-elements. Could you please point me in the right direction? Thanks in regard.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top