]> source.dussan.org Git - vaadin-framework.git/commitdiff
#6493 API for getting the deepest clicked component in layout click events
authorHenri Sara <henri.sara@itmill.com>
Wed, 23 Feb 2011 11:10:59 +0000 (11:10 +0000)
committerHenri Sara <henri.sara@itmill.com>
Wed, 23 Feb 2011 11:10:59 +0000 (11:10 +0000)
svn changeset:17390/svn branch:6.5

src/com/vaadin/event/LayoutEvents.java
src/com/vaadin/terminal/gwt/client/Util.java
src/com/vaadin/terminal/gwt/client/ui/VAbsoluteLayout.java
src/com/vaadin/terminal/gwt/client/ui/VCssLayout.java
src/com/vaadin/terminal/gwt/client/ui/VGridLayout.java
src/com/vaadin/terminal/gwt/client/ui/VOrderedLayout.java
src/com/vaadin/ui/AbsoluteLayout.java
src/com/vaadin/ui/AbstractOrderedLayout.java
src/com/vaadin/ui/CssLayout.java
src/com/vaadin/ui/GridLayout.java

index e35d527347c715cd656d5cfb414573a2607f14f6..0e77bb82edc12a5484854aafd11dae068a46abf3 100644 (file)
@@ -31,18 +31,43 @@ public interface LayoutEvents {
      * An event fired when the layout has been clicked. The event contains
      * information about the target layout (component) and the child component
      * that was clicked. If no child component was found it is set to null.
-     * 
      */
     public static class LayoutClickEvent extends ClickEvent {
 
-        private Component childComponent;
+        private final Component clickedComponent;
+        private final Component childComponent;
 
         public LayoutClickEvent(Component source,
-                MouseEventDetails mouseEventDetails, Component childComponent) {
+                MouseEventDetails mouseEventDetails,
+                Component clickedComponent, Component childComponent) {
             super(source, mouseEventDetails);
+            this.clickedComponent = clickedComponent;
             this.childComponent = childComponent;
         }
 
+        /**
+         * Returns the component that was clicked, which is somewhere inside the
+         * parent layout on which the listener was registered.
+         * 
+         * For the direct child component of the layout, see
+         * {@link #getChildComponent()}.
+         * 
+         * @return clicked {@link Component}, null if none found
+         */
+        public Component getClickedComponent() {
+            return clickedComponent;
+        }
+
+        /**
+         * Returns the direct child component of the layout which contains the
+         * clicked component.
+         * 
+         * For the clicked component inside that child component of the layout,
+         * see {@link #getClickedComponent()}.
+         * 
+         * @return direct child {@link Component} of the layout which contains
+         *         the clicked Component, null if none found
+         */
         public Component getChildComponent() {
             return childComponent;
         }
index 2a92a6c1fd6833fbedbcb7597d1e01c209280337..00519a5c15d5c1e50165f1ee3f5c9d9f6dd63d70 100644 (file)
@@ -816,6 +816,11 @@ public class Util {
      * <literal>element</literal> is not part of any child component, null is
      * returned.
      * 
+     * This method returns the immediate child of the parent that contains the
+     * element. See
+     * {@link #getPaintableForElement(ApplicationConnection, Container, Element)}
+     * for the deepest nested paintable of parent that contains the element.
+     * 
      * @param client
      *            A reference to ApplicationConnection
      * @param parent
@@ -854,6 +859,56 @@ public class Util {
         return null;
     }
 
+    /**
+     * Locates the nested child component of <literal>parent</literal> which
+     * contains the element <literal>element</literal>. The child component is
+     * also returned if "element" is part of its caption. If
+     * <literal>element</literal> is not part of any child component, null is
+     * returned.
+     * 
+     * This method returns the deepest nested Paintable. See
+     * {@link #getChildPaintableForElement(ApplicationConnection, Container, Element)}
+     * for the immediate child component of parent that contains the element.
+     * 
+     * @param client
+     *            A reference to ApplicationConnection
+     * @param parent
+     *            The widget that contains <literal>element</literal>.
+     * @param element
+     *            An element that is a sub element of the parent
+     * @return The Paintable which the element is a part of. Null if the element
+     *         does not belong to a child.
+     */
+    public static Paintable getPaintableForElement(
+            ApplicationConnection client, Container parent, Element element) {
+        Element rootElement = ((Widget) parent).getElement();
+        while (element != null && element != rootElement) {
+            Paintable paintable = client.getPaintable(element);
+            if (paintable == null) {
+                String ownerPid = VCaption.getCaptionOwnerPid(element);
+                if (ownerPid != null) {
+                    paintable = client.getPaintable(ownerPid);
+                }
+            }
+
+            if (paintable != null) {
+                // check that inside the rootElement
+                while (element != null && element != rootElement) {
+                    element = (Element) element.getParentElement();
+                }
+                if (element != rootElement) {
+                    return null;
+                } else {
+                    return paintable;
+                }
+            }
+
+            element = (Element) element.getParentElement();
+        }
+
+        return null;
+    }
+
     /**
      * Will (attempt) to focus the given DOM Element.
      * 
index 50e33c4c19ee3fb48209d83ec8b511c6c65a39ea..747b7eab516120e8466ad693686be65b893e657d 100644 (file)
@@ -427,17 +427,17 @@ public class VAbsoluteLayout extends ComplexPanel implements Container {
     }
 
     /**
-     * Returns the child component which contains "element". The child component
-     * is also returned if "element" is part of its caption.
+     * Returns the deepest nested child component which contains "element". The
+     * child component is also returned if "element" is part of its caption.
      * 
      * @param element
-     *            An element that is a sub element of the root element in this
-     *            layout
+     *            An element that is a nested sub element of the root element in
+     *            this layout
      * @return The Paintable which the element is a part of. Null if the element
      *         belongs to the layout and not to a child.
      */
     private Paintable getComponent(Element element) {
-        return Util.getChildPaintableForElement(client, this, element);
+        return Util.getPaintableForElement(client, this, element);
     }
 
 }
index e0a74735b7a5c19e4db3a8a00a75b1220b19a81a..98677a85560181113f5afe1e7b00e70fcec2b2c8 100644 (file)
@@ -253,8 +253,8 @@ public class VCssLayout extends SimplePanel implements Paintable, Container {
         }
 
         private Paintable getComponent(Element element) {
-            return Util.getChildPaintableForElement(client, VCssLayout.this,
-                    element);
+            return Util
+                    .getPaintableForElement(client, VCssLayout.this, element);
         }
 
     }
index e5e3efd7c85369803a7b6eef53fd1e9e54d14c0d..8556990450273e9cb40ea1c1de8e73c1c48eb524 100644 (file)
@@ -1111,17 +1111,17 @@ public class VGridLayout extends SimplePanel implements Paintable, Container {
     }
 
     /**
-     * Returns the child component which contains "element". The child component
-     * is also returned if "element" is part of its caption.
+     * Returns the deepest nested child component which contains "element". The
+     * child component is also returned if "element" is part of its caption.
      * 
      * @param element
-     *            An element that is a sub element of the root element in this
-     *            layout
+     *            An element that is a nested sub element of the root element in
+     *            this layout
      * @return The Paintable which the element is a part of. Null if the element
      *         belongs to the layout and not to a child.
      */
     private Paintable getComponent(Element element) {
-        return Util.getChildPaintableForElement(client, this, element);
+        return Util.getPaintableForElement(client, this, element);
     }
 
 }
index bf64219a7bf83195292bb4131dd2df89b354c117..4c5a563d6d05794cf2a5c77aa4086dbf253f6e19 100644 (file)
@@ -954,17 +954,17 @@ public class VOrderedLayout extends CellBasedLayout {
     }
 
     /**
-     * Returns the child component which contains "element". The child component
-     * is also returned if "element" is part of its caption.
+     * Returns the deepest nested child component which contains "element". The
+     * child component is also returned if "element" is part of its caption.
      * 
      * @param element
-     *            An element that is a sub element of the root element in this
-     *            layout
+     *            An element that is a nested sub element of the root element in
+     *            this layout
      * @return The Paintable which the element is a part of. Null if the element
      *         belongs to the layout and not to a child.
      */
     private Paintable getComponent(Element element) {
-        return Util.getChildPaintableForElement(client, this, element);
+        return Util.getPaintableForElement(client, this, element);
     }
 
 }
