6

I am trying to create a NSOutlineVew with a custom header group (parent node) for listed objects. (NOTE: I have cell-based NSOutlineView). For example it look like as the Xcode "Navigator" or Numbers sidebar. I used the default groups for the separation properties per category, but it's looks like not as what I want. I need a parent node (cell), which I'll can visually adjust (add a controls elements and image).

I tried to do this by passing an array of objects to NSDictionary, giving each group a certain specific key. And a result, via NSLog everything is displayed correctly, but the transfer of this variable as the source for the program NSOulineView fails.

ProjectViewController.h

@interface ProjectViewController : NSViewController <NSOutlineViewDataSource, NSObject> {
    IBOutlet NSOutlineView          *outlineView;
    FSEntity                        *content;
}

@property (readonly, assign) NSMutableArray *objects;

@end

ProjectViewController.m

@implementation ProjectViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Initialization code here.    
        // Setting default path to the local file or directory
        NSString *home = NSHomeDirectory();
        NSURL *url = [[NSURL alloc] initFileURLWithPath:home];
        content = [[FSEntity alloc] initWithURL:url];

        [self defineContentNSOutlineView];
        NSLog(@"Array: %@",_objects);

        // Basic сonfiguration an instance NSOutlineView
        [self configurationNSOutlineView];
    } return self;
}

@synthesize objects = _objects;

- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
    return (item == nil) ? [content.children objectAtIndex:index] : [((FSEntity *)item).children objectAtIndex:index];
}

- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
    return (item == nil) ? content.children.count > 0 : ((FSEntity *)item).children.count > 0;
}

- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
    return (item == nil) ? content.children.count : ((FSEntity *)item).children.count;
}

- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
    if ([item isKindOfClass:[FSEntity class]]) {
        return [((FSEntity *)item) title];
    }

    return nil;
}

- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item {
    if ([cell isKindOfClass:[ImageAndTextCell class]]) {
        ImageAndTextCell *textField = (ImageAndTextCell *)cell;
        [textField setImage:[item icon]];
    }
}

- (void)defineContentNSOutlineView {
    NSMutableArray *objects = [NSMutableArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"FINDER", @"title", [NSArray arrayWithObjects:[NSDictionary dictionaryWithObject:content.children forKey:@"title"], nil], @"children",[NSNumber numberWithBool:YES], @"header", nil], nil];
    _objects = objects;
}

- (void)configurationNSOutlineView {
    [outlineView sizeLastColumnToFit];
    [outlineView setFloatsGroupRows:NO];
    [outlineView reloadData];
    [outlineView expandItem:nil expandChildren:YES];
}

@end

To easier would imagine how it would look, I showed it on the scheme:

                 +--------------------------------------------+
                 |  ▼ FINDER FILES                        ₪ ✱ |
                 |      03143553.file                         |
                 |    ▶ Desktop                               |
                 |    ▶ Documents                             |
                 |    ▶ Downloads                             |
                 |    ▶ Movies                                |
                 |    ▶ Music                                 |
                 |    ▶ Pictures                              |
                 +--------------------------------------------+

and what I have now (NSOulineView without using NSTreeController);

                 +--------------------------------------------+
                 |      03143553.file                         |
                 |    ▶ Desktop                               |
                 |    ▶ Documents                             |
                 |    ▶ Downloads                             |
                 |    ▶ Movies                                |
                 |    ▶ Music                                 |
                 |    ▶ Pictures                              |
                 +--------------------------------------------+

I know about the example Apple "SourceView", but I don't know how to add to the created group, array of objects (files and folders), NSTreeContoller display only the first elements of the hierarchy (without includes):

                 +--------------------------------------------+
                 |  ▼ FINDER FILES                            |
                 |      03143553.file                         |
                 |      Desktop                               |
                 |      Documents                             |
                 |      Downloads                             |
                 |      Movies                                |
                 |      Music                                 |
                 |      Pictures                              |
                 +--------------------------------------------+

Modified method of SourceView example:

- (void)addFinderSection {
    [self addFolder:@"FINDER FILES"];

    NSError *error = nil;
    NSEnumerator *urls = [[[NSFileManager defaultManager] contentsOfDirectoryAtURL:self.url includingPropertiesForKeys:[NSArray arrayWithObjects: nil] options:(NSDirectoryEnumerationSkipsHiddenFiles) error:&error] objectEnumerator];
    for (NSURL *url in urls) {
        BOOL isDirectory;
        if ([[NSFileManager defaultManager] fileExistsAtPath:[url path] isDirectory:&isDirectory]) {
            if (isDirectory) {
                [self addChild:[url path] withName:NO selectParent:YES];
            } else {
                [self addChild:[url path] withName:NO selectParent:YES];   
            }
        }
    }

    [self selectParentFromSelection];
}

This method displays only the first objects, as shown it on the latter scheme.

And one more question, as I said before, how to add to the node ** "FINDER FILES" ** button to the right side of the cell.

Can you help me with this? I know, maybe is not so hard, but I just began learn Objective-C and I don't know how to do this. Thanks.

