2005-06-02 18:16:43 +00:00
|
|
|
|
|
|
|
#import "DNDArrayController.h"
|
|
|
|
|
2013-10-11 12:03:55 +00:00
|
|
|
#import "Logging.h"
|
|
|
|
|
2005-06-02 18:16:43 +00:00
|
|
|
@implementation DNDArrayController
|
|
|
|
|
|
|
|
NSString *MovedRowsType = @"MOVED_ROWS_TYPE";
|
2008-02-13 17:14:19 +00:00
|
|
|
NSString *CogUrlsPboardType = @"COG_URLS_TYPE";
|
2005-06-02 18:16:43 +00:00
|
|
|
|
2007-02-17 15:58:39 +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
|
2008-02-13 17:14:19 +00:00
|
|
|
[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
|
|
|
{
|
2013-10-11 12:03:55 +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;
|
|
|
|
|
2013-10-11 12:03:55 +00:00
|
|
|
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;
|
|
|
|
}
|
2007-03-09 01:16:06 +00:00
|
|
|
|
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)
|
|
|
|
{
|
2013-10-11 12:03:55 +00:00
|
|
|
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];
|
|
|
|
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"ACCEPTING DROP!");
|
2008-02-24 15:47:04 +00:00
|
|
|
return YES;
|
|
|
|
}
|
2005-06-02 18:16:43 +00:00
|
|
|
}
|
2013-10-11 12:03:55 +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
|
2008-02-10 19:35:58 +00:00
|
|
|
toIndex:(unsigned int)insertIndex
|
|
|
|
{
|
2008-02-24 15:47:04 +00:00
|
|
|
|
|
|
|
NSArray *objects = [self arrangedObjects];
|
2013-10-03 08:00:58 +00:00
|
|
|
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 = index + aboveInsertIndexCount;
|
|
|
|
aboveInsertIndexCount += 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
removeIndex = index;
|
|
|
|
insertIndex -= 1;
|
|
|
|
}
|
2008-02-10 19:35:58 +00:00
|
|
|
|
2008-02-24 15:47:04 +00:00
|
|
|
object = [objects objectAtIndex:removeIndex];
|
|
|
|
|
|
|
|
[object retain];
|
|
|
|
[self removeObjectAtArrangedObjectIndex:removeIndex];
|
|
|
|
[self insertObject:object atArrangedObjectIndex:insertIndex];
|
|
|
|
[object release];
|
2005-06-02 18:16:43 +00:00
|
|
|
|
|
|
|
index = [indexSet indexLessThanIndex:index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@end
|