cog/Playlist/DNDArrayController.m

118 lines
2.9 KiB
Matlab
Raw Normal View History

2005-06-02 18:16:43 +00:00
#import "DNDArrayController.h"
#import "Logging.h"
2005-06-02 18:16:43 +00:00
@implementation DNDArrayController
NSString *MovedRowsType = @"MOVED_ROWS_TYPE";
NSString *CogUrlsPboardType = @"COG_URLS_TYPE";
2005-06-02 18:16:43 +00:00
// @"CorePasteboardFlavorType 0x6974756E" is the "itun" type representing an iTunes plist
NSString *iTunesDropType = @"CorePasteboardFlavorType 0x6974756E";
2005-06-02 18:16:43 +00:00
- (void)awakeFromNib
{
// register for drag and drop
[tableView registerForDraggedTypes:[NSArray arrayWithObjects:MovedRowsType, CogUrlsPboardType, NSFilenamesPboardType, iTunesDropType, nil]];
2005-06-02 18:16:43 +00:00
}
2008-02-24 15:47:04 +00:00
- (BOOL)tableView:(NSTableView *)aTableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)pboard
2005-06-02 18:16:43 +00:00
{
DLog(@"INDEX SET ON DRAG: %@", rowIndexes);
2008-02-24 15:47:04 +00:00
NSData *data = [NSArchiver archivedDataWithRootObject:rowIndexes];
2005-06-02 18:16:43 +00:00
[pboard declareTypes: [NSArray arrayWithObjects:MovedRowsType, nil] owner:self];
2008-02-24 15:47:04 +00:00
[pboard setData:data forType: MovedRowsType];
2005-06-02 18:16:43 +00:00
return YES;
}
- (NSDragOperation)tableView:(NSTableView*)tv
validateDrop:(id <NSDraggingInfo>)info
proposedRow:(int)row
proposedDropOperation:(NSTableViewDropOperation)op
{
NSDragOperation dragOp = NSDragOperationCopy;
if ([info draggingSource] == tv)
dragOp = NSDragOperationMove;
DLog(@"VALIDATING DROP!");
2005-06-02 18:16:43 +00:00
// we want to put the object at, not over,
// the current row (contrast NSTableViewDropOn)
[tv setDropRow:row dropOperation:NSTableViewDropAbove];
return dragOp;
}
- (BOOL)tableView:(NSTableView*)tv
acceptDrop:(id <NSDraggingInfo>)info
row:(int)row
dropOperation:(NSTableViewDropOperation)op
{
if (row < 0)
{
row = 0;
}
2005-06-02 18:16:43 +00:00
// if drag source is self, it's a move
if ([info draggingSource] == tableView)
{
2008-02-24 15:47:04 +00:00
NSIndexSet *indexSet = [NSUnarchiver unarchiveObjectWithData:[[info draggingPasteboard] dataForType:MovedRowsType]];
if (indexSet)
{
DLog(@"INDEX SET ON DROP: %@", indexSet);
2008-02-24 15:47:04 +00:00
NSArray *selected = [[self arrangedObjects] objectsAtIndexes:indexSet];
[self moveObjectsInArrangedObjectsFromIndexes:indexSet toIndex:row];
[self setSelectedObjects:selected];
DLog(@"ACCEPTING DROP!");
2008-02-24 15:47:04 +00:00
return YES;
}
2005-06-02 18:16:43 +00:00
}
DLog(@"REJECTING DROP!");
2005-06-02 18:16:43 +00:00
return NO;
}
2008-02-24 15:47:04 +00:00
-(void) moveObjectsInArrangedObjectsFromIndexes:(NSIndexSet*)indexSet
toIndex:(unsigned int)insertIndex
{
2008-02-24 15:47:04 +00:00
NSArray *objects = [self arrangedObjects];
NSUInteger index = [indexSet lastIndex];
2008-02-24 15:47:04 +00:00
2005-06-02 18:16:43 +00:00
int aboveInsertIndexCount = 0;
2008-02-24 15:47:04 +00:00
id object;
2005-06-02 18:16:43 +00:00
int removeIndex;
while (NSNotFound != index)
{
if (index >= insertIndex) {
removeIndex = (int)(index + aboveInsertIndexCount);
2005-06-02 18:16:43 +00:00
aboveInsertIndexCount += 1;
}
else
{
removeIndex = (int)index;
2005-06-02 18:16:43 +00:00
insertIndex -= 1;
}
2008-02-24 15:47:04 +00:00
object = [objects objectAtIndex:removeIndex];
[self removeObjectAtArrangedObjectIndex:removeIndex];
[self insertObject:object atArrangedObjectIndex:insertIndex];
2005-06-02 18:16:43 +00:00
index = [indexSet indexLessThanIndex:index];
}
}
@end