1. /*
  2. * @(#)ContainerOrderFocusTraversalPolicy.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. /**
  9. * A FocusTraversalPolicy that determines traversal order based on the order
  10. * of child Components in a Container. From a particular focus cycle root, the
  11. * policy makes a pre-order traversal of the Component hierarchy, and traverses
  12. * a Container's children according to the ordering of the array returned by
  13. * <code>Container.getComponents()</code>. Portions of the hierarchy that are
  14. * not visible and displayable will not be searched.
  15. * <p>
  16. * By default, ContainerOrderFocusTraversalPolicy implicitly transfers focus
  17. * down-cycle. That is, during normal forward focus traversal, the Component
  18. * traversed after a focus cycle root will be the focus-cycle-root's default
  19. * Component to focus. This behavior can be disabled using the
  20. * <code>setImplicitDownCycleTraversal</code> method.
  21. * <p>
  22. * By default, methods of this class with return a Component only if it is
  23. * visible, displayable, enabled, and focusable. Subclasses can modify this
  24. * behavior by overriding the <code>accept</code> method.
  25. *
  26. * @author David Mendenhall
  27. * @version 1.3, 01/23/03
  28. *
  29. * @see Container#getComponents
  30. * @since 1.4
  31. */
  32. public class ContainerOrderFocusTraversalPolicy extends FocusTraversalPolicy
  33. implements java.io.Serializable
  34. {
  35. private static class MutableBoolean {
  36. boolean value = false;
  37. }
  38. private static final MutableBoolean found = new MutableBoolean();
  39. private boolean implicitDownCycleTraversal = true;
  40. /**
  41. * Returns the Component that should receive the focus after aComponent.
  42. * focusCycleRoot must be a focus cycle root of aComponent.
  43. * <p>
  44. * By default, ContainerOrderFocusTraversalPolicy implicitly transfers
  45. * focus down-cycle. That is, during normal forward focus traversal, the
  46. * Component traversed after a focus cycle root will be the focus-cycle-
  47. * root's default Component to focus. This behavior can be disabled using
  48. * the <code>setImplicitDownCycleTraversal</code> method.
  49. *
  50. * @param focusCycleRoot a focus cycle root of aComponent
  51. * @param aComponent a (possibly indirect) child of focusCycleRoot, or
  52. * focusCycleRoot itself
  53. * @return the Component that should receive the focus after aComponent, or
  54. * null if no suitable Component can be found
  55. * @throws IllegalArgumentException if focusCycleRoot is not a focus cycle
  56. * root of aComponent, or if either focusCycleRoot or aComponent is
  57. * null
  58. */
  59. public Component getComponentAfter(Container focusCycleRoot,
  60. Component aComponent) {
  61. if (focusCycleRoot == null || aComponent == null) {
  62. throw new IllegalArgumentException("focusCycleRoot and aComponent cannot be null");
  63. }
  64. if (!aComponent.isFocusCycleRoot(focusCycleRoot)) {
  65. throw new IllegalArgumentException("focusCycleRoot is not a focus cyle root of aComponent");
  66. }
  67. synchronized(focusCycleRoot.getTreeLock()) {
  68. found.value = false;
  69. Component retval = getComponentAfter(focusCycleRoot, aComponent,
  70. found);
  71. if (retval != null) {
  72. return retval;
  73. } else if (found.value) {
  74. return getFirstComponent(focusCycleRoot);
  75. } else {
  76. return null;
  77. }
  78. }
  79. }
  80. private Component getComponentAfter(Container aContainer,
  81. Component aComponent,
  82. MutableBoolean found) {
  83. if (!(aContainer.isVisible() && aContainer.isDisplayable())) {
  84. return null;
  85. }
  86. if (found.value) {
  87. if (accept(aContainer)) {
  88. return aContainer;
  89. }
  90. } else if (aContainer == aComponent) {
  91. found.value = true;
  92. }
  93. for (int i = 0; i < aContainer.ncomponents; i++) {
  94. Component comp = aContainer.component[i];
  95. if ((comp instanceof Container) &&
  96. !((Container)comp).isFocusCycleRoot()) {
  97. Component retval = getComponentAfter((Container)comp,
  98. aComponent,
  99. found);
  100. if (retval != null) {
  101. return retval;
  102. }
  103. } else if (found.value) {
  104. if (accept(comp)) {
  105. return comp;
  106. }
  107. } else if (comp == aComponent) {
  108. found.value = true;
  109. }
  110. if (found.value &&
  111. getImplicitDownCycleTraversal() &&
  112. (comp instanceof Container) &&
  113. ((Container)comp).isFocusCycleRoot())
  114. {
  115. Container cont = (Container)comp;
  116. Component retval = cont.getFocusTraversalPolicy().
  117. getDefaultComponent(cont);
  118. if (retval != null) {
  119. return retval;
  120. }
  121. }
  122. }
  123. return null;
  124. }
  125. /**
  126. * Returns the Component that should receive the focus before aComponent.
  127. * focusCycleRoot must be a focus cycle root of aComponent.
  128. *
  129. * @param focusCycleRoot a focus cycle root of aComponent
  130. * @param aComponent a (possibly indirect) child of focusCycleRoot, or
  131. * focusCycleRoot itself
  132. * @return the Component that should receive the focus before aComponent,
  133. * or null if no suitable Component can be found
  134. * @throws IllegalArgumentException if focusCycleRoot is not a focus cycle
  135. * root of aComponent, or if either focusCycleRoot or aComponent is
  136. * null
  137. */
  138. public Component getComponentBefore(Container focusCycleRoot,
  139. Component aComponent) {
  140. if (focusCycleRoot == null || aComponent == null) {
  141. throw new IllegalArgumentException("focusCycleRoot and aComponent cannot be null");
  142. }
  143. if (!aComponent.isFocusCycleRoot(focusCycleRoot)) {
  144. throw new IllegalArgumentException("focusCycleRoot is not a focus cyle root of aComponent");
  145. }
  146. synchronized(focusCycleRoot.getTreeLock()) {
  147. found.value = false;
  148. Component retval = getComponentBefore(focusCycleRoot, aComponent,
  149. found);
  150. if (retval != null) {
  151. return retval;
  152. } else if (found.value) {
  153. return getLastComponent(focusCycleRoot);
  154. } else {
  155. return null;
  156. }
  157. }
  158. }
  159. private Component getComponentBefore(Container aContainer,
  160. Component aComponent,
  161. MutableBoolean found) {
  162. if (!(aContainer.isVisible() && aContainer.isDisplayable())) {
  163. return null;
  164. }
  165. for (int i = aContainer.ncomponents - 1; i >= 0; i--) {
  166. Component comp = aContainer.component[i];
  167. if (comp == aComponent) {
  168. found.value = true;
  169. } else if ((comp instanceof Container) &&
  170. !((Container)comp).isFocusCycleRoot()) {
  171. Component retval = getComponentBefore((Container)comp,
  172. aComponent,
  173. found);
  174. if (retval != null) {
  175. return retval;
  176. }
  177. } else if (found.value) {
  178. if (accept(comp)) {
  179. return comp;
  180. }
  181. }
  182. }
  183. if (found.value) {
  184. if (accept(aContainer)) {
  185. return aContainer;
  186. }
  187. } else if (aContainer == aComponent) {
  188. found.value = true;
  189. }
  190. return null;
  191. }
  192. /**
  193. * Returns the first Component in the traversal cycle. This method is used
  194. * to determine the next Component to focus when traversal wraps in the
  195. * forward direction.
  196. *
  197. * @param focusCycleRoot the focus cycle root whose first Component is to
  198. * be returned
  199. * @return the first Component in the traversal cycle when focusCycleRoot
  200. * is the focus cycle root, or null if no suitable Component can be
  201. * found
  202. * @throws IllegalArgumentException if focusCycleRoot is null
  203. */
  204. public Component getFirstComponent(Container focusCycleRoot) {
  205. if (focusCycleRoot == null) {
  206. throw new IllegalArgumentException("focusCycleRoot cannot be null");
  207. }
  208. synchronized(focusCycleRoot.getTreeLock()) {
  209. if (!(focusCycleRoot.isVisible() &&
  210. focusCycleRoot.isDisplayable()))
  211. {
  212. return null;
  213. }
  214. if (accept(focusCycleRoot)) {
  215. return focusCycleRoot;
  216. }
  217. for (int i = 0; i < focusCycleRoot.ncomponents; i++) {
  218. Component comp = focusCycleRoot.component[i];
  219. if (comp instanceof Container &&
  220. !((Container)comp).isFocusCycleRoot())
  221. {
  222. Component retval = getFirstComponent((Container)comp);
  223. if (retval != null) {
  224. return retval;
  225. }
  226. } else if (accept(comp)) {
  227. return comp;
  228. }
  229. }
  230. }
  231. return null;
  232. }
  233. /**
  234. * Returns the last Component in the traversal cycle. This method is used
  235. * to determine the next Component to focus when traversal wraps in the
  236. * reverse direction.
  237. *
  238. * @param focusCycleRoot the focus cycle root whose last Component is to be
  239. * returned
  240. * @return the last Component in the traversal cycle when focusCycleRoot is
  241. * the focus cycle root, or null if no suitable Component can be
  242. * found
  243. * @throws IllegalArgumentException if focusCycleRoot is null
  244. */
  245. public Component getLastComponent(Container focusCycleRoot) {
  246. if (focusCycleRoot == null) {
  247. throw new IllegalArgumentException("focusCycleRoot cannot be null");
  248. }
  249. synchronized(focusCycleRoot.getTreeLock()) {
  250. if (!(focusCycleRoot.isVisible() &&
  251. focusCycleRoot.isDisplayable()))
  252. {
  253. return null;
  254. }
  255. for (int i = focusCycleRoot.ncomponents - 1; i >= 0; i--) {
  256. Component comp = focusCycleRoot.component[i];
  257. if (comp instanceof Container &&
  258. !((Container)comp).isFocusCycleRoot())
  259. {
  260. Component retval = getLastComponent((Container)comp);
  261. if (retval != null) {
  262. return retval;
  263. }
  264. } else if (accept(comp)) {
  265. return comp;
  266. }
  267. }
  268. if (accept(focusCycleRoot)) {
  269. return focusCycleRoot;
  270. }
  271. }
  272. return null;
  273. }
  274. /**
  275. * Returns the default Component to focus. This Component will be the first
  276. * to receive focus when traversing down into a new focus traversal cycle
  277. * rooted at focusCycleRoot. The default implementation of this method
  278. * returns the same Component as <code>getFirstComponent</code>.
  279. *
  280. * @param focusCycleRoot the focus cycle root whose default Component is to
  281. * be returned
  282. * @return the default Component in the traversal cycle when focusCycleRoot
  283. * is the focus cycle root, or null if no suitable Component can be
  284. * found
  285. * @see #getFirstComponent
  286. * @throws IllegalArgumentException if focusCycleRoot is null
  287. */
  288. public Component getDefaultComponent(Container focusCycleRoot) {
  289. return getFirstComponent(focusCycleRoot);
  290. }
  291. /**
  292. * Sets whether this ContainerOrderFocusTraversalPolicy transfers focus
  293. * down-cycle implicitly. If <code>true</code>, during normal forward focus
  294. * traversal, the Component traversed after a focus cycle root will be the
  295. * focus-cycle-root's default Component to focus. If <code>false</code>,
  296. * the next Component in the focus traversal cycle rooted at the specified
  297. * focus cycle root will be traversed instead. The default value for this
  298. * property is <code>true</code>.
  299. *
  300. * @param implicitDownCycleTraversal whether this
  301. * ContainerOrderFocusTraversalPolicy transfers focus down-cycle
  302. * implicitly
  303. * @see #getImplicitDownCycleTraversal
  304. * @see #getFirstComponent
  305. */
  306. public void setImplicitDownCycleTraversal(boolean
  307. implicitDownCycleTraversal) {
  308. this.implicitDownCycleTraversal = implicitDownCycleTraversal;
  309. }
  310. /**
  311. * Returns whether this ContainerOrderFocusTraversalPolicy transfers focus
  312. * down-cycle implicitly. If <code>true</code>, during normal forward focus
  313. * traversal, the Component traversed after a focus cycle root will be the
  314. * focus-cycle-root's default Component to focus. If <code>false</code>,
  315. * the next Component in the focus traversal cycle rooted at the specified
  316. * focus cycle root will be traversed instead.
  317. *
  318. * @return whether this ContainerOrderFocusTraversalPolicy transfers focus
  319. * down-cycle implicitly
  320. * @see #setImplicitDownCycleTraversal
  321. * @see #getFirstComponent
  322. */
  323. public boolean getImplicitDownCycleTraversal() {
  324. return implicitDownCycleTraversal;
  325. }
  326. /**
  327. * Determines whether a Component is an acceptable choice as the new
  328. * focus owner. By default, this method will accept a Component if and
  329. * only if it is visible, displayable, enabled, and focusable.
  330. *
  331. * @param aComponent the Component whose fitness as a focus owner is to
  332. * be tested
  333. * @return <code>true</code> if aComponent is visible, displayable,
  334. * enabled, and focusable; <code>false</code> otherwise
  335. */
  336. protected boolean accept(Component aComponent) {
  337. if (!(aComponent.isVisible() && aComponent.isDisplayable() &&
  338. aComponent.isFocusable() && aComponent.isEnabled())) {
  339. return false;
  340. }
  341. // Verify that the Component is recursively enabled. Disabling a
  342. // heavyweight Container disables its children, whereas disabling
  343. // a lightweight Container does not.
  344. if (!(aComponent instanceof Window)) {
  345. for (Container enableTest = aComponent.getParent();
  346. enableTest != null;
  347. enableTest = enableTest.getParent())
  348. {
  349. if (!(enableTest.isEnabled() || enableTest.isLightweight())) {
  350. return false;
  351. }
  352. if (enableTest instanceof Window) {
  353. break;
  354. }
  355. }
  356. }
  357. return true;
  358. }
  359. }