//
//  NSFormExtensions.m
//  longspear
//
//  Created by Niko Matsakis on Sun Jun 20 2004.
//  Copyright (c) 2004 __MyCompanyName__. All rights reserved.
//

#import "NSFormExtensions.h"


@implementation NSForm (LongSpear)

- (NSMutableDictionary*) valuesAsDictionary
{
    int i, numentries = [self numberOfRows];
    NSMutableDictionary *result = [[[NSMutableDictionary alloc] initWithCapacity:numentries] autorelease];
    for (i = 0; i < numentries; i++) {
        id cell = [self cellAtIndex:i];
        NSString *title = [cell title];
        
        // Check if the last character in the title is ':'
        assert ([title length] > 0);
        int lastchar = [title length] - 1;
        if ([title characterAtIndex:lastchar] == ':') {
            title = [title substringToIndex:lastchar];
        }

        [result setObject:[cell stringValue]
                   forKey:title];
    }
    
    return result;
}

- (void) setValuesFromDictionary:(NSDictionary*)dict
{
    int i, numentries = [self numberOfRows];
    for (i = 0; i < numentries; i++) {
        id cell = [self cellAtIndex:i];
        NSString *title = [cell title];
        
        // Check if the last character in the title is ':'
        assert ([title length] > 0);
        int lastchar = [title length] - 1;
        if ([title characterAtIndex:lastchar] == ':') {
            title = [title substringToIndex:lastchar];
        }
        
        id value = [dict objectForKey:title];
        if (value) {
            [cell setStringValue:[value description]];
        }
    }    
}

@end
