Custom filtering using Matchers

Just as you've seen TextComponentMatcherEditor filter issues with a JTextField, you can create a custom MatcherEditor to filter with the users JList. The first step is to create a simple Matcher for static filtering. Then we'll create MatcherEditor to implement dynamic filtering using our static Matcher.

Implementing the Matcher will require you to write a single method, matches() to test whether a given element should be filtered out. You'll need to create a Matcher that accepts issues for a list of users.

Warning

It's unfortunate that Glazed Lists' Matcher uses the same class name as java.util.regex.Matcher. If you find yourself implementing a Glazed Lists Matcher that requires regular expressions, you'll need to fully qualify classnames throughout your code, and we apologize. We considered 'Predicate' for the interface name but decided it was too presumptuous. Naming is very important to us at Glazed Lists!

import java.util.*;
// glazed lists
import ca.odell.glazedlists.*;
import ca.odell.glazedlists.matchers.*;
// a simple issues library
import ca.odell.issuezilla.*;

/**
 * This {@link Matcher} only matches users in a predefined set.
 * 
 * @author <a href="mailto:jesse@odel.on.ca">Jesse Wilson</a>
 */
public class IssuesForUsersMatcher implements Matcher {
    
    /** the users to match */
    private Set users = new HashSet();
    
    /**
     * Create a new {@link IssuesForUsersMatcher} that matches only 
     * {@link Issue}s that have one or more user in the specified list.
     */
    public IssuesForUsersMatcher(Collection users) {
        // make a defensive copy of the users
        this.users.addAll(users);
    }
    
    /**
     * Test whether to include or not include the specified issue based
     * on whether or not their user is selected.
     */
    public boolean matches(Object o) {
        if(o == null) return false;
        if(users.isEmpty()) return true;
        
        Issue issue = (Issue)o;
        String user = issue.getReporter();
        return users.contains(user);
    }
}

With this IssuesForUsersMatcher in place, create an EventList that contains the issues that match only the specified users:

        List users = new ArrayList();
        users.add("jessewilson");
        users.add("kevinmaltby");
        users.add("tmao");
        Matcher usersMatcher = new IssuesForUsersMatcher(users);
        
        EventList issues = ...
        FilterList IssuesForUsers = new FilterList(issues, usersMatcher);

To avoid concurrency problems, make your Matchers immutable. This enables your matches() method to be used from multiple threads without synchronization.