// // NavigatorController.swift // DocHub // // Created by Tristan Juricek on 6/24/14. // Copyright (c) 2014 Perforce. All rights reserved. // import AppKit import Foundation // This is more of a placeholder than anything else at the moment class NavigatorController : NSViewController, NSBrowserDelegate { let docHubLocation:DocHubLocation @IBOutlet var browser:NSBrowser { didSet { browser.maxVisibleColumns = 1 } } @IBOutlet var splitView:NSSplitView init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!, docHubLocation:DocHubLocation) { self.docHubLocation = docHubLocation super.init(nibName:nibNameOrNil, bundle: nibBundleOrNil) self.docHubLocation.areaObserver.listeners.append({ self.areaChanged($0) }) } override func viewWillAppear() { splitView.setPosition(120, ofDividerAtIndex: 0) } func areaChanged(area: Area) { browser.reloadColumn(0) } // The column index should indicate the directory count in the location func browser(sender: NSBrowser!, isColumnValid column: Int) -> Bool { return column <= self.docHubLocation.directoryCount } func updateSelection() { if let indexPath = browser.selectionIndexPath { if indexPath.length > 0 { let dirColumn = indexPath.length - 1 let row = indexPath.indexAtPosition(dirColumn) if let dir = self.docHubLocation.directoryAt(dirColumn) { if row < dir.sorted.count { let namable = dir.sorted[row] if namable is Directory { docHubLocation.directory = namable as? Directory } else if namable is File { docHubLocation.file = namable as? File } } } } } } func browser(sender: NSBrowser!, numberOfRowsInColumn column: Int) -> Int { updateSelection() if let dir = self.docHubLocation.directoryAt(column) { return dir.children.count + dir.files.count } return 0 } func browser(browser: NSBrowser!, numberOfChildrenOfItem item: AnyObject!) -> Int { println("item? \(item)") return 0 } func browser(sender: NSBrowser!, willDisplayCell cell: AnyObject!, atRow row: Int, column: Int) { let browserCell = cell as NSBrowserCell if let dir = self.docHubLocation.directoryAt(column) { let namable = dir.sorted[row] browserCell.stringValue = namable.name browserCell.leaf = namable is File } } }