22 * CA 95054 USA or visit www.sun.com if you need additional information or |
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
23 * have any questions. |
23 * have any questions. |
24 */ |
24 */ |
25 package java.awt; |
25 package java.awt; |
26 |
26 |
27 import java.util.logging.*; |
|
28 import java.util.List; |
27 import java.util.List; |
29 import java.util.ArrayList; |
28 import java.util.ArrayList; |
|
29 import sun.util.logging.PlatformLogger; |
30 |
30 |
31 /** |
31 /** |
32 * A FocusTraversalPolicy that determines traversal order based on the order |
32 * A FocusTraversalPolicy that determines traversal order based on the order |
33 * of child Components in a Container. From a particular focus cycle root, the |
33 * of child Components in a Container. From a particular focus cycle root, the |
34 * policy makes a pre-order traversal of the Component hierarchy, and traverses |
34 * policy makes a pre-order traversal of the Component hierarchy, and traverses |
58 * @since 1.4 |
58 * @since 1.4 |
59 */ |
59 */ |
60 public class ContainerOrderFocusTraversalPolicy extends FocusTraversalPolicy |
60 public class ContainerOrderFocusTraversalPolicy extends FocusTraversalPolicy |
61 implements java.io.Serializable |
61 implements java.io.Serializable |
62 { |
62 { |
63 private static final Logger log = Logger.getLogger("java.awt.ContainerOrderFocusTraversalPolicy"); |
63 private static final PlatformLogger log = PlatformLogger.getLogger("java.awt.ContainerOrderFocusTraversalPolicy"); |
64 |
64 |
65 final private int FORWARD_TRAVERSAL = 0; |
65 final private int FORWARD_TRAVERSAL = 0; |
66 final private int BACKWARD_TRAVERSAL = 1; |
66 final private int BACKWARD_TRAVERSAL = 1; |
67 |
67 |
68 /* |
68 /* |
163 |
163 |
164 if (cont.isFocusCycleRoot()) { |
164 if (cont.isFocusCycleRoot()) { |
165 if (getImplicitDownCycleTraversal()) { |
165 if (getImplicitDownCycleTraversal()) { |
166 retComp = cont.getFocusTraversalPolicy().getDefaultComponent(cont); |
166 retComp = cont.getFocusTraversalPolicy().getDefaultComponent(cont); |
167 |
167 |
168 if (retComp != null && log.isLoggable(Level.FINE)) { |
168 if (retComp != null && log.isLoggable(PlatformLogger.FINE)) { |
169 log.fine("### Transfered focus down-cycle to " + retComp + |
169 log.fine("### Transfered focus down-cycle to " + retComp + |
170 " in the focus cycle root " + cont); |
170 " in the focus cycle root " + cont); |
171 } |
171 } |
172 } else { |
172 } else { |
173 return null; |
173 return null; |
175 } else if (cont.isFocusTraversalPolicyProvider()) { |
175 } else if (cont.isFocusTraversalPolicyProvider()) { |
176 retComp = (traversalDirection == FORWARD_TRAVERSAL ? |
176 retComp = (traversalDirection == FORWARD_TRAVERSAL ? |
177 cont.getFocusTraversalPolicy().getDefaultComponent(cont) : |
177 cont.getFocusTraversalPolicy().getDefaultComponent(cont) : |
178 cont.getFocusTraversalPolicy().getLastComponent(cont)); |
178 cont.getFocusTraversalPolicy().getLastComponent(cont)); |
179 |
179 |
180 if (retComp != null && log.isLoggable(Level.FINE)) { |
180 if (retComp != null && log.isLoggable(PlatformLogger.FINE)) { |
181 log.fine("### Transfered focus to " + retComp + " in the FTP provider " + cont); |
181 log.fine("### Transfered focus to " + retComp + " in the FTP provider " + cont); |
182 } |
182 } |
183 } |
183 } |
184 } |
184 } |
185 return retComp; |
185 return retComp; |
206 * @throws IllegalArgumentException if aContainer is not a focus cycle |
206 * @throws IllegalArgumentException if aContainer is not a focus cycle |
207 * root of aComponent or focus traversal policy provider, or if either aContainer or |
207 * root of aComponent or focus traversal policy provider, or if either aContainer or |
208 * aComponent is null |
208 * aComponent is null |
209 */ |
209 */ |
210 public Component getComponentAfter(Container aContainer, Component aComponent) { |
210 public Component getComponentAfter(Container aContainer, Component aComponent) { |
211 if (log.isLoggable(Level.FINE)) log.fine("### Searching in " + aContainer + " for component after " + aComponent); |
211 if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Searching in " + aContainer + " for component after " + aComponent); |
212 |
212 |
213 if (aContainer == null || aComponent == null) { |
213 if (aContainer == null || aComponent == null) { |
214 throw new IllegalArgumentException("aContainer and aComponent cannot be null"); |
214 throw new IllegalArgumentException("aContainer and aComponent cannot be null"); |
215 } |
215 } |
216 if (!aContainer.isFocusTraversalPolicyProvider() && !aContainer.isFocusCycleRoot()) { |
216 if (!aContainer.isFocusTraversalPolicyProvider() && !aContainer.isFocusCycleRoot()) { |
234 } |
234 } |
235 |
235 |
236 // See if the component is inside of policy provider. |
236 // See if the component is inside of policy provider. |
237 Container provider = getTopmostProvider(aContainer, aComponent); |
237 Container provider = getTopmostProvider(aContainer, aComponent); |
238 if (provider != null) { |
238 if (provider != null) { |
239 if (log.isLoggable(Level.FINE)) { |
239 if (log.isLoggable(PlatformLogger.FINE)) { |
240 log.fine("### Asking FTP " + provider + " for component after " + aComponent); |
240 log.fine("### Asking FTP " + provider + " for component after " + aComponent); |
241 } |
241 } |
242 |
242 |
243 // FTP knows how to find component after the given. We don't. |
243 // FTP knows how to find component after the given. We don't. |
244 FocusTraversalPolicy policy = provider.getFocusTraversalPolicy(); |
244 FocusTraversalPolicy policy = provider.getFocusTraversalPolicy(); |
245 Component afterComp = policy.getComponentAfter(provider, aComponent); |
245 Component afterComp = policy.getComponentAfter(provider, aComponent); |
246 |
246 |
247 // Null result means that we overstepped the limit of the FTP's cycle. |
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. |
248 // In that case we must quit the cycle, otherwise return the component found. |
249 if (afterComp != null) { |
249 if (afterComp != null) { |
250 if (log.isLoggable(Level.FINE)) log.fine("### FTP returned " + afterComp); |
250 if (log.isLoggable(PlatformLogger.FINE)) log.fine("### FTP returned " + afterComp); |
251 return afterComp; |
251 return afterComp; |
252 } |
252 } |
253 aComponent = provider; |
253 aComponent = provider; |
254 } |
254 } |
255 |
255 |
256 List<Component> cycle = getFocusTraversalCycle(aContainer); |
256 List<Component> cycle = getFocusTraversalCycle(aContainer); |
257 |
257 |
258 if (log.isLoggable(Level.FINE)) log.fine("### Cycle is " + cycle + ", component is " + aComponent); |
258 if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Cycle is " + cycle + ", component is " + aComponent); |
259 |
259 |
260 int index = getComponentIndex(cycle, aComponent); |
260 int index = getComponentIndex(cycle, aComponent); |
261 |
261 |
262 if (index < 0) { |
262 if (index < 0) { |
263 if (log.isLoggable(Level.FINE)) { |
263 if (log.isLoggable(PlatformLogger.FINE)) { |
264 log.fine("### Didn't find component " + aComponent + " in a cycle " + aContainer); |
264 log.fine("### Didn't find component " + aComponent + " in a cycle " + aContainer); |
265 } |
265 } |
266 return getFirstComponent(aContainer); |
266 return getFirstComponent(aContainer); |
267 } |
267 } |
268 |
268 |
323 } |
323 } |
324 |
324 |
325 // See if the component is inside of policy provider. |
325 // See if the component is inside of policy provider. |
326 Container provider = getTopmostProvider(aContainer, aComponent); |
326 Container provider = getTopmostProvider(aContainer, aComponent); |
327 if (provider != null) { |
327 if (provider != null) { |
328 if (log.isLoggable(Level.FINE)) { |
328 if (log.isLoggable(PlatformLogger.FINE)) { |
329 log.fine("### Asking FTP " + provider + " for component after " + aComponent); |
329 log.fine("### Asking FTP " + provider + " for component after " + aComponent); |
330 } |
330 } |
331 |
331 |
332 // FTP knows how to find component after the given. We don't. |
332 // FTP knows how to find component after the given. We don't. |
333 FocusTraversalPolicy policy = provider.getFocusTraversalPolicy(); |
333 FocusTraversalPolicy policy = provider.getFocusTraversalPolicy(); |
334 Component beforeComp = policy.getComponentBefore(provider, aComponent); |
334 Component beforeComp = policy.getComponentBefore(provider, aComponent); |
335 |
335 |
336 // Null result means that we overstepped the limit of the FTP's cycle. |
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. |
337 // In that case we must quit the cycle, otherwise return the component found. |
338 if (beforeComp != null) { |
338 if (beforeComp != null) { |
339 if (log.isLoggable(Level.FINE)) log.fine("### FTP returned " + beforeComp); |
339 if (log.isLoggable(PlatformLogger.FINE)) log.fine("### FTP returned " + beforeComp); |
340 return beforeComp; |
340 return beforeComp; |
341 } |
341 } |
342 aComponent = provider; |
342 aComponent = provider; |
343 |
343 |
344 // If the provider is traversable it's returned. |
344 // If the provider is traversable it's returned. |
347 } |
347 } |
348 } |
348 } |
349 |
349 |
350 List<Component> cycle = getFocusTraversalCycle(aContainer); |
350 List<Component> cycle = getFocusTraversalCycle(aContainer); |
351 |
351 |
352 if (log.isLoggable(Level.FINE)) log.fine("### Cycle is " + cycle + ", component is " + aComponent); |
352 if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Cycle is " + cycle + ", component is " + aComponent); |
353 |
353 |
354 int index = getComponentIndex(cycle, aComponent); |
354 int index = getComponentIndex(cycle, aComponent); |
355 |
355 |
356 if (index < 0) { |
356 if (index < 0) { |
357 if (log.isLoggable(Level.FINE)) { |
357 if (log.isLoggable(PlatformLogger.FINE)) { |
358 log.fine("### Didn't find component " + aComponent + " in a cycle " + aContainer); |
358 log.fine("### Didn't find component " + aComponent + " in a cycle " + aContainer); |
359 } |
359 } |
360 return getLastComponent(aContainer); |
360 return getLastComponent(aContainer); |
361 } |
361 } |
362 |
362 |
399 * @throws IllegalArgumentException if aContainer is null |
399 * @throws IllegalArgumentException if aContainer is null |
400 */ |
400 */ |
401 public Component getFirstComponent(Container aContainer) { |
401 public Component getFirstComponent(Container aContainer) { |
402 List<Component> cycle; |
402 List<Component> cycle; |
403 |
403 |
404 if (log.isLoggable(Level.FINE)) log.fine("### Getting first component in " + aContainer); |
404 if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Getting first component in " + aContainer); |
405 if (aContainer == null) { |
405 if (aContainer == null) { |
406 throw new IllegalArgumentException("aContainer cannot be null"); |
406 throw new IllegalArgumentException("aContainer cannot be null"); |
407 |
407 |
408 } |
408 } |
409 |
409 |
418 } else { |
418 } else { |
419 cycle = getFocusTraversalCycle(aContainer); |
419 cycle = getFocusTraversalCycle(aContainer); |
420 } |
420 } |
421 |
421 |
422 if (cycle.size() == 0) { |
422 if (cycle.size() == 0) { |
423 if (log.isLoggable(Level.FINE)) log.fine("### Cycle is empty"); |
423 if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Cycle is empty"); |
424 return null; |
424 return null; |
425 } |
425 } |
426 if (log.isLoggable(Level.FINE)) log.fine("### Cycle is " + cycle); |
426 if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Cycle is " + cycle); |
427 |
427 |
428 for (Component comp : cycle) { |
428 for (Component comp : cycle) { |
429 if (accept(comp)) { |
429 if (accept(comp)) { |
430 return comp; |
430 return comp; |
431 } else if (comp != aContainer && |
431 } else if (comp != aContainer && |
449 * or null if no suitable Component can be found |
449 * or null if no suitable Component can be found |
450 * @throws IllegalArgumentException if aContainer is null |
450 * @throws IllegalArgumentException if aContainer is null |
451 */ |
451 */ |
452 public Component getLastComponent(Container aContainer) { |
452 public Component getLastComponent(Container aContainer) { |
453 List<Component> cycle; |
453 List<Component> cycle; |
454 if (log.isLoggable(Level.FINE)) log.fine("### Getting last component in " + aContainer); |
454 if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Getting last component in " + aContainer); |
455 |
455 |
456 if (aContainer == null) { |
456 if (aContainer == null) { |
457 throw new IllegalArgumentException("aContainer cannot be null"); |
457 throw new IllegalArgumentException("aContainer cannot be null"); |
458 } |
458 } |
459 |
459 |
468 } else { |
468 } else { |
469 cycle = getFocusTraversalCycle(aContainer); |
469 cycle = getFocusTraversalCycle(aContainer); |
470 } |
470 } |
471 |
471 |
472 if (cycle.size() == 0) { |
472 if (cycle.size() == 0) { |
473 if (log.isLoggable(Level.FINE)) log.fine("### Cycle is empty"); |
473 if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Cycle is empty"); |
474 return null; |
474 return null; |
475 } |
475 } |
476 if (log.isLoggable(Level.FINE)) log.fine("### Cycle is " + cycle); |
476 if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Cycle is " + cycle); |
477 |
477 |
478 for (int i= cycle.size() - 1; i >= 0; i--) { |
478 for (int i= cycle.size() - 1; i >= 0; i--) { |
479 Component comp = cycle.get(i); |
479 Component comp = cycle.get(i); |
480 if (accept(comp)) { |
480 if (accept(comp)) { |
481 return comp; |
481 return comp; |