Permalink
Browse files

Allow dragging of accounts between groups in roster view

  • Loading branch information...
1 parent 01f5a4c commit e2b9939c50ed45e6998b7d8409bb4680a4100336 @benlangfeld committed Nov 8, 2010
Showing with 65 additions and 0 deletions.
  1. +65 −0 Controllers/AccountsController.j
View
65 Controllers/AccountsController.j
@@ -29,12 +29,15 @@
var SharedController = nil;
+RosterViewDragType = @"RosterViewDragType";
+
@implementation AccountsController : CPViewController
{
CPArray accounts @accessors;
CPDictionary chatWindows;
@outlet CPScrollView scrollView @accessors;
@outlet CPOutlineView rosterView @accessors;
+ CPArray draggedItems;
}
+ (AccountsController)sharedController
@@ -199,6 +202,8 @@ var SharedController = nil;
[rosterView setDelegate:self];
[rosterView setTarget:self];
+ [rosterView registerForDraggedTypes:[RosterViewDragType]];
+
[rosterView setDoubleAction:@selector(rosterDidReceiveDoubleClick:)];
[rosterView setDataSource:self];
@@ -458,4 +463,64 @@ var SharedController = nil;
return textDataView;
}
+- (BOOL)outlineView:(CPOutlineView)anOutlineView writeItems:(CPArray)theItems toPasteboard:(CPPasteBoard)thePasteBoard
+{
+ for (var i = 0; i < [theItems count]; i++)
+ {
+ if (![[theItems objectAtIndex:i] isKindOfClass:[TNStropheContact class]])
+ return NO;
+ }
+ draggedItems = theItems;
+ [thePasteBoard declareTypes:[RosterViewDragType] owner:self];
+ [thePasteBoard setData:[CPKeyedArchiver archivedDataWithRootObject:theItems] forType:RosterViewDragType];
+
+ return YES;
+}
+
+- (CPDragOperation)outlineView:(CPOutlineView)anOutlineView validateDrop:(id < CPDraggingInfo >)theInfo proposedItem:(id)theItem proposedChildIndex:(int)theIndex
+{
+ if (!theItem)
+ [anOutlineView setDropItem:nil dropChildIndex:theIndex];
+
+ if (![theItem isKindOfClass:[TNStropheGroup class]])
+ return CPDragOperationNone;
+
+ // Check we're not trying to move stuff between accounts
+ var targetAccount = [anOutlineView parentForItem:theItem],
+ betweenAccounts = NO;
+
+ for (var i = 0; i < [draggedItems count]; i++)
+ {
+ if (targetAccount != [anOutlineView parentForItem:[rosterView parentForItem:[draggedItems objectAtIndex:i]]])
+ betweenAccounts = YES;
+ }
+
+ if (betweenAccounts)
+ return CPDragOperationNone;
+
+ [anOutlineView setDropItem:theItem dropChildIndex:theIndex];
+
+ return CPDragOperationMove;
+}
+
+- (BOOL)outlineView:(CPOutlineView)outlineView acceptDrop:(id < CPDraggingInfo >)theInfo item:(id)theItem childIndex:(int)theIndex
+{
+ if (theItem === nil)
+ theItem = accounts;
+
+ var contactIndex = [draggedItems count];
+ while (contactIndex--)
+ {
+ var contact = [draggedItems objectAtIndex:contactIndex];
+
+ if (contact === theItem)
+ continue;
+
+ [[[outlineView parentForItem:[rosterView parentForItem:contact]] roster] changeGroup:theItem ofContact:contact];
+ theIndex += 1;
+ }
+
+ return YES;
+}
+
@end

0 comments on commit e2b9939

Please sign in to comment.