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