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 {
return ""
}
func apply(closure:(ASFileNode)->()) {
closure(self)
}
}
class ASFileGroup : ASFileNode {
@ -70,6 +73,18 @@ class ASFileGroup : ASFileNode {
override func nodeName() -> String {
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 {
@ -86,7 +101,7 @@ class ASFileItem : ASFileNode {
}
class ASFileTree : NSObject, NSOutlineViewDataSource {
let root = ASFileGroup()
let root = ASProject()
func addFileURL(url: NSURL, omitUnknown: Bool = true) {
let type = ASFileType.guessForURL(url)
@ -94,22 +109,32 @@ class ASFileTree : NSObject, NSOutlineViewDataSource {
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 {
if item == nil {
return root.children.count
return 1
} else {
return (item as ASFileGroup).children.count
}
}
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]
}
}
func outlineView(outlineView: NSOutlineView, isItemExpandable item: AnyObject) -> Bool {
return item is ASFileGroup
}
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()
// 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) {
super.windowControllerDidLoadNib(aController)
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 {
@ -63,6 +79,9 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate {
} else {
success = true
}
if success {
files.setProjectURL(fileURL!)
}
return success
}
@ -77,11 +96,13 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate {
editor.setMode(UInt(file.type.aceMode))
}
}
func saveCurEditor() {
if let file = (mainEditor as? ASFileItem) {
editor.string().writeToURL(file.url, atomically: true, encoding: NSUTF8StringEncoding, error: nil)
func outlineViewItemDidExpand(notification: NSNotification) {
let group = notification.userInfo!["NSObject"] as ASFileGroup
group.expanded = true
}
func outlineViewItemDidCollapse(notification: NSNotification) {
let group = notification.userInfo!["NSObject"] as ASFileGroup
group.expanded = false
}
}