1. /*
  2. * @(#)DefaultFocusTraversalPolicy.java 1.5 03/12/19
  3. *
  4. * Copyright 2004 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. * <p>
  35. * Please see
  36. * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html">
  37. * How to Use the Focus Subsystem</a>,
  38. * a section in <em>The Java Tutorial</em>, and the
  39. * <a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>
  40. * for more information.
  41. *
  42. * @author David Mendenhall
  43. * @version 1.5, 12/19/03
  44. *
  45. * @see Container#getComponents
  46. * @see Component#isFocusable
  47. * @see Component#setFocusable
  48. * @since 1.4
  49. */
  50. public class DefaultFocusTraversalPolicy
  51. extends ContainerOrderFocusTraversalPolicy
  52. {
  53. /**
  54. * Determines whether a Component is an acceptable choice as the new
  55. * focus owner. The Component must be visible, displayable, and enabled
  56. * to be accepted. If client code has explicitly set the focusability
  57. * of the Component by either overriding
  58. * <code>Component.isFocusTraversable()</code> or
  59. * <code>Component.isFocusable()</code>, or by calling
  60. * <code>Component.setFocusable()</code>, then the Component will be
  61. * accepted if and only if it is focusable. If, however, the Component is
  62. * relying on default focusability, then all Canvases, Labels, Panels,
  63. * Scrollbars, ScrollPanes, Windows, and lightweight Components will be
  64. * rejected.
  65. *
  66. * @param aComponent the Component whose fitness as a focus owner is to
  67. * be tested
  68. * @return <code>true</code> if aComponent meets the above requirements;
  69. * <code>false</code> otherwise
  70. */
  71. protected boolean accept(Component aComponent) {
  72. if (!(aComponent.isVisible() && aComponent.isDisplayable() &&
  73. aComponent.isEnabled()))
  74. {
  75. return false;
  76. }
  77. // Verify that the Component is recursively enabled. Disabling a
  78. // heavyweight Container disables its children, whereas disabling
  79. // a lightweight Container does not.
  80. if (!(aComponent instanceof Window)) {
  81. for (Container enableTest = aComponent.getParent();
  82. enableTest != null;
  83. enableTest = enableTest.getParent())
  84. {
  85. if (!(enableTest.isEnabled() || enableTest.isLightweight())) {
  86. return false;
  87. }
  88. if (enableTest instanceof Window) {
  89. break;
  90. }
  91. }
  92. }
  93. boolean focusable = aComponent.isFocusable();
  94. if (aComponent.isFocusTraversableOverridden()) {
  95. return focusable;
  96. }
  97. ComponentPeer peer = aComponent.getPeer();
  98. return (peer != null && peer.isFocusable());
  99. }
  100. }