index 538cc26c779a6c7149d9e5fca8509968a300518f..a30cae529b17545f4b6bc1e4e48de05886976535 100644 (file)
@@ -585,9 +585,14 @@ public class AbsoluteLayout extends AbstractLayout {
     private void fireClick(Map<String, Object> parameters) {
         MouseEventDetails mouseDetails = MouseEventDetails
                 .deSerialize((String) parameters.get("mouseDetails"));
-        Component childComponent = (Component) parameters.get("component");
+        Component clickedComponent = (Component) parameters.get("component");
+        Component childComponent = clickedComponent;
+        while (childComponent != null && !components.contains(childComponent)) {
+            childComponent = childComponent.getParent();
+        }
 
-        fireEvent(new LayoutClickEvent(this, mouseDetails, childComponent));
+        fireEvent(new LayoutClickEvent(this, mouseDetails, clickedComponent,
+                childComponent));
     }
 
     /**
index 7bd52f8feb43028e6db29eedfb7a87ed15c7d10a..28d6f66792c12055efa7c75c5b13b804bbab9200 100644 (file)
@@ -363,9 +363,14 @@ public abstract class AbstractOrderedLayout extends AbstractLayout implements
     private void fireClick(Map<String, Object> parameters) {
         MouseEventDetails mouseDetails = MouseEventDetails
                 .deSerialize((String) parameters.get("mouseDetails"));
-        Component childComponent = (Component) parameters.get("component");
+        Component clickedComponent = (Component) parameters.get("component");
+        Component childComponent = clickedComponent;
+        while (childComponent != null && !components.contains(childComponent)) {
+            childComponent = childComponent.getParent();
+        }
 
-        fireEvent(new LayoutClickEvent(this, mouseDetails, childComponent));
+        fireEvent(new LayoutClickEvent(this, mouseDetails, clickedComponent,
+                childComponent));
     }
 
     /**
index db5bae678bb23478f546e150c1688f0027ba1fb7..5953ba5181f2f79b348a8ce3abda74f201250a90 100644 (file)
@@ -264,9 +264,14 @@ public class CssLayout extends AbstractLayout {
     private void fireClick(Map<String, Object> parameters) {
         MouseEventDetails mouseDetails = MouseEventDetails
                 .deSerialize((String) parameters.get("mouseDetails"));
-        Component childComponent = (Component) parameters.get("component");
+        Component clickedComponent = (Component) parameters.get("component");
+        Component childComponent = clickedComponent;
+        while (childComponent != null && !components.contains(childComponent)) {
+            childComponent = childComponent.getParent();
+        }
 
-        fireEvent(new LayoutClickEvent(this, mouseDetails, childComponent));
+        fireEvent(new LayoutClickEvent(this, mouseDetails, clickedComponent,
+                childComponent));
     }
 
     /**
index fc16daa7c756d8d94db1fd1c59dc5e98de4f37a2..503b3d2b8eb9a5faf1f8917dd42ab285c0b6f996 100644 (file)
@@ -1392,9 +1392,14 @@ public class GridLayout extends AbstractLayout implements
     private void fireClick(Map<String, Object> parameters) {
         MouseEventDetails mouseDetails = MouseEventDetails
                 .deSerialize((String) parameters.get("mouseDetails"));
-        Component childComponent = (Component) parameters.get("component");
+        Component clickedComponent = (Component) parameters.get("component");
+        Component childComponent = clickedComponent;
+        while (childComponent != null && !components.contains(childComponent)) {
+            childComponent = childComponent.getParent();
+        }
 
-        fireEvent(new LayoutClickEvent(this, mouseDetails, childComponent));
+        fireEvent(new LayoutClickEvent(this, mouseDetails, clickedComponent,
+                childComponent));
     }
 
     /**