public
Description: Examples from the new 3rd edition of Cocoa Programming for Mac OS X, ported to Nu
Homepage: http://programming.nu/posts/2008/05/23/cocoa-programming-with-nu
Clone URL: git://github.com/timburks/cocoa-programming-with-nu.git
Completed conversion of 09_Undo.
jsyedidia (author)
Sun Jun 01 17:52:29 -0700 2008
commit  6c225a8c1cbed4a1e37b0ebb1491bcce5a756e83
tree    c834717bcdf770f9cbadeb2b07ffd889eae578aa
parent  8abeb11a0e6919b7c1f2b1d1d044e16a8b10ba07
...
3
4
5
 
6
...
3
4
5
6
7
0
@@ -3,4 +3,5 @@
0
 *.app
0
 *.pbxuser
0
 *.perspectivev3
0
+*.mode1v3
0
 TemplateIcon.icns
...
11
12
13
14
15
16
17
18
19
20
...
23
24
25
26
27
28
29
30
...
34
35
36
37
38
39
40
41
...
83
84
85
86
87
88
89
...
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
...
200
201
202
203
204
205
206
207
208
...
11
12
13
 
14
15
 
16
17
18
...
21
22
23
 
 
24
25
26
...
30
31
32
 
 
33
34
35
...
77
78
79
 
80
81
82
...
85
86
87
 
 
 
 
 
 
 
 
 
 
 
88
89
90
...
182
183
184
 
185
 
186
187
188
0
@@ -11,10 +11,8 @@
0
     8D15AC2D0486D014006FF6A4 /* MainMenu.nib in Resources */ = {isa = PBXBuildFile; fileRef = 2A37F4B6FDCFA73011CA2CEA /* MainMenu.nib */; };
0
     8D15AC2E0486D014006FF6A4 /* MyDocument.nib in Resources */ = {isa = PBXBuildFile; fileRef = 2A37F4B4FDCFA73011CA2CEA /* MyDocument.nib */; };
0
     8D15AC2F0486D014006FF6A4 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 089C165FFE840EACC02AAC07 /* InfoPlist.strings */; };
0
- 8D15AC310486D014006FF6A4 /* MyDocument.m in Sources */ = {isa = PBXBuildFile; fileRef = 2A37F4ACFDCFA73011CA2CEA /* MyDocument.m */; settings = {ATTRIBUTES = (); }; };
0
     8D15AC320486D014006FF6A4 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 2A37F4B0FDCFA73011CA2CEA /* main.m */; settings = {ATTRIBUTES = (); }; };
0
     8D15AC340486D014006FF6A4 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1058C7A7FEA54F5311CA2CBB /* Cocoa.framework */; };
0
- 9379BB510CA8616B006830B4 /* Person.m in Sources */ = {isa = PBXBuildFile; fileRef = 9379BB500CA8616B006830B4 /* Person.m */; };
0
     AB0670A50DED05BE00E0ABDA /* Nu.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AB0670A40DED05BE00E0ABDA /* Nu.framework */; };
0
     AB0670A70DED05E400E0ABDA /* main.nu in Resources */ = {isa = PBXBuildFile; fileRef = AB0670A60DED05E400E0ABDA /* main.nu */; };
0
 /* End PBXBuildFile section */
0
@@ -23,8 +21,6 @@
0
     089C1660FE840EACC02AAC07 /* English */ = {isa = PBXFileReference; fileEncoding = 10; lastKnownFileType = text.plist.strings; name = English; path = English.lproj/InfoPlist.strings; sourceTree = "<group>"; };
0
     1058C7A7FEA54F5311CA2CBB /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = /System/Library/Frameworks/Cocoa.framework; sourceTree = "<absolute>"; };
0
     13E42FBA07B3F13500E4EEF1 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = /System/Library/Frameworks/CoreData.framework; sourceTree = "<absolute>"; };
0
- 2A37F4ACFDCFA73011CA2CEA /* MyDocument.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MyDocument.m; sourceTree = "<group>"; };
0
- 2A37F4AEFDCFA73011CA2CEA /* MyDocument.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MyDocument.h; sourceTree = "<group>"; };
0
     2A37F4B0FDCFA73011CA2CEA /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
0
     2A37F4B5FDCFA73011CA2CEA /* English */ = {isa = PBXFileReference; lastKnownFileType = wrapper.nib; name = English; path = English.lproj/MyDocument.nib; sourceTree = "<group>"; };
