Add project node, handle item expansion

This commit is contained in:
Matthias Neeracher 2014-11-16 21:10:04 +01:00 committed by Matthias Neeracher
parent 2c59b97dd9
commit 5fe686b133
2 changed files with 57 additions and 11 deletions

View File

@ -52,6 +52,9 @@ class ASFileNode {
func nodeName() -> String { func nodeName() -> String {
return "" return ""
} }
func apply(closure:(ASFileNode)->()) {
closure(self)
}
} }
class ASFileGroup : ASFileNode { class ASFileGroup : ASFileNode {
@ -70,6 +73,18 @@ class ASFileGroup : ASFileNode {
override func nodeName() -> String { override func nodeName() -> String {
return (expanded ? "📂" : "📁")+" "+name return (expanded ? "📂" : "📁")+" "+name
} }
override func apply(closure: (ASFileNode) -> ()) {
super.apply(closure)
for child in children {
child.apply(closure)
}
}
}
class ASProject : ASFileGroup {
override func nodeName() -> String {
return "📘 "+name
}
} }
class ASFileItem : ASFileNode { class ASFileItem : ASFileNode {
@ -86,7 +101,7 @@ class ASFileItem : ASFileNode {
} }
class ASFileTree : NSObject, NSOutlineViewDataSource { class ASFileTree : NSObject, NSOutlineViewDataSource {
let root = ASFileGroup() let root = ASProject()
func addFileURL(url: NSURL, omitUnknown: Bool = true) { func addFileURL(url: NSURL, omitUnknown: Bool = true) {
let type = ASFileType.guessForURL(url) let type = ASFileType.guessForURL(url)
@ -94,22 +109,32 @@ class ASFileTree : NSObject, NSOutlineViewDataSource {
root.children.append(ASFileItem(url: url, type: type)) root.children.append(ASFileItem(url: url, type: type))
} }
} }
func setProjectURL(url: NSURL) {
root.name = url.lastPathComponent.stringByDeletingPathExtension
}
func apply(closure: (ASFileNode) -> ()) {
root.apply(closure)
}
func outlineView(outlineView: NSOutlineView, numberOfChildrenOfItem item: AnyObject?) -> Int { func outlineView(outlineView: NSOutlineView, numberOfChildrenOfItem item: AnyObject?) -> Int {
if item == nil { if item == nil {
return root.children.count return 1
} else { } else {
return (item as ASFileGroup).children.count return (item as ASFileGroup).children.count
} }
} }
func outlineView(outlineView: NSOutlineView, child index: Int, ofItem item: AnyObject?) -> AnyObject { func outlineView(outlineView: NSOutlineView, child index: Int, ofItem item: AnyObject?) -> AnyObject {
let group = (item == nil) ? root : (item as ASFileGroup) if item == nil {
return root
} else {
let group = item as ASFileGroup
return group.children[index] return group.children[index]
} }
}
func outlineView(outlineView: NSOutlineView, isItemExpandable item: AnyObject) -> Bool { func outlineView(outlineView: NSOutlineView, isItemExpandable item: AnyObject) -> Bool {
return item is ASFileGroup return item is ASFileGroup
} }
func outlineView(outlineView: NSOutlineView, objectValueForTableColumn tableColumn: NSTableColumn?, byItem item: AnyObject?) -> AnyObject? { func outlineView(outlineView: NSOutlineView, objectValueForTableColumn tableColumn: NSTableColumn?, byItem item: AnyObject?) -> AnyObject? {
return (item as ASFileItem).nodeName() return (item as ASFileNode).nodeName()
} }
} }

View File

@ -18,10 +18,26 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate {
super.init() super.init()
// Add your subclass-specific initialization here. // Add your subclass-specific initialization here.
} }
override func finalize() {
saveCurEditor()
}
func saveCurEditor() {
if let file = (mainEditor as? ASFileItem) {
editor.string().writeToURL(file.url, atomically: true, encoding: NSUTF8StringEncoding, error: nil)
}
}
override func windowControllerDidLoadNib(aController: NSWindowController) { override func windowControllerDidLoadNib(aController: NSWindowController) {
super.windowControllerDidLoadNib(aController) super.windowControllerDidLoadNib(aController)
outline.setDataSource(files) outline.setDataSource(files)
files.apply() { node in
if let group = node as? ASFileGroup {
if group.expanded {
self.outline.expandItem(node)
}
}
}
} }
override class func autosavesInPlace() -> Bool { override class func autosavesInPlace() -> Bool {
@ -63,6 +79,9 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate {
} else { } else {
success = true success = true
} }
if success {
files.setProjectURL(fileURL!)
}
return success return success
} }
@ -77,11 +96,13 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate {
editor.setMode(UInt(file.type.aceMode)) editor.setMode(UInt(file.type.aceMode))
} }
} }
func outlineViewItemDidExpand(notification: NSNotification) {
func saveCurEditor() { let group = notification.userInfo!["NSObject"] as ASFileGroup
if let file = (mainEditor as? ASFileItem) { group.expanded = true
editor.string().writeToURL(file.url, atomically: true, encoding: NSUTF8StringEncoding, error: nil)
} }
func outlineViewItemDidCollapse(notification: NSNotification) {
let group = notification.userInfo!["NSObject"] as ASFileGroup
group.expanded = false
} }
} }