//
//  CFIDocument.m
//  IDEKit Demo
//
//  Created by Robert Widmann on 9/30/12.
//  Copyright (c) 2012 CodaFi. All rights reserved.
//

#import "CFIDocument.h"

@implementation CFIDocument

- (id)init
{
    self = [super init];
    if (self) {
		// Add your subclass-specific initialization here.
    }
    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 @"CFIDocument";
}

- (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.
}

+ (BOOL)autosavesInPlace
{
    return YES;
}

- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
	// Insert code here to write your document to data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning nil.
	// You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
	NSException *exception = [NSException exceptionWithName:@"UnimplementedMethod" reason:[NSString stringWithFormat:@"%@ is unimplemented", NSStringFromSelector(_cmd)] userInfo:nil];
	@throw exception;
	return nil;
}

- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
	// Insert code here to read your document from the given data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning NO.
	// You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead.
	// If you override either of these, you should also override -isEntireFileLoaded to return NO if the contents are lazily loaded.

	return [self tryOpenFile:[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]];
}

- (NSTextView *) textView
{
    return myTextView;
}

- (NSString *)currentSource
{
    return [[myTextView textStorage]string];
}


#pragma mark Menu Options
- (BOOL)tryOpenFile: (NSString *) selection
{
    id controller = [NSDocumentController sharedDocumentController];
    id document = [controller openDocumentWithContentsOfFile: selection display: YES];
    if (!document && [self fileURL])
        document = [controller openDocumentWithContentsOfURL:[[[self fileURL] URLByDeletingLastPathComponent] URLByAppendingPathComponent: selection] display:YES error:nil];
    if (document)
        [controller addDocument: document];
    return YES;
}

- (IBAction)openSelection:(id) sender
{
    NSRange selectedRange = [myTextView selectedRange];
    NSString *selection = [[myTextView string] substringWithRange: selectedRange];
    // and try to open it
    if ([self tryOpenFile: selection] == NO) {
        NSBeep();
    }
}





@end
