Created
December 17, 2012 00:02
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
doh - forgot to sign in before saving it