PreferenceController
1
#import
"
PreferenceController.h
"
2
3
4 @implementation PreferenceController
5
6 NSString * const BNRTableBgColorKey = @" TableBackgroundColor " ;
7 NSString * const BNREmptyDocKey = @" EmptyDocuementFlag " ;
8 NSString * const BNRColorChangedNotification = @" BNRColorChanged " ;
9
10 - (id)init
11 {
12 if ( ! [super initWithWindowNibName: @" Preferences " ])
13 {
14 return nil;
15 }
16
17 return self;
18 }
19
20 - (NSColor * )tableBgColor
21 {
22 NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
23 NSData * colorAsData = [defaults objectForKey:BNRTableBgColorKey];
24 return [NSKeyedUnarchiver unarchiveObjectWithData:colorAsData];
25 }
26
27 - (BOOL)emptyDoc
28 {
29 NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
30 return [defaults boolForKey:BNREmptyDocKey];
31 }
32
33
34 - ( void )windowDidLoad
35 {
36 [colorWell setColor:[self tableBgColor]];
37 [checkbox setState:[self emptyDoc]];
38 NSLog( @" Nib file is loaded " );
39 }
40
41 - (IBAction)changeBackgroundColor:(id)sender
42 {
43 NSColor * color = [colorWell color];
44 NSData * colorAsData = [NSKeyedArchiver archivedDataWithRootObject:color];
45 NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
46 [defaults setObject:colorAsData forKey:BNRTableBgColorKey];
47 NSLog( @" Color changed: %@ " , color);
48
49 NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
50 NSLog( @" Sending notification " );
51 NSDictionary * d = [NSDictionary dictionaryWithObject:color forKey: @" color " ];
52 [nc postNotificationName:BNRColorChangedNotification object :self userInfo:d];
53 }
54
55 - (IBAction)changeNewEmptyDoc:(id)sender
56 {
57 int state = [checkbox state];
58 NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
59 [defaults setBool:state forKey:BNREmptyDocKey];
60 NSLog( @" Checkbox changed %d " , state);
61 }
62
63 @end
2
3
4 @implementation PreferenceController
5
6 NSString * const BNRTableBgColorKey = @" TableBackgroundColor " ;
7 NSString * const BNREmptyDocKey = @" EmptyDocuementFlag " ;
8 NSString * const BNRColorChangedNotification = @" BNRColorChanged " ;
9
10 - (id)init
11 {
12 if ( ! [super initWithWindowNibName: @" Preferences " ])
13 {
14 return nil;
15 }
16
17 return self;
18 }
19
20 - (NSColor * )tableBgColor
21 {
22 NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
23 NSData * colorAsData = [defaults objectForKey:BNRTableBgColorKey];
24 return [NSKeyedUnarchiver unarchiveObjectWithData:colorAsData];
25 }
26
27 - (BOOL)emptyDoc
28 {
29 NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
30 return [defaults boolForKey:BNREmptyDocKey];
31 }
32
33
34 - ( void )windowDidLoad
35 {
36 [colorWell setColor:[self tableBgColor]];
37 [checkbox setState:[self emptyDoc]];
38 NSLog( @" Nib file is loaded " );
39 }
40
41 - (IBAction)changeBackgroundColor:(id)sender
42 {
43 NSColor * color = [colorWell color];
44 NSData * colorAsData = [NSKeyedArchiver archivedDataWithRootObject:color];
45 NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
46 [defaults setObject:colorAsData forKey:BNRTableBgColorKey];
47 NSLog( @" Color changed: %@ " , color);
48
49 NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
50 NSLog( @" Sending notification " );
51 NSDictionary * d = [NSDictionary dictionaryWithObject:color forKey: @" color " ];
52 [nc postNotificationName:BNRColorChangedNotification object :self userInfo:d];
53 }
54
55 - (IBAction)changeNewEmptyDoc:(id)sender
56 {
57 int state = [checkbox state];
58 NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
59 [defaults setBool:state forKey:BNREmptyDocKey];
60 NSLog( @" Checkbox changed %d " , state);
61 }
62
63 @end
MyDocument
1
#import
"
PreferenceController.h
"
2 #import " MyDocument.h "
3 #import " Person.h "
4
5 @implementation MyDocument
6
7 - (id)init
8 {
9 if ( ! [super init])
10 {
11 return nil;
12 }
13
14 employees = [[NSMutableArray alloc] init];
15
16 NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
17 [nc addObserver:self selector:@selector(handleColorChange:) name:BNRColorChangedNotification object :nil];
18 NSLog( @" Registered with notification center " );
19 return self;
20 }
21
22 - ( void )dealloc
23 {
24 [super setEmployees:nil];
25 NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
26 [nc removeObserver:self];
27 [super dealloc];
28 }
29
30 - ( void )handleColorChange:(NSNotification * )note
31 {
32 NSLog( @" Received notification: %@ " , note);
33 NSColor * color = [[note userInfo] objectForKey: @" color " ];
34 [tableView setBackgroundColor:color];
35 }
36
37 - ( void )setEmployees:(NSMutableArray * )a
38 {
39 if (a == employees)
40 {
41 return ;
42 }
43
44 for (Person * person in employees)
45 {
46 [self stopObservingPerson:person];
47 }
48
49 [a retain];
50 [employees release];
51 employees = a;
52
53 for (Person * person in employees)
54 {
55 [self startObservingPerson:person];
56 }
57 }
58
59 - ( void )insertObject:(Person * )p inEmployeesAtIndex:( int )index
60 {
61 NSLog( @" adding %@ to %@ " , p, employees);
62 NSUndoManager * undo = [self undoManager];
63 [[undo prepareWithInvocationTarget:self] removeObjectFromEmployeesAtIndex:index];
64 if ( ! [undo isUndoing])
65 {
66 [undo setActionName: @" Insert Person " ];
67 }
68 [self startObservingPerson:p];
69 [employees insertObject:p atIndex:index];
70 }
71
72 - ( void )removeObjectFromEmployeesAtIndex:( int )index
73 {
74 Person * p = [employees objectAtIndex:index];
75 NSLog( @" removing %@ from %@ " , p, employees);
76 NSUndoManager * undo = [self undoManager];
77 [[undo prepareWithInvocationTarget:self] insertObject:p inEmployeesAtIndex:index];
78
79 if ( ! [undo isUndoing])
80 {
81 [undo setActionName: @" Delete Person " ];
82 }
83 [self stopObservingPerson:p];
84 [employees removeObjectAtIndex:index];
85 }
86
87 - ( void )startObservingPerson:(Person * )person
88 {
89 [person addObserver:self forKeyPath: @" personName " options:NSKeyValueObservingOptionOld context:NULL];
90 [person addObserver:self forKeyPath: @" expectedRaise " options:NSKeyValueObservingOptionOld context:NULL];
91 }
92
93 - ( void )stopObservingPerson:(Person * )person
94 {
95 [person removeObserver:self forKeyPath: @" personName " ];
96 [person removeObserver:self forKeyPath: @" expectedRaise " ];
97 }
98
99 - ( void )changeKeyPath:(NSString * )keyPath ofObject:(id)obj toValue:(id)newValue
100 {
101 [obj setValue:newValue forKeyPath:keyPath];
102 }
103
104 - ( void )observeValueForKeyPath:(NSString * )keyPath ofObject:(id) object change:(NSDictionary * )change context:( void * )context
105 {
106 NSUndoManager * undo = [self undoManager];
107 id oldValue = [change objectForKey:NSKeyValueChangeOldKey];
108
109 if (oldValue == [NSNull null ])
110 {
111 oldValue = nil;
112 }
113
114 NSLog( @" oldValue = %@ " , oldValue);
115 [[undo prepareWithInvocationTarget:self] changeKeyPath:keyPath ofObject: object toValue:oldValue];
116 [undo setActionName: @" Edit " ];
117 }
118
119 - (IBAction)createEmployee:(id)sender
120 {
121 NSWindow * w = [tableView window];
122
123 BOOL editingEnded = [w makeFirstResponder:w];
124 if ( ! editingEnded)
125 {
126 NSLog( @" Unable to end editing " );
127 return ;
128 }
129
130 NSUndoManager * undo = [self undoManager];
131 if ([undo groupingLevel])
132 {
133 [undo endUndoGrouping];
134 [undo beginUndoGrouping];
135 }
136 Person * p = [employeeController newObject];
137 [employeeController addObject:p];
138 [p release];
139 [employeeController rearrangeObjects];
140
141 NSArray * a = [employeeController arrangedObjects];
142 int row = [a indexOfObjectIdenticalTo:p];
143 NSLog( @" starting edit of %@ in row %@ " , p, row);
144 [tableView editColumn: 0 row:row withEvent:nil select:YES];
145 }
146
147 - (NSString * )windowNibName
148 {
149 // Override returning the nib file name of the document
150 // 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.
151 return @" MyDocument " ;
152 }
153
154 - ( void )windowControllerDidLoadNib:(NSWindowController * ) aController
155 {
156 [super windowControllerDidLoadNib:aController];
157 NSUserDefaults * defautls = [NSUserDefaults standardUserDefaults];
158 NSData * colorAsData = [defautls objectForKey:BNRTableBgColorKey];
159 [tableView setBackgroundColor:[NSKeyedUnarchiver unarchiveObjectWithData:colorAsData]];
160 // Add any code here that needs to be executed once the windowController has loaded the document's window.
161 }
162
163 - (NSData * )dataOfType:(NSString * )typeName error:(NSError ** )outError
164 {
165 // 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.
166
167 // You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
168
169 // 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.
170
171 if ( outError != NULL ) {
172 * outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
173 }
174 return nil;
175 }
176
177 - (BOOL)readFromData:(NSData * )data ofType:(NSString * )typeName error:(NSError ** )outError
178 {
179 // 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.
180
181 // You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead.
182
183 // 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.
184
185 if ( outError != NULL ) {
186 * outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
187 }
188 return YES;
189 }
190
191 @end
2 #import " MyDocument.h "
3 #import " Person.h "
4
5 @implementation MyDocument
6
7 - (id)init
8 {
9 if ( ! [super init])
10 {
11 return nil;
12 }
13
14 employees = [[NSMutableArray alloc] init];
15
16 NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
17 [nc addObserver:self selector:@selector(handleColorChange:) name:BNRColorChangedNotification object :nil];
18 NSLog( @" Registered with notification center " );
19 return self;
20 }
21
22 - ( void )dealloc
23 {
24 [super setEmployees:nil];
25 NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
26 [nc removeObserver:self];
27 [super dealloc];
28 }
29
30 - ( void )handleColorChange:(NSNotification * )note
31 {
32 NSLog( @" Received notification: %@ " , note);
33 NSColor * color = [[note userInfo] objectForKey: @" color " ];
34 [tableView setBackgroundColor:color];
35 }
36
37 - ( void )setEmployees:(NSMutableArray * )a
38 {
39 if (a == employees)
40 {
41 return ;
42 }
43
44 for (Person * person in employees)
45 {
46 [self stopObservingPerson:person];
47 }
48
49 [a retain];
50 [employees release];
51 employees = a;
52
53 for (Person * person in employees)
54 {
55 [self startObservingPerson:person];
56 }
57 }
58
59 - ( void )insertObject:(Person * )p inEmployeesAtIndex:( int )index
60 {
61 NSLog( @" adding %@ to %@ " , p, employees);
62 NSUndoManager * undo = [self undoManager];
63 [[undo prepareWithInvocationTarget:self] removeObjectFromEmployeesAtIndex:index];
64 if ( ! [undo isUndoing])
65 {
66 [undo setActionName: @" Insert Person " ];
67 }
68 [self startObservingPerson:p];
69 [employees insertObject:p atIndex:index];
70 }
71
72 - ( void )removeObjectFromEmployeesAtIndex:( int )index
73 {
74 Person * p = [employees objectAtIndex:index];
75 NSLog( @" removing %@ from %@ " , p, employees);
76 NSUndoManager * undo = [self undoManager];
77 [[undo prepareWithInvocationTarget:self] insertObject:p inEmployeesAtIndex:index];
78
79 if ( ! [undo isUndoing])
80 {
81 [undo setActionName: @" Delete Person " ];
82 }
83 [self stopObservingPerson:p];
84 [employees removeObjectAtIndex:index];
85 }
86
87 - ( void )startObservingPerson:(Person * )person
88 {
89 [person addObserver:self forKeyPath: @" personName " options:NSKeyValueObservingOptionOld context:NULL];
90 [person addObserver:self forKeyPath: @" expectedRaise " options:NSKeyValueObservingOptionOld context:NULL];
91 }
92
93 - ( void )stopObservingPerson:(Person * )person
94 {
95 [person removeObserver:self forKeyPath: @" personName " ];
96 [person removeObserver:self forKeyPath: @" expectedRaise " ];
97 }
98
99 - ( void )changeKeyPath:(NSString * )keyPath ofObject:(id)obj toValue:(id)newValue
100 {
101 [obj setValue:newValue forKeyPath:keyPath];
102 }
103
104 - ( void )observeValueForKeyPath:(NSString * )keyPath ofObject:(id) object change:(NSDictionary * )change context:( void * )context
105 {
106 NSUndoManager * undo = [self undoManager];
107 id oldValue = [change objectForKey:NSKeyValueChangeOldKey];
108
109 if (oldValue == [NSNull null ])
110 {
111 oldValue = nil;
112 }
113
114 NSLog( @" oldValue = %@ " , oldValue);
115 [[undo prepareWithInvocationTarget:self] changeKeyPath:keyPath ofObject: object toValue:oldValue];
116 [undo setActionName: @" Edit " ];
117 }
118
119 - (IBAction)createEmployee:(id)sender
120 {
121 NSWindow * w = [tableView window];
122
123 BOOL editingEnded = [w makeFirstResponder:w];
124 if ( ! editingEnded)
125 {
126 NSLog( @" Unable to end editing " );
127 return ;
128 }
129
130 NSUndoManager * undo = [self undoManager];
131 if ([undo groupingLevel])
132 {
133 [undo endUndoGrouping];
134 [undo beginUndoGrouping];
135 }
136 Person * p = [employeeController newObject];
137 [employeeController addObject:p];
138 [p release];
139 [employeeController rearrangeObjects];
140
141 NSArray * a = [employeeController arrangedObjects];
142 int row = [a indexOfObjectIdenticalTo:p];
143 NSLog( @" starting edit of %@ in row %@ " , p, row);
144 [tableView editColumn: 0 row:row withEvent:nil select:YES];
145 }
146
147 - (NSString * )windowNibName
148 {
149 // Override returning the nib file name of the document
150 // 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.
151 return @" MyDocument " ;
152 }
153
154 - ( void )windowControllerDidLoadNib:(NSWindowController * ) aController
155 {
156 [super windowControllerDidLoadNib:aController];
157 NSUserDefaults * defautls = [NSUserDefaults standardUserDefaults];
158 NSData * colorAsData = [defautls objectForKey:BNRTableBgColorKey];
159 [tableView setBackgroundColor:[NSKeyedUnarchiver unarchiveObjectWithData:colorAsData]];
160 // Add any code here that needs to be executed once the windowController has loaded the document's window.
161 }
162
163 - (NSData * )dataOfType:(NSString * )typeName error:(NSError ** )outError
164 {
165 // 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.
166
167 // You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
168
169 // 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.
170
171 if ( outError != NULL ) {
172 * outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
173 }
174 return nil;
175 }
176
177 - (BOOL)readFromData:(NSData * )data ofType:(NSString * )typeName error:(NSError ** )outError
178 {
179 // 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.
180
181 // You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead.
182
183 // 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.
184
185 if ( outError != NULL ) {
186 * outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
187 }
188 return YES;
189 }
190
191 @end