/
MyDocument.m
119 lines (94 loc) · 4.1 KB
/
MyDocument.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
#import "MyDocument.h"
@implementation MyDocument
- (id)init {
[super init];
employees = [[NSMutableArray alloc] init];
return self;
}
- (void)dealloc {
[employees release];
[super dealloc];
}
#pragma mark Action methods
- (IBAction)deleteSelectedEmployees:(id)sender {
// Which row is selected?
NSIndexSet *rows = [tableView selectedRowIndexes];
// Is the selection empty?
if ([rows count] == 0) {
NSBeep();
return;
}
NSLog(@"-deleteSelectedEmployees: is deleting %@", rows);
[employees removeObjectsAtIndexes:rows];
[tableView reloadData];
}
- (IBAction)createEmployee:(id)sender {
Person *newEmployee = [[Person alloc] init];
NSLog(@"-createEmployee: is creating %@", newEmployee);
[employees addObject:newEmployee];
[newEmployee release];
[tableView reloadData];
}
#pragma mark Table view dataSource methods
- (int)numberOfRowsInTableView:(NSTableView *)aTableView {
return [employees count];
}
- (id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex {
// What is the identifier for the column?
NSString *identifier = [aTableColumn identifier];
// What person?
Person *person = [employees objectAtIndex:rowIndex];
// What is the value of the attribute named identifier?
return [person valueForKey:identifier];
}
- (void)tableView:(NSTableView *)aTableView
setObjectValue:(id)anObject
forTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex {
NSString *identifier = [aTableColumn identifier];
Person *person = [employees objectAtIndex:rowIndex];
// Set the value for the attribute named identifier
[person setValue:anObject forKey:identifier];
}
- (void)tableView:(NSTableView *)aTableView
sortDescriptorsDidChange:(NSArray *)oldDescriptors {
NSArray *newDescriptors = [tableView sortDescriptors];
NSLog(@"-tableView:sortDescriptorsDidChange:, newDescriptors == @%", newDescriptors);
[employees sortUsingDescriptors:newDescriptors];
[tableView reloadData];
}
#pragma mark Pre-generated
- (NSString *)windowNibName
{
// Override returning the nib file name of the document
// If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
return @"MyDocument";
}
- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
[super windowControllerDidLoadNib:aController];
// Add any code here that needs to be executed once the windowController has loaded the document's window.
}
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
// Insert code here to write your document to data of the specified type. If the given outError != NULL, ensure that you set *outError when returning nil.
// You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
// For applications targeted for Panther or earlier systems, you should use the deprecated API -dataRepresentationOfType:. In this case you can also choose to override -fileWrapperRepresentationOfType: or -writeToFile:ofType: instead.
if ( outError != NULL ) {
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
}
return nil;
}
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
// Insert code here to read your document from the given data of the specified type. If the given outError != NULL, ensure that you set *outError when returning NO.
// You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead.
// For applications targeted for Panther or earlier systems, you should use the deprecated API -loadDataRepresentation:ofType. In this case you can also choose to override -readFromFile:ofType: or -loadFileWrapperRepresentation:ofType: instead.
if ( outError != NULL ) {
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
}
return YES;
}
@end