J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | package javax.swing; |
| 26 | |
| 27 | import java.awt.Component; |
| 28 | import java.awt.Container; |
| 29 | import java.awt.ComponentOrientation; |
| 30 | import java.util.Comparator; |
| 31 | import java.io.*; |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * A SortingFocusTraversalPolicy which sorts Components based on their size, |
| 36 | * position, and orientation. Based on their size and position, Components are |
| 37 | * roughly categorized into rows and columns. For a Container with horizontal |
| 38 | * orientation, columns run left-to-right or right-to-left, and rows run top- |
| 39 | * to-bottom. For a Container with vertical orientation, columns run top-to- |
| 40 | * bottom and rows run left-to-right or right-to-left. See |
| 41 | * <code>ComponentOrientation</code> for more information. All columns in a |
| 42 | * row are fully traversed before proceeding to the next row. |
| 43 | * |
| 44 | * @author David Mendenhall |
| 45 | * |
| 46 | * @see java.awt.ComponentOrientation |
| 47 | * @since 1.4 |
| 48 | */ |
| 49 | public class LayoutFocusTraversalPolicy extends SortingFocusTraversalPolicy |
| 50 | implements Serializable |
| 51 | { |
| 52 | // Delegate most of our fitness test to Default so that we only have to |
| 53 | // code the algorithm once. |
| 54 | private static final SwingDefaultFocusTraversalPolicy fitnessTestPolicy = |
| 55 | new SwingDefaultFocusTraversalPolicy(); |
| 56 | |
| 57 | /** |
| 58 | * Constructs a LayoutFocusTraversalPolicy. |
| 59 | */ |
| 60 | public LayoutFocusTraversalPolicy() { |
| 61 | super(new LayoutComparator()); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Constructs a LayoutFocusTraversalPolicy with the passed in |
| 66 | * <code>Comparator</code>. |
| 67 | */ |
| 68 | LayoutFocusTraversalPolicy(Comparator c) { |
| 69 | super(c); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns the Component that should receive the focus after aComponent. |
| 74 | * aContainer must be a focus cycle root of aComponent. |
| 75 | * <p> |
| 76 | * By default, LayoutFocusTraversalPolicy implicitly transfers focus down- |
| 77 | * cycle. That is, during normal focus traversal, the Component |
| 78 | * traversed after a focus cycle root will be the focus-cycle-root's |
| 79 | * default Component to focus. This behavior can be disabled using the |
| 80 | * <code>setImplicitDownCycleTraversal</code> method. |
| 81 | * <p> |
| 82 | * If aContainer is <a href="../../java/awt/doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus |
| 83 | * traversal policy provider</a>, the focus is always transferred down-cycle. |
| 84 | * |
| 85 | * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider |
| 86 | * @param aComponent a (possibly indirect) child of aContainer, or |
| 87 | * aContainer itself |
| 88 | * @return the Component that should receive the focus after aComponent, or |
| 89 | * null if no suitable Component can be found |
| 90 | * @throws IllegalArgumentException if aContainer is not a focus cycle |
| 91 | * root of aComponent or a focus traversal policy provider, or if either aContainer or |
| 92 | * aComponent is null |
| 93 | */ |
| 94 | public Component getComponentAfter(Container aContainer, |
| 95 | Component aComponent) { |
| 96 | if (aContainer == null || aComponent == null) { |
| 97 | throw new IllegalArgumentException("aContainer and aComponent cannot be null"); |
| 98 | } |
| 99 | Comparator comparator = getComparator(); |
| 100 | if (comparator instanceof LayoutComparator) { |
| 101 | ((LayoutComparator)comparator). |
| 102 | setComponentOrientation(aContainer. |
| 103 | getComponentOrientation()); |
| 104 | } |
| 105 | return super.getComponentAfter(aContainer, aComponent); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Returns the Component that should receive the focus before aComponent. |
| 110 | * aContainer must be a focus cycle root of aComponent. |
| 111 | * <p> |
| 112 | * By default, LayoutFocusTraversalPolicy implicitly transfers focus down- |
| 113 | * cycle. That is, during normal focus traversal, the Component |
| 114 | * traversed after a focus cycle root will be the focus-cycle-root's |
| 115 | * default Component to focus. This behavior can be disabled using the |
| 116 | * <code>setImplicitDownCycleTraversal</code> method. |
| 117 | * <p> |
| 118 | * If aContainer is <a href="../../java/awt/doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus |
| 119 | * traversal policy provider</a>, the focus is always transferred down-cycle. |
| 120 | * |
| 121 | * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider |
| 122 | * @param aComponent a (possibly indirect) child of aContainer, or |
| 123 | * aContainer itself |
| 124 | * @return the Component that should receive the focus before aComponent, |
| 125 | * or null if no suitable Component can be found |
| 126 | * @throws IllegalArgumentException if aContainer is not a focus cycle |
| 127 | * root of aComponent or a focus traversal policy provider, or if either aContainer or |
| 128 | * aComponent is null |
| 129 | */ |
| 130 | public Component getComponentBefore(Container aContainer, |
| 131 | Component aComponent) { |
| 132 | if (aContainer == null || aComponent == null) { |
| 133 | throw new IllegalArgumentException("aContainer and aComponent cannot be null"); |
| 134 | } |
| 135 | Comparator comparator = getComparator(); |
| 136 | if (comparator instanceof LayoutComparator) { |
| 137 | ((LayoutComparator)comparator). |
| 138 | setComponentOrientation(aContainer. |
| 139 | getComponentOrientation()); |
| 140 | } |
| 141 | return super.getComponentBefore(aContainer, aComponent); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Returns the first Component in the traversal cycle. This method is used |
| 146 | * to determine the next Component to focus when traversal wraps in the |
| 147 | * forward direction. |
| 148 | * |
| 149 | * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider whose |
| 150 | * first Component is to be returned |
| 151 | * @return the first Component in the traversal cycle of aContainer, |
| 152 | * or null if no suitable Component can be found |
| 153 | * @throws IllegalArgumentException if aContainer is null |
| 154 | */ |
| 155 | public Component getFirstComponent(Container aContainer) { |
| 156 | if (aContainer == null) { |
| 157 | throw new IllegalArgumentException("aContainer cannot be null"); |
| 158 | } |
| 159 | Comparator comparator = getComparator(); |
| 160 | if (comparator instanceof LayoutComparator) { |
| 161 | ((LayoutComparator)comparator). |
| 162 | setComponentOrientation(aContainer. |
| 163 | getComponentOrientation()); |
| 164 | } |
| 165 | return super.getFirstComponent(aContainer); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Returns the last Component in the traversal cycle. This method is used |
| 170 | * to determine the next Component to focus when traversal wraps in the |
| 171 | * reverse direction. |
| 172 | * |
| 173 | * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider whose |
| 174 | * last Component is to be returned |
| 175 | * @return the last Component in the traversal cycle of aContainer, |
| 176 | * or null if no suitable Component can be found |
| 177 | * @throws IllegalArgumentException if aContainer is null |
| 178 | */ |
| 179 | public Component getLastComponent(Container aContainer) { |
| 180 | if (aContainer == null) { |
| 181 | throw new IllegalArgumentException("aContainer cannot be null"); |
| 182 | } |
| 183 | Comparator comparator = getComparator(); |
| 184 | if (comparator instanceof LayoutComparator) { |
| 185 | ((LayoutComparator)comparator). |
| 186 | setComponentOrientation(aContainer. |
| 187 | getComponentOrientation()); |
| 188 | } |
| 189 | return super.getLastComponent(aContainer); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Determines whether the specified <code>Component</code> |
| 194 | * is an acceptable choice as the new focus owner. |
| 195 | * This method performs the following sequence of operations: |
| 196 | * <ol> |
| 197 | * <li>Checks whether <code>aComponent</code> is visible, displayable, |
| 198 | * enabled, and focusable. If any of these properties is |
| 199 | * <code>false</code>, this method returns <code>false</code>. |
| 200 | * <li>If <code>aComponent</code> is an instance of <code>JTable</code>, |
| 201 | * returns <code>true</code>. |
| 202 | * <li>If <code>aComponent</code> is an instance of <code>JComboBox</code>, |
| 203 | * then returns the value of |
| 204 | * <code>aComponent.getUI().isFocusTraversable(aComponent)</code>. |
| 205 | * <li>If <code>aComponent</code> is a <code>JComponent</code> |
| 206 | * with a <code>JComponent.WHEN_FOCUSED</code> |
| 207 | * <code>InputMap</code> that is neither <code>null</code> |
| 208 | * nor empty, returns <code>true</code>. |
| 209 | * <li>Returns the value of |
| 210 | * <code>DefaultFocusTraversalPolicy.accept(aComponent)</code>. |
| 211 | * </ol> |
| 212 | * |
| 213 | * @param aComponent the <code>Component</code> whose fitness |
| 214 | * as a focus owner is to be tested |
| 215 | * @see java.awt.Component#isVisible |
| 216 | * @see java.awt.Component#isDisplayable |
| 217 | * @see java.awt.Component#isEnabled |
| 218 | * @see java.awt.Component#isFocusable |
| 219 | * @see javax.swing.plaf.ComboBoxUI#isFocusTraversable |
| 220 | * @see javax.swing.JComponent#getInputMap |
| 221 | * @see java.awt.DefaultFocusTraversalPolicy#accept |
| 222 | * @return <code>true</code> if <code>aComponent</code> is a valid choice |
| 223 | * for a focus owner; |
| 224 | * otherwise <code>false</code> |
| 225 | */ |
| 226 | protected boolean accept(Component aComponent) { |
| 227 | if (!super.accept(aComponent)) { |
| 228 | return false; |
| 229 | } else if (aComponent instanceof JTable) { |
| 230 | // JTable only has ancestor focus bindings, we thus force it |
| 231 | // to be focusable by returning true here. |
| 232 | return true; |
| 233 | } else if (aComponent instanceof JComboBox) { |
| 234 | JComboBox box = (JComboBox)aComponent; |
| 235 | return box.getUI().isFocusTraversable(box); |
| 236 | } else if (aComponent instanceof JComponent) { |
| 237 | JComponent jComponent = (JComponent)aComponent; |
| 238 | InputMap inputMap = jComponent.getInputMap(JComponent.WHEN_FOCUSED, |
| 239 | false); |
| 240 | while (inputMap != null && inputMap.size() == 0) { |
| 241 | inputMap = inputMap.getParent(); |
| 242 | } |
| 243 | if (inputMap != null) { |
| 244 | return true; |
| 245 | } |
| 246 | // Delegate to the fitnessTestPolicy, this will test for the |
| 247 | // case where the developer has overriden isFocusTraversable to |
| 248 | // return true. |
| 249 | } |
| 250 | return fitnessTestPolicy.accept(aComponent); |
| 251 | } |
| 252 | |
| 253 | private void writeObject(ObjectOutputStream out) throws IOException { |
| 254 | out.writeObject(getComparator()); |
| 255 | out.writeBoolean(getImplicitDownCycleTraversal()); |
| 256 | } |
| 257 | private void readObject(ObjectInputStream in) |
| 258 | throws IOException, ClassNotFoundException |
| 259 | { |
| 260 | setComparator((Comparator)in.readObject()); |
| 261 | setImplicitDownCycleTraversal(in.readBoolean()); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | // Create our own subclass and change accept to public so that we can call |
| 266 | // accept. |
| 267 | class SwingDefaultFocusTraversalPolicy |
| 268 | extends java.awt.DefaultFocusTraversalPolicy |
| 269 | { |
| 270 | public boolean accept(Component aComponent) { |
| 271 | return super.accept(aComponent); |
| 272 | } |
| 273 | } |