PDA

View Full Version : TreeStore filtering & IndexOutOfBoundsException



meliniak
9 Mar 2011, 1:32 AM
I'm using a TreeStore. When calling TreeStore.add(ModelData, boolean) and there are filters applied to the store, I keep getting following exception:


java.lang.IndexOutOfBoundsException: Index: 6, Size: 6
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at com.extjs.gxt.ui.client.store.TreeStore.getChild(TreeStore.java:249)
at com.extjs.gxt.ui.client.widget.treegrid.TreeGrid.onAdd(TreeGrid.java:696)
at com.extjs.gxt.ui.client.widget.treegrid.TreeGrid$2.storeAdd(TreeGrid.java:152)
at com.extjs.gxt.ui.client.store.StoreListener.handleEvent(StoreListener.java:22)
at com.extjs.gxt.ui.client.store.StoreListener.handleEvent(StoreListener.java:1)
at com.extjs.gxt.ui.client.event.BaseObservable.callListener(BaseObservable.java:178)
at com.extjs.gxt.ui.client.event.BaseObservable.fireEvent(BaseObservable.java:86)
at com.extjs.gxt.ui.client.store.TreeStore.doInsert(TreeStore.java:922)
at com.extjs.gxt.ui.client.store.TreeStore.insert(TreeStore.java:519)
at com.extjs.gxt.ui.client.store.TreeStore.add(TreeStore.java:174)
at com.extjs.gxt.ui.client.store.TreeStore.add(TreeStore.java:185)
at com.mycrmlite.gui.client.impl.display.ShareDisplayImpl.addContact(ShareDisplayImpl.java:951)

Obviously, index and size values depend on the tree size and the number of elements filtered out.

I guess it's possibly a bug. There are very similar bugs already posted on the forum and supposedely already fixed. Any hints on this?

version: 2.2.1

tp_blue
1 Jun 2011, 3:18 AM
Hi,

I came along the same problem. In TreeGrid.onAdd the getChild method of TreeStore is called with the index taken from the store.Add event (index without filtering). getChild looks into the filtered children collection and the exception occurs. I fixed this by overriding method in TreeStore which is not nice because i have to copy private methods from the base class, but it seems to work for me. Here is the modified store class:




public class CustomTreeStore<M extends ModelData> extends TreeStore<M> {

public CustomTreeStore(final BaseTreeLoader<M> loader) {
super(loader);
}

public CustomTreeStore() {
super();
}

@Override
public M getChild(final int index) {
return safeGetChild(this.rootWrapper, index);
}

@Override
public M getChild(final M parent, final int index) {
if (parent == null) {
return getChild(index);
}
final TreeModel p = findWrapper(parent);
if (p != null) {
return safeGetChild(p, index);
}
return null;
}

private M safeGetChild(final TreeModel parent, final int index) {
final List<M> filteredChildren = getFilteredChildren(parent);

if (index < filteredChildren.size()) {
return filteredChildren.get(index);
}
return null;
}

private List<M> getFilteredChildren(final TreeModel wrap) {
final List<TreeModel> filtered = new ArrayList<TreeModel>();
for (final ModelData child : wrap.getChildren()) {
if (!isFiltered((TreeModel) child)) {
filtered.add((TreeModel) child);
}
}
return unwrap(filtered);
}

private boolean isFiltered(final TreeModel wrap) {
return "true".equals(wrap.get("filtered"));
}
}



Cheers,
Alex