0
     2A37F4B7FDCFA73011CA2CEA /* English */ = {isa = PBXFileReference; lastKnownFileType = wrapper.nib; name = English; path = English.lproj/MainMenu.nib; sourceTree = "<group>"; };
0
@@ -34,8 +30,6 @@
0
     32DBCF750370BD2300C91783 /* RaiseMan_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RaiseMan_Prefix.pch; sourceTree = "<group>"; };
0
     8D15AC360486D014006FF6A4 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
0
     8D15AC370486D014006FF6A4 /* RaiseMan.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = RaiseMan.app; sourceTree = BUILT_PRODUCTS_DIR; };
0
- 9379BB4F0CA8616B006830B4 /* Person.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Person.h; sourceTree = "<group>"; };
0
- 9379BB500CA8616B006830B4 /* Person.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Person.m; sourceTree = "<group>"; };
0
     AB0670A40DED05BE00E0ABDA /* Nu.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Nu.framework; path = /Library/Frameworks/Nu.framework; sourceTree = "<absolute>"; };
0
     AB0670A60DED05E400E0ABDA /* main.nu */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = main.nu; path = nu/main.nu; sourceTree = "<group>"; };
0
 /* End PBXFileReference section */
0
@@ -83,7 +77,6 @@
0
       isa = PBXGroup;
0
       children = (
0
         AB0670A30DED05A900E0ABDA /* Nu */,
0
- 2A37F4ABFDCFA73011CA2CEA /* Classes */,
0
         2A37F4AFFDCFA73011CA2CEA /* Other Sources */,
0
         2A37F4B8FDCFA73011CA2CEA /* Resources */,
0
         2A37F4C3FDCFA73011CA2CEA /* Frameworks */,
0
@@ -92,17 +85,6 @@
0
       name = RaiseMan;
0
       sourceTree = "<group>";
0
     };
