From 418d04374ca2b195a69700c81511287f16b152ea Mon Sep 17 00:00:00 2001 From: Mitchell Livingston Date: Sat, 6 Dec 2008 20:41:18 +0000 Subject: [PATCH] simplify the code for moving rows in the groups table and fix a bug with disabled elements when adding a group when there are no previous groups --- macosx/GroupsController.h | 2 +- macosx/GroupsController.m | 29 ++++++----------------------- macosx/GroupsPrefsController.m | 23 ++++++++++++++++++----- 3 files changed, 25 insertions(+), 29 deletions(-) diff --git a/macosx/GroupsController.h b/macosx/GroupsController.h index 754513158..7d3a7cdff 100644 --- a/macosx/GroupsController.h +++ b/macosx/GroupsController.h @@ -53,7 +53,7 @@ - (void) addNewGroup; - (void) removeGroupWithRowIndex: (NSInteger) row; -- (NSIndexSet *) moveGroupsAtRowIndexes: (NSIndexSet *) indexes toRow: (NSInteger) newRow oldSelected: (NSIndexSet *) selectedIndexes; +- (void) moveGroupAtRow: (NSInteger) oldRow toRow: (NSInteger) newRow; - (NSMenu *) groupMenuWithTarget: (id) target action: (SEL) action isSmall: (BOOL) small; diff --git a/macosx/GroupsController.m b/macosx/GroupsController.m index ba820d020..8a9ceff30 100644 --- a/macosx/GroupsController.m +++ b/macosx/GroupsController.m @@ -243,39 +243,22 @@ GroupsController * fGroupsInstance = nil; [self saveGroups]; } -#warning rework for single row? -- (NSIndexSet *) moveGroupsAtRowIndexes: (NSIndexSet *) indexes toRow: (NSInteger) newRow oldSelected: (NSIndexSet *) selectedIndexes +- (void) moveGroupAtRow: (NSInteger) oldRow toRow: (NSInteger) newRow { - NSArray * selectedGroups = [fGroups objectsAtIndexes: selectedIndexes]; - - //determine where to move them - for (NSInteger i = [indexes firstIndex], startRow = newRow; i < startRow && i != NSNotFound; i = [indexes indexGreaterThanIndex: i]) + if (oldRow < newRow) newRow--; //remove objects to reinsert - NSArray * movingGroups = [[fGroups objectsAtIndexes: indexes] retain]; - [fGroups removeObjectsAtIndexes: indexes]; + id movingGroup = [[fGroups objectAtIndex: oldRow] retain]; + [fGroups removeObjectAtIndex: oldRow]; //insert objects at new location - for (NSInteger i = 0; i < [movingGroups count]; i++) - [fGroups insertObject: [movingGroups objectAtIndex: i] atIndex: newRow + i]; + [fGroups insertObject: movingGroup atIndex: newRow]; - [movingGroups release]; + [movingGroup release]; [self saveGroups]; [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateGroups" object: self]; - - NSMutableIndexSet * newSelectedIndexes = nil; - if ([selectedGroups count] > 0) - { - newSelectedIndexes = [NSMutableIndexSet indexSet]; - NSEnumerator * enumerator = [selectedGroups objectEnumerator]; - NSDictionary * dict; - while ((dict = [enumerator nextObject])) - [newSelectedIndexes addIndex: [fGroups indexOfObject: dict]]; - } - - return newSelectedIndexes; } - (NSMenu *) groupMenuWithTarget: (id) target action: (SEL) action isSmall: (BOOL) small diff --git a/macosx/GroupsPrefsController.m b/macosx/GroupsPrefsController.m index 29d6256d1..1fe00cb36 100644 --- a/macosx/GroupsPrefsController.m +++ b/macosx/GroupsPrefsController.m @@ -125,11 +125,23 @@ NSPasteboard * pasteboard = [info draggingPasteboard]; if ([[pasteboard types] containsObject: GROUP_TABLE_VIEW_DATA_TYPE]) { - NSIndexSet * indexes = [NSKeyedUnarchiver unarchiveObjectWithData: [pasteboard dataForType: GROUP_TABLE_VIEW_DATA_TYPE]], - * selectedIndexes = [[GroupsController groups] moveGroupsAtRowIndexes: indexes toRow: newRow - oldSelected: [fTableView selectedRowIndexes]]; + NSIndexSet * indexes = [NSKeyedUnarchiver unarchiveObjectWithData: [pasteboard dataForType: GROUP_TABLE_VIEW_DATA_TYPE]]; + NSInteger oldRow = [indexes firstIndex], selectedRow = [fTableView selectedRow]; - [fTableView selectRowIndexes: selectedIndexes byExtendingSelection: NO]; + [[GroupsController groups] moveGroupAtRow: oldRow toRow: newRow]; + + if (oldRow < newRow) + newRow--; + + if (selectedRow == oldRow) + selectedRow = newRow; + else if (selectedRow > oldRow && selectedRow <= newRow) + selectedRow--; + else if (selectedRow < oldRow && selectedRow >= newRow) + selectedRow++; + else; + + [fTableView selectRow: selectedRow byExtendingSelection: NO]; [fTableView reloadData]; } @@ -238,7 +250,8 @@ [fSelectedColorNameField setStringValue: [[GroupsController groups] nameForIndex: index]]; [fSelectedColorNameField setEnabled: YES]; [fCustomLocationEnableCheck setState: [[GroupsController groups] usesCustomDownloadLocationForIndex: index]]; - [fCustomLocationPopUp setEnabled: ([fCustomLocationEnableCheck state] == NSOnState)]; + [fCustomLocationEnableCheck setEnabled: YES]; + [fCustomLocationPopUp setEnabled: [fCustomLocationEnableCheck state] == NSOnState]; if ([[GroupsController groups] customDownloadLocationForIndex: index]) { NSString * location = [[GroupsController groups] customDownloadLocationForIndex: index]; -- 2.40.0