All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.richfaces.component.ComponentIterators Maven / Gradle / Ivy

/*
 * JBoss, Home of Professional Open Source
 * Copyright 2013, Red Hat, Inc. and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.richfaces.component;

import java.util.Iterator;

import javax.faces.component.UIComponent;

import com.google.common.base.Predicate;
import com.google.common.collect.AbstractIterator;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterators;

/**
 * @author Nick Belaevski
 *
 */
public final class ComponentIterators {
    private ComponentIterators() {
    }

    // TODO nick - convert to filter/find functions
    public static Iterator parents(final UIComponent component) {
        if (component == null) {
            return ImmutableSet.of().iterator();
        }

        return new AbstractIterator() {
            private UIComponent currentComponent = component;

            @Override
            protected UIComponent computeNext() {
                currentComponent = currentComponent.getParent();

                if (currentComponent == null) {
                    endOfData();
                }

                return currentComponent;
            }
        };
    }

    public static Iterator parentsAndSelf(final UIComponent component) {
        if (component == null) {
            return ImmutableSet.of().iterator();
        }

        return Iterators.concat(Iterators.singletonIterator(component), parents(component));
    }

    public static UIComponent getParent(UIComponent component, Predicate predicat) {
        if (component == null || predicat == null) {
            return null;
        }

        UIComponent parent = component.getParent();
        while (parent != null) {
            if (predicat.apply(parent)) {
                return parent;
            }
            parent = parent.getParent();
        }
        return null;
    }

    /**
     * Finds a parent of given UI component.
     *
     * @param component UIComponent
     * @param parentClass Class of desired parent
     * @return UIComponent
     */
    public static  T getParent(UIComponent component, Class parentClass) {
        if (component == null || parentClass == null) {
            return null;
        }

        UIComponent parent = component.getParent();
        while (parent != null) {
            if (parentClass.isInstance(parent)) {
                return (T) parent;
            }
            parent = parent.getParent();
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy