< prev index next >

src/java.desktop/share/classes/java/awt/ContainerOrderFocusTraversalPolicy.java

Print this page




  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.awt;
  26 
  27 import java.util.List;
  28 import java.util.ArrayList;
  29 import sun.util.logging.PlatformLogger;
  30 
  31 /**
  32  * A FocusTraversalPolicy that determines traversal order based on the order
  33  * of child Components in a Container. From a particular focus cycle root, the
  34  * policy makes a pre-order traversal of the Component hierarchy, and traverses
  35  * a Container's children according to the ordering of the array returned by
  36  * <code>Container.getComponents()</code>. Portions of the hierarchy that are
  37  * not visible and displayable will not be searched.
  38  * <p>
  39  * By default, ContainerOrderFocusTraversalPolicy implicitly transfers focus
  40  * down-cycle. That is, during normal forward focus traversal, the Component
  41  * traversed after a focus cycle root will be the focus-cycle-root's default
  42  * Component to focus. This behavior can be disabled using the
  43  * <code>setImplicitDownCycleTraversal</code> method.
  44  * <p>
  45  * By default, methods of this class will return a Component only if it is
  46  * visible, displayable, enabled, and focusable. Subclasses can modify this
  47  * behavior by overriding the <code>accept</code> method.
  48  * <p>
  49  * This policy takes into account <a
  50  * href="doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus traversal
  51  * policy providers</a>.  When searching for first/last/next/previous Component,
  52  * if a focus traversal policy provider is encountered, its focus traversal
  53  * policy is used to perform the search operation.
  54  *
  55  * @author David Mendenhall
  56  *
  57  * @see Container#getComponents
  58  * @since 1.4
  59  */
  60 public class ContainerOrderFocusTraversalPolicy extends FocusTraversalPolicy
  61     implements java.io.Serializable
  62 {
  63     private static final PlatformLogger log = PlatformLogger.getLogger("java.awt.ContainerOrderFocusTraversalPolicy");
  64 
  65     private final int FORWARD_TRAVERSAL = 0;
  66     private final int BACKWARD_TRAVERSAL = 1;
  67 


 176                 retComp = (traversalDirection == FORWARD_TRAVERSAL ?
 177                            cont.getFocusTraversalPolicy().getDefaultComponent(cont) :
 178                            cont.getFocusTraversalPolicy().getLastComponent(cont));
 179 
 180                 if (retComp != null && log.isLoggable(PlatformLogger.Level.FINE)) {
 181                     log.fine("### Transferred focus to " + retComp + " in the FTP provider " + cont);
 182                 }
 183             }
 184         }
 185         return retComp;
 186     }
 187 
 188     /**
 189      * Returns the Component that should receive the focus after aComponent.
 190      * aContainer must be a focus cycle root of aComponent or a focus traversal policy provider.
 191      * <p>
 192      * By default, ContainerOrderFocusTraversalPolicy implicitly transfers
 193      * focus down-cycle. That is, during normal forward focus traversal, the
 194      * Component traversed after a focus cycle root will be the focus-cycle-
 195      * root's default Component to focus. This behavior can be disabled using
 196      * the <code>setImplicitDownCycleTraversal</code> method.
 197      * <p>
 198      * If aContainer is <a href="doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus
 199      * traversal policy provider</a>, the focus is always transferred down-cycle.
 200      *
 201      * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider
 202      * @param aComponent a (possibly indirect) child of aContainer, or
 203      *        aContainer itself
 204      * @return the Component that should receive the focus after aComponent, or
 205      *         null if no suitable Component can be found
 206      * @throws IllegalArgumentException if aContainer is not a focus cycle
 207      *         root of aComponent or focus traversal policy provider, or if either aContainer or
 208      *         aComponent is null
 209      */
 210     public Component getComponentAfter(Container aContainer, Component aComponent) {
 211         if (log.isLoggable(PlatformLogger.Level.FINE)) {
 212             log.fine("### Searching in " + aContainer + " for component after " + aComponent);
 213         }
 214 
 215         if (aContainer == null || aComponent == null) {
 216             throw new IllegalArgumentException("aContainer and aComponent cannot be null");


 502                 if (accept(comp)) {
 503                     return comp;
 504                 } else if (comp instanceof Container && comp != aContainer) {
 505                     Container cont = (Container)comp;
 506                     if (cont.isFocusTraversalPolicyProvider()) {
 507                         Component retComp = cont.getFocusTraversalPolicy().getLastComponent(cont);
 508                         if (retComp != null) {
 509                             return retComp;
 510                         }
 511                     }
 512                 }
 513             }
 514         }
 515         return null;
 516     }
 517 
 518     /**
 519      * Returns the default Component to focus. This Component will be the first
 520      * to receive focus when traversing down into a new focus traversal cycle
 521      * rooted at aContainer. The default implementation of this method
 522      * returns the same Component as <code>getFirstComponent</code>.
 523      *
 524      * @param aContainer the focus cycle root or focus traversal policy provider whose default
 525      *        Component is to be returned
 526      * @return the default Component in the traversal cycle of aContainer,
 527      *         or null if no suitable Component can be found
 528      * @see #getFirstComponent
 529      * @throws IllegalArgumentException if aContainer is null
 530      */
 531     public Component getDefaultComponent(Container aContainer) {
 532         return getFirstComponent(aContainer);
 533     }
 534 
 535     /**
 536      * Sets whether this ContainerOrderFocusTraversalPolicy transfers focus
 537      * down-cycle implicitly. If <code>true</code>, during normal forward focus
 538      * traversal, the Component traversed after a focus cycle root will be the
 539      * focus-cycle-root's default Component to focus. If <code>false</code>,
 540      * the next Component in the focus traversal cycle rooted at the specified
 541      * focus cycle root will be traversed instead. The default value for this
 542      * property is <code>true</code>.
 543      *
 544      * @param implicitDownCycleTraversal whether this
 545      *        ContainerOrderFocusTraversalPolicy transfers focus down-cycle
 546      *        implicitly
 547      * @see #getImplicitDownCycleTraversal
 548      * @see #getFirstComponent
 549      */
 550     public void setImplicitDownCycleTraversal(boolean implicitDownCycleTraversal) {
 551         this.implicitDownCycleTraversal = implicitDownCycleTraversal;
 552     }
 553 
 554     /**
 555      * Returns whether this ContainerOrderFocusTraversalPolicy transfers focus
 556      * down-cycle implicitly. If <code>true</code>, during normal forward focus
 557      * traversal, the Component traversed after a focus cycle root will be the
 558      * focus-cycle-root's default Component to focus. If <code>false</code>,
 559      * the next Component in the focus traversal cycle rooted at the specified
 560      * focus cycle root will be traversed instead.
 561      *
 562      * @return whether this ContainerOrderFocusTraversalPolicy transfers focus
 563      *         down-cycle implicitly
 564      * @see #setImplicitDownCycleTraversal
 565      * @see #getFirstComponent
 566      */
 567     public boolean getImplicitDownCycleTraversal() {
 568         return implicitDownCycleTraversal;
 569     }
 570 
 571     /**
 572      * Determines whether a Component is an acceptable choice as the new
 573      * focus owner. By default, this method will accept a Component if and
 574      * only if it is visible, displayable, enabled, and focusable.
 575      *
 576      * @param aComponent the Component whose fitness as a focus owner is to
 577      *        be tested
 578      * @return <code>true</code> if aComponent is visible, displayable,
 579      *         enabled, and focusable; <code>false</code> otherwise
 580      */
 581     protected boolean accept(Component aComponent) {
 582         if (!aComponent.canBeFocusOwner()) {
 583             return false;
 584         }
 585 
 586         // Verify that the Component is recursively enabled. Disabling a
 587         // heavyweight Container disables its children, whereas disabling
 588         // a lightweight Container does not.
 589         if (!(aComponent instanceof Window)) {
 590             for (Container enableTest = aComponent.getParent();
 591                  enableTest != null;
 592                  enableTest = enableTest.getParent())
 593             {
 594                 if (!(enableTest.isEnabled() || enableTest.isLightweight())) {
 595                     return false;
 596                 }
 597                 if (enableTest instanceof Window) {
 598                     break;
 599                 }


  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.awt;
  26 
  27 import java.util.List;
  28 import java.util.ArrayList;
  29 import sun.util.logging.PlatformLogger;
  30 
  31 /**
  32  * A FocusTraversalPolicy that determines traversal order based on the order
  33  * of child Components in a Container. From a particular focus cycle root, the
  34  * policy makes a pre-order traversal of the Component hierarchy, and traverses
  35  * a Container's children according to the ordering of the array returned by
  36  * {@code Container.getComponents()}. Portions of the hierarchy that are
  37  * not visible and displayable will not be searched.
  38  * <p>
  39  * By default, ContainerOrderFocusTraversalPolicy implicitly transfers focus
  40  * down-cycle. That is, during normal forward focus traversal, the Component
  41  * traversed after a focus cycle root will be the focus-cycle-root's default
  42  * Component to focus. This behavior can be disabled using the
  43  * {@code setImplicitDownCycleTraversal} method.
  44  * <p>
  45  * By default, methods of this class will return a Component only if it is
  46  * visible, displayable, enabled, and focusable. Subclasses can modify this
  47  * behavior by overriding the {@code accept} method.
  48  * <p>
  49  * This policy takes into account <a
  50  * href="doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus traversal
  51  * policy providers</a>.  When searching for first/last/next/previous Component,
  52  * if a focus traversal policy provider is encountered, its focus traversal
  53  * policy is used to perform the search operation.
  54  *
  55  * @author David Mendenhall
  56  *
  57  * @see Container#getComponents
  58  * @since 1.4
  59  */
  60 public class ContainerOrderFocusTraversalPolicy extends FocusTraversalPolicy
  61     implements java.io.Serializable
  62 {
  63     private static final PlatformLogger log = PlatformLogger.getLogger("java.awt.ContainerOrderFocusTraversalPolicy");
  64 
  65     private final int FORWARD_TRAVERSAL = 0;
  66     private final int BACKWARD_TRAVERSAL = 1;
  67 


 176                 retComp = (traversalDirection == FORWARD_TRAVERSAL ?
 177                            cont.getFocusTraversalPolicy().getDefaultComponent(cont) :
 178                            cont.getFocusTraversalPolicy().getLastComponent(cont));
 179 
 180                 if (retComp != null && log.isLoggable(PlatformLogger.Level.FINE)) {
 181                     log.fine("### Transferred focus to " + retComp + " in the FTP provider " + cont);
 182                 }
 183             }
 184         }
 185         return retComp;
 186     }
 187 
 188     /**
 189      * Returns the Component that should receive the focus after aComponent.
 190      * aContainer must be a focus cycle root of aComponent or a focus traversal policy provider.
 191      * <p>
 192      * By default, ContainerOrderFocusTraversalPolicy implicitly transfers
 193      * focus down-cycle. That is, during normal forward focus traversal, the
 194      * Component traversed after a focus cycle root will be the focus-cycle-
 195      * root's default Component to focus. This behavior can be disabled using
 196      * the {@code setImplicitDownCycleTraversal} method.
 197      * <p>
 198      * If aContainer is <a href="doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus
 199      * traversal policy provider</a>, the focus is always transferred down-cycle.
 200      *
 201      * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider
 202      * @param aComponent a (possibly indirect) child of aContainer, or
 203      *        aContainer itself
 204      * @return the Component that should receive the focus after aComponent, or
 205      *         null if no suitable Component can be found
 206      * @throws IllegalArgumentException if aContainer is not a focus cycle
 207      *         root of aComponent or focus traversal policy provider, or if either aContainer or
 208      *         aComponent is null
 209      */
 210     public Component getComponentAfter(Container aContainer, Component aComponent) {
 211         if (log.isLoggable(PlatformLogger.Level.FINE)) {
 212             log.fine("### Searching in " + aContainer + " for component after " + aComponent);
 213         }
 214 
 215         if (aContainer == null || aComponent == null) {
 216             throw new IllegalArgumentException("aContainer and aComponent cannot be null");


 502                 if (accept(comp)) {
 503                     return comp;
 504                 } else if (comp instanceof Container && comp != aContainer) {
 505                     Container cont = (Container)comp;
 506                     if (cont.isFocusTraversalPolicyProvider()) {
 507                         Component retComp = cont.getFocusTraversalPolicy().getLastComponent(cont);
 508                         if (retComp != null) {
 509                             return retComp;
 510                         }
 511                     }
 512                 }
 513             }
 514         }
 515         return null;
 516     }
 517 
 518     /**
 519      * Returns the default Component to focus. This Component will be the first
 520      * to receive focus when traversing down into a new focus traversal cycle
 521      * rooted at aContainer. The default implementation of this method
 522      * returns the same Component as {@code getFirstComponent}.
 523      *
 524      * @param aContainer the focus cycle root or focus traversal policy provider whose default
 525      *        Component is to be returned
 526      * @return the default Component in the traversal cycle of aContainer,
 527      *         or null if no suitable Component can be found
 528      * @see #getFirstComponent
 529      * @throws IllegalArgumentException if aContainer is null
 530      */
 531     public Component getDefaultComponent(Container aContainer) {
 532         return getFirstComponent(aContainer);
 533     }
 534 
 535     /**
 536      * Sets whether this ContainerOrderFocusTraversalPolicy transfers focus
 537      * down-cycle implicitly. If {@code true}, during normal forward focus
 538      * traversal, the Component traversed after a focus cycle root will be the
 539      * focus-cycle-root's default Component to focus. If {@code false},
 540      * the next Component in the focus traversal cycle rooted at the specified
 541      * focus cycle root will be traversed instead. The default value for this
 542      * property is {@code true}.
 543      *
 544      * @param implicitDownCycleTraversal whether this
 545      *        ContainerOrderFocusTraversalPolicy transfers focus down-cycle
 546      *        implicitly
 547      * @see #getImplicitDownCycleTraversal
 548      * @see #getFirstComponent
 549      */
 550     public void setImplicitDownCycleTraversal(boolean implicitDownCycleTraversal) {
 551         this.implicitDownCycleTraversal = implicitDownCycleTraversal;
 552     }
 553 
 554     /**
 555      * Returns whether this ContainerOrderFocusTraversalPolicy transfers focus
 556      * down-cycle implicitly. If {@code true}, during normal forward focus
 557      * traversal, the Component traversed after a focus cycle root will be the
 558      * focus-cycle-root's default Component to focus. If {@code false},
 559      * the next Component in the focus traversal cycle rooted at the specified
 560      * focus cycle root will be traversed instead.
 561      *
 562      * @return whether this ContainerOrderFocusTraversalPolicy transfers focus
 563      *         down-cycle implicitly
 564      * @see #setImplicitDownCycleTraversal
 565      * @see #getFirstComponent
 566      */
 567     public boolean getImplicitDownCycleTraversal() {
 568         return implicitDownCycleTraversal;
 569     }
 570 
 571     /**
 572      * Determines whether a Component is an acceptable choice as the new
 573      * focus owner. By default, this method will accept a Component if and
 574      * only if it is visible, displayable, enabled, and focusable.
 575      *
 576      * @param aComponent the Component whose fitness as a focus owner is to
 577      *        be tested
 578      * @return {@code true} if aComponent is visible, displayable,
 579      *         enabled, and focusable; {@code false} otherwise
 580      */
 581     protected boolean accept(Component aComponent) {
 582         if (!aComponent.canBeFocusOwner()) {
 583             return false;
 584         }
 585 
 586         // Verify that the Component is recursively enabled. Disabling a
 587         // heavyweight Container disables its children, whereas disabling
 588         // a lightweight Container does not.
 589         if (!(aComponent instanceof Window)) {
 590             for (Container enableTest = aComponent.getParent();
 591                  enableTest != null;
 592                  enableTest = enableTest.getParent())
 593             {
 594                 if (!(enableTest.isEnabled() || enableTest.isLightweight())) {
 595                     return false;
 596                 }
 597                 if (enableTest instanceof Window) {
 598                     break;
 599                 }
< prev index next >