Thursday, October 6, 2011

ListCollectionViewCursor and 0-length lists

There's an ugly bug in ListCollectionView's cursor class (ListCollectionViewCursor). To see it, do the following:
  1. Start with a List whose dataProvider is a ListCollectionView around the array [ "a", "b", "c", "d", "e", "f" ].
  2. Click "c" in the list.  yourList.selectedItem is now "c"
  3. Do something that filters "c" out of the dataProvider but keeps at least 1 item.  yourList.selectedItem is now null
  4. Reset the filter
  5. Click "c" again.  yourList.selectedItem is now "c"
  6. Do something that filters the dataProvider down to 0 length
  7. yourList.selectedItem is still "c"

#3 is the correct behavior - you should not be allowed to have selectedItem be something that's not included in the dataProvider. #6 is a bug.



So what's going on here?

ListCollectionView creates a cursor of type ListCollectionViewCursor (a private inner class in LisytCollectionView). Its seek() method does something like this:

if (view.length == 0)
{
  clear current
  return;
}
else
{
  try to seek to the provided bookmark item
  if it's not found, throw a CursorError
}

ListBase catches the CursorError and clears selectedItem. So in the 0-length case, no error thrown means no clearing selectedItem.

The ideal solution here would have be to subclass ListCollectionViewCursor, overriding seek(), and then come up with a way to tell ListCollectionView to always use your cursor class. But ListCollectionView doesn't let you tell it what cursor class to use.

So the next ideal solution would be to subclass ListCollectionViewCursor, overriding seek(), and then have your own ListCollectionView subclass that uses it. But ListCollectionViewCursor is a private inner class of ListCollectionView, so it's not overridable.

So the only solution for now is to copy ListCollectionViewCursor to your own MyListCollectionViewCursor, and then have your own MyListCollectionView that uses it. Annoying, but effective. The down side is that if Adobe ever fixes ListCollectionViewCursor (or changes it), you don't benefit.

Bottom line lesson for the nice people at Adobe: don't use private inner classes in libraries.

No comments: