-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ticky_GenericTableDelegate.m
143 lines (113 loc) · 4.44 KB
/
Ticky_GenericTableDelegate.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//
// Ticky_GenericTableDelegate.m
// Ticky
//
// Created by Thomas PELLETIER on 26/05/10.
// Copyright 2010 Thomas PELLETIER. All rights reserved.
//
#import "Ticky_GenericTableDelegate.h"
@implementation Ticky_GenericTableDelegate
@synthesize tableView;
@synthesize tasksController;
@synthesize otherTasksController;
/*
* Initiate a drag from the table
*/
- (BOOL)tableView:(NSTableView*)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard {
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];
[pboard declareTypes:[NSArray arrayWithObject:PrivateTableViewDataType] owner:self];
[pboard setData:data forType:PrivateTableViewDataType];
return YES;
}
/*
* Validate a drop operation
*/
- (NSDragOperation)tableView:(NSTableView*)tv validateDrop:(id <NSDraggingInfo>)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)op {
NSArray *tvlist = [[NSApp delegate] tableViews];
if([tvlist containsObject:[info draggingSource]]) {
if(op == NSTableViewDropOn) {
[tv setDropRow:row dropOperation:NSTableViewDropAbove];
}
return NSDragOperationMove;
}
else {
return NSDragOperationNone;
}
}
/*
* Accept drops
*/
- (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id <NSDraggingInfo>)info row:(int)row dropOperation:(NSTableViewDropOperation)operation
{ // Note for dummies: the <row> argument is the position of the drop row!
/* Extract data */
NSPasteboard *pasteboard = [info draggingPasteboard];
NSData *rowData = [pasteboard dataForType:PrivateTableViewDataType];
NSIndexSet *rowIndexes = [NSKeyedUnarchiver unarchiveObjectWithData:rowData];
/* Make a copy of the actual tasks array */
NSMutableArray *tasksArrayCopy = [NSMutableArray arrayWithArray:[tasksController arrangedObjects]];
if ([info draggingSource] == tableView) {
/* Construct an array containg the moved rows */
NSMutableArray *draggedItemsArray = [NSMutableArray arrayWithCapacity:[rowIndexes count]];
NSRange range = NSMakeRange( 0, [rowIndexes lastIndex] + 1 );
NSUInteger currentItemIndex = 0;
while([rowIndexes getIndexes:¤tItemIndex maxCount:1 inIndexRange:&range] > 0)
{ /* For each moved index */
NSManagedObject *thisItem = [tasksArrayCopy objectAtIndex:currentItemIndex];
[draggedItemsArray addObject:thisItem];
[tasksArrayCopy removeObjectAtIndex:currentItemIndex];
/* Let's put a NULL placeholder here */
[tasksArrayCopy insertObject:[NSNull null] atIndex:currentItemIndex];
}
/* For each moved task */
for (id object in draggedItemsArray) {
[tasksArrayCopy insertObject:object atIndex:row];
}
/* Remove NULL placeholders */
[tasksArrayCopy removeObject:[NSNull null]];
/* Re number */
int num = 0;
for (id object in tasksArrayCopy) {
[object setValue:[NSNumber numberWithInt:num] forKey:@"Order"];
num += 1;
}
/* Done */
return YES;
}
/* This drop comes from the other table view */
else {
/* First, make a copy of the source tasks list */
NSMutableArray *sourceTasksListCopy = [NSMutableArray arrayWithArray:[otherTasksController arrangedObjects]];
/* Construct an array containg the moved rows */
NSMutableArray *draggedItemsArray = [NSMutableArray arrayWithCapacity:[rowIndexes count]];
NSRange range = NSMakeRange( 0, [rowIndexes lastIndex] + 1 );
NSUInteger currentItemIndex = 0;
while([rowIndexes getIndexes:¤tItemIndex maxCount:1 inIndexRange:&range] > 0)
{ /* For each moved index */
NSManagedObject *thisItem = [sourceTasksListCopy objectAtIndex:currentItemIndex];
int newDoneStatus = 0;
if ([[thisItem valueForKey:@"Done"] isEqualToNumber:[NSNumber numberWithInt:0]]) { newDoneStatus = 1; }
[sourceTasksListCopy removeObjectAtIndex:currentItemIndex];
NSLog(@"%@ switched from %d to %d", [thisItem valueForKey:@"Content"], [thisItem valueForKey:@"Done"], newDoneStatus);
[thisItem setValue:[NSNumber numberWithInt:newDoneStatus] forKey:@"Done"];
[draggedItemsArray addObject:thisItem];
}
/* For each moved task */
for (id object in draggedItemsArray) {
[tasksArrayCopy insertObject:object atIndex:row];
}
/* Re number target list */
int num = 0;
for (id object in sourceTasksListCopy) {
[object setValue:[NSNumber numberWithInt:num] forKey:@"Order"];
num += 1;
}
/* Re number source list */
num = 0;
for (id object in tasksArrayCopy) {
[object setValue:[NSNumber numberWithInt:num] forKey:@"Order"];
num += 1;
}
return YES;
}
}
@end