0
- 2A37F4ABFDCFA73011CA2CEA /* Classes */ = {
0
- isa = PBXGroup;
0
- children = (
0
- 9379BB4F0CA8616B006830B4 /* Person.h */,
0
- 9379BB500CA8616B006830B4 /* Person.m */,
0
- 2A37F4AEFDCFA73011CA2CEA /* MyDocument.h */,
0
- 2A37F4ACFDCFA73011CA2CEA /* MyDocument.m */,
0
- );
0
- name = Classes;
0
- sourceTree = "<group>";
0
- };
0
     2A37F4AFFDCFA73011CA2CEA /* Other Sources */ = {
0
       isa = PBXGroup;
0
       children = (
0
@@ -200,9 +182,7 @@
0
       isa = PBXSourcesBuildPhase;
0
       buildActionMask = 2147483647;
0
       files = (
0
- 8D15AC310486D014006FF6A4 /* MyDocument.m in Sources */,
0
         8D15AC320486D014006FF6A4 /* main.m in Sources */,
0
- 9379BB510CA8616B006830B4 /* Person.m in Sources */,
0
       );
0
       runOnlyForDeploymentPostprocessing = 0;
0
     };
...
1
2
3
4
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
7
8
9
10
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
13
14
15
16
17
...
22
23
24
25
26
27
28
29
30
31
32
33
...
39
40
41
42
43
44
45
...
50
51
52
53
54
55
56
57
58
59
60
61
62
 
 
 
 
 
 
 
 
 
 
 
63
 
 
 
 
 
 
64
65
66
...
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
...
62
63
64
 
65
66
67
68
 
69
70
71
...
77
78
79
 
80
81
82
...
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
0
@@ -1,17 +1,57 @@
0
 (import Cocoa)    ;; bridgesupport
0
 (load "console")  ;; interactive console
0
 
0
-(class MyDocument
0
- (ivar (id) employees)
0
+(class Person is NSObject
0
+ (ivar (id) personName (float) expectedRaise)
0
+ (ivar-accessors)
0
+
0
+ (- (id) init is
0
+ (super init)
0
+ (set @expectedRaise 5.0)
0
+ (set @personName "New Person")
0
+ self)
0
+
0
+ (- (void) setNilValueForKey: (id) key is
0
+ (if (key isEqual:"expectedRaise")
0
+ (self setExpectedRaise:0.0)
0
+ (else (super setNilValueForKey:key)))))
0
+
0
+(class MyDocument is NSDocument
0
+ (ivar (id) employees (id) employeeController (id) tableView)
0
     
0
     (- (id) init is
0
- (NSLog "init")
0
         (super init)
0
         (self setEmployees:(array))
0
         self)
0
+
0
+ (- (BOOL) endEditing is
0
+ (set w (@tableView window))
0
+ (set editingEnded (w makeFirstResponder:w))
0
+ (unless (editingEnded) NO)
0
+ (set undo (self undoManager))
0
+ (if (undo groupingLevel)
0
+ (undo endUndoGrouping)
0
+ (undo beginUndoGrouping))
0
+ YES)
0
+
0
+ (- (void) createEmployee: (id) sender is
0
+ ; If a field is being edited, end editing
0
+ (unless (eq NO (self endEditing))
0
+
0
+ (set p (@employeeController newObject))
0
+ (@employeeController addObject:p)
0
+
0
+ ; In case the user has sorted the content array
0
+ (@employeeController rearrangeObjects)
0
+
0
+ ; Find the row of the new object
0
+ (set a (@employeeController arrangedObjects))
0
+ (set row (a indexOfObjectIdenticalTo:p))
0
+
0
+ ; Start editing in the first column
0
+ (@tableView editColumn:0 row:row withEvent:nil select:YES)))
0
         
0
     (- (void) startObservingPerson: (id) person is
0
- (NSLog "startObservingPerson: #{person}")
0
         (person addObserver:self
0
                  forKeyPath:"personName"
0
                     options:NSKeyValueObservingOptionOld
0
@@ -22,12 +62,10 @@
0
                     context:nil))
0
                     
0
     (- (void) stopObservingPerson: (id) person is
0
- (NSLog "stopObservingPerson: #{person}")
0
         (person removeObserver:self forKeyPath:"personName")
0
         (person removeObserver:self forKeyPath:"expectedRaise"))
0
 
0
     (- (void) insertObject: (id) p inEmployeesAtIndex: (int) r is
0
- (NSLog "InsertObject: #{p} inEmployeesAtIndex: #{r}")
0
         ;; Register the undo
0
         (set undo (self undoManager))
0
         ((undo prepareWithInvocationTarget:self)
0
@@ -39,7 +77,6 @@
0
         (@employees insertObject:p atIndex:r))
0
 
0
     (- (void) removeObjectFromEmployeesAtIndex: (int) r is
0
- (NSLog "removeObjectFromEmployeesAtIndex: #{r}")
0
         (set p (@employees r))
0
         (set undo (self undoManager))
0
         ((undo prepareWithInvocationTarget:self)
0
@@ -50,17 +87,30 @@
0
         (@employees removeObjectAtIndex:r))
0
 
0
     (- (void) setEmployees: (id) arr is
0
- (NSLog "setEmployees: #{arr}")
0
         (@employees each:(do (p) (self stopObservingPerson:p)))
0
         (set @employees arr)
0
         (@employees each:(do (p) (self startObservingPerson:p))))
0
 
0
     (- (void) changeKeyPath: (id) keyPath ofObject: (id) obj toValue:(id) newValue is
0
         (obj setValue:newValue forKeyPath:keyPath))
0
-
0
- )
0
-
0
+
0
+ (- (void) observeValueForKeyPath: (id) keyPath ofObject: (id) obj
0
+ change: (id) change context: (id) context is
0
+ (set undo (self undoManager))
0
+ (set oldValue (change objectForKey:NSKeyValueChangeOldKey))
0
+ (if (eq oldValue (NSNull null))
0
+ (set oldValue nil))
0
+ (NSLog "oldValue = #{oldValue}")
0
+ ((undo prepareWithInvocationTarget:self)
0
+ changeKeyPath:keyPath ofObject:obj toValue:oldValue)
0
+ (undo setActionName:"Edit"))
0
 
0
+ (- (id) windowNibName is "MyDocument")
0
+
0
+ (- (void) windowControllerDidLoadNib: (id) aController is
0
+ (super windowControllerDidLoadNib:aController)
0
+ ;; Add any code here that needs to be executed once the windowController has loaded the document's window.
0
+ ))
0
 
0
 (set SHOW_CONSOLE_AT_STARTUP nil)
0
 

Comments

    No one has commented yet.