Java examples for JSF:UIComponent
find JSF Components Of Type
import javax.faces.component.UIComponent; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Main{ public static <S extends UIComponent> List<S> findComponentsOfType( UIComponent baseComponent, final Class<S> clazz) { final List<S> results = new ArrayList<>(); recurseChildren(baseComponent, 0, new UIComponentVisitor() { @Override/*from www .j a v a2 s. c om*/ public boolean visit(UIComponent component, int depth) { if (clazz.isInstance(component)) { results.add((S) component); } return true; } }); return results; } /** * Recursively traverse the passed in component structure and call the passed in visitor. * * @param baseComponent * @param depth * @param visitor * @return */ public static boolean recurseChildren(UIComponent baseComponent, int depth, UIComponentVisitor visitor) { if (visitor.visit(baseComponent, depth) == false) { // Abort return false; } if (baseComponent.getFacetsAndChildren() != null) { Iterator<UIComponent> childComponentIterator = baseComponent .getFacetsAndChildren(); while (childComponentIterator.hasNext()) { UIComponent childComponent = childComponentIterator.next(); if (recurseChildren(childComponent, depth + 1, visitor) == false) { // Abort return false; } } } // Keep going return true; } }