-
Notifications
You must be signed in to change notification settings - Fork 11
/
TSCheckedListDataSource.m
117 lines (96 loc) · 4 KB
/
TSCheckedListDataSource.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
//
// TSCheckedListDataSource.m
// ThisService
//
// Created by Jesper on 2011-07-14.
// Copyright 2011-2012 waffle software. All rights reserved.
// BSD licensed - see license.txt for more information.
//
#import "TSCheckedListDataSource.h"
@implementation TSCheckedListDataSource
+ (NSIndexSet *)indexSetOfSelectedIndexesForRowData:(NSArray *)rowData selectedValues:(NSSet *)values {
NSMutableIndexSet *selectedIndexSet = [NSMutableIndexSet indexSet];
if ([values count] == 0) return selectedIndexSet;
NSUInteger i = 0;
for (NSDictionary *row in rowData) {
if ([values containsObject:[row objectForKey:@"value"]]) {
[selectedIndexSet addIndex:i];
}
i++;
}
return selectedIndexSet;
}
- (id)initWithRowData:(NSArray *)rowData_ selectedChoices:(NSIndexSet *)selectedIndexes_ labelKey:(NSString *)labelKey_ delegate:(id<TSCheckedListDataSourceDelegate>)aDelegate {
self = [super init];
if (self != nil) {
rowData = [rowData_ retain];
NSUInteger indexGreaterThanMaxValidIndex = (selectedIndexes_ == nil ? NSNotFound : [selectedIndexes_ indexGreaterThanIndex:(NSUInteger)[rowData_ count]-1]);
if (indexGreaterThanMaxValidIndex != NSNotFound) {
[NSException raise:@"Can't construct checked list data source; selected choices extend beyond choices" format:@""];
}
selectedIndexes = selectedIndexes_ == nil ? [[NSMutableIndexSet alloc] init] : [selectedIndexes_ mutableCopy];
delegate = aDelegate;
labelKey = [labelKey_ retain];
}
return self;
}
- (BOOL)anyChoicesSelected {
return [selectedIndexes count] > 0;
}
- (NSArray *)selectedChoiceValues {
return [[self selectedChoices] valueForKey:@"value"];
}
- (NSArray *)selectedChoices {
return [rowData objectsAtIndexes:selectedIndexes];
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return [rowData count];
}
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
NSUInteger idxOfColumn = [[tableView tableColumns] indexOfObject:tableColumn];
if (idxOfColumn == 0) {
return ([NSNumber numberWithInt:[selectedIndexes containsIndex:row] ? NSOnState : NSOffState]);
} else {
NSDictionary *columns = (NSDictionary *)[rowData objectAtIndex:row];
NSString *columnValue = (NSString *)[columns objectForKey:[tableColumn identifier]];
return columnValue == nil ? @"" : columnValue;
}
}
- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
NSUInteger idxOfColumn = [[tableView tableColumns] indexOfObject:tableColumn];
if (idxOfColumn != 0) return;
NSCellStateValue state = (NSCellStateValue)[(NSNumber *)object intValue];
if (state == NSOnState) {
[selectedIndexes addIndex:(NSUInteger)row];
} else if (state == NSOffState) {
[selectedIndexes removeIndex:(NSUInteger)row];
}
[delegate dataSource:self checkedChoicesChanged:[self selectedChoices]];
}
- (void)tableView:(NSTableView *)tableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
if (tableColumn == nil) return;
NSUInteger idxOfColumn = [[tableView tableColumns] indexOfObject:tableColumn];
if (idxOfColumn == 0) {
NSString *label = (NSString *)[(NSDictionary *)[rowData objectAtIndex:row] objectForKey:labelKey];
NSButtonCell *checkBox = (NSButtonCell *)aCell;
[checkBox setTitle:label];
[checkBox setButtonType:NSSwitchButton];
[checkBox setState:([selectedIndexes containsIndex:row] ? NSOnState : NSOffState)];
} else {
NSDictionary *columns = (NSDictionary *)[rowData objectAtIndex:row];
NSString *columnValue = (NSString *)[columns objectForKey:[tableColumn identifier]];
columnValue = (columnValue == nil ? @"" : columnValue);
NSTextFieldCell *textCell = (NSTextFieldCell *)aCell;
[textCell setStringValue:columnValue];
[textCell setTextColor:[NSColor disabledControlTextColor]];
[textCell setBordered:NO];
[textCell setEditable:NO];
}
}
- (void)dealloc {
[rowData release];
[selectedIndexes release];
[labelKey release];
[super dealloc];
}
@end