-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathTNTableViewDataSource.j
363 lines (285 loc) · 9.21 KB
/
TNTableViewDataSource.j
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
* TNTableViewDataSource.j
*
* Copyright (C) 2010 Antoine Mercadal <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import <Foundation/Foundation.j>
@import <AppKit/CPSearchField.j>
@import <AppKit/CPTableView.j>
/*! @ingroup tnkit
Simple table view datasource with filtering support
Exemple of uses
var colA = [[CPTableColumn alloc] initWithIdentifier:@"keypath1"],
colB = [[CPTableColumn alloc] initWithIdentifier:@"keypath2"];
[aTableView addTableColumn:colA];
[aTableView addTableColumn:colB];
...
datasource = [[TNTableViewDataSource alloc] init];
[datasource setTable:aTableView];
[datasource setSearchableKeyPaths:[@"keypath1"]]; // only key path1 is searchable
[aTableView setDataSource:datasource];
...
// bind a CPSearchField to the datasource
[aSearchField setTarget:datasource];
[aSearchField setAction:@selector(filterObjects:)];
...
[datasource addObject:anObject];
[aTableView reloadData];
Not the anObject must have keypath1, keypath2 if you want to be able to search and display
*/
@implementation TNTableViewDataSource: CPObject
{
CPArray _content @accessors(property=content);
CPArray _searchableKeyPaths @accessors(property=searchableKeyPaths);
CPTableView _table @accessors(property=table);
CPPredicate _displayFilter @accessors(property=displayFilter);
CPArray _filteredContent;
CPSearchField _searchField;
CPString _filter;
BOOL _needsFilter;
}
#pragma mark -
#pragma mark Initialization
- (id)init
{
if (self = [super init])
{
_content = [CPArray array];
_filteredContent = [CPArray array];
_searchableKeyPaths = [CPArray array];
_filter = @"";
_needsFilter = NO;
}
return self;
}
#pragma mark -
#pragma mark Filtering
/*! this action should be bound to a CPSearchField
it will filter the content of the datasource according to the sender value
@param sender the sender of the action
*/
- (IBAction)filterObjects:(id)sender
{
if (!_searchField)
_searchField = sender;
_filteredContent = [CPArray array];
_filter = [[sender stringValue] uppercaseString];
if (!(_filter) || (_filter == @""))
{
_filteredContent = _displayFilter ? [[_content copy] filteredArrayUsingPredicate:_displayFilter] : [_content copy];
[_table reloadData];
return;
}
for (var i = 0; i < [_content count]; i++)
{
var entry = [_content objectAtIndex:i];
for (var j = 0; j < [_searchableKeyPaths count]; j++)
{
var entryValue = [entry valueForKeyPath:[_searchableKeyPaths objectAtIndex:j]];
if ([entryValue uppercaseString].indexOf(_filter) != -1)
{
if (![_filteredContent containsObject:entry])
if (!_displayFilter || [_displayFilter evaluateWithObject:entry])
[_filteredContent addObject:entry];
}
}
}
[_table reloadData];
}
#pragma mark -
#pragma mark Content management
/*! set the given array of object as the content of the datasource
@param aContent CPArray containing the datas of the datasource
*/
- (void)setContent:(CPArray)aContent
{
_content = aContent;
_filteredContent = _displayFilter ? [[_content copy] filteredArrayUsingPredicate:_displayFilter] : [_content copy];
_needsFilter = YES;
}
/*! add an object to the datasource
@param anObject object to add
*/
- (void)addObject:(id)anObject
{
[_content addObject:anObject];
if (!_displayFilter || [_displayFilter evaluateWithObject:anObject])
[_filteredContent addObject:anObject];
_needsFilter = YES;
}
/*! Chek if contents contains given object
@param anObject the object to search
@return YES if anObject is in the contents
*/
- (void)containsObject:(id)anObject
{
return [_filteredContent containsObject:anObject];
}
/*! insert an object at a given index in the datasource
@param anObject object to add
@param anObject int representing the position
*/
- (void)insertObject:(id)anObject atIndex:(int)anIndex
{
[_content insertObject:anObject atIndex:anIndex];
if (!_displayFilter || [_displayFilter evaluateWithObject:anObject])
[_filteredContent insertObject:anObject atIndex:anIndex];
_needsFilter = YES;
}
/*! return the object at given index
@param anObject int representing the position
@return the object
*/
- (void)objectAtIndex:(int)index
{
return [_filteredContent objectAtIndex:index];
}
/*! return the objects at given indexes contained in a CPIndexSet
@param anObject CPIndexSet representing the positions
@return the objects
*/
- (CPArray)objectsAtIndexes:(CPIndexSet)aSet
{
return [_filteredContent objectsAtIndexes:aSet];
}
/*! removes the object at given index
@param anObject int representing the position
*/
- (void)removeObjectAtIndex:(int)index
{
var object = [_filteredContent objectAtIndex:index];
[_filteredContent removeObjectAtIndex:index];
[_content removeObject:object];
_needsFilter = YES;
}
/*! removes the object at given indexes contained in a CPIndexSet
@param anObject CPIndexSet representing the positions
*/
- (void)removeObjectsAtIndexes:(CPIndexSet)aSet
{
try
{
var objects = [_filteredContent objectsAtIndexes:aSet];
[_filteredContent removeObjectsAtIndexes:aSet];
[_content removeObjectsInArray:objects];
_needsFilter = YES;
}
catch(e)
{
CPLog.error(e);
}
}
/*! remove the given object from the array
@param anObject the object to remove
*/
- (void)removeObject:(id)anObject
{
[_content removeObject:anObject];
[_filteredContent removeObject:anObject];
_needsFilter = YES;
}
/*! remove all objects
*/
- (void)removeAllObjects
{
[_content removeAllObjects];
[_filteredContent removeAllObjects];
_needsFilter = YES;
}
/*! remove last object
*/
- (void)removeLastObject
{
[_content removeLastObject];
[_filteredContent removeLastObject];
_needsFilter = YES;
}
/*! remove first object
*/
- (void)removeFirstObject
{
[_content removeFirstObject];
[_filteredContent removeFirstObject];
_needsFilter = YES;
}
/*! return the index of the given object
@param anObject the object
@return the index of the given object
*/
- (int)indexOfObject:(id)anObject
{
return [_filteredContent indexOfObject:anObject];
}
/*! returns the number of object in the datasource
If a filter is applied, it will return the number of objects
matching the filyter , not the total
@return the number of object
*/
- (int)count
{
return [_filteredContent count];
}
#pragma mark -
#pragma mark Helpers
/*! expose selected objects
@return array of selected objects
*/
- (CPArray)selectedObjects
{
return [_filteredContent objectsAtIndexes:[_table selectedRowIndexes]];
}
#pragma mark -
#pragma mark Datasource implementation
- (CPNumber)numberOfRowsInTableView:(CPTableView)aTable
{
return [_filteredContent count];
}
- (id)tableView:(CPTableView)aTable objectValueForTableColumn:(CPNumber)aCol row:(CPNumber)aRow
{
if (_needsFilter && _searchField)
{
[self filterObjects:_searchField];
_needsFilter = NO;
}
if (aRow >= [_filteredContent count])
return nil;
var identifier = [aCol identifier];
return [[_filteredContent objectAtIndex:aRow] valueForKeyPath:identifier];
}
- (void)tableView:(CPTableView)aTableView sortDescriptorsDidChange:(CPArray)oldDescriptors
{
var indexes = [aTableView selectedRowIndexes],
selectedObjects = [_filteredContent objectsAtIndexes:indexes],
indexesToSelect = [[CPIndexSet alloc] init];
[_filteredContent sortUsingDescriptors:[aTableView sortDescriptors]];
[_content sortUsingDescriptors:[aTableView sortDescriptors]];
[_table reloadData];
for (var i = 0; i < [selectedObjects count]; i++)
{
var object = [selectedObjects objectAtIndex:i];
[indexesToSelect addIndex:[_filteredContent indexOfObject:object]];
}
[_table selectRowIndexes:indexesToSelect byExtendingSelection:NO];
}
- (void)tableView:(CPTableView)aTableView setObjectValue:(id)aValue forTableColumn:(CPTableColumn)aCol row:(int)aRow
{
if (aRow >= [_filteredContent count])
return;
var identifier = [aCol identifier];
[[_filteredContent objectAtIndex:aRow] setValue:aValue forKeyPath:identifier];
}
@end