java.awt
public class: ContainerOrderFocusTraversalPolicy [javadoc |
source]
java.lang.Object
java.awt.FocusTraversalPolicy
java.awt.ContainerOrderFocusTraversalPolicy
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
SwingContainerOrderFocusTraversalPolicy, SwingDefaultFocusTraversalPolicy, DefaultFocusTraversalPolicy
A FocusTraversalPolicy that determines traversal order based on the order
of child Components in a Container. From a particular focus cycle root, the
policy makes a pre-order traversal of the Component hierarchy, and traverses
a Container's children according to the ordering of the array returned by
Container.getComponents(). Portions of the hierarchy that are
not visible and displayable will not be searched.
By default, ContainerOrderFocusTraversalPolicy implicitly transfers focus
down-cycle. That is, during normal forward focus traversal, the Component
traversed after a focus cycle root will be the focus-cycle-root's default
Component to focus. This behavior can be disabled using the
setImplicitDownCycleTraversal method.
By default, methods of this class with return a Component only if it is
visible, displayable, enabled, and focusable. Subclasses can modify this
behavior by overriding the accept method.
This policy takes into account focus traversal
policy providers. When searching for first/last/next/previous Component,
if a focus traversal policy provider is encountered, its focus traversal
policy is used to perform the search operation.
Also see:
- Container#getComponents
- author:
David - Mendenhall
- since:
1.4 -
| Methods from java.lang.Object: |
|---|
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method from java.awt.ContainerOrderFocusTraversalPolicy Detail: |
protected boolean accept(Component aComponent) {
if (!(aComponent.isVisible() && aComponent.isDisplayable() &&
aComponent.isFocusable() && aComponent.isEnabled())) {
return false;
}
// Verify that the Component is recursively enabled. Disabling a
// heavyweight Container disables its children, whereas disabling
// a lightweight Container does not.
if (!(aComponent instanceof Window)) {
for (Container enableTest = aComponent.getParent();
enableTest != null;
enableTest = enableTest.getParent())
{
if (!(enableTest.isEnabled() || enableTest.isLightweight())) {
return false;
}
if (enableTest instanceof Window) {
break;
}
}
}
return true;
}
Determines whether a Component is an acceptable choice as the new
focus owner. By default, this method will accept a Component if and
only if it is visible, displayable, enabled, and focusable. |
public Component getComponentAfter(Container aContainer,
Component aComponent) {
if (log.isLoggable(Level.FINE)) log.fine("### Searching in " + aContainer + " for component after " + aComponent);
if (aContainer == null || aComponent == null) {
throw new IllegalArgumentException("aContainer and aComponent cannot be null");
}
if (!aContainer.isFocusTraversalPolicyProvider() && !aContainer.isFocusCycleRoot()) {
throw new IllegalArgumentException("aContainer should be focus cycle root or focus traversal policy provider");
} else if (aContainer.isFocusCycleRoot() && !aComponent.isFocusCycleRoot(aContainer)) {
throw new IllegalArgumentException("aContainer is not a focus cycle root of aComponent");
}
synchronized(aContainer.getTreeLock()) {
if (!(aContainer.isVisible() && aContainer.isDisplayable())) {
return null;
}
// Before all the ckecks below we first see if it's an FTP provider or a focus cycle root.
// If it's the case just go down cycle (if it's set to "implicit").
Component comp = getComponentDownCycle(aComponent, FORWARD_TRAVERSAL);
if (comp != null) {
return comp;
}
// See if the component is inside of policy provider.
Container provider = getTopmostProvider(aContainer, aComponent);
if (provider != null) {
if (log.isLoggable(Level.FINE)) {
log.fine("### Asking FTP " + provider + " for component after " + aComponent);
}
// FTP knows how to find component after the given. We don't.
FocusTraversalPolicy policy = provider.getFocusTraversalPolicy();
Component afterComp = policy.getComponentAfter(provider, aComponent);
// Null result means that we overstepped the limit of the FTP's cycle.
// In that case we must quit the cycle, otherwise return the component found.
if (afterComp != null) {
if (log.isLoggable(Level.FINE)) log.fine("### FTP returned " + afterComp);
return afterComp;
}
aComponent = provider;
}
List< Component > cycle = getFocusTraversalCycle(aContainer);
if (log.isLoggable(Level.FINE)) log.fine("### Cycle is " + cycle + ", component is " + aComponent);
int index = getComponentIndex(cycle, aComponent);
if (index < 0) {
if (log.isLoggable(Level.FINE)) {
log.fine("### Didn't find component " + aComponent + " in a cycle " + aContainer);
}
return getFirstComponent(aContainer);
}
for (index++; index < cycle.size(); index++) {
comp = cycle.get(index);
if (accept(comp)) {
return comp;
} else if ((comp = getComponentDownCycle(comp, FORWARD_TRAVERSAL)) != null) {
return comp;
}
}
if (aContainer.isFocusCycleRoot()) {
this.cachedRoot = aContainer;
this.cachedCycle = cycle;
comp = getFirstComponent(aContainer);
this.cachedRoot = null;
this.cachedCycle = null;
return comp;
}
}
return null;
}
Returns the Component that should receive the focus after aComponent.
aContainer must be a focus cycle root of aComponent or a focus traversal policy provider.
By default, ContainerOrderFocusTraversalPolicy implicitly transfers
focus down-cycle. That is, during normal forward focus traversal, the
Component traversed after a focus cycle root will be the focus-cycle-
root's default Component to focus. This behavior can be disabled using
the setImplicitDownCycleTraversal method.
If aContainer is focus
traversal policy provider, the focus is always transferred down-cycle. |
public Component getComponentBefore(Container aContainer,
Component aComponent) {
if (aContainer == null || aComponent == null) {
throw new IllegalArgumentException("aContainer and aComponent cannot be null");
}
if (!aContainer.isFocusTraversalPolicyProvider() && !aContainer.isFocusCycleRoot()) {
throw new IllegalArgumentException("aContainer should be focus cycle root or focus traversal policy provider");
} else if (aContainer.isFocusCycleRoot() && !aComponent.isFocusCycleRoot(aContainer)) {
throw new IllegalArgumentException("aContainer is not a focus cycle root of aComponent");
}
synchronized(aContainer.getTreeLock()) {
if (!(aContainer.isVisible() && aContainer.isDisplayable())) {
return null;
}
// See if the component is inside of policy provider.
Container provider = getTopmostProvider(aContainer, aComponent);
if (provider != null) {
if (log.isLoggable(Level.FINE)) {
log.fine("### Asking FTP " + provider + " for component after " + aComponent);
}
// FTP knows how to find component after the given. We don't.
FocusTraversalPolicy policy = provider.getFocusTraversalPolicy();
Component beforeComp = policy.getComponentBefore(provider, aComponent);
// Null result means that we overstepped the limit of the FTP's cycle.
// In that case we must quit the cycle, otherwise return the component found.
if (beforeComp != null) {
if (log.isLoggable(Level.FINE)) log.fine("### FTP returned " + beforeComp);
return beforeComp;
}
aComponent = provider;
// If the provider is traversable it's returned.
if (accept(aComponent)) {
return aComponent;
}
}
List< Component > cycle = getFocusTraversalCycle(aContainer);
if (log.isLoggable(Level.FINE)) log.fine("### Cycle is " + cycle + ", component is " + aComponent);
int index = getComponentIndex(cycle, aComponent);
if (index < 0) {
if (log.isLoggable(Level.FINE)) {
log.fine("### Didn't find component " + aComponent + " in a cycle " + aContainer);
}
return getLastComponent(aContainer);
}
Component comp = null;
Component tryComp = null;
for (index--; index >=0; index--) {
comp = cycle.get(index);
if (comp != aContainer && (tryComp = getComponentDownCycle(comp, BACKWARD_TRAVERSAL)) != null) {
return tryComp;
} else if (accept(comp)) {
return comp;
}
}
if (aContainer.isFocusCycleRoot()) {
this.cachedRoot = aContainer;
this.cachedCycle = cycle;
comp = getLastComponent(aContainer);
this.cachedRoot = null;
this.cachedCycle = null;
return comp;
}
}
return null;
}
Returns the Component that should receive the focus before aComponent.
aContainer must be a focus cycle root of aComponent or a focus traversal policy
provider. |
public Component getDefaultComponent(Container aContainer) {
return getFirstComponent(aContainer);
}
Returns the default Component to focus. This Component will be the first
to receive focus when traversing down into a new focus traversal cycle
rooted at aContainer. The default implementation of this method
returns the same Component as getFirstComponent. |
public Component getFirstComponent(Container aContainer) {
List< Component > cycle;
if (log.isLoggable(Level.FINE)) log.fine("### Getting first component in " + aContainer);
if (aContainer == null) {
throw new IllegalArgumentException("aContainer cannot be null");
}
synchronized(aContainer.getTreeLock()) {
if (!(aContainer.isVisible() && aContainer.isDisplayable())) {
return null;
}
if (this.cachedRoot == aContainer) {
cycle = this.cachedCycle;
} else {
cycle = getFocusTraversalCycle(aContainer);
}
if (cycle.size() == 0) {
if (log.isLoggable(Level.FINE)) log.fine("### Cycle is empty");
return null;
}
if (log.isLoggable(Level.FINE)) log.fine("### Cycle is " + cycle);
for (int i = 0; i < cycle.size(); i++) {
Component comp = cycle.get(i);
if (accept(comp)) {
return comp;
} else if (comp instanceof Container && comp != aContainer) {
Container cont = (Container)comp;
if (cont.isFocusTraversalPolicyProvider()) {
return cont.getFocusTraversalPolicy().getDefaultComponent(cont);
}
}
}
}
return null;
}
Returns the first Component in the traversal cycle. This method is used
to determine the next Component to focus when traversal wraps in the
forward direction. |
public boolean getImplicitDownCycleTraversal() {
return implicitDownCycleTraversal;
}
Returns whether this ContainerOrderFocusTraversalPolicy transfers focus
down-cycle implicitly. If true, during normal forward focus
traversal, the Component traversed after a focus cycle root will be the
focus-cycle-root's default Component to focus. If false,
the next Component in the focus traversal cycle rooted at the specified
focus cycle root will be traversed instead. |
public Component getLastComponent(Container aContainer) {
List< Component > cycle;
if (log.isLoggable(Level.FINE)) log.fine("### Getting last component in " + aContainer);
if (aContainer == null) {
throw new IllegalArgumentException("aContainer cannot be null");
}
synchronized(aContainer.getTreeLock()) {
if (!(aContainer.isVisible() && aContainer.isDisplayable())) {
return null;
}
if (this.cachedRoot == aContainer) {
cycle = this.cachedCycle;
} else {
cycle = getFocusTraversalCycle(aContainer);
}
if (cycle.size() == 0) {
if (log.isLoggable(Level.FINE)) log.fine("### Cycle is empty");
return null;
}
if (log.isLoggable(Level.FINE)) log.fine("### Cycle is " + cycle);
for (int i= cycle.size() - 1; i >= 0; i--) {
Component comp = cycle.get(i);
if (accept(comp)) {
return comp;
} else if (comp instanceof Container && comp != aContainer) {
Container cont = (Container)comp;
if (cont.isFocusTraversalPolicyProvider()) {
return cont.getFocusTraversalPolicy().getLastComponent(cont);
}
}
}
}
return null;
}
Returns the last Component in the traversal cycle. This method is used
to determine the next Component to focus when traversal wraps in the
reverse direction. |
public void setImplicitDownCycleTraversal(boolean implicitDownCycleTraversal) {
this.implicitDownCycleTraversal = implicitDownCycleTraversal;
}
Sets whether this ContainerOrderFocusTraversalPolicy transfers focus
down-cycle implicitly. If true, during normal forward focus
traversal, the Component traversed after a focus cycle root will be the
focus-cycle-root's default Component to focus. If false,
the next Component in the focus traversal cycle rooted at the specified
focus cycle root will be traversed instead. The default value for this
property is true. |