Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
1,451 additions
and
30 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// GNFileSystemItem.h | ||
// | ||
// Based on code examples in NSOutlineView documentation. | ||
|
||
#import <Cocoa/Cocoa.h> | ||
|
||
|
||
@interface GNFileSystemItem : NSObject | ||
{ | ||
NSString *relativePath; | ||
GNFileSystemItem *parent; | ||
NSMutableArray *children; | ||
NSImage *image; | ||
} | ||
|
||
+ (GNFileSystemItem *)rootItem; | ||
+ (NSString *)repoRoot; | ||
- (int)numberOfChildren;// Returns -1 for leaf nodes | ||
- (GNFileSystemItem *)childAtIndex:(int)n;// Invalid to call on leaf nodes | ||
- (NSString *)fullPath; | ||
- (NSString *)relativePath; | ||
- (void)setRelativePath:(NSString *)aString; | ||
- (NSImage*)icon; | ||
- (BOOL)isLeafNode; | ||
- (NSDictionary*)attributes; | ||
- (BOOL)isHeading; | ||
- (BOOL)ignoredByGit:(NSString *)fileItem; | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
// | ||
// GNFileSystemItem.m | ||
// | ||
|
||
#import "GNFileSystemItem.h" | ||
|
||
|
||
@implementation GNFileSystemItem | ||
|
||
static GNFileSystemItem *rootItem = nil; | ||
#define IsALeafNode ((id)-1) | ||
|
||
- (id)initWithPath:(NSString *)path parent:(GNFileSystemItem *)obj | ||
{ | ||
if (self = [super init]) | ||
{ | ||
relativePath = [[path lastPathComponent] copy]; | ||
parent = obj; | ||
} | ||
return self; | ||
} | ||
|
||
|
||
+ (GNFileSystemItem *)rootItem | ||
{ | ||
if (rootItem == nil) | ||
{ | ||
rootItem = [[GNFileSystemItem alloc] initWithPath:[GNFileSystemItem repoRoot] parent:nil]; | ||
} | ||
return rootItem; | ||
} | ||
|
||
+ (NSString *)repoRoot { | ||
return [[[NSApplication sharedApplication] delegate] repository_location]; | ||
} | ||
|
||
// - (BOOL)ignoredByGit:(NSString *)fileItem | ||
// { | ||
// NSString *format; | ||
// if([[self fullPath] hasSuffix:@"/"]) | ||
// format = @"%@%@"; | ||
// else | ||
// format = @"%@/%@"; | ||
// | ||
// NSString *file = [NSString stringWithFormat:format, [self fullPath], fileItem]; | ||
// | ||
// BOOL ignored = [[[NSApplication sharedApplication] delegate] is_file_ignored:file]; | ||
// return !ignored; | ||
// } | ||
|
||
// Creates, caches, and returns the array of children | ||
// Loads children incrementally | ||
- (NSArray *)children | ||
{ | ||
if (children == NULL) | ||
{ | ||
NSFileManager *fileManager = [NSFileManager defaultManager]; | ||
NSString *fullPath = [self fullPath]; | ||
BOOL isDir, valid = [fileManager fileExistsAtPath:fullPath isDirectory:&isDir]; | ||
|
||
if (valid && isDir) | ||
{ | ||
NSArray *array = [fileManager directoryContentsAtPath:fullPath]; | ||
int numChildren = [array count]; | ||
children = [[NSMutableArray alloc] initWithCapacity:numChildren]; | ||
for (NSString *sourceFile in array) | ||
{ | ||
if([[sourceFile lastPathComponent] hasPrefix:@"."]) { | ||
continue; | ||
} | ||
|
||
// if([self ignoredByGit:sourceFile]) { | ||
// continue; | ||
// } | ||
|
||
GNFileSystemItem *newChild = [[GNFileSystemItem alloc] initWithPath:sourceFile parent:self]; | ||
[children addObject:newChild]; | ||
[newChild release]; | ||
} | ||
} | ||
else | ||
{ | ||
children = IsALeafNode; | ||
} | ||
} | ||
return children; | ||
} | ||
|
||
|
||
- (NSImage*)icon | ||
{ | ||
if (image == nil) | ||
{ | ||
image = [[[NSWorkspace sharedWorkspace] iconForFile: [self fullPath]] retain]; | ||
} | ||
|
||
return image; | ||
} | ||
|
||
|
||
- (NSDictionary*)attributes | ||
{ | ||
return [NSDictionary dictionaryWithDictionary: [[NSFileManager defaultManager] fileAttributesAtPath: [self fullPath] traverseLink: YES]]; | ||
} | ||
|
||
|
||
- (NSString *)relativePath | ||
{ | ||
return relativePath; | ||
} | ||
|
||
|
||
- (void)setRelativePath:(NSString *)aString | ||
{ | ||
if (aString != relativePath) | ||
{ | ||
[relativePath release]; | ||
[relativePath = aString copy]; | ||
} | ||
} | ||
|
||
|
||
- (NSString *)fullPath | ||
{ | ||
// If no parent, return our own relative path | ||
if (parent == nil) { | ||
return [GNFileSystemItem repoRoot]; | ||
} | ||
// recurse up the hierarchy, prepending each parent’s path | ||
return [[parent fullPath] stringByAppendingPathComponent:relativePath]; | ||
} | ||
|
||
|
||
- (GNFileSystemItem *)childAtIndex:(int)n | ||
{ | ||
return [[self children] objectAtIndex:n]; | ||
} | ||
|
||
|
||
- (BOOL)isLeafNode | ||
{ | ||
return [self children] == IsALeafNode; | ||
} | ||
|
||
- (int)numberOfChildren | ||
{ | ||
id tmp = [self children]; | ||
return (tmp == IsALeafNode) ? (-1) : [tmp count]; | ||
} | ||
|
||
- (BOOL)isHeading | ||
{ | ||
return [[self fullPath] isEqualToString:[GNFileSystemItem repoRoot]]; | ||
} | ||
|
||
- (void)dealloc | ||
{ | ||
[image release]; | ||
if (children != IsALeafNode) [children release]; | ||
[relativePath release]; | ||
[super dealloc]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#import <Cocoa/Cocoa.h> | ||
|
||
@interface GNTreeDataSource : NSObject | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#import "GNTreeDataSource.h" | ||
#import "GNFileSystemItem.h" | ||
|
||
@implementation GNTreeDataSource | ||
|
||
// Data Source methods | ||
|
||
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item { | ||
return (item == nil) ? 1 : [item numberOfChildren]; | ||
} | ||
|
||
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item { | ||
return (item == nil) ? YES : ([item numberOfChildren] != -1); | ||
} | ||
|
||
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item { | ||
return (item == nil) ? [GNFileSystemItem rootItem] : [(GNFileSystemItem *)item childAtIndex:index]; | ||
} | ||
|
||
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item { | ||
return (item == nil) ? @"/" : (id)[item relativePath]; | ||
} | ||
|
||
- (NSImage *)outlineView: (NSOutlineView *)outlineView iconOfItem: (id)item | ||
{ | ||
// Note: -observedObject is needed here due to use of Bindings and the | ||
// mess surrounding NSTreeController (described at the URL above). | ||
// If you avoid Bindings and use a normal dataSource, you can simply | ||
// return [item icon] here, as the actual item will be passed in. | ||
|
||
return [item icon]; | ||
} | ||
|
||
// Delegate methods | ||
- (BOOL)outlineView:(NSOutlineView *)sender isGroupItem:(id)item { | ||
return [item isHeading]; | ||
} | ||
|
||
- (void)outlineView:(NSOutlineView *)sender willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item { | ||
if ([item isHeading]) { | ||
NSMutableAttributedString *newTitle = [[cell attributedStringValue] mutableCopy]; | ||
[newTitle replaceCharactersInRange:NSMakeRange(0,[newTitle length]) withString:[[newTitle string] uppercaseString]]; | ||
[cell setAttributedStringValue:newTitle]; | ||
[newTitle release]; | ||
} | ||
} | ||
|
||
- (NSCell *)outlineView:(NSOutlineView *)sender dataCellForTableColumn:(id)cell item:(id)item | ||
{ | ||
if([item isHeading]) | ||
return [[[NSTextFieldCell alloc] init] autorelease]; | ||
|
||
return nil; | ||
} | ||
|
||
- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item | ||
{ | ||
BOOL isDir; | ||
[[NSFileManager defaultManager] fileExistsAtPath:[item fullPath] isDirectory:&isDir]; | ||
|
||
if([item isHeading] || isDir) | ||
return NO; | ||
|
||
return YES; | ||
} | ||
|
||
@end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.