KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > DefaultFocusTraversalPolicy


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
9 import java.awt.peer.ComponentPeer;
10
11
12 /**
13  * A FocusTraversalPolicy that determines traversal order based on the order
14  * of child Components in a Container. From a particular focus cycle root, the
15  * policy makes a pre-order traversal of the Component hierarchy, and traverses
16  * a Container's children according to the ordering of the array returned by
17  * <code>Container.getComponents()</code>. Portions of the hierarchy that are
18  * not visible and displayable will not be searched.
19  * <p>
20  * If client code has explicitly set the focusability of a Component by either
21  * overriding <code>Component.isFocusTraversable()</code> or
22  * <code>Component.isFocusable()</code>, or by calling
23  * <code>Component.setFocusable()</code>, then a DefaultFocusTraversalPolicy
24  * behaves exactly like a ContainerOrderFocusTraversalPolicy. If, however, the
25  * Component is relying on default focusability, then a
26  * DefaultFocusTraversalPolicy will reject all Components with non-focusable
27  * peers. This is the default FocusTraversalPolicy for all AWT Containers.
28  * <p>
29  * The focusability of a peer is implementation-dependent. Sun recommends that
30  * all implementations for a particular native platform construct peers with
31  * the same focusability. The recommendations for Windows and Unix are that
32  * Canvases, Labels, Panels, Scrollbars, ScrollPanes, Windows, and lightweight
33  * Components have non-focusable peers, and all other Components have focusable
34  * peers. These recommendations are used in the Sun AWT implementations. Note
35  * that the focusability of a Component's peer is different from, and does not
36  * impact, the focusability of the Component itself.
37  * <p>
38  * Please see
39  * <a HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html">
40  * How to Use the Focus Subsystem</a>,
41  * a section in <em>The Java Tutorial</em>, and the
42  * <a HREF="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>
43  * for more information.
44  *
45  * @author David Mendenhall
46  * @version 1.5, 12/19/03
47  *
48  * @see Container#getComponents
49  * @see Component#isFocusable
50  * @see Component#setFocusable
51  * @since 1.4
52  */

53 public class DefaultFocusTraversalPolicy
54     extends ContainerOrderFocusTraversalPolicy JavaDoc
55 {
56
57     /**
58      * Determines whether a Component is an acceptable choice as the new
59      * focus owner. The Component must be visible, displayable, and enabled
60      * to be accepted. If client code has explicitly set the focusability
61      * of the Component by either overriding
62      * <code>Component.isFocusTraversable()</code> or
63      * <code>Component.isFocusable()</code>, or by calling
64      * <code>Component.setFocusable()</code>, then the Component will be
65      * accepted if and only if it is focusable. If, however, the Component is
66      * relying on default focusability, then all Canvases, Labels, Panels,
67      * Scrollbars, ScrollPanes, Windows, and lightweight Components will be
68      * rejected.
69      *
70      * @param aComponent the Component whose fitness as a focus owner is to
71      * be tested
72      * @return <code>true</code> if aComponent meets the above requirements;
73      * <code>false</code> otherwise
74      */

75     protected boolean accept(Component JavaDoc aComponent) {
76         if (!(aComponent.isVisible() && aComponent.isDisplayable() &&
77           aComponent.isEnabled()))
78     {
79         return false;
80     }
81
82     // Verify that the Component is recursively enabled. Disabling a
83
// heavyweight Container disables its children, whereas disabling
84
// a lightweight Container does not.
85
if (!(aComponent instanceof Window JavaDoc)) {
86         for (Container JavaDoc enableTest = aComponent.getParent();
87          enableTest != null;
88          enableTest = enableTest.getParent())
89         {
90         if (!(enableTest.isEnabled() || enableTest.isLightweight())) {
91             return false;
92         }
93         if (enableTest instanceof Window JavaDoc) {
94             break;
95         }
96         }
97     }
98
99     boolean focusable = aComponent.isFocusable();
100     if (aComponent.isFocusTraversableOverridden()) {
101         return focusable;
102     }
103
104     ComponentPeer peer = aComponent.getPeer();
105     return (peer != null && peer.isFocusable());
106     }
107 }
108
Popular Tags