/** * Finds the first component instance in each {@link Element} subtree by * traversing the {@link Element} tree starting from the given element. * * @param element * the element to start scanning from * @param componentConsumer * a consumer which is called for each found component */ public static void findComponents(Element element, Consumer<Component> componentConsumer) { assert element != null; assert componentConsumer != null; Optional<Component> maybeComponent = element.getComponent(); if (maybeComponent.isPresent()) { Component component = maybeComponent.get(); componentConsumer.accept(component); return; } element.getChildren().forEach(childElement -> ComponentUtil .findComponents(childElement, componentConsumer)); }
@Override public Stream<Component> getChildren() { Builder<Component> childComponents = Stream.builder(); container.getChildren().forEach(childElement -> ComponentUtil .findComponents(childElement, childComponents::add)); return childComponents.build(); }
/** * Gets the child components of this component. * <p> * The default implementation finds child components by traversing each * child {@link Element} tree. * <p> * If the component is injected to a PolymerTemplate using the * <code>@Id</code> annotation the getChildren method will only return * children added from the server side and will not return any children * declared in the template file. * * @see Id * * @return the child components of this component */ public Stream<Component> getChildren() { // This should not ever be called for a Composite as it will return // wrong results assert !(this instanceof Composite); if (!getElement().getComponent().isPresent()) { throw new IllegalStateException( "You cannot use getChildren() on a wrapped component. Use Component.wrapAndMap to include the component in the hierarchy"); } Builder<Component> childComponents = Stream.builder(); getElement().getChildren().forEach(childElement -> ComponentUtil .findComponents(childElement, childComponents::add)); return childComponents.build(); }