| 1 | /* Copyright (C) 2008-2009 Peter Speck |
|---|
| 2 | * |
|---|
| 3 | * This program is free software: you can redistribute it and/or modify |
|---|
| 4 | * it under the terms of the GNU General Public License as published by |
|---|
| 5 | * the Free Software Foundation, either version 3 of the License, or |
|---|
| 6 | * (at your option) any later version. |
|---|
| 7 | * |
|---|
| 8 | * This program is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * GNU General Public License for more details. |
|---|
| 12 | * |
|---|
| 13 | * You should have received a copy of the GNU General Public License |
|---|
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | #import "TableViewHelper.h" |
|---|
| 18 | #import "GBDebug.h" |
|---|
| 19 | |
|---|
| 20 | #ifdef DEBUG |
|---|
| 21 | //#defined DEBUG_TABLEVIEW 1 |
|---|
| 22 | #endif |
|---|
| 23 | |
|---|
| 24 | @implementation TableViewHelper |
|---|
| 25 | |
|---|
| 26 | - (BOOL)removeRow:(NSInteger)row fromTable:(NSTableView *)tableView |
|---|
| 27 | { |
|---|
| 28 | NSInteger numRows = [tableView numberOfRows]; |
|---|
| 29 | DebugNSLog(@"removeRow %ld, num rows = %ld", row, numRows); |
|---|
| 30 | if (row < 0) { |
|---|
| 31 | NSLog(@"removeSelectedRows without selected row"); |
|---|
| 32 | return NO; |
|---|
| 33 | } |
|---|
| 34 | if (row >= numRows) { |
|---|
| 35 | NSLog(@"Invalid row = %ld in removeSelectedRows, has only %ld rows", (long)row, (long)numRows); |
|---|
| 36 | return NO; |
|---|
| 37 | } |
|---|
| 38 | if ([tableView editedRow] == row) { |
|---|
| 39 | DebugNSLog(@"Ends editing as we're deleting the edited row"); |
|---|
| 40 | [[tableView window] endEditingFor:tableView]; |
|---|
| 41 | } |
|---|
| 42 | if (![self deleteRowData:tableView forRow:row]) |
|---|
| 43 | return NO; |
|---|
| 44 | [tableView reloadData]; |
|---|
| 45 | if (row + 1 < numRows) { |
|---|
| 46 | DebugNSLog(@"Changes selection to next row"); |
|---|
| 47 | NSIndexSet* indexes = [NSIndexSet indexSetWithIndex:row]; // already deleted. |
|---|
| 48 | [tableView selectRowIndexes:indexes byExtendingSelection:NO]; |
|---|
| 49 | } else if (row > 0) { |
|---|
| 50 | DebugNSLog(@"Changes selection to previous row"); |
|---|
| 51 | NSIndexSet* indexes = [NSIndexSet indexSetWithIndex:row - 1]; |
|---|
| 52 | [tableView selectRowIndexes:indexes byExtendingSelection:NO]; |
|---|
| 53 | } |
|---|
| 54 | return YES; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | - (BOOL)removeSelectedRows:(NSTableView *)tableView |
|---|
| 58 | { |
|---|
| 59 | NSIndexSet* set = [tableView selectedRowIndexes]; |
|---|
| 60 | for (NSInteger row = [set lastIndex]; row != NSNotFound; row = [set indexLessThanIndex:row]) { |
|---|
| 61 | if (![self removeRow:row fromTable:tableView]) |
|---|
| 62 | return NO; |
|---|
| 63 | } |
|---|
| 64 | return YES; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | - (BOOL)deleteRowData:(NSTableView *)tableView forRow:(NSUInteger)row |
|---|
| 68 | { |
|---|
| 69 | NSAssert(NO, @"TableViewHelper.deleteRowData must be overridden."); |
|---|
| 70 | return YES; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | @end |
|---|