1. /*
  2. * @(#)DefaultFocusTraversalPolicy.java 1.3 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt;
  8. import java.awt.peer.ComponentPeer;
  9. /**
  10. * A FocusTraversalPolicy that determines traversal order based on the order
  11. * of child Components in a Container. From a particular focus cycle root, the
  12. * policy makes a pre-order traversal of the Component hierarchy, and traverses
  13. * a Container's children according to the ordering of the array returned by
  14. * <code>Container.getComponents()</code>. Portions of the hierarchy that are
  15. * not visible and displayable will not be searched.
  16. * <p>
  17. * If client code has explicitly set the focusability of a Component by either
  18. * overriding <code>Component.isFocusTraversable()</code> or
  19. * <code>Component.isFocusable()</code>, or by calling
  20. * <code>Component.setFocusable()</code>, then a DefaultFocusTraversalPolicy
  21. * behaves exactly like a ContainerOrderFocusTraversalPolicy. If, however, the
  22. * Component is relying on default focusability, then a
  23. * DefaultFocusTraversalPolicy will reject all Components with non-focusable
  24. * peers. This is the default FocusTraversalPolicy for all AWT Containers.
  25. * <p>
  26. * The focusability of a peer is implementation-dependent. Sun recommends that
  27. * all implementations for a particular native platform construct peers with
  28. * the same focusability. The recommendations for Windows and Unix are that
  29. * Canvases, Labels, Panels, Scrollbars, ScrollPanes, Windows, and lightweight
  30. * Components have non-focusable peers, and all other Components have focusable
  31. * peers. These recommendations are used in the Sun AWT implementations. Note
  32. * that the focusability of a Component's peer is different from, and does not
  33. * impact, the focusability of the Component itself.
  34. *
  35. * @author David Mendenhall
  36. * @version 1.3, 01/23/03
  37. *
  38. * @see Container#getComponents
  39. * @see Component#isFocusable
  40. * @see Component#setFocusable
  41. * @since 1.4
  42. */
  43. public class DefaultFocusTraversalPolicy
  44. extends ContainerOrderFocusTraversalPolicy
  45. {
  46. /**
  47. * Determines whether a Component is an acceptable choice as the new
  48. * focus owner. The Component must be visible, displayable, and enabled
  49. * to be accepted. If client code has explicitly set the focusability
  50. * of the Component by either overriding
  51. * <code>Component.isFocusTraversable()</code> or
  52. * <code>Component.isFocusable()</code>, or by calling
  53. * <code>Component.setFocusable()</code>, then the Component will be
  54. * accepted if and only if it is focusable. If, however, the Component is
  55. * relying on default focusability, then all Canvases, Labels, Panels,
  56. * Scrollbars, ScrollPanes, Windows, and lightweight Components will be
  57. * rejected.
  58. *
  59. * @param aComponent the Component whose fitness as a focus owner is to
  60. * be tested
  61. * @return <code>true</code> if aComponent meets the above requirements;
  62. * <code>false</code> otherwise
  63. */
  64. protected boolean accept(Component aComponent) {
  65. if (!(aComponent.isVisible() && aComponent.isDisplayable() &&
  66. aComponent.isEnabled()))
  67. {
  68. return false;
  69. }
  70. // Verify that the Component is recursively enabled. Disabling a
  71. // heavyweight Container disables its children, whereas disabling
  72. // a lightweight Container does not.
  73. if (!(aComponent instanceof Window)) {
  74. for (Container enableTest = aComponent.getParent();
  75. enableTest != null;
  76. enableTest = enableTest.getParent())
  77. {
  78. if (!(enableTest.isEnabled() || enableTest.isLightweight())) {
  79. return false;
  80. }
  81. if (enableTest instanceof Window) {
  82. break;
  83. }
  84. }
  85. }
  86. boolean focusable = aComponent.isFocusable();
  87. if (aComponent.isFocusTraversableOverridden()) {
  88. return focusable;
  89. }
  90. ComponentPeer peer = aComponent.getPeer();
  91. return (peer != null && peer.isFocusable());
  92. }
  93. }