Showing the changes to java/pkg/javax/swing/SortingFocusTraversalPolicy.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,596 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2000, 2008, Oracle and/or its affiliates. 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. Oracle designates this | ||
| 8 | * particular file as subject to the "Classpath" exception as provided | ||
| 9 | * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| 22 | * or visit www.oracle.com if you need additional information or have any | ||
| 23 | * questions. | ||
| 24 | */ | ||
| 25 | package javax.swing; | ||
| 26 | |||
| 27 | import java.awt.Component; | ||
| 28 | import java.awt.Container; | ||
| 29 | import java.awt.Window; | ||
| 30 | import java.util.*; | ||
| 31 | import java.awt.FocusTraversalPolicy; | ||
| 32 | import sun.util.logging.PlatformLogger; | ||
| 33 | |||
| 34 | /** | ||
| 35 | * A FocusTraversalPolicy that determines traversal order by sorting the | ||
| 36 | * Components of a focus traversal cycle based on a given Comparator. Portions | ||
| 37 | * of the Component hierarchy that are not visible and displayable will not be | ||
| 38 | * included. | ||
| 39 | * <p> | ||
| 40 | * By default, SortingFocusTraversalPolicy implicitly transfers focus down- | ||
| 41 | * cycle. That is, during normal focus traversal, the Component | ||
| 42 | * traversed after a focus cycle root will be the focus-cycle-root's default | ||
| 43 | * Component to focus. This behavior can be disabled using the | ||
| 44 | * <code>setImplicitDownCycleTraversal</code> method. | ||
| 45 | * <p> | ||
| 46 | * By default, methods of this class with return a Component only if it is | ||
| 47 | * visible, displayable, enabled, and focusable. Subclasses can modify this | ||
| 48 | * behavior by overriding the <code>accept</code> method. | ||
| 49 | * <p> | ||
| 50 | * This policy takes into account <a | ||
| 51 | * href="../../java/awt/doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus traversal | ||
| 52 | * policy providers</a>. When searching for first/last/next/previous Component, | ||
| 53 | * if a focus traversal policy provider is encountered, its focus traversal | ||
| 54 | * policy is used to perform the search operation. | ||
| 55 | * | ||
| 56 | * @author David Mendenhall | ||
| 57 | * | ||
| 58 | * @see java.util.Comparator | ||
| 59 | * @since 1.4 | ||
| 60 | */ | ||
| 61 | public class SortingFocusTraversalPolicy | ||
| 62 | extends InternalFrameFocusTraversalPolicy | ||
| 63 | { | ||
| 64 | private Comparator<? super Component> comparator; | ||
| 65 | private boolean implicitDownCycleTraversal = true; | ||
| 66 | |||
| 67 | private PlatformLogger log = PlatformLogger.getLogger("javax.swing.SortingFocusTraversalPolicy"); | ||
| 68 | |||
| 69 | /** | ||
| 70 | * Used by getComponentAfter and getComponentBefore for efficiency. In | ||
| 71 | * order to maintain compliance with the specification of | ||
| 72 | * FocusTraversalPolicy, if traversal wraps, we should invoke | ||
| 73 | * getFirstComponent or getLastComponent. These methods may be overriden in | ||
| 74 | * subclasses to behave in a non-generic way. However, in the generic case, | ||
| 75 | * these methods will simply return the first or last Components of the | ||
| 76 | * sorted list, respectively. Since getComponentAfter and | ||
| 77 | * getComponentBefore have already built the sorted list before determining | ||
| 78 | * that they need to invoke getFirstComponent or getLastComponent, the | ||
| 79 | * sorted list should be reused if possible. | ||
| 80 | */ | ||
| 81 | transient private Container cachedRoot; | ||
| 82 | transient private List<Component> cachedCycle; | ||
| 83 | |||
| 84 | // Delegate our fitness test to ContainerOrder so that we only have to | ||
| 85 | // code the algorithm once. | ||
| 86 | private static final SwingContainerOrderFocusTraversalPolicy | ||
| 87 | fitnessTestPolicy = new SwingContainerOrderFocusTraversalPolicy(); | ||
| 88 | |||
| 89 | final private int FORWARD_TRAVERSAL = 0; | ||
| 90 | final private int BACKWARD_TRAVERSAL = 1; | ||
| 91 | |||
| 92 | /** | ||
| 93 | * Constructs a SortingFocusTraversalPolicy without a Comparator. | ||
| 94 | * Subclasses must set the Comparator using <code>setComparator</code> | ||
| 95 | * before installing this FocusTraversalPolicy on a focus cycle root or | ||
| 96 | * KeyboardFocusManager. | ||
| 97 | */ | ||
| 98 | protected SortingFocusTraversalPolicy() { | ||
| 99 | } | ||
| 100 | |||
| 101 | /** | ||
| 102 | * Constructs a SortingFocusTraversalPolicy with the specified Comparator. | ||
| 103 | */ | ||
| 104 | public SortingFocusTraversalPolicy(Comparator<? super Component> comparator) { | ||
| 105 | this.comparator = comparator; | ||
| 106 | } | ||
| 107 | |||
| 108 | private List<Component> getFocusTraversalCycle(Container aContainer) { | ||
| 109 | List<Component> cycle = new ArrayList<Component>(); | ||
| 110 | enumerateAndSortCycle(aContainer, cycle); | ||
| 111 | return cycle; | ||
| 112 | } | ||
| 113 | private int getComponentIndex(List<Component> cycle, Component aComponent) { | ||
| 114 | int index; | ||
| 115 | try { | ||
| 116 | index = Collections.binarySearch(cycle, aComponent, comparator); | ||
| 117 | } catch (ClassCastException e) { | ||
| 118 | if (log.isLoggable(PlatformLogger.FINE)) { | ||
| 119 | log.fine("### During the binary search for " + aComponent + " the exception occured: ", e); | ||
| 120 | } | ||
| 121 | return -1; | ||
| 122 | } | ||
| 123 | if (index < 0) { | ||
| 124 | // Fix for 5070991. | ||
| 125 | // A workaround for a transitivity problem caused by ROW_TOLERANCE, | ||
| 126 | // because of that the component may be missed in the binary search. | ||
| 127 | // Try to search it again directly. | ||
| 128 | index = cycle.indexOf(aComponent); | ||
| 129 | } | ||
| 130 | return index; | ||
| 131 | } | ||
| 132 | |||
| 133 | private void enumerateAndSortCycle(Container focusCycleRoot, List<Component> cycle) { | ||
| 134 | if (focusCycleRoot.isShowing()) { | ||
| 135 | enumerateCycle(focusCycleRoot, cycle); | ||
| 136 | Collections.sort(cycle, comparator); | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | private void enumerateCycle(Container container, List<Component> cycle) { | ||
| 141 | if (!(container.isVisible() && container.isDisplayable())) { | ||
| 142 | return; | ||
| 143 | } | ||
| 144 | |||
| 145 | cycle.add(container); | ||
| 146 | |||
| 147 | Component[] components = container.getComponents(); | ||
| 148 | for (Component comp : components) { | ||
| 149 | if (comp instanceof Container) { | ||
| 150 | Container cont = (Container)comp; | ||
| 151 | |||
| 152 | if (!cont.isFocusCycleRoot() && | ||
| 153 | !cont.isFocusTraversalPolicyProvider() && | ||
| 154 | !((cont instanceof JComponent) && ((JComponent)cont).isManagingFocus())) | ||
| 155 | { | ||
| 156 | enumerateCycle(cont, cycle); | ||
| 157 | continue; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | cycle.add(comp); | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | Container getTopmostProvider(Container focusCycleRoot, Component aComponent) { | ||
| 165 | Container aCont = aComponent.getParent(); | ||
| 166 | Container ftp = null; | ||
| 167 | while (aCont != focusCycleRoot && aCont != null) { | ||
| 168 | if (aCont.isFocusTraversalPolicyProvider()) { | ||
| 169 | ftp = aCont; | ||
| 170 | } | ||
| 171 | aCont = aCont.getParent(); | ||
| 172 | } | ||
| 173 | if (aCont == null) { | ||
| 174 | return null; | ||
| 175 | } | ||
| 176 | return ftp; | ||
| 177 | } | ||
| 178 | |||
| 179 | /* | ||
| 180 | * Checks if a new focus cycle takes place and returns a Component to traverse focus to. | ||
| 181 | * @param comp a possible focus cycle root or policy provider | ||
| 182 | * @param traversalDirection the direction of the traversal | ||
| 183 | * @return a Component to traverse focus to if {@code comp} is a root or provider | ||
| 184 | * and implicit down-cycle is set, otherwise {@code null} | ||
| 185 | */ | ||
| 186 | private Component getComponentDownCycle(Component comp, int traversalDirection) { | ||
| 187 | Component retComp = null; | ||
| 188 | |||
| 189 | if (comp instanceof Container) { | ||
| 190 | Container cont = (Container)comp; | ||
| 191 | |||
| 192 | if (cont.isFocusCycleRoot()) { | ||
| 193 | if (getImplicitDownCycleTraversal()) { | ||
| 194 | retComp = cont.getFocusTraversalPolicy().getDefaultComponent(cont); | ||
| 195 | |||
| 196 | if (retComp != null && log.isLoggable(PlatformLogger.FINE)) { | ||
| 197 | log.fine("### Transfered focus down-cycle to " + retComp + | ||
| 198 | " in the focus cycle root " + cont); | ||
| 199 | } | ||
| 200 | } else { | ||
| 201 | return null; | ||
| 202 | } | ||
| 203 | } else if (cont.isFocusTraversalPolicyProvider()) { | ||
| 204 | retComp = (traversalDirection == FORWARD_TRAVERSAL ? | ||
| 205 | cont.getFocusTraversalPolicy().getDefaultComponent(cont) : | ||
| 206 | cont.getFocusTraversalPolicy().getLastComponent(cont)); | ||
| 207 | |||
| 208 | if (retComp != null && log.isLoggable(PlatformLogger.FINE)) { | ||
| 209 | log.fine("### Transfered focus to " + retComp + " in the FTP provider " + cont); | ||
| 210 | } | ||
| 211 | } | ||
| 212 | } | ||
| 213 | return retComp; | ||
| 214 | } | ||
| 215 | |||
| 216 | /** | ||
| 217 | * Returns the Component that should receive the focus after aComponent. | ||
| 218 | * aContainer must be a focus cycle root of aComponent or a focus traversal policy provider. | ||
| 219 | * <p> | ||
| 220 | * By default, SortingFocusTraversalPolicy implicitly transfers focus down- | ||
| 221 | * cycle. That is, during normal focus traversal, the Component | ||
| 222 | * traversed after a focus cycle root will be the focus-cycle-root's | ||
| 223 | * default Component to focus. This behavior can be disabled using the | ||
| 224 | * <code>setImplicitDownCycleTraversal</code> method. | ||
| 225 | * <p> | ||
| 226 | * If aContainer is <a href="../../java/awt/doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus | ||
| 227 | * traversal policy provider</a>, the focus is always transferred down-cycle. | ||
| 228 | * | ||
| 229 | * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider | ||
| 230 | * @param aComponent a (possibly indirect) child of aContainer, or | ||
| 231 | * aContainer itself | ||
| 232 | * @return the Component that should receive the focus after aComponent, or | ||
| 233 | * null if no suitable Component can be found | ||
| 234 | * @throws IllegalArgumentException if aContainer is not a focus cycle | ||
| 235 | * root of aComponent or a focus traversal policy provider, or if either aContainer or | ||
| 236 | * aComponent is null | ||
| 237 | */ | ||
| 238 | public Component getComponentAfter(Container aContainer, Component aComponent) { | ||
| 239 | if (log.isLoggable(PlatformLogger.FINE)) { | ||
| 240 | log.fine("### Searching in " + aContainer + " for component after " + aComponent); | ||
| 241 | } | ||
| 242 | |||
| 243 | if (aContainer == null || aComponent == null) { | ||
| 244 | throw new IllegalArgumentException("aContainer and aComponent cannot be null"); | ||
| 245 | } | ||
| 246 | if (!aContainer.isFocusTraversalPolicyProvider() && !aContainer.isFocusCycleRoot()) { | ||
| 247 | throw new IllegalArgumentException("aContainer should be focus cycle root or focus traversal policy provider"); | ||
| 248 | |||
| 249 | } else if (aContainer.isFocusCycleRoot() && !aComponent.isFocusCycleRoot(aContainer)) { | ||
| 250 | throw new IllegalArgumentException("aContainer is not a focus cycle root of aComponent"); | ||
| 251 | } | ||
| 252 | |||
| 253 | // Before all the ckecks below we first see if it's an FTP provider or a focus cycle root. | ||
| 254 | // If it's the case just go down cycle (if it's set to "implicit"). | ||
| 255 | Component comp = getComponentDownCycle(aComponent, FORWARD_TRAVERSAL); | ||
| 256 | if (comp != null) { | ||
| 257 | return comp; | ||
| 258 | } | ||
| 259 | |||
| 260 | // See if the component is inside of policy provider. | ||
| 261 | Container provider = getTopmostProvider(aContainer, aComponent); | ||
| 262 | if (provider != null) { | ||
| 263 | if (log.isLoggable(PlatformLogger.FINE)) { | ||
| 264 | log.fine("### Asking FTP " + provider + " for component after " + aComponent); | ||
| 265 | } | ||
| 266 | |||
| 267 | // FTP knows how to find component after the given. We don't. | ||
| 268 | FocusTraversalPolicy policy = provider.getFocusTraversalPolicy(); | ||
| 269 | Component afterComp = policy.getComponentAfter(provider, aComponent); | ||
| 270 | |||
| 271 | // Null result means that we overstepped the limit of the FTP's cycle. | ||
| 272 | // In that case we must quit the cycle, otherwise return the component found. | ||
| 273 | if (afterComp != null) { | ||
| 274 | if (log.isLoggable(PlatformLogger.FINE)) log.fine("### FTP returned " + afterComp); | ||
| 275 | return afterComp; | ||
| 276 | } | ||
| 277 | aComponent = provider; | ||
| 278 | } | ||
| 279 | |||
| 280 | List<Component> cycle = getFocusTraversalCycle(aContainer); | ||
| 281 | |||
| 282 | if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Cycle is " + cycle + ", component is " + aComponent); | ||
| 283 | |||
| 284 | int index = getComponentIndex(cycle, aComponent); | ||
| 285 | |||
| 286 | if (index < 0) { | ||
| 287 | if (log.isLoggable(PlatformLogger.FINE)) { | ||
| 288 | log.fine("### Didn't find component " + aComponent + " in a cycle " + aContainer); | ||
| 289 | } | ||
| 290 | return getFirstComponent(aContainer); | ||
| 291 | } | ||
| 292 | |||
| 293 | for (index++; index < cycle.size(); index++) { | ||
| 294 | comp = cycle.get(index); | ||
| 295 | if (accept(comp)) { | ||
| 296 | return comp; | ||
| 297 | } else if ((comp = getComponentDownCycle(comp, FORWARD_TRAVERSAL)) != null) { | ||
| 298 | return comp; | ||
| 299 | } | ||
| 300 | } | ||
| 301 | |||
| 302 | if (aContainer.isFocusCycleRoot()) { | ||
| 303 | this.cachedRoot = aContainer; | ||
| 304 | this.cachedCycle = cycle; | ||
| 305 | |||
| 306 | comp = getFirstComponent(aContainer); | ||
| 307 | |||
| 308 | this.cachedRoot = null; | ||
| 309 | this.cachedCycle = null; | ||
| 310 | |||
| 311 | return comp; | ||
| 312 | } | ||
| 313 | return null; | ||
| 314 | } | ||
| 315 | |||
| 316 | /** | ||
| 317 | * Returns the Component that should receive the focus before aComponent. | ||
| 318 | * aContainer must be a focus cycle root of aComponent or a focus traversal policy provider. | ||
| 319 | * <p> | ||
| 320 | * By default, SortingFocusTraversalPolicy implicitly transfers focus down- | ||
| 321 | * cycle. That is, during normal focus traversal, the Component | ||
| 322 | * traversed after a focus cycle root will be the focus-cycle-root's | ||
| 323 | * default Component to focus. This behavior can be disabled using the | ||
| 324 | * <code>setImplicitDownCycleTraversal</code> method. | ||
| 325 | * <p> | ||
| 326 | * If aContainer is <a href="../../java/awt/doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus | ||
| 327 | * traversal policy provider</a>, the focus is always transferred down-cycle. | ||
| 328 | * | ||
| 329 | * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider | ||
| 330 | * @param aComponent a (possibly indirect) child of aContainer, or | ||
| 331 | * aContainer itself | ||
| 332 | * @return the Component that should receive the focus before aComponent, | ||
| 333 | * or null if no suitable Component can be found | ||
| 334 | * @throws IllegalArgumentException if aContainer is not a focus cycle | ||
| 335 | * root of aComponent or a focus traversal policy provider, or if either aContainer or | ||
| 336 | * aComponent is null | ||
| 337 | */ | ||
| 338 | public Component getComponentBefore(Container aContainer, Component aComponent) { | ||
| 339 | if (aContainer == null || aComponent == null) { | ||
| 340 | throw new IllegalArgumentException("aContainer and aComponent cannot be null"); | ||
| 341 | } | ||
| 342 | if (!aContainer.isFocusTraversalPolicyProvider() && !aContainer.isFocusCycleRoot()) { | ||
| 343 | throw new IllegalArgumentException("aContainer should be focus cycle root or focus traversal policy provider"); | ||
| 344 | |||
| 345 | } else if (aContainer.isFocusCycleRoot() && !aComponent.isFocusCycleRoot(aContainer)) { | ||
| 346 | throw new IllegalArgumentException("aContainer is not a focus cycle root of aComponent"); | ||
| 347 | } | ||
| 348 | |||
| 349 | // See if the component is inside of policy provider. | ||
| 350 | Container provider = getTopmostProvider(aContainer, aComponent); | ||
| 351 | if (provider != null) { | ||
| 352 | if (log.isLoggable(PlatformLogger.FINE)) { | ||
| 353 | log.fine("### Asking FTP " + provider + " for component after " + aComponent); | ||
| 354 | } | ||
| 355 | |||
| 356 | // FTP knows how to find component after the given. We don't. | ||
| 357 | FocusTraversalPolicy policy = provider.getFocusTraversalPolicy(); | ||
| 358 | Component beforeComp = policy.getComponentBefore(provider, aComponent); | ||
| 359 | |||
| 360 | // Null result means that we overstepped the limit of the FTP's cycle. | ||
| 361 | // In that case we must quit the cycle, otherwise return the component found. | ||
| 362 | if (beforeComp != null) { | ||
| 363 | if (log.isLoggable(PlatformLogger.FINE)) log.fine("### FTP returned " + beforeComp); | ||
| 364 | return beforeComp; | ||
| 365 | } | ||
| 366 | aComponent = provider; | ||
| 367 | |||
| 368 | // If the provider is traversable it's returned. | ||
| 369 | if (accept(aComponent)) { | ||
| 370 | return aComponent; | ||
| 371 | } | ||
| 372 | } | ||
| 373 | |||
| 374 | List<Component> cycle = getFocusTraversalCycle(aContainer); | ||
| 375 | |||
| 376 | if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Cycle is " + cycle + ", component is " + aComponent); | ||
| 377 | |||
| 378 | int index = getComponentIndex(cycle, aComponent); | ||
| 379 | |||
| 380 | if (index < 0) { | ||
| 381 | if (log.isLoggable(PlatformLogger.FINE)) { | ||
| 382 | log.fine("### Didn't find component " + aComponent + " in a cycle " + aContainer); | ||
| 383 | } | ||
| 384 | return getLastComponent(aContainer); | ||
| 385 | } | ||
| 386 | |||
| 387 | Component comp; | ||
| 388 | Component tryComp; | ||
| 389 | |||
| 390 | for (index--; index>=0; index--) { | ||
| 391 | comp = cycle.get(index); | ||
| 392 | if (comp != aContainer && (tryComp = getComponentDownCycle(comp, BACKWARD_TRAVERSAL)) != null) { | ||
| 393 | return tryComp; | ||
| 394 | } else if (accept(comp)) { | ||
| 395 | return comp; | ||
| 396 | } | ||
| 397 | } | ||
| 398 | |||
| 399 | if (aContainer.isFocusCycleRoot()) { | ||
| 400 | this.cachedRoot = aContainer; | ||
| 401 | this.cachedCycle = cycle; | ||
| 402 | |||
| 403 | comp = getLastComponent(aContainer); | ||
| 404 | |||
| 405 | this.cachedRoot = null; | ||
| 406 | this.cachedCycle = null; | ||
| 407 | |||
| 408 | return comp; | ||
| 409 | } | ||
| 410 | return null; | ||
| 411 | } | ||
| 412 | |||
| 413 | /** | ||
| 414 | * Returns the first Component in the traversal cycle. This method is used | ||
| 415 | * to determine the next Component to focus when traversal wraps in the | ||
| 416 | * forward direction. | ||
| 417 | * | ||
| 418 | * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider whose | ||
| 419 | * first Component is to be returned | ||
| 420 | * @return the first Component in the traversal cycle of aContainer, | ||
| 421 | * or null if no suitable Component can be found | ||
| 422 | * @throws IllegalArgumentException if aContainer is null | ||
| 423 | */ | ||
| 424 | public Component getFirstComponent(Container aContainer) { | ||
| 425 | List<Component> cycle; | ||
| 426 | |||
| 427 | if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Getting first component in " + aContainer); | ||
| 428 | if (aContainer == null) { | ||
| 429 | throw new IllegalArgumentException("aContainer cannot be null"); | ||
| 430 | } | ||
| 431 | |||
| 432 | if (this.cachedRoot == aContainer) { | ||
| 433 | cycle = this.cachedCycle; | ||
| 434 | } else { | ||
| 435 | cycle = getFocusTraversalCycle(aContainer); | ||
| 436 | } | ||
| 437 | |||
| 438 | if (cycle.size() == 0) { | ||
| 439 | if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Cycle is empty"); | ||
| 440 | return null; | ||
| 441 | } | ||
| 442 | if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Cycle is " + cycle); | ||
| 443 | |||
| 444 | for (Component comp : cycle) { | ||
| 445 | if (accept(comp)) { | ||
| 446 | return comp; | ||
| 447 | } else if (comp != aContainer && | ||
| 448 | (comp = getComponentDownCycle(comp, FORWARD_TRAVERSAL)) != null) | ||
| 449 | { | ||
| 450 | return comp; | ||
| 451 | } | ||
| 452 | } | ||
| 453 | return null; | ||
| 454 | } | ||
| 455 | |||
| 456 | /** | ||
| 457 | * Returns the last Component in the traversal cycle. This method is used | ||
| 458 | * to determine the next Component to focus when traversal wraps in the | ||
| 459 | * reverse direction. | ||
| 460 | * | ||
| 461 | * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider whose | ||
| 462 | * last Component is to be returned | ||
| 463 | * @return the last Component in the traversal cycle of aContainer, | ||
| 464 | * or null if no suitable Component can be found | ||
| 465 | * @throws IllegalArgumentException if aContainer is null | ||
| 466 | */ | ||
| 467 | public Component getLastComponent(Container aContainer) { | ||
| 468 | List<Component> cycle; | ||
| 469 | if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Getting last component in " + aContainer); | ||
| 470 | |||
| 471 | if (aContainer == null) { | ||
| 472 | throw new IllegalArgumentException("aContainer cannot be null"); | ||
| 473 | } | ||
| 474 | |||
| 475 | if (this.cachedRoot == aContainer) { | ||
| 476 | cycle = this.cachedCycle; | ||
| 477 | } else { | ||
| 478 | cycle = getFocusTraversalCycle(aContainer); | ||
| 479 | } | ||
| 480 | |||
| 481 | if (cycle.size() == 0) { | ||
| 482 | if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Cycle is empty"); | ||
| 483 | return null; | ||
| 484 | } | ||
| 485 | if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Cycle is " + cycle); | ||
| 486 | |||
| 487 | for (int i= cycle.size() - 1; i >= 0; i--) { | ||
| 488 | Component comp = cycle.get(i); | ||
| 489 | if (accept(comp)) { | ||
| 490 | return comp; | ||
| 491 | } else if (comp instanceof Container && comp != aContainer) { | ||
| 492 | Container cont = (Container)comp; | ||
| 493 | if (cont.isFocusTraversalPolicyProvider()) { | ||
| 494 | return cont.getFocusTraversalPolicy().getLastComponent(cont); | ||
| 495 | } | ||
| 496 | } | ||
| 497 | } | ||
| 498 | return null; | ||
| 499 | } | ||
| 500 | |||
| 501 | /** | ||
| 502 | * Returns the default Component to focus. This Component will be the first | ||
| 503 | * to receive focus when traversing down into a new focus traversal cycle | ||
| 504 | * rooted at aContainer. The default implementation of this method | ||
| 505 | * returns the same Component as <code>getFirstComponent</code>. | ||
| 506 | * | ||
| 507 | * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider whose | ||
| 508 | * default Component is to be returned | ||
| 509 | * @return the default Component in the traversal cycle of aContainer, | ||
| 510 | * or null if no suitable Component can be found | ||
| 511 | * @see #getFirstComponent | ||
| 512 | * @throws IllegalArgumentException if aContainer is null | ||
| 513 | */ | ||
| 514 | public Component getDefaultComponent(Container aContainer) { | ||
| 515 | return getFirstComponent(aContainer); | ||
| 516 | } | ||
| 517 | |||
| 518 | /** | ||
| 519 | * Sets whether this SortingFocusTraversalPolicy transfers focus down-cycle | ||
| 520 | * implicitly. If <code>true</code>, during normal focus traversal, | ||
| 521 | * the Component traversed after a focus cycle root will be the focus- | ||
| 522 | * cycle-root's default Component to focus. If <code>false</code>, the | ||
| 523 | * next Component in the focus traversal cycle rooted at the specified | ||
| 524 | * focus cycle root will be traversed instead. The default value for this | ||
| 525 | * property is <code>true</code>. | ||
| 526 | * | ||
| 527 | * @param implicitDownCycleTraversal whether this | ||
| 528 | * SortingFocusTraversalPolicy transfers focus down-cycle implicitly | ||
| 529 | * @see #getImplicitDownCycleTraversal | ||
| 530 | * @see #getFirstComponent | ||
| 531 | */ | ||
| 532 | public void setImplicitDownCycleTraversal(boolean implicitDownCycleTraversal) { | ||
| 533 | this.implicitDownCycleTraversal = implicitDownCycleTraversal; | ||
| 534 | } | ||
| 535 | |||
| 536 | /** | ||
| 537 | * Returns whether this SortingFocusTraversalPolicy transfers focus down- | ||
| 538 | * cycle implicitly. If <code>true</code>, during normal focus | ||
| 539 | * traversal, the Component traversed after a focus cycle root will be the | ||
| 540 | * focus-cycle-root's default Component to focus. If <code>false</code>, | ||
| 541 | * the next Component in the focus traversal cycle rooted at the specified | ||
| 542 | * focus cycle root will be traversed instead. | ||
| 543 | * | ||
| 544 | * @return whether this SortingFocusTraversalPolicy transfers focus down- | ||
| 545 | * cycle implicitly | ||
| 546 | * @see #setImplicitDownCycleTraversal | ||
| 547 | * @see #getFirstComponent | ||
| 548 | */ | ||
| 549 | public boolean getImplicitDownCycleTraversal() { | ||
| 550 | return implicitDownCycleTraversal; | ||
| 551 | } | ||
| 552 | |||
| 553 | /** | ||
| 554 | * Sets the Comparator which will be used to sort the Components in a | ||
| 555 | * focus traversal cycle. | ||
| 556 | * | ||
| 557 | * @param comparator the Comparator which will be used for sorting | ||
| 558 | */ | ||
| 559 | protected void setComparator(Comparator<? super Component> comparator) { | ||
| 560 | this.comparator = comparator; | ||
| 561 | } | ||
| 562 | |||
| 563 | /** | ||
| 564 | * Returns the Comparator which will be used to sort the Components in a | ||
| 565 | * focus traversal cycle. | ||
| 566 | * | ||
| 567 | * @return the Comparator which will be used for sorting | ||
| 568 | */ | ||
| 569 | protected Comparator<? super Component> getComparator() { | ||
| 570 | return comparator; | ||
| 571 | } | ||
| 572 | |||
| 573 | /** | ||
| 574 | * Determines whether a Component is an acceptable choice as the new | ||
| 575 | * focus owner. By default, this method will accept a Component if and | ||
| 576 | * only if it is visible, displayable, enabled, and focusable. | ||
| 577 | * | ||
| 578 | * @param aComponent the Component whose fitness as a focus owner is to | ||
| 579 | * be tested | ||
| 580 | * @return <code>true</code> if aComponent is visible, displayable, | ||
| 581 | * enabled, and focusable; <code>false</code> otherwise | ||
| 582 | */ | ||
| 583 | protected boolean accept(Component aComponent) { | ||
| 584 | return fitnessTestPolicy.accept(aComponent); | ||
| 585 | } | ||
| 586 | } | ||
| 587 | |||
| 588 | // Create our own subclass and change accept to public so that we can call | ||
| 589 | // accept. | ||
| 590 | class SwingContainerOrderFocusTraversalPolicy | ||
| 591 | extends java.awt.ContainerOrderFocusTraversalPolicy | ||
| 592 | { | ||
| 593 | public boolean accept(Component aComponent) { | ||
| 594 | return super.accept(aComponent); | ||
| 595 | } | ||
| 596 | } | ||