148 return ftp; 149 } 150 151 /* 152 * Checks if a new focus cycle takes place and returns a Component to traverse focus to. 153 * @param comp a possible focus cycle root or policy provider 154 * @param traversalDirection the direction of the traversal 155 * @return a Component to traverse focus to if {@code comp} is a root or provider 156 * and implicit down-cycle is set, otherwise {@code null} 157 */ 158 private Component getComponentDownCycle(Component comp, int traversalDirection) { 159 Component retComp = null; 160 161 if (comp instanceof Container) { 162 Container cont = (Container)comp; 163 164 if (cont.isFocusCycleRoot()) { 165 if (getImplicitDownCycleTraversal()) { 166 retComp = cont.getFocusTraversalPolicy().getDefaultComponent(cont); 167 168 if (retComp != null && log.isLoggable(PlatformLogger.FINE)) { 169 log.fine("### Transfered focus down-cycle to " + retComp + 170 " in the focus cycle root " + cont); 171 } 172 } else { 173 return null; 174 } 175 } else if (cont.isFocusTraversalPolicyProvider()) { 176 retComp = (traversalDirection == FORWARD_TRAVERSAL ? 177 cont.getFocusTraversalPolicy().getDefaultComponent(cont) : 178 cont.getFocusTraversalPolicy().getLastComponent(cont)); 179 180 if (retComp != null && log.isLoggable(PlatformLogger.FINE)) { 181 log.fine("### Transfered focus to " + retComp + " in the FTP provider " + cont); 182 } 183 } 184 } 185 return retComp; 186 } 187 188 /** 189 * Returns the Component that should receive the focus after aComponent. 190 * aContainer must be a focus cycle root of aComponent or a focus traversal policy provider. 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()) { 285 this.cachedRoot = aContainer; 286 this.cachedCycle = cycle; 287 288 comp = getFirstComponent(aContainer); 289 314 public Component getComponentBefore(Container aContainer, Component aComponent) { 315 if (aContainer == null || aComponent == null) { 316 throw new IllegalArgumentException("aContainer and aComponent cannot be null"); 317 } 318 if (!aContainer.isFocusTraversalPolicyProvider() && !aContainer.isFocusCycleRoot()) { 319 throw new IllegalArgumentException("aContainer should be focus cycle root or focus traversal policy provider"); 320 321 } else if (aContainer.isFocusCycleRoot() && !aComponent.isFocusCycleRoot(aContainer)) { 322 throw new IllegalArgumentException("aContainer is not a focus cycle root of aComponent"); 323 } 324 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 } 383 } 384 385 if (aContainer.isFocusCycleRoot()) { 386 this.cachedRoot = aContainer; 387 this.cachedCycle = cycle; 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 | 148 return ftp; 149 } 150 151 /* 152 * Checks if a new focus cycle takes place and returns a Component to traverse focus to. 153 * @param comp a possible focus cycle root or policy provider 154 * @param traversalDirection the direction of the traversal 155 * @return a Component to traverse focus to if {@code comp} is a root or provider 156 * and implicit down-cycle is set, otherwise {@code null} 157 */ 158 private Component getComponentDownCycle(Component comp, int traversalDirection) { 159 Component retComp = null; 160 161 if (comp instanceof Container) { 162 Container cont = (Container)comp; 163 164 if (cont.isFocusCycleRoot()) { 165 if (getImplicitDownCycleTraversal()) { 166 retComp = cont.getFocusTraversalPolicy().getDefaultComponent(cont); 167 168 if (retComp != null && log.isLoggable(PlatformLogger.Level.FINE)) { 169 log.fine("### Transfered focus down-cycle to " + retComp + 170 " in the focus cycle root " + cont); 171 } 172 } else { 173 return null; 174 } 175 } else if (cont.isFocusTraversalPolicyProvider()) { 176 retComp = (traversalDirection == FORWARD_TRAVERSAL ? 177 cont.getFocusTraversalPolicy().getDefaultComponent(cont) : 178 cont.getFocusTraversalPolicy().getLastComponent(cont)); 179 180 if (retComp != null && log.isLoggable(PlatformLogger.Level.FINE)) { 181 log.fine("### Transfered focus to " + retComp + " in the FTP provider " + cont); 182 } 183 } 184 } 185 return retComp; 186 } 187 188 /** 189 * Returns the Component that should receive the focus after aComponent. 190 * aContainer must be a focus cycle root of aComponent or a focus traversal policy provider. 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.Level.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.Level.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.Level.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.Level.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.Level.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()) { 285 this.cachedRoot = aContainer; 286 this.cachedCycle = cycle; 287 288 comp = getFirstComponent(aContainer); 289 314 public Component getComponentBefore(Container aContainer, Component aComponent) { 315 if (aContainer == null || aComponent == null) { 316 throw new IllegalArgumentException("aContainer and aComponent cannot be null"); 317 } 318 if (!aContainer.isFocusTraversalPolicyProvider() && !aContainer.isFocusCycleRoot()) { 319 throw new IllegalArgumentException("aContainer should be focus cycle root or focus traversal policy provider"); 320 321 } else if (aContainer.isFocusCycleRoot() && !aComponent.isFocusCycleRoot(aContainer)) { 322 throw new IllegalArgumentException("aContainer is not a focus cycle root of aComponent"); 323 } 324 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.Level.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.Level.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.Level.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.Level.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 } 383 } 384 385 if (aContainer.isFocusCycleRoot()) { 386 this.cachedRoot = aContainer; 387 this.cachedCycle = cycle; 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.Level.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.Level.FINE)) { 436 log.fine("### Cycle is empty"); 437 } 438 return null; 439 } 440 if (log.isLoggable(PlatformLogger.Level.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.Level.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.Level.FINE)) { 492 log.fine("### Cycle is empty"); 493 } 494 return null; 495 } 496 if (log.isLoggable(PlatformLogger.Level.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 |