Author: rwhitcomb
Date: Wed Oct 24 16:57:07 2012
New Revision: 1401781

URL: http://svn.apache.org/viewvc?rev=1401781&view=rev
Log:
PIVOT-834: Provide a way to get the Form.Section that a component
belongs to, even if the component is nested inside other containers.
The original suggestion was to make Form.getSection do this always.
But, this leads to other potential problems inside the skin, so
implement another method: Form.getEnclosingSection that implements
this functionality without potentially breaking other things.
Add Javadoc to both these methods to explain their usage.

Modified:
    pivot/trunk/wtk/src/org/apache/pivot/wtk/Form.java

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Form.java
URL: 
http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/Form.java?rev=1401781&r1=1401780&r2=1401781&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/Form.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Form.java Wed Oct 24 16:57:07 2012
@@ -545,10 +545,38 @@ public class Form extends Container {
         return formAttributeListeners;
     }
 
+    /**
+     * Finds the {@link Form.Section} that the given component belongs to.
+     * Only finds the section if the component is a direct child of the 
section.
+     *
+     * @see #getEnclosingSection getEnclosingSection(Component)
+     */
     public static Section getSection(Component component) {
         return (Section)component.getAttribute(Attribute.SECTION);
     }
 
+    /**
+     * Finds the {@link Form.Section} that the given component belongs to.
+     * Will search up the parent hierarchy in case the component is nested 
inside
+     * other containers inside the form.
+     *
+     * @return
+     * The form section this component (or one of its parents) belongs to or
+     * <code>null</code> if the component does not belong to a form.
+     *
+     * @see #getSection getSection(Component)
+     */
+    public static Section getEnclosingSection(Component component) {
+        Section section = (Section)component.getAttribute(Attribute.SECTION);
+        if (section == null) {
+            for (Container parent = component.getParent();
+                 parent != null && (section = 
(Section)parent.getAttribute(Attribute.SECTION)) == null; ) {
+                parent = parent.getParent();
+            }
+        }
+        return section;
+    }
+
     public static String getLabel(Component component) {
         return (String)component.getAttribute(Attribute.LABEL);
     }


Reply via email to