Vaadin controls, just like every other UI, creates a child-parent tree. This class helps to retrieve parent of a specified type. getParent() is extremely useful when invoked deep withing the tree structure to e.g. retrieve the main tab control and close it.
/**
* The purpose of this class is to help with the management of Vaadin controls child-parent tree.
*/
public class ControlHelper {
private static final int _MAXIMUM_PARENT_ITERATION_COUNT = 100;
/**
* Get parent of the control which has a specified type.
*
* @param cls type of the control to return
* @param control origin control (where to start traversing the tree)
* @param <T>
* @return
*/
public static <T extends AbstractComponent> T getParent(Class<T> cls, AbstractComponent control)
{
if (control == null)
return null;
AbstractComponent parent = control;
for (int i = 0; i < _MAXIMUM_PARENT_ITERATION_COUNT; i++)
{
if (parent.getParent() == null)
return null;
//Return parent of the desired class type
if (parent.getParent().getClass().isAssignableFrom(cls))
return (T) parent.getParent();
parent = (AbstractComponent) parent.getParent();
}
return null;
}
}