|
8 | 8 |
|
9 | 9 | import Cocoa
|
10 | 10 |
|
11 |
| -class Document: NSDocument { |
| 11 | +private var KVOContext: Int = 0 |
12 | 12 |
|
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 | + } |
14 | 63 |
|
15 | 64 | override init() {
|
16 | 65 | super.init()
|
17 | 66 | // Add your subclass-specific initialization here.
|
18 | 67 | }
|
19 |
| - |
| 68 | + |
20 | 69 | override class func autosavesInPlace() -> Bool {
|
21 | 70 | return true
|
22 | 71 | }
|
23 |
| - |
| 72 | + |
24 | 73 | override var windowNibName: String? {
|
25 | 74 | // Returns the nib file name of the document
|
26 | 75 | // 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.
|
27 | 76 | return "Document"
|
28 | 77 | }
|
29 |
| - |
| 78 | + |
30 | 79 | override func dataOfType(typeName: String) throws -> NSData {
|
31 | 80 | // 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.
|
32 | 81 | // You can also choose to override fileWrapperOfType:error:, writeToURL:ofType:error:, or writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
|
33 | 82 | return NSKeyedArchiver.archivedDataWithRootObject(employees)
|
34 | 83 | }
|
35 |
| - |
| 84 | + |
36 | 85 | override func readFromData(data: NSData, ofType typeName: String) throws {
|
37 | 86 | // 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.
|
38 | 87 | // You can also choose to override readFromFileWrapper:ofType:error: or readFromURL:ofType:error: instead.
|
39 | 88 | // If you override either of these, you should also override -isEntireFileLoaded to return false if the contents are lazily loaded.
|
40 | 89 | print("read???")
|
41 | 90 | }
|
| 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 | + |
42 | 151 |
|
43 |
| - |
| 152 | + func windowWillClose(notification: NSNotification) { |
| 153 | + employees=[] |
| 154 | + } |
44 | 155 | }
|
45 | 156 |
|
0 commit comments