From 2db8b527fc089a853bee7fdb5226aa890aa193b4 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Thu, 19 Apr 2012 17:33:18 +0300 Subject: [PATCH] Method for finding the first ancestor of a given type (#8182) --- src/com/vaadin/ui/AbstractComponent.java | 53 +++++++++---------- .../components/window/CloseSubWindow.java | 2 +- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/com/vaadin/ui/AbstractComponent.java b/src/com/vaadin/ui/AbstractComponent.java index cca0edc001..79a07ae00e 100644 --- a/src/com/vaadin/ui/AbstractComponent.java +++ b/src/com/vaadin/ui/AbstractComponent.java @@ -534,6 +534,31 @@ public abstract class AbstractComponent implements Component, MethodEventSource return parent; } + /** + * Returns the closest ancestor with the given type. + *

+ * To find the Window that contains the component, use {@code Window w = + * getParent(Window.class);} + *

+ * + * @param + * The type of the ancestor + * @param parentType + * The ancestor class we are looking for + * @return The first ancestor that can be assigned to the given class. Null + * if no ancestor with the correct type could be found. + */ + public T findAncestor(Class parentType) { + HasComponents p = getParent(); + while (p != null) { + if (parentType.isAssignableFrom(p.getClass())) { + return parentType.cast(p); + } + p = p.getParent(); + } + return null; + } + /* * Sets the parent component. Don't add a JavaDoc comment here, we use the * default documentation from implemented interface. @@ -1461,7 +1486,7 @@ public abstract class AbstractComponent implements Component, MethodEventSource private void setActionManagerViewer() { if (actionManager != null && getRoot() != null) { // Attached and has action manager - Window w = findParentOfType(Window.class, this); + Window w = findAncestor(Window.class); if (w != null) { actionManager.setViewer(w); } else { @@ -1471,32 +1496,6 @@ public abstract class AbstractComponent implements Component, MethodEventSource } - /** - * Helper method for finding the first parent component of a given type. - * Useful e.g. for finding the Window the component is inside. - * - * @param - * @param parentType - * The type to look for - * @param c - * The target component - * @return A parent component of type {@literal parentType} or null if no - * parent component in the hierarchy can be assigned to the given - * type. - */ - private static T findParentOfType( - Class parentType, Component c) { - Component p = c.getParent(); - if (p == null) { - return null; - } - - if (parentType.isAssignableFrom(p.getClass())) { - return (T) p; - } - return findParentOfType(parentType, p); - } - public void addShortcutListener(ShortcutListener shortcut) { getActionManager().addAction(shortcut); } diff --git a/tests/testbench/com/vaadin/tests/components/window/CloseSubWindow.java b/tests/testbench/com/vaadin/tests/components/window/CloseSubWindow.java index 4cd3854d6f..bcfbc7f55a 100644 --- a/tests/testbench/com/vaadin/tests/components/window/CloseSubWindow.java +++ b/tests/testbench/com/vaadin/tests/components/window/CloseSubWindow.java @@ -37,7 +37,7 @@ public class CloseSubWindow extends TestBase { Button closeButton = new Button("Close"); closeButton.addListener(new ClickListener() { public void buttonClick(ClickEvent event) { - window.close(); + event.getButton().findAncestor(Window.class).close(); } }); window.addComponent(closeButton); -- 2.39.5