src/share/classes/java/awt/ContainerOrderFocusTraversalPolicy.java

Print this page




 191      * <p>
 192      * By default, ContainerOrderFocusTraversalPolicy implicitly transfers
 193      * focus down-cycle. That is, during normal forward focus traversal, the
 194      * Component traversed after a focus cycle root will be the focus-cycle-
 195      * root's default Component to focus. This behavior can be disabled using
 196      * the <code>setImplicitDownCycleTraversal</code> method.
 197      * <p>
 198      * If aContainer is <a href="doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus
 199      * traversal policy provider</a>, the focus is always transferred down-cycle.
 200      *
 201      * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider
 202      * @param aComponent a (possibly indirect) child of aContainer, or
 203      *        aContainer itself
 204      * @return the Component that should receive the focus after aComponent, or
 205      *         null if no suitable Component can be found
 206      * @throws IllegalArgumentException if aContainer is not a focus cycle
 207      *         root of aComponent or focus traversal policy provider, or if either aContainer or
 208      *         aComponent is null
 209      */
 210     public Component getComponentAfter(Container aContainer, Component aComponent) {
 211         if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Searching in " + aContainer + " for component after " + aComponent);


 212 
 213         if (aContainer == null || aComponent == null) {
 214             throw new IllegalArgumentException("aContainer and aComponent cannot be null");
 215         }
 216         if (!aContainer.isFocusTraversalPolicyProvider() && !aContainer.isFocusCycleRoot()) {
 217             throw new IllegalArgumentException("aContainer should be focus cycle root or focus traversal policy provider");
 218 
 219         } else if (aContainer.isFocusCycleRoot() && !aComponent.isFocusCycleRoot(aContainer)) {
 220             throw new IllegalArgumentException("aContainer is not a focus cycle root of aComponent");
 221         }
 222 
 223         synchronized(aContainer.getTreeLock()) {
 224 
 225             if (!(aContainer.isVisible() && aContainer.isDisplayable())) {
 226                 return null;
 227             }
 228 
 229             // Before all the ckecks below we first see if it's an FTP provider or a focus cycle root.
 230             // If it's the case just go down cycle (if it's set to "implicit").
 231             Component comp = getComponentDownCycle(aComponent, FORWARD_TRAVERSAL);
 232             if (comp != null) {
 233                 return comp;
 234             }
 235 
 236             // See if the component is inside of policy provider.
 237             Container provider = getTopmostProvider(aContainer, aComponent);
 238             if (provider != null) {
 239                 if (log.isLoggable(PlatformLogger.FINE)) {
 240                     log.fine("### Asking FTP " + provider + " for component after " + aComponent);
 241                 }
 242 
 243                 // FTP knows how to find component after the given. We don't.
 244                 FocusTraversalPolicy policy = provider.getFocusTraversalPolicy();
 245                 Component afterComp = policy.getComponentAfter(provider, aComponent);
 246 
 247                 // Null result means that we overstepped the limit of the FTP's cycle.
 248                 // In that case we must quit the cycle, otherwise return the component found.
 249                 if (afterComp != null) {
 250                     if (log.isLoggable(PlatformLogger.FINE)) log.fine("### FTP returned " + afterComp);


 251                     return afterComp;
 252                 }
 253                 aComponent = provider;
 254             }
 255 
 256             List<Component> cycle = getFocusTraversalCycle(aContainer);
 257 
 258             if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Cycle is " + cycle + ", component is " + aComponent);


 259 
 260             int index = getComponentIndex(cycle, aComponent);
 261 
 262             if (index < 0) {
 263                 if (log.isLoggable(PlatformLogger.FINE)) {
 264                     log.fine("### Didn't find component " + aComponent + " in a cycle " + aContainer);
 265                 }
 266                 return getFirstComponent(aContainer);
 267             }
 268 
 269             for (index++; index < cycle.size(); index++) {
 270                 comp = cycle.get(index);
 271                 if (accept(comp)) {
 272                     return comp;
 273                 } else if ((comp = getComponentDownCycle(comp, FORWARD_TRAVERSAL)) != null) {
 274                     return comp;
 275                 }
 276             }
 277 
 278             if (aContainer.isFocusCycleRoot()) {


 319         synchronized(aContainer.getTreeLock()) {
 320 
 321             if (!(aContainer.isVisible() && aContainer.isDisplayable())) {
 322                 return null;
 323             }
 324 
 325             // See if the component is inside of policy provider.
 326             Container provider = getTopmostProvider(aContainer, aComponent);
 327             if (provider != null) {
 328                 if (log.isLoggable(PlatformLogger.FINE)) {
 329                     log.fine("### Asking FTP " + provider + " for component after " + aComponent);
 330                 }
 331 
 332                 // FTP knows how to find component after the given. We don't.
 333                 FocusTraversalPolicy policy = provider.getFocusTraversalPolicy();
 334                 Component beforeComp = policy.getComponentBefore(provider, aComponent);
 335 
 336                 // Null result means that we overstepped the limit of the FTP's cycle.
 337                 // In that case we must quit the cycle, otherwise return the component found.
 338                 if (beforeComp != null) {
 339                     if (log.isLoggable(PlatformLogger.FINE)) log.fine("### FTP returned " + beforeComp);


 340                     return beforeComp;
 341                 }
 342                 aComponent = provider;
 343 
 344                 // If the provider is traversable it's returned.
 345                 if (accept(aComponent)) {
 346                     return aComponent;
 347                 }
 348             }
 349 
 350             List<Component> cycle = getFocusTraversalCycle(aContainer);
 351 
 352             if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Cycle is " + cycle + ", component is " + aComponent);


 353 
 354             int index = getComponentIndex(cycle, aComponent);
 355 
 356             if (index < 0) {
 357                 if (log.isLoggable(PlatformLogger.FINE)) {
 358                     log.fine("### Didn't find component " + aComponent + " in a cycle " + aContainer);
 359                 }
 360                 return getLastComponent(aContainer);
 361             }
 362 
 363             Component comp = null;
 364             Component tryComp = null;
 365 
 366             for (index--; index>=0; index--) {
 367                 comp = cycle.get(index);
 368                 if (comp != aContainer && (tryComp = getComponentDownCycle(comp, BACKWARD_TRAVERSAL)) != null) {
 369                     return tryComp;
 370                 } else if (accept(comp)) {
 371                     return comp;
 372                 }


 384                 return comp;
 385             }
 386         }
 387         return null;
 388     }
 389 
 390     /**
 391      * Returns the first Component in the traversal cycle. This method is used
 392      * to determine the next Component to focus when traversal wraps in the
 393      * forward direction.
 394      *
 395      * @param aContainer the focus cycle root or focus traversal policy provider whose first
 396      *        Component is to be returned
 397      * @return the first Component in the traversal cycle of aContainer,
 398      *         or null if no suitable Component can be found
 399      * @throws IllegalArgumentException if aContainer is null
 400      */
 401     public Component getFirstComponent(Container aContainer) {
 402         List<Component> cycle;
 403 
 404         if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Getting first component in " + aContainer);


 405         if (aContainer == null) {
 406             throw new IllegalArgumentException("aContainer cannot be null");
 407 
 408         }
 409 
 410         synchronized(aContainer.getTreeLock()) {
 411 
 412             if (!(aContainer.isVisible() && aContainer.isDisplayable())) {
 413                 return null;
 414             }
 415 
 416             if (this.cachedRoot == aContainer) {
 417                 cycle = this.cachedCycle;
 418             } else {
 419                 cycle = getFocusTraversalCycle(aContainer);
 420             }
 421 
 422             if (cycle.size() == 0) {
 423                 if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Cycle is empty");


 424                 return null;
 425             }
 426             if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Cycle is " + cycle);


 427 
 428             for (Component comp : cycle) {
 429                 if (accept(comp)) {
 430                     return comp;
 431                 } else if (comp != aContainer &&
 432                            (comp = getComponentDownCycle(comp, FORWARD_TRAVERSAL)) != null)
 433                 {
 434                     return comp;
 435                 }
 436             }
 437         }
 438         return null;
 439     }
 440 
 441     /**
 442      * Returns the last Component in the traversal cycle. This method is used
 443      * to determine the next Component to focus when traversal wraps in the
 444      * reverse direction.
 445      *
 446      * @param aContainer the focus cycle root or focus traversal policy provider whose last
 447      *        Component is to be returned
 448      * @return the last Component in the traversal cycle of aContainer,
 449      *         or null if no suitable Component can be found
 450      * @throws IllegalArgumentException if aContainer is null
 451      */
 452     public Component getLastComponent(Container aContainer) {
 453         List<Component> cycle;
 454         if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Getting last component in " + aContainer);


 455 
 456         if (aContainer == null) {
 457             throw new IllegalArgumentException("aContainer cannot be null");
 458         }
 459 
 460         synchronized(aContainer.getTreeLock()) {
 461 
 462             if (!(aContainer.isVisible() && aContainer.isDisplayable())) {
 463                 return null;
 464             }
 465 
 466             if (this.cachedRoot == aContainer) {
 467                 cycle = this.cachedCycle;
 468             } else {
 469                 cycle = getFocusTraversalCycle(aContainer);
 470             }
 471 
 472             if (cycle.size() == 0) {
 473                 if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Cycle is empty");


 474                 return null;
 475             }
 476             if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Cycle is " + cycle);


 477 
 478             for (int i= cycle.size() - 1; i >= 0; i--) {
 479                 Component comp = cycle.get(i);
 480                 if (accept(comp)) {
 481                     return comp;
 482                 } else if (comp instanceof Container && comp != aContainer) {
 483                     Container cont = (Container)comp;
 484                     if (cont.isFocusTraversalPolicyProvider()) {
 485                         return cont.getFocusTraversalPolicy().getLastComponent(cont);
 486                     }
 487                 }
 488             }
 489         }
 490         return null;
 491     }
 492 
 493     /**
 494      * Returns the default Component to focus. This Component will be the first
 495      * to receive focus when traversing down into a new focus traversal cycle
 496      * rooted at aContainer. The default implementation of this method




 191      * <p>
 192      * By default, ContainerOrderFocusTraversalPolicy implicitly transfers
 193      * focus down-cycle. That is, during normal forward focus traversal, the
 194      * Component traversed after a focus cycle root will be the focus-cycle-
 195      * root's default Component to focus. This behavior can be disabled using
 196      * the <code>setImplicitDownCycleTraversal</code> method.
 197      * <p>
 198      * If aContainer is <a href="doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus
 199      * traversal policy provider</a>, the focus is always transferred down-cycle.
 200      *
 201      * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider
 202      * @param aComponent a (possibly indirect) child of aContainer, or
 203      *        aContainer itself
 204      * @return the Component that should receive the focus after aComponent, or
 205      *         null if no suitable Component can be found
 206      * @throws IllegalArgumentException if aContainer is not a focus cycle
 207      *         root of aComponent or focus traversal policy provider, or if either aContainer or
 208      *         aComponent is null
 209      */
 210     public Component getComponentAfter(Container aContainer, Component aComponent) {
 211         if (log.isLoggable(PlatformLogger.FINE)) {
 212             log.fine("### Searching in " + aContainer + " for component after " + aComponent);
 213         }
 214 
 215         if (aContainer == null || aComponent == null) {
 216             throw new IllegalArgumentException("aContainer and aComponent cannot be null");
 217         }
 218         if (!aContainer.isFocusTraversalPolicyProvider() && !aContainer.isFocusCycleRoot()) {
 219             throw new IllegalArgumentException("aContainer should be focus cycle root or focus traversal policy provider");
 220 
 221         } else if (aContainer.isFocusCycleRoot() && !aComponent.isFocusCycleRoot(aContainer)) {
 222             throw new IllegalArgumentException("aContainer is not a focus cycle root of aComponent");
 223         }
 224 
 225         synchronized(aContainer.getTreeLock()) {
 226 
 227             if (!(aContainer.isVisible() && aContainer.isDisplayable())) {
 228                 return null;
 229             }
 230 
 231             // Before all the ckecks below we first see if it's an FTP provider or a focus cycle root.
 232             // If it's the case just go down cycle (if it's set to "implicit").
 233             Component comp = getComponentDownCycle(aComponent, FORWARD_TRAVERSAL);
 234             if (comp != null) {
 235                 return comp;
 236             }
 237 
 238             // See if the component is inside of policy provider.
 239             Container provider = getTopmostProvider(aContainer, aComponent);
 240             if (provider != null) {
 241                 if (log.isLoggable(PlatformLogger.FINE)) {
 242                     log.fine("### Asking FTP " + provider + " for component after " + aComponent);
 243                 }
 244 
 245                 // FTP knows how to find component after the given. We don't.
 246                 FocusTraversalPolicy policy = provider.getFocusTraversalPolicy();
 247                 Component afterComp = policy.getComponentAfter(provider, aComponent);
 248 
 249                 // Null result means that we overstepped the limit of the FTP's cycle.
 250                 // In that case we must quit the cycle, otherwise return the component found.
 251                 if (afterComp != null) {
 252                     if (log.isLoggable(PlatformLogger.FINE)) {
 253                         log.fine("### FTP returned " + afterComp);
 254                     }
 255                     return afterComp;
 256                 }
 257                 aComponent = provider;
 258             }
 259 
 260             List<Component> cycle = getFocusTraversalCycle(aContainer);
 261 
 262             if (log.isLoggable(PlatformLogger.FINE)) {
 263                 log.fine("### Cycle is " + cycle + ", component is " + aComponent);
 264             }
 265 
 266             int index = getComponentIndex(cycle, aComponent);
 267 
 268             if (index < 0) {
 269                 if (log.isLoggable(PlatformLogger.FINE)) {
 270                     log.fine("### Didn't find component " + aComponent + " in a cycle " + aContainer);
 271                 }
 272                 return getFirstComponent(aContainer);
 273             }
 274 
 275             for (index++; index < cycle.size(); index++) {
 276                 comp = cycle.get(index);
 277                 if (accept(comp)) {
 278                     return comp;
 279                 } else if ((comp = getComponentDownCycle(comp, FORWARD_TRAVERSAL)) != null) {
 280                     return comp;
 281                 }
 282             }
 283 
 284             if (aContainer.isFocusCycleRoot()) {


 325         synchronized(aContainer.getTreeLock()) {
 326 
 327             if (!(aContainer.isVisible() && aContainer.isDisplayable())) {
 328                 return null;
 329             }
 330 
 331             // See if the component is inside of policy provider.
 332             Container provider = getTopmostProvider(aContainer, aComponent);
 333             if (provider != null) {
 334                 if (log.isLoggable(PlatformLogger.FINE)) {
 335                     log.fine("### Asking FTP " + provider + " for component after " + aComponent);
 336                 }
 337 
 338                 // FTP knows how to find component after the given. We don't.
 339                 FocusTraversalPolicy policy = provider.getFocusTraversalPolicy();
 340                 Component beforeComp = policy.getComponentBefore(provider, aComponent);
 341 
 342                 // Null result means that we overstepped the limit of the FTP's cycle.
 343                 // In that case we must quit the cycle, otherwise return the component found.
 344                 if (beforeComp != null) {
 345                     if (log.isLoggable(PlatformLogger.FINE)) {
 346                         log.fine("### FTP returned " + beforeComp);
 347                     }
 348                     return beforeComp;
 349                 }
 350                 aComponent = provider;
 351 
 352                 // If the provider is traversable it's returned.
 353                 if (accept(aComponent)) {
 354                     return aComponent;
 355                 }
 356             }
 357 
 358             List<Component> cycle = getFocusTraversalCycle(aContainer);
 359 
 360             if (log.isLoggable(PlatformLogger.FINE)) {
 361                 log.fine("### Cycle is " + cycle + ", component is " + aComponent);
 362             }
 363 
 364             int index = getComponentIndex(cycle, aComponent);
 365 
 366             if (index < 0) {
 367                 if (log.isLoggable(PlatformLogger.FINE)) {
 368                     log.fine("### Didn't find component " + aComponent + " in a cycle " + aContainer);
 369                 }
 370                 return getLastComponent(aContainer);
 371             }
 372 
 373             Component comp = null;
 374             Component tryComp = null;
 375 
 376             for (index--; index>=0; index--) {
 377                 comp = cycle.get(index);
 378                 if (comp != aContainer && (tryComp = getComponentDownCycle(comp, BACKWARD_TRAVERSAL)) != null) {
 379                     return tryComp;
 380                 } else if (accept(comp)) {
 381                     return comp;
 382                 }


 394                 return comp;
 395             }
 396         }
 397         return null;
 398     }
 399 
 400     /**
 401      * Returns the first Component in the traversal cycle. This method is used
 402      * to determine the next Component to focus when traversal wraps in the
 403      * forward direction.
 404      *
 405      * @param aContainer the focus cycle root or focus traversal policy provider whose first
 406      *        Component is to be returned
 407      * @return the first Component in the traversal cycle of aContainer,
 408      *         or null if no suitable Component can be found
 409      * @throws IllegalArgumentException if aContainer is null
 410      */
 411     public Component getFirstComponent(Container aContainer) {
 412         List<Component> cycle;
 413 
 414         if (log.isLoggable(PlatformLogger.FINE)) {
 415             log.fine("### Getting first component in " + aContainer);
 416         }
 417         if (aContainer == null) {
 418             throw new IllegalArgumentException("aContainer cannot be null");
 419 
 420         }
 421 
 422         synchronized(aContainer.getTreeLock()) {
 423 
 424             if (!(aContainer.isVisible() && aContainer.isDisplayable())) {
 425                 return null;
 426             }
 427 
 428             if (this.cachedRoot == aContainer) {
 429                 cycle = this.cachedCycle;
 430             } else {
 431                 cycle = getFocusTraversalCycle(aContainer);
 432             }
 433 
 434             if (cycle.size() == 0) {
 435                 if (log.isLoggable(PlatformLogger.FINE)) {
 436                     log.fine("### Cycle is empty");
 437                 }
 438                 return null;
 439             }
 440             if (log.isLoggable(PlatformLogger.FINE)) {
 441                 log.fine("### Cycle is " + cycle);
 442             }
 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         }
 454         return null;
 455     }
 456 
 457     /**
 458      * Returns the last Component in the traversal cycle. This method is used
 459      * to determine the next Component to focus when traversal wraps in the
 460      * reverse direction.
 461      *
 462      * @param aContainer the focus cycle root or focus traversal policy provider whose last
 463      *        Component is to be returned
 464      * @return the last Component in the traversal cycle of aContainer,
 465      *         or null if no suitable Component can be found
 466      * @throws IllegalArgumentException if aContainer is null
 467      */
 468     public Component getLastComponent(Container aContainer) {
 469         List<Component> cycle;
 470         if (log.isLoggable(PlatformLogger.FINE)) {
 471             log.fine("### Getting last component in " + aContainer);
 472         }
 473 
 474         if (aContainer == null) {
 475             throw new IllegalArgumentException("aContainer cannot be null");
 476         }
 477 
 478         synchronized(aContainer.getTreeLock()) {
 479 
 480             if (!(aContainer.isVisible() && aContainer.isDisplayable())) {
 481                 return null;
 482             }
 483 
 484             if (this.cachedRoot == aContainer) {
 485                 cycle = this.cachedCycle;
 486             } else {
 487                 cycle = getFocusTraversalCycle(aContainer);
 488             }
 489 
 490             if (cycle.size() == 0) {
 491                 if (log.isLoggable(PlatformLogger.FINE)) {
 492                     log.fine("### Cycle is empty");
 493                 }
 494                 return null;
 495             }
 496             if (log.isLoggable(PlatformLogger.FINE)) {
 497                 log.fine("### Cycle is " + cycle);
 498             }
 499 
 500             for (int i= cycle.size() - 1; i >= 0; i--) {
 501                 Component comp = cycle.get(i);
 502                 if (accept(comp)) {
 503                     return comp;
 504                 } else if (comp instanceof Container && comp != aContainer) {
 505                     Container cont = (Container)comp;
 506                     if (cont.isFocusTraversalPolicyProvider()) {
 507                         return cont.getFocusTraversalPolicy().getLastComponent(cont);
 508                     }
 509                 }
 510             }
 511         }
 512         return null;
 513     }
 514 
 515     /**
 516      * Returns the default Component to focus. This Component will be the first
 517      * to receive focus when traversing down into a new focus traversal cycle
 518      * rooted at aContainer. The default implementation of this method