find Component Child Or JSF Facet From - Java JSF

Java examples for JSF:UIComponent

Description

find Component Child Or JSF Facet From

Demo Code

/*//from   ww w .  j  ava  2s  .c  o  m
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
import javax.el.ValueExpression;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.el.EvaluationException;
import javax.faces.el.MethodBinding;
import javax.faces.el.ValueBinding;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import java.util.Collection;

public class Main{
    static UIComponent findComponentChildOrFacetFrom(UIComponent parent,
            String id, String innerExpr) {
        if (parent.getFacetCount() > 0) {
            for (UIComponent facet : parent.getFacets().values()) {
                if (id.equals(facet.getId())) {
                    if (innerExpr == null) {
                        return facet;
                    } else if (facet instanceof NamingContainer) {
                        UIComponent find = facet.findComponent(innerExpr);
                        if (find != null) {
                            return find;
                        }
                    }
                } else if (!(facet instanceof NamingContainer)) {
                    UIComponent find = findComponentChildOrFacetFrom(facet,
                            id, innerExpr);
                    if (find != null) {
                        return find;
                    }
                }
            }
        }
        if (parent.getChildCount() > 0) {
            for (int i = 0, childCount = parent.getChildCount(); i < childCount; i++) {
                UIComponent child = parent.getChildren().get(i);
                if (id.equals(child.getId())) {
                    if (innerExpr == null) {
                        return child;
                    } else if (child instanceof NamingContainer) {
                        UIComponent find = child.findComponent(innerExpr);
                        if (find != null) {
                            return find;
                        }
                    }
                } else if (!(child instanceof NamingContainer)) {
                    UIComponent find = findComponentChildOrFacetFrom(child,
                            id, innerExpr);
                    if (find != null) {
                        return find;
                    }
                }
            }
        }
        return null;
    }
    /**
     * Find the component with the specified id starting from the specified component.
     * <p>
     * Param id must not contain any NamingContainer.SEPARATOR_CHAR characters (ie ":"). This method explicitly does
     * <i>not</i> search into any child naming container components; this is expected to be handled by the caller of
     * this method.
     * <p>
     * For an implementation of findComponent which does descend into child naming components, see
     * org.apache.myfaces.custom.util.ComponentUtils.
     * 
     * @return findBase, a descendant of findBase, or null.
     */
    static UIComponent findComponent(UIComponent findBase, String id,
            final char separatorChar) {
        if (!(findBase instanceof NamingContainer)
                && idsAreEqual(id, findBase)) {
            return findBase;
        }

        int facetCount = findBase.getFacetCount();
        if (facetCount > 0) {
            for (UIComponent facet : findBase.getFacets().values()) {
                if (!(facet instanceof NamingContainer)) {
                    UIComponent find = findComponent(facet, id,
                            separatorChar);
                    if (find != null) {
                        return find;
                    }
                } else if (idsAreEqual(id, facet)) {
                    return facet;
                }
            }
        }

        for (int i = 0, childCount = findBase.getChildCount(); i < childCount; i++) {
            UIComponent child = findBase.getChildren().get(i);
            if (!(child instanceof NamingContainer)) {
                UIComponent find = findComponent(child, id, separatorChar);
                if (find != null) {
                    return find;
                }
            } else if (idsAreEqual(id, child)) {
                return child;
            }
        }

        if (findBase instanceof NamingContainer
                && idsAreEqual(id, findBase)) {
            return findBase;
        }

        return null;
    }
    private static boolean idsAreEqual(String id, UIComponent cmp) {
        if (id.equals(cmp.getId())) {
            return true;
        }

        return false;
    }
}

Related Tutorials