Android Open Source - android-view-selector Class Checker






From Project

Back to project page android-view-selector.

License

The source code is released under:

Copyright (c) 2013 Nik Haldimann Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Soft...

If you think the Android project android-view-selector listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.nikhaldimann.viewselector.checker;
// w ww .j a v  a  2 s.c  om
import java.util.Set;

import se.fishtank.css.selectors.Selector;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import com.nikhaldimann.viewselector.selection.ViewSelection;

/**
 * Checks views based on matching the tag from a selector to the class of
 * a view.
 */
public class ClassChecker implements ViewTraversalChecker {

    private final Selector selector;
    private final MatchPredicate matchPredicate;

    public ClassChecker(Selector selector, View root) {
        this.selector = selector;

        final String className = selector.getTagName();
        if (className.equals(Selector.UNIVERSAL_TAG)) {
            matchPredicate = MatchPredicates.ALWAYS_TRUE_PREDICATE;
        } else {
            matchPredicate = new MatchPredicate() {
                public boolean matches(View view) {
                    return className.equals(view.getClass().getSimpleName());
                }
            };
        }
    }

    public ViewSelection check(Set<View> views) {
        ViewSelection result = new ViewSelection();
        for (View view : views) {
            switch (selector.getCombinator()) {
                case DESCENDANT:
                    checkDescendantsRecursively(view, result);
                    break;
                case CHILD:
                    if (!(view instanceof ViewGroup)) {
                        continue;
                    }
                    checkChildren((ViewGroup) view, result);
                    break;
                case ADJACENT_SIBLING:
                    checkSiblings(view, result, true);
                    break;
                case GENERAL_SIBLING:
                    checkSiblings(view, result, false);
                    break;
                default:
                    throw new UnsupportedOperationException(
                            "Unsupported combinator " + selector.getCombinator());
            }

        }
        return result;
    }

    private void checkDescendantsRecursively(View view, Set<View> result) {
        // Note: Using recursion. We hope that real-world layouts don't get deep
        // enough that this causes a problem.
        if (matchesView(view)) {
            result.add(view);
        }
        if (view instanceof ViewGroup) {
            ViewGroup group = (ViewGroup) view;
            for (int i = 0; i < group.getChildCount(); i++) {
                View childView = group.getChildAt(i);
                checkDescendantsRecursively(childView, result);
            }
        }
    }

    private void checkChildren(ViewGroup group, Set<View> result) {
        for (int i = 0; i < group.getChildCount(); i++) {
            View childView = group.getChildAt(i);
            if (matchesView(childView)) {
                result.add(childView);
            }
        }
    }

    private void checkSiblings(View view, Set<View> result, boolean isAdjacent) {
        ViewParent parent = view.getParent();
        if (!(parent instanceof ViewGroup)) {
            return;
        }
        ViewGroup parentGroup = (ViewGroup) parent;

        int childPos = 0;
        while (parentGroup.getChildAt(childPos) != view) {
            childPos++;
        }

        if (isAdjacent && parentGroup.getChildCount() > childPos + 1) {
            View sibling = parentGroup.getChildAt(childPos + 1);
            if (matchesView(sibling)) {
                result.add(sibling);
            }
        } else {
            for (int i = childPos + 1; parentGroup.getChildCount() > i; i++) {
                View sibling = parentGroup.getChildAt(i);
                if (matchesView(sibling)) {
                    result.add(sibling);
                }
            }
        }
    }

    private boolean matchesView(View view) {
        return matchPredicate.matches(view);
    }
}




Java Source Code List

com.nikhaldimann.viewselector.Attributes.java
com.nikhaldimann.viewselector.InvalidSelectorException.java
com.nikhaldimann.viewselector.ViewSelectionAssert.java
com.nikhaldimann.viewselector.ViewSelectionAttributeAssert.java
com.nikhaldimann.viewselector.ViewSelectorAssertions.java
com.nikhaldimann.viewselector.ViewSelector.java
com.nikhaldimann.viewselector.android.AllTests.java
com.nikhaldimann.viewselector.android.ViewSelectionAssertTest.java
com.nikhaldimann.viewselector.android.ViewSelectorAssertionsTest.java
com.nikhaldimann.viewselector.android.ViewSelectorTest.java
com.nikhaldimann.viewselector.android.activities.HelloWorldExampleActivityTest.java
com.nikhaldimann.viewselector.android.activities.HelloWorldExampleFragmentActivityTest.java
com.nikhaldimann.viewselector.android.activities.ListViewExampleActivityTest.java
com.nikhaldimann.viewselector.android.activities.SimpleFragmentTest.java
com.nikhaldimann.viewselector.android.attributes.ViewAttributesTest.java
com.nikhaldimann.viewselector.android.checker.AttributeSpecifierCheckerTest.java
com.nikhaldimann.viewselector.android.checker.ClassCheckerTest.java
com.nikhaldimann.viewselector.android.testutil.AndroidTestViewFactory.java
com.nikhaldimann.viewselector.attributes.AttributeAccessException.java
com.nikhaldimann.viewselector.attributes.ViewAttributes.java
com.nikhaldimann.viewselector.attributes.ViewSelectionAttribute.java
com.nikhaldimann.viewselector.checker.AttributeSpecifierChecker.java
com.nikhaldimann.viewselector.checker.ClassChecker.java
com.nikhaldimann.viewselector.checker.MatchPredicate.java
com.nikhaldimann.viewselector.checker.MatchPredicates.java
com.nikhaldimann.viewselector.checker.ViewTraversalChecker.java
com.nikhaldimann.viewselector.robolectric.RobolectricTestHelloWorldActivity.java
com.nikhaldimann.viewselector.robolectric.RobolectricTestHelloWorldFragmentActivity.java
com.nikhaldimann.viewselector.selection.ViewSelection.java