From 4ff89eab04555f67e9ed96f4d42aa41b221fdeb1 Mon Sep 17 00:00:00 2001 From: Alexander Ljungberg Date: Sat, 13 Feb 2010 23:14:02 -0300 Subject: [PATCH] Fixed: CPArrayController would throw out of bounds exceptions when its content array was shortened and items at the end were selected. Extended avoid empty selection support so that when the last item is selected and deleted, the (previously) second to last item is selected instead. Works as expected with multiple selections. --- AppKit/CPArrayController.j | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/AppKit/CPArrayController.j b/AppKit/CPArrayController.j index 5b7db2a68..236502f9e 100644 --- a/AppKit/CPArrayController.j +++ b/AppKit/CPArrayController.j @@ -159,6 +159,8 @@ if ([self preservesSelection]) oldSelection = [self selectedObjects]; + // Avoid out of bounds selections. + _selectionIndexes = [CPIndexSet indexSet]; //FIXME: copy? [super setContent:value]; @@ -279,10 +281,20 @@ if ([_selectionIndexes isEqualToIndexSet:indexes]) return NO; - if(![indexes count] && _avoidsEmptySelection && [[self arrangedObjects] count]) - indexes = [CPIndexSet indexSetWithIndex:0]; - - [indexes removeIndexesInRange:CPMakeRange([[self arrangedObjects] count]+1, CPNotFound)]; + if (![indexes count]) + { + if(_avoidsEmptySelection && [[self arrangedObjects] count]) + indexes = [CPIndexSet indexSetWithIndex:0]; + } + else + { + var objectsCount = [[self arrangedObjects] count]; + // Remove out of bounds indexes. + [indexes removeIndexesInRange:CPMakeRange(objectsCount, [indexes lastIndex])]; + // When avoiding empty selection and the deleted selection was at the bottom, select the last item. + if(![indexes count] && _avoidsEmptySelection && objectsCount) + indexes = [CPIndexSet indexSetWithIndex:objectsCount-1]; + } [self willChangeValueForKey:@"selectionIndexes"]; [self _selectionWillChange];