Created
July 9, 2009 22:11
-
-
Save keithrbennett/144041 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.apache.wicket.MarkupContainer; | |
| import org.apache.wicket.Component; | |
| import org.apache.wicket.util.tester.WicketTester; | |
| import java.util.List; | |
| import java.util.ArrayList; | |
| /** | |
| * Provides a method to get a list or descriptive string of descendants | |
| * of a container, with their wicket ID's, and class types. | |
| */ | |
| public class ContainerDescendantReporter { | |
| /** | |
| * For the specified container Wicked id path, returns a string containing, | |
| * for each descendant component, the Wicket id path and the class | |
| * of the component. Note that although it is possible to find descendants | |
| * for a MarkupContainer with getReportString(MarkupContainer), it is | |
| * not possible to do so with this method, since this method looks for | |
| * Components, not MarkupContainers. | |
| * | |
| * Uses WicketTester.getComponentFromLastRenderedPage(). | |
| * | |
| * @param wicketIdPath id path of the container, relative to the page | |
| * can be "" to specify the page | |
| * @param tester | |
| */ | |
| public static String getReportString(String wicketIdPath, WicketTester tester) { | |
| return getReportString( | |
| (MarkupContainer) tester.getComponentFromLastRenderedPage(wicketIdPath)); | |
| } | |
| /** | |
| * For the specified container, returns a string containing, for each | |
| * descendant component, the Wicket id path and the class of the component. | |
| * | |
| * @param markupContainer | |
| */ | |
| public static String getReportString(MarkupContainer markupContainer) { | |
| StringBuffer sbuf = new StringBuffer(); | |
| sbuf.append("\n\nDescendants:\n"); | |
| { | |
| List<Component> formComponents = getDescendantComponents(markupContainer); | |
| for (Component c : formComponents) { | |
| sbuf.append(c.getPageRelativePath()).append(": ") | |
| .append(c.getClass().getName()).append('\n'); | |
| } | |
| } | |
| return sbuf.toString(); | |
| } | |
| /** | |
| * Gets a list of descendant components in this container. For example: | |
| * | |
| * MarkupContainer form = (MarkupContainer) tester.getComponentFromLastRenderedPage("form"); | |
| * List<Component> formComponents = tester.getDescendantComponents(form); | |
| * | |
| * @param markupContainer the container (e.g. Panel) | |
| * @return list of components (goes all levels down the hierarchy) | |
| */ | |
| public static List<Component> getDescendantComponents(MarkupContainer markupContainer) { | |
| DescendantCollector childCollector = new DescendantCollector(); | |
| markupContainer.visitChildren(childCollector); | |
| return childCollector.getChildren(); | |
| } | |
| /** | |
| * Gets a list of descendant components in the container at the specified path. | |
| * For example: | |
| * | |
| * List<Component> formComponents = tester.getDescendantComponents(wicketIdPath , tester); | |
| * | |
| * @param wicketIdPath id path of the container, relative to the page | |
| * can be "" to specify the page | |
| * @param tester | |
| * | |
| * @return list of components (goes all levels down the hierarchy) | |
| */ | |
| public static List<Component> getDescendantComponents(String wicketIdPath, WicketTester tester) { | |
| return getDescendantComponents( | |
| (MarkupContainer) tester.getComponentFromLastRenderedPage(wicketIdPath)); | |
| } | |
| /** | |
| * Uses the visitor pattern to accumulate a list of all child | |
| * components of a MarkupContainer. | |
| * | |
| * Used by getDescendantComponents(). | |
| */ | |
| private static class DescendantCollector implements Component.IVisitor<Component> { | |
| private List<Component> components = new ArrayList<Component>(); | |
| public Object component(Component component) { | |
| components.add(component); | |
| return CONTINUE_TRAVERSAL; | |
| } | |
| public List<Component> getChildren() { | |
| return components; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment