Skip to content

Instantly share code, notes, and snippets.

Created December 17, 2012 00:02
Show Gist options
  • Save anonymous/4314414 to your computer and use it in GitHub Desktop.
Save anonymous/4314414 to your computer and use it in GitHub Desktop.
A filterable ListDataProvider that takes another ListDataProvider for it's source. Handy if you want, say, a CellTable that has a Filter text box. Note that for very large data sets you probably don't want to use this, since it creates a copy of the data each time it applies the filter.
package app.kiss.client.data;
import java.util.List;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.ListDataProvider;
public class FilteredListDataProvider<T> extends ListDataProvider<T> {
public FilteredListDataProvider(ListDataProvider<T> originalProvider) {
super(originalProvider.getList(), originalProvider.getKeyProvider());
}
public Predicate<T> filter;
public Predicate<T> getFilter() {
return filter;
}
/**
*
* @param filter
*
* Example use:
* setFilter(new Predicate<T>() {
* @Override public boolean apply(@Nullable T value) {
* if (value == null)
* return false;
* else return (value.getSomeString().toLowerCase()
* .contains(someFilterString));
* } });
*/
public void setFilter(Predicate<T> filter) {
if (filter == null)
resetFilter();
else {
this.filter = filter;
refresh();
}
}
public void resetFilter() {
filter = null;
refresh();
}
@Override
protected void updateRowData(HasData<T> display, int start, List<T> values) {
if (filter != null)
// apply the filter to the list
values = Lists.newArrayList(Iterables.filter(values, filter));
super.updateRowData(display, start, values);
// need to do this since the filter may have changed the row count
int size = values.size();
if (size != display.getRowCount())
display.setRowCount(size);
}
public boolean hasFilter() {
return filter != null;
}
}
@carchrae
Copy link

doh - forgot to sign in before saving it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment