1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
/* * @(#)FocusTraversalPolicy.java 1.9 05/11/17 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package java.awt; /** * A FocusTraversalPolicy defines the order in which Components with a * particular focus cycle root are traversed. Instances can apply the policy to * arbitrary focus cycle roots, allowing themselves to be shared across * Containers. They do not need to be reinitialized when the focus cycle roots * of a Component hierarchy change. * <p> * The core responsibility of a FocusTraversalPolicy is to provide algorithms * determining the next and previous Components to focus when traversing * forward or backward in a UI. Each FocusTraversalPolicy must also provide * algorithms for determining the first, last, and default Components in a * traversal cycle. First and last Components are used when normal forward and * backward traversal, respectively, wraps. The default Component is the first * to receive focus when traversing down into a new focus traversal cycle. * A FocusTraversalPolicy can optionally provide an algorithm for determining * a Window's initial Component. The initial Component is the first to receive * focus when a Window is first made visible. * <p> * FocusTraversalPolicy takes into account <a * href="doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus traversal * policy providers</a>. When searching for first/last/next/previous Component, * if a focus traversal policy provider is encountered, its focus traversal * policy is used to perform the search operation. * <p> * Please see * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html"> * How to Use the Focus Subsystem</a>, * a section in <em>The Java Tutorial</em>, and the * <a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a> * for more information. * * @author David Mendenhall * @version 1.9, 11/17/05 * * @see Container#setFocusTraversalPolicy * @see Container#getFocusTraversalPolicy * @see Container#setFocusCycleRoot * @see Container#isFocusCycleRoot * @see Container#setFocusTraversalPolicyProvider * @see Container#isFocusTraversalPolicyProvider * @see KeyboardFocusManager#setDefaultFocusTraversalPolicy * @see KeyboardFocusManager#getDefaultFocusTraversalPolicy * @since 1.4 */ public abstract class FocusTraversalPolicy { /** * Returns the Component that should receive the focus after aComponent. * aContainer must be a focus cycle root of aComponent or a focus traversal * policy provider. * * @param aContainer a focus cycle root of aComponent or focus traversal * policy provider * @param aComponent a (possibly indirect) child of aContainer, or * aContainer itself * @return the Component that should receive the focus after aComponent, or * null if no suitable Component can be found * @throws IllegalArgumentException if aContainer is not a focus cycle * root of aComponent or a focus traversal policy provider, or if * either aContainer or aComponent is null */ public abstract Component getComponentAfter(Container aContainer, Component aComponent); /** * Returns the Component that should receive the focus before aComponent. * aContainer must be a focus cycle root of aComponent or a focus traversal * policy provider. * * @param aContainer a focus cycle root of aComponent or focus traversal * policy provider * @param aComponent a (possibly indirect) child of aContainer, or * aContainer itself * @return the Component that should receive the focus before aComponent, * or null if no suitable Component can be found * @throws IllegalArgumentException if aContainer is not a focus cycle * root of aComponent or a focus traversal policy provider, or if * either aContainer or aComponent is null */ public abstract Component getComponentBefore(Container aContainer, Component aComponent); /** * Returns the first Component in the traversal cycle. This method is used * to determine the next Component to focus when traversal wraps in the * forward direction. * * @param aContainer the focus cycle root or focus traversal policy provider * whose first Component is to be returned * @return the first Component in the traversal cycle of aContainer, * or null if no suitable Component can be found * @throws IllegalArgumentException if aContainer is null */ public abstract Component getFirstComponent(Container aContainer); /** * Returns the last Component in the traversal cycle. This method is used * to determine the next Component to focus when traversal wraps in the * reverse direction. * * @param aContainer the focus cycle root or focus traversal policy * provider whose last Component is to be returned * @return the last Component in the traversal cycle of aContainer, * or null if no suitable Component can be found * @throws IllegalArgumentException if aContainer is null */ public abstract Component getLastComponent(Container aContainer); /** * Returns the default Component to focus. This Component will be the first * to receive focus when traversing down into a new focus traversal cycle * rooted at aContainer. * * @param aContainer the focus cycle root or focus traversal policy * provider whose default Component is to be returned * @return the default Component in the traversal cycle of aContainer, * or null if no suitable Component can be found * @throws IllegalArgumentException if aContainer is null */ public abstract Component getDefaultComponent(Container aContainer); /** * Returns the Component that should receive the focus when a Window is * made visible for the first time. Once the Window has been made visible * by a call to <code>show()</code> or <code>setVisible(true)</code>, the * initial Component will not be used again. Instead, if the Window loses * and subsequently regains focus, or is made invisible or undisplayable * and subsequently made visible and displayable, the Window's most * recently focused Component will become the focus owner. The default * implementation of this method returns the default Component. * * @param window the Window whose initial Component is to be returned * @return the Component that should receive the focus when window is made * visible for the first time, or null if no suitable Component can * be found * @see #getDefaultComponent * @see Window#getMostRecentFocusOwner * @throws IllegalArgumentException if window is null */ public Component getInitialComponent(Window window) { if ( window == null ){ throw new IllegalArgumentException("window cannot be equal to null."); } Component def = getDefaultComponent(window); if (def == null && window.isFocusableWindow()) { def = window; } return def; } }