#pragma .h #import <Foundation/NSObject.h>
#pragma .h #import <Foundation/NSArray.h>
#pragma .h 
#pragma .h #import "IKBrowser.h"
#pragma .h #import "IKIconObject.h"
#pragma .h #import "IKDependency.h"
#pragma .h 
#pragma .h @protocol IKGraphNode <IKSimpleObject, IKDependency>
#pragma .h - children;
#pragma .h - (BOOL) isLeaf;
#pragma .h - (BOOL) isHidden;
#pragma .h @end
#pragma .h 

#import <Foundation/NSUtilities.h>

#import <AppKit/AppKit.h>
#import <InterfaceBuilder/InterfaceBuilder.h>

#import "IKBrowser.h"
#import "IKBrowserCell.h"
#import "IKFolder.h"
#import "IKList.h"
#import "IKCell.h"
#import "IKIconPath.h"
#import "NSObject.LoadImage.h"

@implementation IKBrowserManager:NSObject
#pragma .h <IKBrowserDelegate>
#pragma .h {
#pragma .h 		id	browser;
#pragma .h 		id	root;
#pragma .h }


- (NSImage *) getIBImage
{
    return [self loadImageFromBundle:@"IKBrowserManager"];
}

- init
{
    return [self  initBrowser: nil  root: nil];
}


- initBrowser: (id) theBrowser  root: (id) theRoot;
{
    if ((self = [super init]) != nil) {
        browser = theBrowser;
        root = theRoot;
        [self  doAwake];
    }
    return self;
}

- (void)dealloc
{
#if DEBUG
    NSLog(@"freeing browser manager: %p\n", self);
#endif
    [(IKBrowser *)browser setDelegate:nil];
    [browser removeAllFolders];
    if ([[browser window] delegate] == self)
        [(NSWindow *)[browser window] setDelegate:nil];
    if(root) {
        [[root children] makeObjectsPerform: @selector(removeParent:) withObject:root];
        [root release];
        root = nil;
    }
    [super dealloc];
}

- awakeAfterUsingCoder:(NSCoder *)aDecoder
{
    [self doAwake];
    return self;
}

- (void)doAwake
{
    NSArray	*pasteTypes = NULL;

    if(root) {
        pasteTypes = [[root class] respondsToSelector: @selector(pasteTypes)] ?
        [[root class]  pasteTypes]:
        NULL;
    }
    [[browser  iconPath] registerForDraggedTypes:pasteTypes];
    [(IKBrowser *)browser setDelegate:self];
    [[browser window] setDelegate:self];
}

- (IKBrowser *)browser	{	return browser;			}
- root			{	return root;			}

- (void)setBrowser:(IKBrowser *)theBrowser;
{
    [(IKBrowser *)browser setDelegate:nil];
    browser = theBrowser;
    [self doAwake];
}

- (void)setRoot: theRoot
{
    if(root)
        [root release];
    root = theRoot;
    [self doAwake];
}

- (void)browser:sender createRowsForColumn:(int)column inMatrix:(NSMatrix *)matrix
{
    id  node;
    int k = 0, n, i;
	
    if (column == 0) {
        node = root;
    } else {
        NSArray *selection = [sender selectionInColumn: column - 1];
        node = ([selection count] == 1) ? [selection objectAtIndex: 0] : nil;
    }

    if (column == 0  ||  ![node  isLeaf]) {
        NSArray *children = [node  children];
        n = [children count];

        {
            id child [n];

            for (i = 0;  i < n;  i++)
                if (![(child[k] = [children objectAtIndex: i])  isHidden])
                    k++;

            [self  sort: child  count: k  for: column];

            for (i = 0;  i < k;  i++) {
                [matrix addRow];
                [(IKBrowserCell *)[matrix cellAtRow:i column:0] setDelegate: child[i]];
            }
        }
    }
}

