Java 怎么从中查找子组件或JSF Facet From

2019年8月14日22:03:52 评论 31

1.简介

Java 怎么从中查找子组件或JSF Facet From?——请看下文。

2.代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

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;
    }
    
    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;
    }
}

 

3.总结

示例代码演示了 Java 怎么从中查找子组件或JSF Facet From,具体步骤参见示例代码。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: