Java examples for JSF:UIComponent
Find a list of JSF components starting at the base Component that is of the type clazz.
import javax.faces.component.UIComponent; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Main{ /**/*from w ww .jav a2s . c om*/ * Find a list of components starting at the baseComponent that is of * the type clazz. This does not check 'instanceof'. This is an exact * match. * * @param baseComponent * @param clazz * @param <S> * @return */ public static <S extends UIComponent> List<S> findComponentsByClassName( UIComponent baseComponent, final Class<S> clazz) { final List<S> results = new ArrayList<>(); recurseChildren(baseComponent, 0, new UIComponentVisitor() { @Override public boolean visit(UIComponent component, int depth) { if (clazz.getName().equals(component.getClass().getName())) { 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; } }