ContainerOrderFocusTraversalPolicy.java revision 1336
2976N/A/*
2976N/A * Copyright 2000-2003 Sun Microsystems, Inc. All Rights Reserved.
2976N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2976N/A *
2976N/A * This code is free software; you can redistribute it and/or modify it
2976N/A * under the terms of the GNU General Public License version 2 only, as
2976N/A * published by the Free Software Foundation. Sun designates this
2976N/A * particular file as subject to the "Classpath" exception as provided
2976N/A * by Sun in the LICENSE file that accompanied this code.
2976N/A *
2976N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2976N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2976N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2976N/A * version 2 for more details (a copy is included in the LICENSE file that
2976N/A * accompanied this code).
2976N/A *
2976N/A * You should have received a copy of the GNU General Public License version
2976N/A * 2 along with this work; if not, write to the Free Software Foundation,
2976N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2976N/A *
2976N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
2976N/A * CA 95054 USA or visit www.sun.com if you need additional information or
2976N/A * have any questions.
2976N/A */
5022N/Apackage java.awt;
6238N/A
2976N/Aimport java.util.logging.*;
2976N/Aimport java.util.List;
2976N/Aimport java.util.ArrayList;
2976N/A
2976N/A/**
3824N/A * A FocusTraversalPolicy that determines traversal order based on the order
2976N/A * of child Components in a Container. From a particular focus cycle root, the
2976N/A * policy makes a pre-order traversal of the Component hierarchy, and traverses
2976N/A * a Container's children according to the ordering of the array returned by
2976N/A * <code>Container.getComponents()</code>. Portions of the hierarchy that are
2976N/A * not visible and displayable will not be searched.
2976N/A * <p>
2976N/A * By default, ContainerOrderFocusTraversalPolicy implicitly transfers focus
2976N/A * down-cycle. That is, during normal forward focus traversal, the Component
6238N/A * traversed after a focus cycle root will be the focus-cycle-root's default
2976N/A * Component to focus. This behavior can be disabled using the
2976N/A * <code>setImplicitDownCycleTraversal</code> method.
2976N/A * <p>
2976N/A * By default, methods of this class with return a Component only if it is
3824N/A * visible, displayable, enabled, and focusable. Subclasses can modify this
3824N/A * behavior by overriding the <code>accept</code> method.
3824N/A * <p>
3824N/A * This policy takes into account <a
3824N/A * href="doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus traversal
5728N/A * policy providers</a>. When searching for first/last/next/previous Component,
3842N/A * if a focus traversal policy provider is encountered, its focus traversal
3842N/A * policy is used to perform the search operation.
3824N/A *
3842N/A * @author David Mendenhall
2976N/A *
2976N/A * @see Container#getComponents
2976N/A * @since 1.4
2976N/A */
6238N/Apublic class ContainerOrderFocusTraversalPolicy extends FocusTraversalPolicy
3824N/A implements java.io.Serializable
3824N/A{
3824N/A private static final Logger log = Logger.getLogger("java.awt.ContainerOrderFocusTraversalPolicy");
4287N/A
3824N/A final private int FORWARD_TRAVERSAL = 0;
3824N/A final private int BACKWARD_TRAVERSAL = 1;
2976N/A
2976N/A /*
2976N/A * JDK 1.4 serialVersionUID
2976N/A */
2976N/A private static final long serialVersionUID = 486933713763926351L;
2976N/A
2976N/A private boolean implicitDownCycleTraversal = true;
6238N/A
2976N/A /**
2976N/A * Used by getComponentAfter and getComponentBefore for efficiency. In
2976N/A * order to maintain compliance with the specification of
2976N/A * FocusTraversalPolicy, if traversal wraps, we should invoke
2976N/A * getFirstComponent or getLastComponent. These methods may be overriden in
2976N/A * subclasses to behave in a non-generic way. However, in the generic case,
2976N/A * these methods will simply return the first or last Components of the
2976N/A * sorted list, respectively. Since getComponentAfter and
2976N/A * getComponentBefore have already built the list before determining
6238N/A * that they need to invoke getFirstComponent or getLastComponent, the
5022N/A * list should be reused if possible.
3824N/A */
5636N/A transient private Container cachedRoot;
2976N/A transient private List cachedCycle;
2976N/A
2976N/A /*
2976N/A * We suppose to use getFocusTraversalCycle & getComponentIndex methods in order
5022N/A * to divide the policy into two parts:
5636N/A * 1) Making the focus traversal cycle.
5636N/A * 2) Traversing the cycle.
5636N/A * The 1st point assumes producing a list of components representing the focus
5636N/A * traversal cycle. The two methods mentioned above should implement this logic.
5636N/A * The 2nd point assumes implementing the common concepts of operating on the
5636N/A * cycle: traversing back and forth, retrieving the initial/default/first/last
5636N/A * component. These concepts are described in the AWT Focus Spec and they are
5636N/A * applied to the FocusTraversalPolicy in general.
2976N/A * Thus, a descendant of this policy may wish to not reimplement the logic of
2976N/A * the 2nd point but just override the implementation of the 1st one.
5636N/A * A striking example of such a descendant is the javax.swing.SortingFocusTraversalPolicy.
5636N/A */
5636N/A /*protected*/ private List<Component> getFocusTraversalCycle(Container aContainer) {
5636N/A List<Component> cycle = new ArrayList<Component>();
5636N/A enumerateCycle(aContainer, cycle);
5636N/A return cycle;
5636N/A }
5636N/A /*protected*/ private int getComponentIndex(List<Component> cycle, Component aComponent) {
4915N/A return cycle.indexOf(aComponent);
4915N/A }
5636N/A
2976N/A private void enumerateCycle(Container container, List cycle) {
2976N/A if (!(container.isVisible() && container.isDisplayable())) {
4915N/A return;
4915N/A }
4915N/A
5636N/A cycle.add(container);
4915N/A
4915N/A Component[] components = container.getComponents();
2976N/A for (int i = 0; i < components.length; i++) {
2976N/A Component comp = components[i];
2976N/A if (comp instanceof Container) {
5636N/A Container cont = (Container)comp;
2976N/A
2976N/A if (!cont.isFocusCycleRoot() && !cont.isFocusTraversalPolicyProvider()) {
2976N/A enumerateCycle(cont, cycle);
4915N/A continue;
2976N/A }
5636N/A }
2976N/A cycle.add(comp);
2976N/A }
2976N/A }
4261N/A
5224N/A private Container getTopmostProvider(Container focusCycleRoot, Component aComponent) {
2976N/A Container aCont = aComponent.getParent();
2976N/A Container ftp = null;
5636N/A while (aCont != focusCycleRoot && aCont != null) {
5218N/A if (aCont.isFocusTraversalPolicyProvider()) {
4261N/A ftp = aCont;
2976N/A }
4261N/A aCont = aCont.getParent();
4261N/A }
2976N/A if (aCont == null) {
3824N/A return null;
2976N/A }
2976N/A return ftp;
2976N/A }
2976N/A
6238N/A /*
3007N/A * Checks if a new focus cycle takes place and returns a Component to traverse focus to.
2976N/A * @param comp a possible focus cycle root or policy provider
2976N/A * @param traversalDirection the direction of the traversal
2976N/A * @return a Component to traverse focus to if {@code comp} is a root or provider
2976N/A * and implicit down-cycle is set, otherwise {@code null}
2976N/A */
5636N/A private Component getComponentDownCycle(Component comp, int traversalDirection) {
5636N/A Component retComp = null;
5636N/A
3449N/A if (comp instanceof Container) {
3449N/A Container cont = (Container)comp;
3449N/A
6238N/A if (cont.isFocusCycleRoot()) {
2976N/A if (getImplicitDownCycleTraversal()) {
5636N/A retComp = cont.getFocusTraversalPolicy().getDefaultComponent(cont);
5636N/A
5636N/A if (retComp != null && log.isLoggable(Level.FINE)) {
5636N/A log.fine("### Transfered focus down-cycle to " + retComp +
5636N/A " in the focus cycle root " + cont);
2976N/A }
5636N/A } else {
2976N/A return null;
4261N/A }
5636N/A } else if (cont.isFocusTraversalPolicyProvider()) {
5224N/A retComp = (traversalDirection == FORWARD_TRAVERSAL ?
2976N/A cont.getFocusTraversalPolicy().getDefaultComponent(cont) :
2976N/A cont.getFocusTraversalPolicy().getLastComponent(cont));
5636N/A
5218N/A if (retComp != null && log.isLoggable(Level.FINE)) {
4261N/A log.fine("### Transfered focus to " + retComp + " in the FTP provider " + cont);
2976N/A }
4261N/A }
4261N/A }
2976N/A return retComp;
3824N/A }
2976N/A
2976N/A /**
2976N/A * Returns the Component that should receive the focus after aComponent.
3449N/A * aContainer must be a focus cycle root of aComponent or a focus traversal policy provider.
5218N/A * <p>
3449N/A * By default, ContainerOrderFocusTraversalPolicy implicitly transfers
5636N/A * focus down-cycle. That is, during normal forward focus traversal, the
3449N/A * Component traversed after a focus cycle root will be the focus-cycle-
3449N/A * root's default Component to focus. This behavior can be disabled using
3449N/A * the <code>setImplicitDownCycleTraversal</code> method.
2976N/A * <p>
4261N/A * If aContainer is <a href="doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus
5636N/A * traversal policy provider</a>, the focus is always transferred down-cycle.
4261N/A *
5636N/A * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider
5224N/A * @param aComponent a (possibly indirect) child of aContainer, or
2976N/A * aContainer itself
2976N/A * @return the Component that should receive the focus after aComponent, or
5636N/A * null if no suitable Component can be found
5218N/A * @throws IllegalArgumentException if aContainer is not a focus cycle
4261N/A * root of aComponent or focus traversal policy provider, or if either aContainer or
2976N/A * aComponent is null
4261N/A */
4261N/A public Component getComponentAfter(Container aContainer, Component aComponent) {
2976N/A if (log.isLoggable(Level.FINE)) log.fine("### Searching in " + aContainer + " for component after " + aComponent);
3824N/A
2976N/A if (aContainer == null || aComponent == null) {
2976N/A throw new IllegalArgumentException("aContainer and aComponent cannot be null");
2976N/A }
2976N/A if (!aContainer.isFocusTraversalPolicyProvider() && !aContainer.isFocusCycleRoot()) {
2976N/A 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 before aComponent.
* aContainer must be a focus cycle root of aComponent or a <a
* href="doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus traversal policy
* provider</a>.
*
* @param aContainer a focus cycle root of aComponent or focus traversal policy provider
* @param aComponent a (possibly indirect) child of aContainer, or
* aContainer itself
* @return the Component that should receive the focus before aComponent,
* or null if no suitable Component can be found
* @throws IllegalArgumentException if aContainer is not a focus cycle
* root of aComponent or focus traversal policy provider, or if either aContainer or
* aComponent is null
*/
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 first Component in the traversal cycle. This method is used
* to determine the next Component to focus when traversal wraps in the
* forward direction.
*
* @param aContainer the focus cycle root or focus traversal policy provider whose first
* Component is to be returned
* @return the first Component in the traversal cycle of aContainer,
* or null if no suitable Component can be found
* @throws IllegalArgumentException if aContainer is null
*/
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 (Component comp : cycle) {
if (accept(comp)) {
return comp;
} else if (comp != aContainer &&
(comp = getComponentDownCycle(comp, FORWARD_TRAVERSAL)) != null)
{
return comp;
}
}
}
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.
*
* @param aContainer the focus cycle root or focus traversal policy provider whose last
* Component is to be returned
* @return the last Component in the traversal cycle of aContainer,
* or null if no suitable Component can be found
* @throws IllegalArgumentException if aContainer is null
*/
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 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 <code>getFirstComponent</code>.
*
* @param aContainer the focus cycle root or focus traversal policy provider whose default
* Component is to be returned
* @return the default Component in the traversal cycle of aContainer,
* or null if no suitable Component can be found
* @see #getFirstComponent
* @throws IllegalArgumentException if aContainer is null
*/
public Component getDefaultComponent(Container aContainer) {
return getFirstComponent(aContainer);
}
/**
* Sets whether this ContainerOrderFocusTraversalPolicy transfers focus
* down-cycle implicitly. If <code>true</code>, 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 <code>false</code>,
* 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 <code>true</code>.
*
* @param implicitDownCycleTraversal whether this
* ContainerOrderFocusTraversalPolicy transfers focus down-cycle
* implicitly
* @see #getImplicitDownCycleTraversal
* @see #getFirstComponent
*/
public void setImplicitDownCycleTraversal(boolean implicitDownCycleTraversal) {
this.implicitDownCycleTraversal = implicitDownCycleTraversal;
}
/**
* Returns whether this ContainerOrderFocusTraversalPolicy transfers focus
* down-cycle implicitly. If <code>true</code>, 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 <code>false</code>,
* the next Component in the focus traversal cycle rooted at the specified
* focus cycle root will be traversed instead.
*
* @return whether this ContainerOrderFocusTraversalPolicy transfers focus
* down-cycle implicitly
* @see #setImplicitDownCycleTraversal
* @see #getFirstComponent
*/
public boolean getImplicitDownCycleTraversal() {
return implicitDownCycleTraversal;
}
/**
* 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.
*
* @param aComponent the Component whose fitness as a focus owner is to
* be tested
* @return <code>true</code> if aComponent is visible, displayable,
* enabled, and focusable; <code>false</code> otherwise
*/
protected boolean accept(Component aComponent) {
if (!aComponent.canBeFocusOwner()) {
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;
}
}