/
EntityDataTableView.m
174 lines (141 loc) · 6.74 KB
/
EntityDataTableView.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
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
//
// EntityDataTableView.m
// CoreDataUtil
//
// Created by Laurie Caires on 6/6/12.
// Copyright (c) 2012 mFluent LLC. All rights reserved.
//
#import "EntityDataTableView.h"
#import "MFLMainWindowController.h"
#import "MFLTextTableCellView.h"
@implementation EntityDataTableView
- (NSInteger)getRightClickedCol
{
return rightClickedCol;
}
- (NSInteger)getRightClickedRow
{
return rightClickedRow;
}
- (void)rightMouseDown:(NSEvent *)theEvent {
NSPoint eventLocation = [theEvent locationInWindow];
eventLocation = [self convertPoint:eventLocation fromView:nil];
rightClickedRow = [self rowAtPoint:eventLocation];
rightClickedCol = [self columnAtPoint:eventLocation];
// get currently selected rows
NSIndexSet* indexSet = [self selectedRowIndexes];
//NSLog(@"Right clicked at row:%d, col:%d, point:%@, selected:%d", (int)rightClickedRow, (int)rightClickedCol, NSStringFromPoint(eventLocation), (int)indexSet.firstIndex);
// if user right-clicks on a non-selected row, select that row
if (![indexSet containsIndex:(NSUInteger)rightClickedRow]) {
[self selectRowIndexes:[NSIndexSet indexSetWithIndex:(NSUInteger)rightClickedRow] byExtendingSelection:NO];
}
// now, show menu for newly selected row
[super rightMouseDown:theEvent];
}
- (NSMenu *)menuForEvent:(NSEvent *)event
{
NSMenu *menu = nil;
NSIndexSet* indexSet = [self selectedRowIndexes];
if (indexSet != nil && [indexSet firstIndex] != NSNotFound) {
menu = [[NSMenu alloc] init];
NSMenuItem* copyRowItem = [[NSMenuItem alloc] initWithTitle:@"Copy Plain" action:@selector(copy:) keyEquivalent:@""];
[copyRowItem setKeyEquivalentModifierMask:0];
[copyRowItem setKeyEquivalentModifierMask:NSCommandKeyMask];
[menu addItem:copyRowItem];
copyRowItem = [[NSMenuItem alloc] initWithTitle:@"Copy Formated" action:@selector(copyFormatted:) keyEquivalent:@"C"];
[copyRowItem setKeyEquivalentModifierMask:0];
[copyRowItem setKeyEquivalentModifierMask:NSCommandKeyMask];
[menu addItem:copyRowItem];
// if only 1 row selected, offer 'copy cell' option
if (indexSet.count == 1) {
copyRowItem = [[NSMenuItem alloc] initWithTitle:@"Copy Cell" action:@selector(copyCell:) keyEquivalent:@"C"];
[copyRowItem setKeyEquivalentModifierMask:NSAlternateKeyMask];
[menu addItem:copyRowItem];
}
}
return menu;
}
/**
Copy the single selected row from the table.
The elements are separated by newlines, as text (!{NSStringPboardType}),
and by tabs, as tabular text (!NSTabularTextPboardType).
**/
- (void) copySelectedRow: (BOOL) escapeSpecialChars {
int selectedRow = (int)[self selectedRow]-1;
int numberOfRows = (int)[self numberOfRows];
NSLog(@"Selected Row: %d, Total Rows: %d", selectedRow, numberOfRows);
NSIndexSet* indexSet = [self selectedRowIndexes];
if (indexSet != nil && [indexSet firstIndex] != NSNotFound) {
NSPasteboard *pb = [NSPasteboard generalPasteboard];
NSMutableString *tabsBuf = [NSMutableString string];
NSMutableString *textBuf = [NSMutableString string];
NSArray *tableColumns = [self tableColumns];
NSLog(@"Columns: %@", tableColumns);
for (NSTableColumn* columnName in tableColumns) {
[textBuf appendFormat:@"%@\t", [columnName identifier] ];
[tabsBuf appendFormat:@"%@\t", [columnName identifier]];
}
[textBuf appendFormat:@"\n"];
[tabsBuf appendFormat:@"\n"];
// Step through and copy data from each of the selected rows
NSUInteger currentIndex = [indexSet firstIndex];
while (currentIndex != NSNotFound) {
NSEnumerator *enumerator = [tableColumns objectEnumerator];
NSTableColumn *col;
MFLMainWindowController* dataSource = (MFLMainWindowController*)[self dataSource];
while (nil != (col = [enumerator nextObject]) ) {
id columnValue = [dataSource getValueObjFromDataRows:self: currentIndex: col];
NSString *columnString = @"";
if (nil != columnValue) {
if ([columnValue isKindOfClass:[NSManagedObject class]]) {
columnString = [[columnValue entity] name];
} else if ([columnValue isKindOfClass:[NSArray class]]) {
columnString = [NSString stringWithFormat:@"NSArray[%ld]", [columnValue count]];
} else if ([columnValue isKindOfClass:[NSSet class]]) {
columnString = [NSString stringWithFormat:@"NSSet[%ld]", [columnValue count]];
} else {
columnString = [columnValue description];
}
}
if (columnString == nil) {
columnString = @"";
}
if (escapeSpecialChars) {
// Escape CR and TAB like SQLPro:
// http://code.google.com/p/sequel-pro/source/browse/branches/app-store/Source/SPCopyTable.m?r=3592#239
columnString = [[columnString stringByReplacingOccurrencesOfString:@"\n" withString:@"↵"] stringByReplacingOccurrencesOfString:@"\t" withString:@"⇥"];
}
[tabsBuf appendFormat:@"%@\t",columnString];
[textBuf appendFormat:@"%@\t",columnString];
}
[textBuf appendFormat:@"\n"];
[tabsBuf appendFormat:@"\n"];
// delete the last tab. (But don't delete the last CR)
if ([tabsBuf length]) {
[tabsBuf deleteCharactersInRange:NSMakeRange([tabsBuf length]-1, 1)];
}
// Next Index
currentIndex = [indexSet indexGreaterThanIndex: currentIndex];
}
[pb declareTypes:@[NSStringPboardType] owner:nil];
[pb setString:[NSString stringWithString:textBuf] forType:NSStringPboardType];
}
}
- (IBAction) copy:(id)sender
{
NSLog(@"Copy Selected entityDataTable items. [%@]", sender);
[self copySelectedRow:NO];
}
- (IBAction) copyFormatted:(id)sender
{
NSLog(@"copyFormated Selected entityDataTable items. [%@]", sender);
[self copySelectedRow:YES];
}
- (IBAction) copyCell:(id)sender {
MFLTextTableCellView *cell = [self viewAtColumn:rightClickedCol row:rightClickedRow makeIfNecessary:NO];
NSLog(@"copyCell: r:%d, c:%d, %@", (int)rightClickedRow, (int)rightClickedCol, cell.text);
NSPasteboard *pb = [NSPasteboard generalPasteboard];
[pb declareTypes:@[NSStringPboardType] owner:nil];
[pb setString:cell.text forType:NSStringPboardType];
}
@end