1. /*
  2.      File: AppController.m
  3.  Abstract: Application Controller object, and the NSBrowser delegate. An instance of this object is in the MainMenu.xib.
  4.   Version: 1.1
  5.  
  6.  Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple
  7.  Inc. ("Apple") in consideration of your agreement to the following
  8.  terms, and your use, installation, modification or redistribution of
  9.  this Apple software constitutes acceptance of these terms.  If you do
  10.  not agree with these terms, please do not use, install, modify or
  11.  redistribute this Apple software.
  12.  
  13.  In consideration of your agreement to abide by the following terms, and
  14.  subject to these terms, Apple grants you a personal, non-exclusive
  15.  license, under Apple's copyrights in this original Apple software (the
  16.  "Apple Software"), to use, reproduce, modify and redistribute the Apple
  17.  Software, with or without modifications, in source and/or binary forms;
  18.  provided that if you redistribute the Apple Software in its entirety and
  19.  without modifications, you must retain this notice and the following
  20.  text and disclaimers in all such redistributions of the Apple Software.
  21.  Neither the name, trademarks, service marks or logos of Apple Inc. may
  22.  be used to endorse or promote products derived from the Apple Software
  23.  without specific prior written permission from Apple.  Except as
  24.  expressly stated in this notice, no other rights or licenses, express or
  25.  implied, are granted by Apple herein, including but not limited to any
  26.  patent rights that may be infringed by your derivative works or by other
  27.  works in which the Apple Software may be incorporated.
  28.  
  29.  The Apple Software is provided by Apple on an "AS IS" basis.  APPLE
  30.  MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
  31.  THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
  32.  FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
  33.  OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
  34.  
  35.  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
  36.  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  37.  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  38.  INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
  39.  MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
  40.  AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
  41.  STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
  42.  POSSIBILITY OF SUCH DAMAGE.
  43.  
  44.  Copyright (C) 2011 Apple Inc. All Rights Reserved.
  45.  
  46.  */
  47.  
  48.  
  49.  
  50. #import "AppController.h"
  51. #import "FileSystemNode.h"
  52.  
  53. // Turn on or off this define to use the new SnowLeopard item-based API
  54. #define USE_ITEM_BASED_API 1
  55.  
  56. @implementation AppController
  57.  
  58. - (void)dealloc {
  59.     [_rootNode release];
  60.     [super dealloc];
  61. }
  62.  
  63. - (void) awakeFromNib
  64. {
  65.     [_browser setDelegate:self];
  66.     [_browser setDraggingSourceOperationMask:NSDragOperationAll forLocal:NO];
  67. }
  68.  
  69.  
  70. - (BOOL)browser:(NSBrowser *)browser canDragRowsWithIndexes:(NSIndexSet *)rowIndexes inColumn:(NSInteger)column withEvent:(NSEvent *)event
  71. {
  72.     return YES;
  73. }
  74.  
  75. - (BOOL)browser:(NSBrowser *)browser writeRowsWithIndexes:(NSIndexSet *)rowIndexes inColumn:(NSInteger)column toPasteboard:(NSPasteboard *)pasteboard
  76. {
  77.     BOOL bValue;
  78.    
  79.     NSInteger row = [rowIndexes lastIndex];
  80.     FileSystemNode *item = [browser itemAtRow:row inColumn:column];
  81.    
  82.     [pasteboard declareTypes:@[NSPasteboardTypeString, NSPasteboardTypeHTML] owner:self];
  83.     bValue = [pasteboard setString:item.displayName forType:NSPasteboardTypeString];
  84.     bValue = [pasteboard setString:[NSString stringWithFormat:@"<html><body>%@</body></html>", item.displayName] forType:NSPasteboardTypeHTML];
  85.    
  86.     return YES;
  87. }
  88.  
  89. #if USE_ITEM_BASED_API
  90.  
  91. // This method is optional, but makes the code much easier to understand
  92. - (id)rootItemForBrowser:(NSBrowser *)browser {
  93.     if (_rootNode == nil) {
  94.         _rootNode = [[FileSystemNode alloc] initWithURL:[NSURL fileURLWithPath:@"/"]];
  95.     }
  96.     return _rootNode;    
  97. }
  98.  
  99. - (NSInteger)browser:(NSBrowser *)browser numberOfChildrenOfItem:(id)item {
  100.     FileSystemNode *node = (FileSystemNode *)item;
  101.     return node.children.count;
  102. }
  103.  
  104. - (id)browser:(NSBrowser *)browser child:(NSInteger)index ofItem:(id)item {
  105.     FileSystemNode *node = (FileSystemNode *)item;
  106.     return [node.children objectAtIndex:index];
  107. }
  108.  
  109. - (BOOL)browser:(NSBrowser *)browser isLeafItem:(id)item {
  110.     FileSystemNode *node = (FileSystemNode *)item;
  111.     return !node.isDirectory;
  112. }
  113.  
  114. - (id)browser:(NSBrowser *)browser objectValueForItem:(id)item {
  115.     FileSystemNode *node = (FileSystemNode *)item;
  116.     return node.displayName;
  117. }
  118.  
  119. #else
  120.  
  121. // This is a utility method to find the parent item for a given column. The item based API eliminates the need for this method.
  122. - (FileSystemNode *)parentNodeForColumn:(NSInteger)column {
  123.     if (_rootNode == nil) {
  124.         _rootNode = [[FileSystemNode alloc] initWithURL:[NSURL fileURLWithPath:@"/"]];
  125.     }
  126.    
  127.     FileSystemNode *result = _rootNode;
  128.     // Walk up to this column, finding the selected row in the column before it and using that in the children array
  129.     for (NSInteger i = 0; i < column; i++) {
  130.         NSInteger selectedRowInColumn = [_browser selectedRowInColumn:i];
  131.         FileSystemNode *selectedChildNode = [result.children objectAtIndex:selectedRowInColumn];
  132.         result = selectedChildNode;
  133.     }
  134.    
  135.     return result;
  136. }
  137.  
  138. // Non-item based API example. This code will work on all systems, but applications targeting SnowLeopard and higher should use the new item-based API.
  139.  
  140. - (NSInteger)browser:(NSBrowser *)sender numberOfRowsInColumn:(NSInteger)column {
  141.     FileSystemNode *parentNode = [self parentNodeForColumn:column];
  142.     return parentNode.children.count;
  143. }
  144.  
  145. - (void)browser:(NSBrowser *)sender willDisplayCell:(NSBrowserCell *)cell atRow:(NSInteger)row column:(NSInteger)column {
  146.     // Lazily setup the cell's properties in this method
  147.     FileSystemNode *parentNode = [self parentNodeForColumn:column];
  148.     FileSystemNode *childNode = [parentNode.children objectAtIndex:row];
  149.     [cell setTitle:childNode.displayName];
  150.     [cell setLeaf:!childNode.isDirectory];
  151. }
  152.  
  153. #endif
  154.  
  155.  
  156. @end