Android Open Source - android-view-selector View Selector






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;
//from w  w  w  .  j  a  v a 2  s. c om
import java.util.List;

import se.fishtank.css.selectors.Selector;
import se.fishtank.css.selectors.Specifier;
import se.fishtank.css.selectors.scanner.Scanner;
import se.fishtank.css.selectors.scanner.ScannerException;
import se.fishtank.css.selectors.specifier.AttributeSpecifier;
import android.view.View;

import com.nikhaldimann.viewselector.checker.AttributeSpecifierChecker;
import com.nikhaldimann.viewselector.checker.ClassChecker;
import com.nikhaldimann.viewselector.checker.ViewTraversalChecker;
import com.nikhaldimann.viewselector.selection.ViewSelection;

/**
 * Represents a selector that can be applied to a view.
 */
public class ViewSelector {

    private final List<List<Selector>> selectorGroups;

    public ViewSelector(List<List<Selector>> selectorGroups) {
        this.selectorGroups = selectorGroups;
    }

    /**
     * Applies this selector to the given view, selecting all descendants of the
     * view that match this selector.
     * @return the ordered set of views that matches this selector
     */
    public ViewSelection selectViews(View view) {
        ViewSelection result = new ViewSelection();
        for (List<Selector> selectorParts : selectorGroups) {
            result.addAll(selectViewsForGroup(selectorParts, view));
        }
        return result;
    }

    private ViewSelection selectViewsForGroup(List<Selector> selectorParts, View view) {
        ViewSelection result = new ViewSelection();
        result.add(view);
        for (Selector selector : selectorParts) {
            ViewTraversalChecker checker = new ClassChecker(selector, view);
            result = checker.check(result);
            if (result.isEmpty()) {
                return result;
            }

            if (selector.hasSpecifiers())  {
                for (Specifier specifier : selector.getSpecifiers()) {
                    switch (specifier.getType()) {
                        case ATTRIBUTE:
                            checker = new AttributeSpecifierChecker((AttributeSpecifier) specifier, view);
                            break;
                        default:
                            throw new UnsupportedOperationException();
                    }
                    result = checker.check(result);
                    if (result.isEmpty()) {
                        return result;
                    }
                }
            }
        }
        return result;
    }

    /**
     * @return a new ViewSelector from the given selector string
     * @throws InvalidSelectorException if the given selector string can't be parsed into
     *     a valid selector
     */
    public static ViewSelector compile(String selectorString) {
        List<List<Selector>> groups;
        try {
            Scanner scanner = new Scanner(selectorString);
            groups = scanner.scan();
        } catch (ScannerException ex) {
            throw new InvalidSelectorException("Invalid selector: " + selectorString, ex);
        }
        return new ViewSelector(groups);
    }
}




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