|
| 1 | +/* |
| 2 | + * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
| 4 | + * |
| 5 | + * |
| 6 | + * |
| 7 | + * |
| 8 | + * |
| 9 | + * |
| 10 | + * |
| 11 | + * |
| 12 | + * |
| 13 | + * |
| 14 | + * |
| 15 | + * |
| 16 | + * |
| 17 | + * |
| 18 | + * |
| 19 | + * |
| 20 | + * |
| 21 | + * |
| 22 | + * |
| 23 | + * |
| 24 | + */ |
| 25 | +package java.awt; |
| 26 | + |
| 27 | +import java.awt.peer.ComponentPeer; |
| 28 | + |
| 29 | + |
| 30 | +/** |
| 31 | + * A FocusTraversalPolicy that determines traversal order based on the order |
| 32 | + * of child Components in a Container. From a particular focus cycle root, the |
| 33 | + * policy makes a pre-order traversal of the Component hierarchy, and traverses |
| 34 | + * a Container's children according to the ordering of the array returned by |
| 35 | + * <code>Container.getComponents()</code>. Portions of the hierarchy that are |
| 36 | + * not visible and displayable will not be searched. |
| 37 | + * <p> |
| 38 | + * If client code has explicitly set the focusability of a Component by either |
| 39 | + * overriding <code>Component.isFocusTraversable()</code> or |
| 40 | + * <code>Component.isFocusable()</code>, or by calling |
| 41 | + * <code>Component.setFocusable()</code>, then a DefaultFocusTraversalPolicy |
| 42 | + * behaves exactly like a ContainerOrderFocusTraversalPolicy. If, however, the |
| 43 | + * Component is relying on default focusability, then a |
| 44 | + * DefaultFocusTraversalPolicy will reject all Components with non-focusable |
| 45 | + * peers. This is the default FocusTraversalPolicy for all AWT Containers. |
| 46 | + * <p> |
| 47 | + * The focusability of a peer is implementation-dependent. Sun recommends that |
| 48 | + * all implementations for a particular native platform construct peers with |
| 49 | + * the same focusability. The recommendations for Windows and Unix are that |
| 50 | + * Canvases, Labels, Panels, Scrollbars, ScrollPanes, Windows, and lightweight |
| 51 | + * Components have non-focusable peers, and all other Components have focusable |
| 52 | + * peers. These recommendations are used in the Sun AWT implementations. Note |
| 53 | + * that the focusability of a Component's peer is different from, and does not |
| 54 | + * impact, the focusability of the Component itself. |
| 55 | + * <p> |
| 56 | + * Please see |
| 57 | + * <a href="https://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html"> |
| 58 | + * How to Use the Focus Subsystem</a>, |
| 59 | + * a section in <em>The Java Tutorial</em>, and the |
| 60 | + * <a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a> |
| 61 | + * for more information. |
| 62 | + * |
| 63 | + * @author David Mendenhall |
| 64 | + * |
| 65 | + * @see Container#getComponents |
| 66 | + * @see Component#isFocusable |
| 67 | + * @see Component#setFocusable |
| 68 | + * @since 1.4 |
| 69 | + */ |
| 70 | +public class DefaultFocusTraversalPolicy |
| 71 | + extends ContainerOrderFocusTraversalPolicy |
| 72 | +{ |
| 73 | + /* |
| 74 | + * serialVersionUID |
| 75 | + */ |
| 76 | + private static final long serialVersionUID = 8876966522510157497L; |
| 77 | + |
| 78 | + /** |
| 79 | + * Determines whether a Component is an acceptable choice as the new |
| 80 | + * focus owner. The Component must be visible, displayable, and enabled |
| 81 | + * to be accepted. If client code has explicitly set the focusability |
| 82 | + * of the Component by either overriding |
| 83 | + * <code>Component.isFocusTraversable()</code> or |
| 84 | + * <code>Component.isFocusable()</code>, or by calling |
| 85 | + * <code>Component.setFocusable()</code>, then the Component will be |
| 86 | + * accepted if and only if it is focusable. If, however, the Component is |
| 87 | + * relying on default focusability, then all Canvases, Labels, Panels, |
| 88 | + * Scrollbars, ScrollPanes, Windows, and lightweight Components will be |
| 89 | + * rejected. |
| 90 | + * |
| 91 | + * @param aComponent the Component whose fitness as a focus owner is to |
| 92 | + * be tested |
| 93 | + * @return <code>true</code> if aComponent meets the above requirements; |
| 94 | + * <code>false</code> otherwise |
| 95 | + */ |
| 96 | + protected boolean accept(Component aComponent) { |
| 97 | + if (!(aComponent.isVisible() && aComponent.isDisplayable() && |
| 98 | + aComponent.isEnabled())) |
| 99 | + { |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + // Verify that the Component is recursively enabled. Disabling a |
| 104 | + // heavyweight Container disables its children, whereas disabling |
| 105 | + // a lightweight Container does not. |
| 106 | + if (!(aComponent instanceof Window)) { |
| 107 | + for (Container enableTest = aComponent.getParent(); |
| 108 | + enableTest != null; |
| 109 | + enableTest = enableTest.getParent()) |
| 110 | + { |
| 111 | + if (!(enableTest.isEnabled() || enableTest.isLightweight())) { |
| 112 | + return false; |
| 113 | + } |
| 114 | + if (enableTest instanceof Window) { |
| 115 | + break; |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + boolean focusable = aComponent.isFocusable(); |
| 121 | + if (aComponent.isFocusTraversableOverridden()) { |
| 122 | + return focusable; |
| 123 | + } |
| 124 | + |
| 125 | + ComponentPeer peer = aComponent.getPeer(); |
| 126 | + return (peer != null && peer.isFocusable()); |
| 127 | + } |
| 128 | +} |
0 commit comments