- browser:(IKBrowser *)sender setColumnIcon:cell for: (int) column
{
    id	node = nil;
    id	selection;

    if (column == 0) {
        node = root;
    } else {
        selection = [sender selectionInColumn: column - 1];
        if ([selection  count] == 1) {
            node = [selection  objectAtIndex: 0];
        } else if ([selection  count] > 1) {
            node =
            [[root class] respondsToSelector: @selector(multipleSelectionClass)] ?
            [[[[root class] multipleSelectionClass]  alloc]  init]:
            [[IKList alloc] init];

            [(IKList *)node appendList: selection];
        }
    }
    [(IKCell *)cell setDelegate:node];
    return self;
}

- browser: sender  emptyMatrix: matrix  inColumn: (int) column
{
    NSArray *cellList = [matrix cells];
    if([cellList count] > 0) {
        id	oldParents = [[[cellList objectAtIndex:0] delegate] parents];
        int	i, count = [oldParents count];

        [cellList makeObjectsPerform:@selector(setDelegate:) withObject:nil];

        for(i = count-1; i >= 0; i--) {
            id	oldParent = [oldParents objectAtIndex:i];
            [oldParent removeChildren:[oldParent children] withAnnouncement:NO];
        }
    }
    return self;
}


- browser:sender removeColumnIcon: cell  for: (int) column
{
    [(IKCell *)cell setDelegate:nil];
    return self;
}


- browserWillFinishChange:sender
{
    return self;
}


- browserDidChange:sender
{
    return self;
}

- sort: (id *) folders  count: (int) n  for: (int) column
{
    return self;
}


- (NSArray *)takePathFrom:sender
{
    IKFolder	*node;
    NSArray		*path = nil;
    NSMutableString *pathName;
    int		i, count;

    if ([sender  respondsToSelector: @selector(delegate)] &&
        [node = [sender delegate] respondsToSelector: @selector(pathFromNode:)]) {
        path = [node  pathFromNode: root];
        pathName = [NSMutableString stringWithCString:"/"];

        if (path != nil)
            for (i = 1;  i < (count = [path  count]);  i++) {
                IKFolder *aFolder = [path objectAtIndex: i];
                [pathName appendFormat:@"/%s", [[aFolder name] cString]];
            }
        [browser setPath:pathName];
    }
    return path;
}


- (void)deleteLink:(id)sender
{
    int n = [browser selectedColumn];

    if (n != -1) {
        id parent = [[browser iconPath] objectInColumn:n];
        if ([parent respondsToSelector: @selector(removeChildren:)]) {
            NSArray *selection = [browser selectionInColumn: n];
            if(selection) {
                [selection retain];
                [parent  removeChildren: selection];
                [selection release];
            }
        }	
    }
}


- (void)delete:(id)sender
{
    int n = [browser selectedColumn];

    if (n != -1) {
        NSArray *selection = [browser selectionInColumn: n];
        if(selection) {
            [selection retain];
            [self deleteLink:self];
            [selection release];
        }
    }
}

- (void)duplicate:(id)sender
{
    int n = [browser  selectedColumn];
    id  parent;
	
    if (n != -1) {
        parent = [[browser iconPath] objectInColumn:n];
        if ([parent respondsToSelector: @selector(addChildren:)]) {
            NSMutableArray *selection = [browser selectionInColumn: n];
            if(selection) {
                int i, count;
                [selection retain];
                for (i = 0, count = [selection count]; i < count; i++)
                    [selection replaceObjectAtIndex:i withObject:[[selection objectAtIndex:i] copy]];
                [parent addChildren:selection];
                [selection release];
            }
        }
    }
}

- (void)newFolder:(id)sender
{
    static int count = 1;
    id     delegate = [[browser iconPath] objectInColumn: [browser  selectedColumn] + 1];

    if ([delegate  respondsToSelector: @selector(addChild:)]) {
        NSString *aName = [NSString stringWithFormat:@"NewFolder%d", count++];
        [delegate  addChild: [[[root  class]  alloc]  init: aName]];
    }
}


- (void)toggleLeaf:(id)sender
{
    NSArray *selection = [browser selectionInColumn:[browser  selectedColumn]];

    if(selection) {
        int i, count;
        [selection retain];
        for (i = 0, count = [selection  count]; i < count; i++) {
            id node = [selection objectAtIndex: i];
            if ([node  respondsToSelector: @selector(setLeaf:)])
                [node setLeaf: ![node  isLeaf]];
        }
        [selection release];
    }
}

@end
