]> source.dussan.org Git - vaadin-framework.git/commitdiff
Method for finding the first ancestor of a given type (#8182)
authorArtur Signell <artur@vaadin.com>
Thu, 19 Apr 2012 14:33:18 +0000 (17:33 +0300)
committerArtur Signell <artur@vaadin.com>
Thu, 19 Apr 2012 19:10:41 +0000 (22:10 +0300)
src/com/vaadin/ui/AbstractComponent.java
tests/testbench/com/vaadin/tests/components/window/CloseSubWindow.java

index cca0edc00139ab12398b700a6db1cefbbbc639b1..79a07ae00ec8fe26a37697a41269d95f87e00e08 100644 (file)
@@ -534,6 +534,31 @@ public abstract class AbstractComponent implements Component, MethodEventSource
         return parent;
     }
 
+    /**
+     * Returns the closest ancestor with the given type.
+     * <p>
+     * To find the Window that contains the component, use {@code Window w =
+     * getParent(Window.class);}
+     * </p>
+     * 
+     * @param <T>
+     *            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 extends HasComponents> T findAncestor(Class<T> 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 <T>
-     * @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 extends Component> T findParentOfType(
-            Class<T> 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);
     }
index 4cd3854d6fcb67bc20f262790901f543a0883e9e..bcfbc7f55a2c6ec32dde8d6a131754286d4bf366 100644 (file)
@@ -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);