PDA

View Full Version : [1.2.2] Adding a model rejected by the filter to a filtered store



tikvar
10 Apr 2009, 1:47 AM
Hi,

I found what I believe is a bug in the ListStore code. Suppose the store is filtered and you add a model to the store that the filter rejects.


protected void insert(List<M> items, int index, boolean supressEvent) {
if(items.size() > 0) {
if (storeSorter != null) {
...
} else {
if (!isFiltered()) {
all.addAll(index, items);
} else {
snapshot.addAll(index, items);
}
for (M m : items) {
registerModel(m);
if (isFiltered() && !isFiltered(m, filterProperty)) all.add(m);
}
if (!supressEvent) {
StoreEvent evt = createStoreEvent();
evt.models = items;
evt.index = index;
fireEvent(Add, evt);
}
}
}
}
This code will add the model to the 'snapshot' list, but not to 'all'. So far so good, but then it fires the Add event with an index that refers to the 'all' list and listeners for that event (e.g. GridView) will access a null object and fail.

Here is a full example:


class GridExample extends ContentPanel {

private BaseModelData getTestData(String comp) {
BaseModelData d = new BaseModelData();
d.set("name", comp);
return d;
}

StoreFilter<BaseModelData> getFilter() {
return new StoreFilter<BaseModelData>(){
public boolean select(Store<BaseModelData> store,
BaseModelData parent, BaseModelData item,
String property) {
return "Good Company".equals(item.get(property));
}};
}

public GridExample() {
setLayout(new FitLayout());

List<ColumnConfig> configs = new ArrayList<ColumnConfig>();

ColumnConfig column = new ColumnConfig();
column.setId("name");
column.setHeader("Company");
column.setWidth(200);
configs.add(column);


final ListStore<BaseModelData> store = new ListStore<BaseModelData>();

ColumnModel cm = new ColumnModel(configs);

ContentPanel cp = new ContentPanel();
cp.setBodyBorder(false);
cp.setHeading("Basic Grid");
cp.setButtonAlign(HorizontalAlignment.CENTER);
cp.setLayout(new FitLayout());
cp.setSize(600, 300);

Grid<BaseModelData> grid = new Grid<BaseModelData>(store, cm);
grid.setStyleAttribute("borderTop", "none");
grid.setAutoExpandColumn("name");
grid.setBorders(true);
cp.add(grid);

add(cp);

store.add(getTestData("Good Company"));

store.addFilter(getFilter());
store.applyFilters("name");

addButton(new Button("click me to reproduce bug", new SelectionListener<ButtonEvent>(){
public void componentSelected(ButtonEvent ce) {
store.add(getTestData("Bad Company"));
}}));
}
}