001: /*
002: * Copyright 2000-2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package java.awt;
026:
027: import java.awt.peer.ComponentPeer;
028:
029: /**
030: * A FocusTraversalPolicy that determines traversal order based on the order
031: * of child Components in a Container. From a particular focus cycle root, the
032: * policy makes a pre-order traversal of the Component hierarchy, and traverses
033: * a Container's children according to the ordering of the array returned by
034: * <code>Container.getComponents()</code>. Portions of the hierarchy that are
035: * not visible and displayable will not be searched.
036: * <p>
037: * If client code has explicitly set the focusability of a Component by either
038: * overriding <code>Component.isFocusTraversable()</code> or
039: * <code>Component.isFocusable()</code>, or by calling
040: * <code>Component.setFocusable()</code>, then a DefaultFocusTraversalPolicy
041: * behaves exactly like a ContainerOrderFocusTraversalPolicy. If, however, the
042: * Component is relying on default focusability, then a
043: * DefaultFocusTraversalPolicy will reject all Components with non-focusable
044: * peers. This is the default FocusTraversalPolicy for all AWT Containers.
045: * <p>
046: * The focusability of a peer is implementation-dependent. Sun recommends that
047: * all implementations for a particular native platform construct peers with
048: * the same focusability. The recommendations for Windows and Unix are that
049: * Canvases, Labels, Panels, Scrollbars, ScrollPanes, Windows, and lightweight
050: * Components have non-focusable peers, and all other Components have focusable
051: * peers. These recommendations are used in the Sun AWT implementations. Note
052: * that the focusability of a Component's peer is different from, and does not
053: * impact, the focusability of the Component itself.
054: * <p>
055: * Please see
056: * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html">
057: * How to Use the Focus Subsystem</a>,
058: * a section in <em>The Java Tutorial</em>, and the
059: * <a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>
060: * for more information.
061: *
062: * @author David Mendenhall
063: * @version 1.13, 05/05/07
064: *
065: * @see Container#getComponents
066: * @see Component#isFocusable
067: * @see Component#setFocusable
068: * @since 1.4
069: */
070: public class DefaultFocusTraversalPolicy extends
071: ContainerOrderFocusTraversalPolicy {
072: /*
073: * serialVersionUID
074: */
075: private static final long serialVersionUID = 8876966522510157497L;
076:
077: /**
078: * Determines whether a Component is an acceptable choice as the new
079: * focus owner. The Component must be visible, displayable, and enabled
080: * to be accepted. If client code has explicitly set the focusability
081: * of the Component by either overriding
082: * <code>Component.isFocusTraversable()</code> or
083: * <code>Component.isFocusable()</code>, or by calling
084: * <code>Component.setFocusable()</code>, then the Component will be
085: * accepted if and only if it is focusable. If, however, the Component is
086: * relying on default focusability, then all Canvases, Labels, Panels,
087: * Scrollbars, ScrollPanes, Windows, and lightweight Components will be
088: * rejected.
089: *
090: * @param aComponent the Component whose fitness as a focus owner is to
091: * be tested
092: * @return <code>true</code> if aComponent meets the above requirements;
093: * <code>false</code> otherwise
094: */
095: protected boolean accept(Component aComponent) {
096: if (!(aComponent.isVisible() && aComponent.isDisplayable() && aComponent
097: .isEnabled())) {
098: return false;
099: }
100:
101: // Verify that the Component is recursively enabled. Disabling a
102: // heavyweight Container disables its children, whereas disabling
103: // a lightweight Container does not.
104: if (!(aComponent instanceof Window)) {
105: for (Container enableTest = aComponent.getParent(); enableTest != null; enableTest = enableTest
106: .getParent()) {
107: if (!(enableTest.isEnabled() || enableTest
108: .isLightweight())) {
109: return false;
110: }
111: if (enableTest instanceof Window) {
112: break;
113: }
114: }
115: }
116:
117: boolean focusable = aComponent.isFocusable();
118: if (aComponent.isFocusTraversableOverridden()) {
119: return focusable;
120: }
121:
122: ComponentPeer peer = aComponent.getPeer();
123: return (peer != null && peer.isFocusable());
124: }
125: }
|