jazz man
  • 117
  • 2
  • 7
  • I realize this probably isn't totally helpful, but I suspect you would find this far easier to accomplish with an NSView-based NSOutlineView (vs. NSCell-based). The reason being that with view-based NSOutlineViews, you can add any number of subviews and retain all their standard functionality. With a cell-based approach you will be stuck with a single cell, and making it combine the behaviors of several controls will involve writing a custom NSCell subclass and lots of custom drawing and event handling code. View-based NSOutlineView will give you that for free. – ipmcc Dec 28 '12 at 16:44

1 Answers1

1

Based on the starting code you provided, I was able to get something working, using Cell-based NSOutlineViews. The code you posted seemed incomplete, so it was hard to know what exactly you wanted to get out of the missing FSEntity class. I just used foundation classes: NSArray for lists of nodes (i.e. root nodes and children), NSDictionaries for each node itself (both root nodes and sub-nodes), and NSURLs as the file-system reference. I tried to stay as close to your original code as possible. In the end, it looked something like this:

ProjectViewController.h

@interface ProjectViewController : NSViewController <NSOutlineViewDataSource, NSObject>
{
    IBOutlet NSOutlineView          *outlineView;
    NSURL                           *content;
}

@end

ProjectViewController.m

#import "ProjectViewController.h"

@interface ProjectViewController () {
    NSMutableArray* _objects;
}
@property (nonatomic, retain, readwrite) NSMutableArray* objects;
@end

@implementation ProjectViewController

@synthesize objects = _objects;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Initialization code here.
        // Setting default path to the local file or directory
        NSString *home = NSHomeDirectory();
        content = [[NSURL alloc] initFileURLWithPath:home];

        [self defineContentNSOutlineView];
        NSLog(@"Array: %@",_objects);

        // Basic сonfiguration an instance NSOutlineView
        [self configurationNSOutlineView];
    }
    return self;
}

// nodes have 3 keys: title, url, icon
- (NSArray*)p_childrenForNode: (NSMutableDictionary*)node {
    if (nil == node)
        return self.objects;

    NSArray* retVal = nil;
    if (nil == (retVal = [node valueForKey: @"children"]))
    {
        NSMutableArray* children = [NSMutableArray array];
        for (NSURL* urlInDir in [[NSFileManager defaultManager] contentsOfDirectoryAtURL: [node objectForKey: @"url"]
                                                           includingPropertiesForKeys: [NSArray arrayWithObjects: NSURLNameKey, NSURLEffectiveIconKey, nil]
                                                                              options: 0
                                                                                error: NULL])
        {
            id name = [urlInDir getResourceValue: &name forKey: NSURLNameKey error: NULL] ? name : @"<Couldn't get name>";
            id icon = [urlInDir getResourceValue: &icon forKey: NSURLEffectiveIconKey error: NULL] ? icon : nil;

            NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithObjectsAndKeys: urlInDir, @"url", name, @"title", nil];

            if (icon)
                [dict setObject: icon forKey: @"icon"];

            [children addObject: dict];
        }

        retVal = children;

        if (children)
            [node setValue: children forKey: @"children"];
    }
    return retVal;
}

- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
    NSMutableDictionary* itemDict = (NSMutableDictionary*)item;
    NSArray* children = [self p_childrenForNode: itemDict];
    return children.count > index ? [[[children objectAtIndex: index] retain] autorelease] : nil;
}

- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
    NSMutableDictionary* itemDict = (NSMutableDictionary*)item;
    NSArray* children = [self p_childrenForNode: itemDict];
    return children.count > 0;
}

- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
    NSMutableDictionary* itemDict = (NSMutableDictionary*)item;
    NSArray* children = [self p_childrenForNode: itemDict];
    NSInteger retVal = children.count;
    return retVal;
}

- (id)outlineView:(NSOutlineView *)pOutlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {

    NSImage* icon = [item objectForKey: @"icon"];
    NSString* title = [item objectForKey: @"title"];
    id value = nil;

    if (icon) {
        [icon setSize: NSMakeSize(pOutlineView.rowHeight - 2, pOutlineView.rowHeight - 2)];
        NSTextAttachment* attachment = [[[NSTextAttachment alloc] init] autorelease];
        [(NSCell *)[attachment attachmentCell] setImage: icon];
        NSMutableAttributedString *aString = [[[NSAttributedString attributedStringWithAttachment:attachment] mutableCopy] autorelease];
        [[aString mutableString] appendFormat: @" %@", title];
        value = aString;
    } else {
        value = title;
    }

    return value;
}

- (void)defineContentNSOutlineView {
    // Make root object
    NSMutableDictionary* rootObj = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                    @"FINDER", @"title",
                                    content, @"url",
                                    nil];

    id icon = [content getResourceValue: &icon forKey: NSURLEffectiveIconKey error: NULL] ? icon : nil;
    if (icon)
        [rootObj setObject: icon forKey: @"icon"];

    // Set it
    self.objects = [NSMutableArray arrayWithObject: rootObj];
}

- (void)configurationNSOutlineView {
    [outlineView sizeLastColumnToFit];
    [outlineView setFloatsGroupRows:NO];
    [outlineView reloadData];
    [outlineView expandItem:nil expandChildren:YES];
}

@end

I posted the whole, working sample project to GitHub.

ipmcc
  • 29,581
  • 5
  • 84
  • 147