//
//  MyDocument.m
//  OvalDrawDocument
//
//  Created by Brant on 2009/05/04.
//  Copyright __MyCompanyName__ 2009 . All rights reserved.
//

#import "OvalDrawDocument.h"
#import "OvalView.h"

@implementation OvalDrawDocument

- (id)init
{
    self = [super init];
    if (self) {
    
        // Add your subclass-specific initialization here.
        // If an error occurs here, send a [self release] message and return nil.
    
    }
    return self;
}

- (NSString *)windowNibName
{
    // Override returning the nib file name of the document
    // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers,
	// you should remove this method and override -makeWindowControllers instead.
    return @"OvalDrawDocument";
}

- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
    [super windowControllerDidLoadNib:aController];
    // Add any code here that needs to be executed once the windowController has loaded the document's window.
}

- (NSData *)dataOfType:(NSString *)typeName 
				 error:(NSError **)outError
{
	NSMutableArray *ovalsToSave = [myOvalView ovals];
	NSLog(@"Archiving %d ovals", [ovalsToSave count]);
	return [NSKeyedArchiver archivedDataWithRootObject:ovalsToSave];
}

- (BOOL)readFromData:(NSData *)data 
			  ofType:(NSString *)typeName 
			   error:(NSError **)outError
{
	NSMutableArray *loadedOvals = nil;
	@try
	{
		loadedOvals = [NSKeyedUnarchiver unarchiveObjectWithData:data];
		NSLog(@"Unarchived %d ovals", [loadedOvals count]);
	}
	@catch (NSException *e)
	{
		if (outError)
		{
			NSDictionary *d = [NSDictionary dictionaryWithObject:@"The data is corrupted."
														  forKey:NSLocalizedFailureReasonErrorKey];
			*outError = [NSError errorWithDomain:NSOSStatusErrorDomain
											code:unimpErr
										userInfo:d];
		}
		return NO;
	}
	
	[myOvalView setOvals:loadedOvals];
    return YES;
}

@end
