Skip to content

Commit

Permalink
Enable Drag and Drop support for OutlineView
Browse files Browse the repository at this point in the history
  • Loading branch information
besi committed Oct 22, 2013
1 parent 5640af7 commit e8d2c25
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion OutlineView/OutlineView/AppDelegate.h
Expand Up @@ -7,7 +7,7 @@

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate, NSOutlineViewDelegate>
@interface AppDelegate : NSObject <NSApplicationDelegate, NSOutlineViewDelegate, NSOutlineViewDataSource>

@property (assign) IBOutlet NSWindow *window;

Expand Down
61 changes: 61 additions & 0 deletions OutlineView/OutlineView/AppDelegate.m
Expand Up @@ -21,12 +21,16 @@ @implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
self.outlineView.delegate = self;
self.outlineView.dataSource = self;
self.outlineView.floatsGroupRows = NO; // Prevent a sticky header
[self addData];

// Expand the first group and select the first item in the list
[self.outlineView expandItem:[self.outlineView itemAtRow:0]];
[self.outlineView selectRowIndexes:[NSIndexSet indexSetWithIndex:1] byExtendingSelection:NO];

// Enable Drag and Drop
[self.outlineView registerForDraggedTypes: [NSArray arrayWithObject: @"public.text"]];
}


Expand Down Expand Up @@ -88,4 +92,61 @@ - (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item{
return [self isHeader:item];
}


#pragma mark - Drag & Drop

- (id <NSPasteboardWriting>)outlineView:(NSOutlineView *)outlineView pasteboardWriterForItem:(id)item{
// No dragging if <some condition isn't met>
BOOL dragAllowed = YES;
if (!dragAllowed) {
return nil;
}

Book *book = (Book *)(((NSTreeNode *)item).representedObject);
NSString *identifier = book.title;

NSPasteboardItem *pboardItem = [[NSPasteboardItem alloc] init];
[pboardItem setString:identifier forType: @"public.text"];

return pboardItem;
}


- (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id < NSDraggingInfo >)info proposedItem:(id)targetItem proposedChildIndex:(NSInteger)index{

BOOL canDrag = index >= 0 && targetItem;

if (canDrag) {
return NSDragOperationMove;
}else {
return NSDragOperationNone;
}
}


- (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id < NSDraggingInfo >)info item:(id)targetItem childIndex:(NSInteger)index{

NSPasteboard *p = [info draggingPasteboard];
NSString *title = [p stringForType:@"public.text"];
NSTreeNode *sourceNode;

for(NSTreeNode *b in [targetItem childNodes]){
if ([[[b representedObject] title] isEqualToString:title]){
sourceNode = b;
}
}

if(!sourceNode){
// Not found
return NO;
}

NSUInteger indexArr[] = {0,index};
NSIndexPath *toIndexPATH =[NSIndexPath indexPathWithIndexes:indexArr length:2];

[self.booksController moveNode:sourceNode toIndexPath:toIndexPATH];

return YES;
}

@end
Binary file modified OutlineView/screenshot.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e8d2c25

Please sign in to comment.