Skip to content

Commit 6deefca

Browse files
committed
RaiseMan: Added UndoManager;
When a new person is added, it is switched to the Edit mode automatically.
1 parent bcb6760 commit 6deefca

File tree

3 files changed

+123
-13
lines changed

3 files changed

+123
-13
lines changed

RaiseMan/RaiseMan/AppDelegate.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ import Cocoa
1010

1111
@NSApplicationMain
1212
class AppDelegate: NSObject, NSApplicationDelegate {
13-
14-
15-
1613
func applicationDidFinishLaunching(aNotification: NSNotification) {
1714
// Insert code here to initialize your application
1815
}

RaiseMan/RaiseMan/Base.lproj/Document.xib

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="10116" systemVersion="15E65" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
2+
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="10117" systemVersion="15E65" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
33
<dependencies>
44
<deployment identifier="macosx"/>
5-
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="10116"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="10117"/>
66
</dependencies>
77
<objects>
88
<customObject id="-2" userLabel="File's Owner" customClass="Document" customModule="RaiseMan" customModuleProvider="target">
99
<connections>
10+
<outlet property="arrayController" destination="ste-dU-et9" id="Zza-95-L3L"/>
11+
<outlet property="tableView" destination="kMK-L1-6kC" id="vl3-Z0-M0b"/>
1012
<outlet property="window" destination="xOd-HO-29H" id="JIz-fz-R2o"/>
1113
</connections>
1214
</customObject>
@@ -144,7 +146,7 @@
144146
<font key="font" metaFont="system"/>
145147
</buttonCell>
146148
<connections>
147-
<action selector="add:" target="ste-dU-et9" id="sH1-Bb-RI4"/>
149+
<action selector="addEmployee:" target="-2" id="2Rv-2k-gEK"/>
148150
<binding destination="ste-dU-et9" name="enabled" keyPath="canAdd" id="mNF-7d-LsT"/>
149151
</connections>
150152
</button>

RaiseMan/RaiseMan/Document.swift

Lines changed: 118 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,149 @@
88

99
import Cocoa
1010

11-
class Document: NSDocument {
11+
private var KVOContext: Int = 0
1212

13-
var employees: [Employee] = []
13+
class Document: NSDocument, NSWindowDelegate {
14+
15+
@IBOutlet weak var tableView: NSTableView!
16+
@IBOutlet var arrayController: NSArrayController!
17+
18+
@IBAction func addEmployee(sender: AnyObject) {
19+
let windowController = windowControllers[0]
20+
let window = windowController.window!
21+
22+
let endedEditing = window.makeFirstResponder(window)
23+
if !endedEditing {
24+
print("Unable to end editing.")
25+
return
26+
}
27+
28+
let undo: NSUndoManager = undoManager!
29+
30+
if undo.groupingLevel > 0 {
31+
undo.endUndoGrouping()
32+
undo.beginUndoGrouping()
33+
}
34+
35+
let employee = arrayController.newObject() as! Employee
36+
arrayController.addObject(employee)
37+
38+
arrayController.rearrangeObjects()
39+
40+
let sortedEmployees = arrayController.arrangedObjects as! [Employee]
41+
42+
let row = sortedEmployees.indexOf(employee)!
43+
44+
print("starting edit of \(employee) in row \(row)")
45+
tableView.editColumn(0,
46+
row: row,
47+
withEvent: nil,
48+
select: true)
49+
}
50+
51+
var employees: [Employee] = [] {
52+
willSet {
53+
for employee in employees {
54+
stopObservingEmployee(employee)
55+
}
56+
}
57+
didSet {
58+
for employee in employees {
59+
startObservingEmployee(employee)
60+
}
61+
}
62+
}
1463

1564
override init() {
1665
super.init()
1766
// Add your subclass-specific initialization here.
1867
}
19-
68+
2069
override class func autosavesInPlace() -> Bool {
2170
return true
2271
}
23-
72+
2473
override var windowNibName: String? {
2574
// Returns the nib file name of the document
2675
// If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this property and override -makeWindowControllers instead.
2776
return "Document"
2877
}
29-
78+
3079
override func dataOfType(typeName: String) throws -> NSData {
3180
// Insert code here to write your document to data of the specified type. If outError != nil, ensure that you create and set an appropriate error when returning nil.
3281
// You can also choose to override fileWrapperOfType:error:, writeToURL:ofType:error:, or writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
3382
return NSKeyedArchiver.archivedDataWithRootObject(employees)
3483
}
35-
84+
3685
override func readFromData(data: NSData, ofType typeName: String) throws {
3786
// Insert code here to read your document from the given data of the specified type. If outError != nil, ensure that you create and set an appropriate error when returning false.
3887
// You can also choose to override readFromFileWrapper:ofType:error: or readFromURL:ofType:error: instead.
3988
// If you override either of these, you should also override -isEntireFileLoaded to return false if the contents are lazily loaded.
4089
print("read???")
4190
}
91+
92+
func insertObject(employee: Employee, inEmployeesAtIndex index: Int) {
93+
print("adding \(employee) to the employees array")
94+
95+
let undo: NSUndoManager = undoManager!
96+
undo.prepareWithInvocationTarget(self).removeObjectFromEmployeesAtIndex(employees.count)
97+
98+
if !undo.undoing {
99+
undo.setActionName("Add Person")
100+
}
101+
102+
employees.append(employee)
103+
}
104+
105+
func removeObjectFromEmployeesAtIndex(index: Int) {
106+
let employee: Employee = employees[index]
107+
108+
print("removing \(employee) from the employees array")
109+
110+
let undo: NSUndoManager = undoManager!
111+
undo.prepareWithInvocationTarget(self).insertObject(employee, inEmployeesAtIndex: index)
112+
113+
if !undo.undoing {
114+
undo.setActionName("Remove Person")
115+
}
116+
117+
employees.removeAtIndex(index)
118+
}
119+
120+
func startObservingEmployee(employee: Employee) {
121+
employee.addObserver(self, forKeyPath: "name", options: .Old, context: &KVOContext)
122+
employee.addObserver(self, forKeyPath: "raise", options: .Old, context: &KVOContext)
123+
}
124+
125+
func stopObservingEmployee(employee: Employee) {
126+
employee.removeObserver(self, forKeyPath: "name", context: &KVOContext)
127+
employee.removeObserver(self, forKeyPath: "raise", context: &KVOContext)
128+
}
129+
130+
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
131+
if context != &KVOContext {
132+
// If the context does not match, this message
133+
// must be intended for our superclass.
134+
super.observeValueForKeyPath(keyPath,
135+
ofObject: object,
136+
change: change,
137+
context: context)
138+
return
139+
}
140+
141+
var oldValue: AnyObject? = change![NSKeyValueChangeOldKey]
142+
if oldValue is NSNull {
143+
oldValue = nil
144+
}
145+
146+
let undo: NSUndoManager = undoManager!
147+
print("oldValue=\(oldValue)")
148+
undo.prepareWithInvocationTarget(object!).setValue(oldValue, forKeyPath: keyPath!)
149+
}
150+
42151

43-
152+
func windowWillClose(notification: NSNotification) {
153+
employees=[]
154+
}
44155
}
45156

0 commit comments

Comments
 (0)