forked from mikecsh/mbtablegrid
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMBTableGridController.m
More file actions
553 lines (405 loc) · 17.9 KB
/
MBTableGridController.m
File metadata and controls
553 lines (405 loc) · 17.9 KB
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
Copyright (c) 2008 Matthew Ball - http://www.mattballdesign.com
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#import "MBTableGridController.h"
#import "MBTableGridCell.h"
#import "MBTableGridFooterTextCell.h"
NSString* kAutosavedColumnWidthKey = @"AutosavedColumnWidth";
NSString* kAutosavedColumnIndexKey = @"AutosavedColumnIndex";
NSString* kAutosavedColumnHiddenKey = @"AutosavedColumnHidden";
NSString * const PasteboardTypeColumnClass = @"pasteboardTypeColumnClass";
@interface NSMutableArray (SwappingAdditions)
- (void)moveObjectsAtIndexes:(NSIndexSet *)indexes toIndex:(NSUInteger)index;
@end
@interface MBTableGridController()
@property (nonatomic, strong) MBTableGridCell *textCell;
@property (nonatomic, strong) MBTableGridFooterTextCell *footerTextCell;
@property (nonatomic, strong) NSDictionary *columnWidths;
@property (nonatomic, strong) NSMutableArray *columnIdentifiers;
@end
@implementation MBTableGridController
+ (void)initialize {
// See https://gist.github.com/lukaskubanek/9a61ac71dc0db8bb04db2028f2635779
[NSUserDefaults.standardUserDefaults registerDefaults:@{ @"NSViewUsesAutomaticLayerBackingStores": @NO }];
[super initialize];
}
- (void)awakeFromNib
{
columnSampleWidths = @[@40, @50, @60, @70, @80, @90, @100, @110, @120, @130];
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
NSString *gridComponentID = [infoDict objectForKey:@"GridComponentID"];
tableGrid.autosaveName = [NSString stringWithFormat:@"MBTableGrid Columns records-table-%@", gridComponentID];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
self.columnWidths = [defaults objectForKey:tableGrid.autosaveName];
NSNumberFormatter *decimalFormatter = [[NSNumberFormatter alloc] init];
decimalFormatter.numberStyle = NSNumberFormatterCurrencyStyle;
decimalFormatter.lenient = YES;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = NSDateFormatterShortStyle;
dateFormatter.timeStyle = NSDateFormatterNoStyle;
// Add 10 columns & rows
[self tableGrid:tableGrid addColumns:10 shouldReload:NO];
[self tableGrid:tableGrid addRows:10 shouldReload:NO];
tableGrid.contentInsets = NSEdgeInsetsMake(0, 0, self.controls_view.frame.size.height, 0);
[tableGrid reloadData];
// Register to receive text strings
[tableGrid registerForDraggedTypes:@[NSPasteboardTypeString]];
self.textCell = [[MBTableGridCell alloc] initTextCell:@""];
self.footerTextCell = [[MBTableGridFooterTextCell alloc] initTextCell:@""];
}
-(NSString *) genRandStringLength: (int) len
{
// Create alphanumeric table
NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
// Create mutable string
NSMutableString *randomString = [NSMutableString stringWithCapacity: len];
// Add random character to string
for (int i=0; i<len; i++) {
[randomString appendFormat: @"%C", [letters characterAtIndex: arc4random() % [letters length]]];
}
// return string
return randomString;
}
#pragma mark Actions
- (IBAction)toggleColumnHeaderVisible:(NSButton *)sender {
tableGrid.columnHeaderVisible = !tableGrid.isColumnHeaderVisible;
}
- (IBAction)toggleRowHeaderVisible:(NSButton *)sender {
tableGrid.rowHeaderVisible = !tableGrid.isRowHeaderVisible;
}
- (IBAction)toggleColumnFooterVisible:(NSButton *)sender {
tableGrid.columnFooterVisible = !tableGrid.isColumnFooterVisible;
}
#pragma mark -
#pragma mark Protocol Methods
#pragma mark MBTableGridDataSource
- (NSUInteger)numberOfRowsInTableGrid:(MBTableGrid *)aTableGrid
{
return 1000;
}
- (NSUInteger)numberOfColumnsInTableGrid:(MBTableGrid *)aTableGrid
{
return 10;
}
- (NSIndexSet *)sortableColumnIndexesInTableGrid:(MBTableGrid *)aTableGrid {
NSMutableIndexSet *sortableColumns = [NSMutableIndexSet indexSet];
[sortableColumns addIndex:1];
[sortableColumns addIndex:3];
return sortableColumns;
}
- (NSString *)tableGrid:(MBTableGrid *)aTableGrid headerStringForColumn:(NSUInteger)columnIndex {
return [NSString stringWithFormat:@"Column %lu", columnIndex];
}
- (id)tableGrid:(MBTableGrid *)aTableGrid objectValueForColumn:(NSUInteger)columnIndex row:(NSUInteger)rowIndex
{
return [NSString stringWithFormat:@"%lu %lu", columnIndex, (unsigned long)rowIndex];
}
- (BOOL)tableGrid:(MBTableGrid *)aTableGrid shouldEditColumn:(NSUInteger)columnIndex row:(NSUInteger)rowIndex {
return YES;
}
- (NSCell *)tableGrid:(MBTableGrid *)aTableGrid cellForColumn:(NSUInteger)columnIndex row:(NSUInteger)rowIndex {
NSCell* cell = self.textCell;
cell.objectValue = [self tableGrid:aTableGrid objectValueForColumn:columnIndex row:rowIndex];
return cell;
}
- (void)tableGrid:(MBTableGrid *)aTableGrid setObjectValue:(id)anObject forColumn:(NSUInteger)columnIndex row:(NSUInteger)rowIndex {
if (columnIndex >= [columns count]) {
return;
}
NSMutableArray *column = columns[columnIndex];
if (rowIndex >= [column count]) {
return;
}
if (anObject == nil) {
anObject = @"";
}
column[rowIndex] = anObject;
// NSLog(@"col: %lu, row: %lu, obj: %@", columnIndex, rowIndex, anObject);
}
- (float)tableGrid:(MBTableGrid *)aTableGrid widthForColumn:(NSUInteger)columnIndex {
return (columnIndex < columnSampleWidths.count) ? [columnSampleWidths[columnIndex] floatValue] : 60;
}
#pragma mark Footer
- (NSString *)footerDefaultsKeyForColumn:(NSUInteger)columnIndex;
{
return [NSString stringWithFormat:@"TableGrid-Footer-%ld", columnIndex];
}
- (NSCell *)tableGrid:(MBTableGrid *)aTableGrid footerCellForColumn:(NSUInteger)columnIndex;
{
self.footerTextCell.title = [NSString stringWithFormat:@"Footer %lu", columnIndex];
return self.footerTextCell;
}
#pragma mark Dragging
- (BOOL)tableGrid:(MBTableGrid *)aTableGrid canMoveColumns:(NSIndexSet *)columnIndexes toIndex:(NSUInteger)index
{
// Allow any column movement
return YES;
}
- (BOOL)tableGrid:(MBTableGrid *)aTableGrid moveColumns:(NSIndexSet *)columnIndexes toIndex:(NSUInteger)index
{
[columns moveObjectsAtIndexes:columnIndexes toIndex:index];
[self.columnIdentifiers moveObjectsAtIndexes:columnIndexes toIndex:index];
return YES;
}
- (BOOL)tableGrid:(MBTableGrid *)aTableGrid canMoveRows:(NSIndexSet *)rowIndexes toIndex:(NSUInteger)index
{
// Allow any row movement
return YES;
}
- (BOOL)tableGrid:(MBTableGrid *)aTableGrid moveRows:(NSIndexSet *)rowIndexes toIndex:(NSUInteger)index
{
for (NSMutableArray *column in columns) {
[column moveObjectsAtIndexes:rowIndexes toIndex:index];
}
return YES;
}
- (NSDragOperation)tableGrid:(MBTableGrid *)aTableGrid validateDrop:(id <NSDraggingInfo>)info proposedColumn:(NSUInteger)columnIndex row:(NSUInteger)rowIndex
{
return NSDragOperationCopy;
}
- (BOOL)tableGrid:(MBTableGrid *)aTableGrid acceptDrop:(id <NSDraggingInfo>)info column:(NSUInteger)columnIndex row:(NSUInteger)rowIndex
{
NSPasteboard *pboard = [info draggingPasteboard];
NSString *value = [pboard stringForType:NSPasteboardTypeString];
[self tableGrid:aTableGrid setObjectValue:value forColumn:columnIndex row:rowIndex];
return YES;
}
#pragma mark Copy & Paste
- (void)tableGrid:(MBTableGrid *)aTableGrid copyCellsAtColumns:(NSIndexSet *)columnIndexes rows:(NSIndexSet *)rowIndexes
{
NSMutableArray *colClasses = [NSMutableArray arrayWithCapacity:columnIndexes.count];
[columnIndexes enumerateIndexesUsingBlock:^(NSUInteger columnIndex, BOOL *stop) {
[colClasses addObject:[[self tableGrid:aTableGrid cellForColumn:columnIndex row: 0] className]];
}];
NSMutableArray *rowData = [NSMutableArray arrayWithCapacity:rowIndexes.count];
[rowIndexes enumerateIndexesUsingBlock:^(NSUInteger rowIndex, BOOL *stop) {
NSMutableArray *colData = [NSMutableArray arrayWithCapacity:columnIndexes.count];
[columnIndexes enumerateIndexesUsingBlock:^(NSUInteger columnIndex, BOOL *stop) {
NSArray *row = columns[columnIndex];
id cellData = row[rowIndex];
[colData addObject:cellData];
}];
NSString *rowString = [colData componentsJoinedByString:@"\t"];
[rowData addObject:rowString];
}];
NSString *tsvString = [rowData componentsJoinedByString:@"\n"];
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
[pasteboard clearContents];
[pasteboard declareTypes:@[NSPasteboardTypeTabularText, NSPasteboardTypeString, PasteboardTypeColumnClass] owner:nil];
[pasteboard setString:tsvString forType:NSPasteboardTypeTabularText];
[pasteboard setString:tsvString forType:NSPasteboardTypeString];
[pasteboard setPropertyList:colClasses forType:PasteboardTypeColumnClass];
}
- (void)tableGrid:(MBTableGrid *)aTableGrid pasteCellsAtColumns:(NSIndexSet *)columnIndexes rows:(NSIndexSet *)rowIndexes
{
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
BOOL isSingleSelection = columnIndexes.count == 1 && rowIndexes.count == 1;
NSString *tavString = [pasteboard stringForType:NSPasteboardTypeTabularText];
if (!tavString.length) {
tavString = [pasteboard stringForType:NSPasteboardTypeString];
}
if (!tavString.length) {
return;
}
// Extract the rows and columns to a more convenient form
NSMutableArray *rowArray = [NSMutableArray array];
for (NSString *rowString in [tavString componentsSeparatedByString:@"\n"]) {
NSMutableArray *columnArray = [NSMutableArray array];
for (NSString *columnString in [rowString componentsSeparatedByString:@"\t"]) {
[columnArray addObject:columnString];
}
[rowArray addObject:columnArray];
}
NSUInteger firstSelectedColumn = [columnIndexes firstIndex];
NSUInteger firstSelectedRow = [rowIndexes firstIndex];
NSUInteger numberOfExistingColumns = [columns count];
NSUInteger numberOfExistingRows = [[columns firstObject] count];
NSUInteger numberOfPastingColumns = [[rowArray firstObject] count];
NSUInteger numberOfPastingRows = [rowArray count];
// If pasting to a single cell, we want to paste all of the values, so add rows and columns if needed
if (isSingleSelection) {
NSInteger extraColumns = firstSelectedColumn + numberOfPastingColumns - numberOfExistingColumns;
NSInteger extraRows = firstSelectedRow + numberOfPastingRows - numberOfExistingRows;
if (extraColumns > 0) {
// Add extra columns; a real controller could use the PasteboardTypeColumnClass values to add appropriate column types to the database
[self tableGrid:aTableGrid addColumns:extraColumns shouldReload:NO];
}
if (extraRows > 0) {
[self tableGrid:aTableGrid addRows:extraRows shouldReload:NO];
}
} else {
numberOfPastingColumns = columnIndexes.count;
numberOfPastingRows = rowIndexes.count;
}
// Insert the values
[rowArray enumerateObjectsUsingBlock:^(NSArray *columnArray, NSUInteger rowOffset, BOOL *stopRows) {
[columnArray enumerateObjectsUsingBlock:^(NSString *value, NSUInteger columnOffset, BOOL *stopColumns) {
NSUInteger column = firstSelectedColumn + columnOffset;
NSUInteger row = firstSelectedRow + rowOffset;
[self tableGrid:aTableGrid setObjectValue:value forColumn:column row:row];
if (columnOffset == numberOfPastingColumns - 1) {
*stopColumns = YES;
}
}];
if (rowOffset == numberOfPastingRows - 1) {
*stopRows = YES;
}
}];
}
#pragma mark Adding and Removing Columns & Rows
- (BOOL)tableGrid:(MBTableGrid *)aTableGrid addColumns:(NSUInteger)numberOfColumns shouldReload:(BOOL)shouldReload;
{
// Default number of rows
NSUInteger numberOfRows = 0;
// If there are already other columns, get the number of rows from one of them
if ([columns count] > 0) {
numberOfRows = [columns[0] count];
}
for (NSUInteger column = 0; column < numberOfColumns; column++) {
NSMutableArray *newColumn = [NSMutableArray array];
for (NSUInteger row = 0; row < numberOfRows; row++) {
// Insert blank items for each row
[newColumn addObject:@""];
}
[columns addObject:newColumn];
if (columns.count > self.columnIdentifiers.count) {
[self.columnIdentifiers addObject:[NSString stringWithFormat:@"extra%@", @(columns.count)]];
}
}
if (shouldReload) {
[tableGrid reloadData];
}
return YES;
}
- (BOOL)tableGrid:(MBTableGrid *)aTableGrid addRows:(NSUInteger)numberOfRows;
{
return [self tableGrid:aTableGrid addRows:numberOfRows shouldReload:YES];
}
- (BOOL)tableGrid:(MBTableGrid *)aTableGrid addRows:(NSUInteger)numberOfRows shouldReload:(BOOL)shouldReload;
{
for (NSUInteger row = 0; row < numberOfRows; row++) {
for (NSMutableArray *column in columns) {
// Add a blank item to each row
[column addObject:@""];
}
}
[aTableGrid.columnRects removeAllObjects];
if (shouldReload) {
[aTableGrid reloadData];
}
return YES;
}
- (BOOL)tableGrid:(MBTableGrid *)aTableGrid removeRows:(NSIndexSet *)rowIndexes;
{
for (NSMutableArray *column in columns) {
[column removeObjectsAtIndexes:rowIndexes];
}
[aTableGrid reloadData];
return YES;
}
#pragma mark MBTableGridDelegate
- (void)tableGridDidMoveRows:(NSNotification *)aNotification
{
NSLog(@"moved");
}
- (void)tableGrid:(MBTableGrid *)aTableGrid userDidEnterInvalidStringInColumn:(NSUInteger)columnIndex row:(NSUInteger)rowIndex errorDescription:(NSString *)errorDescription {
NSLog(@"Invalid input at %lu,%lu: %@", (unsigned long)columnIndex, (unsigned long)rowIndex, errorDescription);
}
- (void)tableGrid:(MBTableGrid *)aTableGrid didAddRows:(NSIndexSet *)rowIndexes;
{
// Add the rows to the database, or whatever is needed
}
#pragma mark - QuickLook
-(void)quickLookAction:(id)sender {
// [[NSApp mainWindow] makeFirstResponder:self];
if ([QLPreviewPanel sharedPreviewPanelExists] && [[QLPreviewPanel sharedPreviewPanel] isVisible]) {
[[QLPreviewPanel sharedPreviewPanel] orderOut:nil];
} else {
QLPreviewPanel *previewPanel = [QLPreviewPanel sharedPreviewPanel];
previewPanel.dataSource = self;
previewPanel.delegate = self;
[previewPanel makeKeyAndOrderFront:sender];
}
}
// Quick Look panel support
- (BOOL)acceptsPreviewPanelControl:(QLPreviewPanel *)panel; {
return YES;
}
- (void)beginPreviewPanelControl:(QLPreviewPanel *)panel {
// This document is now responsible of the preview panel
// It is allowed to set the delegate, data source and refresh panel.
panel.delegate = self;
panel.dataSource = self;
}
- (void)endPreviewPanelControl:(QLPreviewPanel *)panel {
panel.delegate = nil;
panel.dataSource = nil;
}
// Quick Look panel data source
- (NSInteger)numberOfPreviewItemsInPreviewPanel:(QLPreviewPanel *)panel {
return 1;
}
- (id <QLPreviewItem>)previewPanel:(QLPreviewPanel *)panel previewItemAtIndex:(NSInteger)index {
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
NSURL *fileURL = nil;
__block NSURL *returnURL = nil;
NSError *error = nil;
fileURL = [[NSBundle mainBundle] URLForImageResource:@"rose"];
[coordinator coordinateReadingItemAtURL:fileURL
options:NSFileCoordinatorReadingWithoutChanges
error:&error
byAccessor:^(NSURL *newURL) {
returnURL = newURL;
}];
return returnURL;
}
// This delegate method provides the rect on screen from which the panel will zoom.
- (NSRect)previewPanel:(QLPreviewPanel *)panel sourceFrameOnScreenForPreviewItem:(id <QLPreviewItem>)item {
// convert selected cell rect to screen coordinates
NSInteger selectedColumn = [tableGrid.selectedColumnIndexes firstIndex];
NSInteger selectedRow = [tableGrid.selectedRowIndexes firstIndex];
NSCell *selectedCell = [tableGrid selectedCell];
NSRect photoPreviewFrame = [tableGrid frameOfCellAtColumn:selectedColumn row:selectedRow];
NSRect rectInWinCoords = [selectedCell.controlView convertRect:photoPreviewFrame toView:nil];
NSRect rectInScreenCoords = [[NSApp mainWindow] convertRectToScreen:rectInWinCoords];
return rectInScreenCoords;
}
@end
@implementation NSMutableArray (SwappingAdditions)
- (void)moveObjectsAtIndexes:(NSIndexSet *)indexes toIndex:(NSUInteger)index
{
NSArray *objects = [self objectsAtIndexes:indexes];
// Determine the new indexes for the objects
NSRange newRange = NSMakeRange(index, [indexes count]);
if (index > [indexes lastIndex]) {
newRange.location -= [indexes count];
} else if (index > [indexes firstIndex]) {
newRange.location = [indexes firstIndex];
}
NSIndexSet *newIndexes = [NSIndexSet indexSetWithIndexesInRange:newRange];
// Determine where the original objects are
NSIndexSet *originalIndexes = indexes;
// Remove the objects from their original locations
[self removeObjectsAtIndexes:originalIndexes];
// Insert the objects at their new location
[self insertObjects:objects atIndexes:newIndexes];
}
@end