containerorderfocustraversalpolicy.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 377 行 · 第 1/2 页

JAVA
377
字号
/* *  * Copyright  1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER *  * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation.  *  * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt).  *  * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA  *  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions.  */package java.awt;/** * 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 * <code>Container.getComponents()</code>. Portions of the hierarchy that are * not visible and displayable will not be searched. * <p> * 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 * <code>setImplicitDownCycleTraversal</code> method. * <p> * 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 <code>accept</code> method. * * @author David Mendenhall * @version 1.3, 01/23/03 * * @see Container#getComponents * @since 1.4 */public class ContainerOrderFocusTraversalPolicy extends FocusTraversalPolicy    implements java.io.Serializable {    private static class MutableBoolean {        boolean value = false;    }    private static final MutableBoolean found = new MutableBoolean();    private boolean implicitDownCycleTraversal = true;    /**     * Returns the Component that should receive the focus after aComponent.     * focusCycleRoot must be a focus cycle root of aComponent.     * <p>     * 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 <code>setImplicitDownCycleTraversal</code> method.     *     * @param focusCycleRoot a focus cycle root of aComponent     * @param aComponent a (possibly indirect) child of focusCycleRoot, or     *        focusCycleRoot itself     * @return the Component that should receive the focus after aComponent, or     *         null if no suitable Component can be found     * @throws IllegalArgumentException if focusCycleRoot is not a focus cycle     *         root of aComponent, or if either focusCycleRoot or aComponent is     *         null     */    public Component getComponentAfter(Container focusCycleRoot,            Component aComponent) {        if (focusCycleRoot == null || aComponent == null) {            throw new IllegalArgumentException("focusCycleRoot and aComponent cannot be null");        }        if (!aComponent.isFocusCycleRoot(focusCycleRoot)) {            throw new IllegalArgumentException("focusCycleRoot is not a focus cyle root of aComponent");        }        synchronized (focusCycleRoot.getTreeLock()) {            found.value = false;            Component retval = getComponentAfter(focusCycleRoot, aComponent,                    found);            if (retval != null) {                return retval;            } else if (found.value) {                return getFirstComponent(focusCycleRoot);            } else {                return null;            }        }    }    private Component getComponentAfter(Container aContainer,            Component aComponent,            MutableBoolean found) {        if (!(aContainer.isVisible() && aContainer.isDisplayable())) {            return null;        }        if (found.value) {            if (accept(aContainer)) {                return aContainer;            }        } else if (aContainer == aComponent) {            found.value = true;        }        for (int i = 0; i < aContainer.ncomponents; i++) {            Component comp = aContainer.component[i];            if ((comp instanceof Container)                    && !((Container) comp).isFocusCycleRoot()) {                Component retval = getComponentAfter((Container) comp,                        aComponent, found);                if (retval != null) {                    return retval;                }            } else if (found.value) {                if (accept(comp)) {                    return comp;                }            } else if (comp == aComponent) {                found.value = true;            }            if (found.value && getImplicitDownCycleTraversal()                    && (comp instanceof Container)                    && ((Container) comp).isFocusCycleRoot()) {                Container cont = (Container) comp;                Component retval = cont.getFocusTraversalPolicy().getDefaultComponent(cont);                if (retval != null) {                    return retval;                }            }        }        return null;    }    /**     * Returns the Component that should receive the focus before aComponent.     * focusCycleRoot must be a focus cycle root of aComponent.     *     * @param focusCycleRoot a focus cycle root of aComponent     * @param aComponent a (possibly indirect) child of focusCycleRoot, or     *        focusCycleRoot itself     * @return the Component that should receive the focus before aComponent,     *         or null if no suitable Component can be found     * @throws IllegalArgumentException if focusCycleRoot is not a focus cycle     *         root of aComponent, or if either focusCycleRoot or aComponent is     *         null     */    public Component getComponentBefore(Container focusCycleRoot,            Component aComponent) {        if (focusCycleRoot == null || aComponent == null) {            throw new IllegalArgumentException("focusCycleRoot and aComponent cannot be null");        }        if (!aComponent.isFocusCycleRoot(focusCycleRoot)) {            throw new IllegalArgumentException("focusCycleRoot is not a focus cyle root of aComponent");        }        synchronized (focusCycleRoot.getTreeLock()) {            found.value = false;            Component retval = getComponentBefore(focusCycleRoot, aComponent,                    found);            if (retval != null) {                return retval;            } else if (found.value) {                return getLastComponent(focusCycleRoot);            } else {                return null;            }        }    }    private Component getComponentBefore(Container aContainer,            Component aComponent,            MutableBoolean found) {        if (!(aContainer.isVisible() && aContainer.isDisplayable())) {            return null;        }        for (int i = aContainer.ncomponents - 1; i >= 0; i--) {            Component comp = aContainer.component[i];            if (comp == aComponent) {                found.value = true;            } else if ((comp instanceof Container)                    && !((Container) comp).isFocusCycleRoot()) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?