// // LibraryOutlineViewDataSource.swift // DocHub // // Created by Tristan Juricek on 6/19/14. // Copyright (c) 2014 Perforce. All rights reserved. // import AppKit import Foundation // Loads all libraries and provides areas as subitems of each library. class LibraryDataSource: NSObject, NSOutlineViewDataSource { // Our current source list is just the different library connections var libraries:Library[] = [] init() { super.init() updateLibraries() } func updateLibraries() { let p4Settings = P4ConnectionSettings.loadAll() var libs:Library[] = [] for p4setting in p4Settings { libs.append(P4Library(settings:p4setting)) } libraries = libs } // Return the child item at the specified index of a given item. // If item is nil, return the appropriate child item of the root. func outlineView(outlineView: NSOutlineView!, child index: Int, ofItem item: AnyObject!) -> AnyObject! { if item == nil { return libraries[index] } else { if let library = getLibraryForItem(item!) { return library.areas[index] } } return nil } func getLibraryForItem(item: AnyObject) -> Library? { for library in libraries { if (item is Library ? true : false) && library == (item as Library){ return library } } return nil } // Returns true if the item is expandable func outlineView(outlineView: NSOutlineView!, isItemExpandable item: AnyObject!) -> Bool { // This appears to be a compiler bug requiring a ternary operator return (item is Library ? true : false) } // Returns the number of child items encompassed by a given item. func outlineView(outlineView: NSOutlineView!, numberOfChildrenOfItem item: AnyObject!) -> Int { if item == nil { return libraries.count } else { if let library = getLibraryForItem(item!) { return library.areas.count } } return 0 } // Invoked by outlineView to return the data object associated with the // specified item. func outlineView(outlineView: NSOutlineView!, objectValueForTableColumn tableColumn: NSTableColumn!, byItem item: AnyObject!) -> AnyObject! { if (item is Library ? true: false) { return (item as Library).name } return "oops" } // Set the data object for a given item in a given column. func outlineView(outlineView: NSOutlineView!, setObjectValue object: AnyObject!, forTableColumn tableColumn: NSTableColumn!, byItem item: AnyObject!) { // no idea what I need to do here println("calling setObjectValue \(item)") } }