[e-cvs] cvs commit: e/src/jsrc/org/erights/e/meta/java/awt ComponentMakerSugar.java

markm@eros.cs.jhu.edu markm@eros.cs.jhu.edu
Wed, 9 Jan 2002 16:39:24 -0500


markm       02/01/09 16:39:24

  Added:       src/jsrc/org/erights/e/meta/java/awt
                        ComponentMakerSugar.java
  Log:
  transferFocus sugar

Revision  Changes    Path
1.1                  e/src/jsrc/org/erights/e/meta/java/awt/ComponentMakerSugar.java

Index: ComponentMakerSugar.java
===================================================================
package org.erights.e.meta.java.awt;

import java.awt.Container;
import java.awt.Component;

//This file is hereby placed in the public domain

/**
 *
 * @author <a href="mailto:markm@caplet.com">Mark Miller</a>
 */
public class ComponentMakerSugar {

    /** prevent instantiation */
    private ComponentMakerSugar() {}

    /**
     * Do any of these have or contain the focus?
     */
    static private boolean anyContainsFocus(Component[] sources) {
        for (int i = 0, len = sources.length; i < len; i++) {
            if (sources[i].hasFocus()) {
                return true;
            }
            if (sources[i] instanceof Container) {
                Container cont = (Container)sources[i];
                if (anyContainsFocus(cont.getComponents())) {
                    return true;
                }
            }
            //XXX bug: doesn't traverse children not enumerated by
            //getComponents(), like JFrame's and JScrollPane's
        }
        return false;
    }

    /**
     * If any of 'sources' currently has or contains the focus, then requests
     * that 'dest' gets the focus.
     *
     * @param 'sources' Provided to show that the requestor has rights to the
     *        focus, in order to authorize the transfer to 'dest'.
     * @return Whether the operation was authorized by 'sources', not whether
     *         the 'requestFocus()' succeeded.
     */
    static public boolean transferFocus(Component[] sources, Component dest) {
        if (! anyContainsFocus(sources)) {
            return false;
        }
        dest.requestFocus();
        return true;
    }
}