JComponentHandler.java :  » Testing » jacareto » jacareto » comp » Java Open Source

Java Open Source » Testing » jacareto 
jacareto » jacareto » comp » JComponentHandler.java
/*
 * Jacareto Copyright (c) 2002-2005
 * Applied Computer Science Research Group, Darmstadt University of
 * Technology, Institute of Mathematics & Computer Science,
 * Ludwigsburg University of Education, and Computer Based
 * Learning Research Group, Aachen University. All rights reserved.
 *
 * Jacareto is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * Jacareto 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 for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with Jacareto; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

package jacareto.comp;


import jacareto.system.Environment;

import java.awt.Component;

import java.util.Vector;

import javax.accessibility.AccessibleContext;

import javax.swing.JComponent;

/**
 * This is a class which provides methods for JComponents. It uses the accessibility API for the
 * retrieval of the child components of a JComponent in addition to standard methods.
 *
 * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
 * @version 1.0
 */
public class JComponentHandler extends ContainerHandler {
    /**
     * Creates a new JComponent handler.
     *
     * @param env the environment
     * @param components the components instance
     */
    public JComponentHandler (Environment env, Components components) {
        super(env, components);
    }

    /**
     * Returns whether this handler is responsible for the given component. This class is
     * responsible for JComponents.
     *
     * @param component the component
     *
     * @return <code>true</code> if this handler is responsible for the given component, otherwise
     *         <code>false</code>
     */
    public boolean handlesComponent (Component component) {
        return (component != null) && component instanceof JComponent;
    }

    /**
     * Returns an array with components contained
     *
     * @param component the component
     *
     * @return the array
     */
    public Component[] getChildren (Component component) {
        Vector children = new Vector();

        // Children retrieved from the accessible context
        AccessibleContext context = ((JComponent) component).getAccessibleContext ();

        if (context != null) {
            int childrenCount = context.getAccessibleChildrenCount ();

            for (int i = 0; i < childrenCount; i++) {
                Object childObject = context.getAccessibleChild (i);

                if ((childObject != null) && (childObject instanceof Component)) {
                    children.add (childObject);
                }
            }
        }

        // Children retrieved from the component as container
        Component[] containerChildren = super.getChildren (component);

        if (containerChildren != null) {
            for (int i = 0; i < containerChildren.length; i++) {
                if ((containerChildren[i] != null) && ! children.contains (containerChildren[i])) {
                    children.add (containerChildren[i]);
                }
            }
        }

        // convert the vector to an array
        Component[] result = new Component[children.size ()];

        for (int i = 0; i < result.length; i++) {
            result[i] = (Component) children.get (i);
        }

        return result;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.