Ignore:
Timestamp:
Dec 17, 2007, 9:11:53 PM (15 years ago)
Author:
livings124
Message:

allow Groups table to be reordered

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/macosx/GroupsWindowController.m

    r4189 r4190  
    2929#import "NSApplicationAdditions.h"
    3030
     31#define GROUP_TABLE_VIEW_DATA_TYPE @"GroupTableViewDataType"
     32
    3133typedef enum
    3234{
     
    113115    [cell release];
    114116   
     117    [fTableView registerForDraggedTypes: [NSArray arrayWithObject: GROUP_TABLE_VIEW_DATA_TYPE]];
     118   
    115119    if ([NSApp isOnLeopardOrBetter])
    116120        [[self window] setContentBorderThickness: [[fTableView enclosingScrollView] frame].origin.y forEdge: NSMinYEdge];
     
    158162- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) tableColumn row: (NSInteger) row
    159163{
    160     #warning consider color column ?
    161164    NSString * identifier = [tableColumn identifier];
    162165    if ([identifier isEqualToString: @"Color"])
     
    194197{
    195198    [fAddRemoveControl setEnabled: [fTableView numberOfSelectedRows] > 0 forSegment: REMOVE_TAG];
     199}
     200
     201- (BOOL) tableView: (NSTableView *) tableView writeRowsWithIndexes: (NSIndexSet *) rowIndexes toPasteboard: (NSPasteboard *) pboard
     202{
     203    [pboard declareTypes: [NSArray arrayWithObject: GROUP_TABLE_VIEW_DATA_TYPE] owner: self];
     204    [pboard setData: [NSKeyedArchiver archivedDataWithRootObject: rowIndexes] forType: GROUP_TABLE_VIEW_DATA_TYPE];
     205    return YES;
     206}
     207
     208- (NSDragOperation) tableView: (NSTableView *) tableView validateDrop: (id <NSDraggingInfo>) info
     209    proposedRow: (int) row proposedDropOperation: (NSTableViewDropOperation) operation
     210{
     211    NSPasteboard * pasteboard = [info draggingPasteboard];
     212    if ([[pasteboard types] containsObject: GROUP_TABLE_VIEW_DATA_TYPE])
     213    {
     214        [fTableView setDropRow: row dropOperation: NSTableViewDropAbove];
     215        return NSDragOperationGeneric;
     216    }
     217   
     218    return NSDragOperationNone;
     219}
     220
     221- (BOOL) tableView: (NSTableView *) t acceptDrop: (id <NSDraggingInfo>) info
     222    row: (int) newRow dropOperation: (NSTableViewDropOperation) operation
     223{
     224    NSPasteboard * pasteboard = [info draggingPasteboard];
     225    if ([[pasteboard types] containsObject: GROUP_TABLE_VIEW_DATA_TYPE])
     226    {
     227        NSIndexSet * indexes = [NSKeyedUnarchiver unarchiveObjectWithData: [pasteboard dataForType: GROUP_TABLE_VIEW_DATA_TYPE]];
     228       
     229        NSArray * selectedGroups = [fGroups objectsAtIndexes: [fTableView selectedRowIndexes]];
     230       
     231        //determine where to move them
     232        int i;
     233        for (i = [indexes firstIndex]; i < newRow && i != NSNotFound; i = [indexes indexGreaterThanIndex: i])
     234            newRow--;
     235       
     236        //remove objects to reinsert
     237        NSArray * movingGroups = [[fGroups objectsAtIndexes: indexes] retain];
     238        [fGroups removeObjectsAtIndexes: indexes];
     239       
     240        //insert objects at new location
     241        for (i = 0; i < [movingGroups count]; i++)
     242            [fGroups insertObject: [movingGroups objectAtIndex: i] atIndex: newRow + i];
     243       
     244        [movingGroups release];
     245       
     246        if ([selectedGroups count] > 0)
     247        {
     248            NSEnumerator * enumerator = [selectedGroups objectEnumerator];
     249            NSMutableIndexSet * indexSet = [[NSMutableIndexSet alloc] init];
     250            NSDictionary * dict;
     251            while ((dict = [enumerator nextObject]))
     252                [indexSet addIndex: [fGroups indexOfObject: dict]];
     253           
     254            [fTableView selectRowIndexes: indexSet byExtendingSelection: NO];
     255            [indexSet release];
     256        }
     257       
     258        [fTableView reloadData];
     259       
     260        [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateUI" object: self];
     261    }
     262   
     263    return YES;
    196264}
    197265
Note: See TracChangeset for help on using the changeset viewer.