0

Component query by tagname (or component type)

asked 2012-07-30 21:22:18 +0800

aktejo gravatar image aktejo
155 3

I Looking for zk functionality to query certain type of components children, given the parent component and the type of children want to be retreive,
but I think I can't find it ,

Then I create this helper, it works for me, I hope it can help those who need it, I believe this is not that heavy, or if anybody have any idea to improve the code i welcome it.

public class ComponentUtil {
    private static <T extends Component> List<T> find(Class<T> clazz, Component parent, int depth, boolean exitOnFirst) {
        List<T> comps = new ArrayList<T>();
        int nextDepth = depth - 1;
        List<Component> children = parent.getChildren();
        System.out.println("parent " + parent + ",   current depth: " + depth + ",  member: " + children.size());
        for (Iterator<Component> iterator = children.iterator(); iterator.hasNext(); ) {
            Component child = iterator.next();
            if (child.getClass().getName().equals(clazz.getName())) {
                comps.add((T) child);
                if (exitOnFirst) {
                    break;
                }
            }
            if (nextDepth > 0) {
                comps.addAll(find(clazz, child, nextDepth, exitOnFirst));
            }
        }
        return comps;
    }

    public static <T extends Component> List<T> find(Class<T> clazz, Component parent, int depth) {
        return find(clazz, parent, depth, false);
    }

    public static <T extends Component> T findFirst(Class<T> clazz, Component parent, int depth) {
        List<T> t = find(clazz, parent, depth, true);
        if (t.size() > 0)
            return t.get(0);
        return null;
    }
}

delete flag offensive retag edit
Be the first one to reply this discussion!
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2012-07-30 21:22:18 +0800

Seen: 249 times

Last updated: Jul 30 '12

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More