//
//  CADraggingView.m
//  SDIFSynth
//
//  Created by cynthia bruyns on 7/13/05.
//  Copyright 2005 __AppleComputer__. All rights reserved.
//
#import "NSViewDragHighlight.h"
#import "AUDraggingFileTableView.h"

@implementation AUDraggingFileTableView

- (void) setListAttributes
{
	float fontHeight = [NSFont systemFontSizeForControlSize:NSSmallControlSize];
	[self setRowHeight:fontHeight + 2.0f];  // 2.0 = 1px padding on top & bottom
	NSFont *font = [NSFont controlContentFontOfSize:fontHeight];
	mExistingFileStringAttributes = [[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName] retain];
	mNotFoundFileStringAttributes = [[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:font, [NSColor redColor], nil]
						forKeys:[NSArray arrayWithObjects:NSFontAttributeName, NSForegroundColorAttributeName, nil]] retain];
}

- (id)initWithFrame:(NSRect)frame 
{
    self = [super initWithFrame:frame];
    if (self) {
		
    }
    return self;
}
- (void) setTableDefaults
{
	[self setFocusRingType:NSFocusRingTypeNone];
	
	[self setDataSource:self];
	[self setDelegate:self];
	[self setAllowsMultipleSelection:YES];
}

-(void) awakeFromNib 
{
	mCurrentDragOp = NSDragOperationNone;		
	mList = [[NSMutableArray alloc] init];
	
	[self registerForDraggedTypes:[NSArray arrayWithObjects: NSFilenamesPboardType, nil]];

	[self setListAttributes]; 
	[self setTableDefaults];
}

- (void) dealloc 
{	
	[mList release];
	[mDisplayList release];
	
	[mNC release];
	
	[mExistingFileStringAttributes release];
	[mNotFoundFileStringAttributes release];
	
	[mSupportedTypeCodes release];
	[super dealloc];
}

- (void)setNotificationCenter:(NSNotificationCenter *)inNC
{
	[mNC release];
	mNC = nil;
	
	if (inNC){
		mNC = [inNC retain];
	}
}

- (void) operateOnData: (NSArray*) data{
	return; // this is overridden
}

- (void) removeItems: (NSIndexSet*) items
{
	return; // overridden
}

- (void) addItems: (NSArray*) items
{
	return; //overridden	
}

- (NSArray*) getExtensions
{
	return nil; //this is overridden
}

- (NSDictionary*) getAttributes: (int) i
{
	return mExistingFileStringAttributes; // get the attributes for the specific row
}

- (void) rebuildFileDisplayList 
{
	if (mDisplayList == nil) {
		mDisplayList = [[NSMutableArray alloc] init];
	} else {
		[mDisplayList removeAllObjects];
	}
	
	int count = [mList count];

	NSString *baseString;
	NSDictionary *attributesToUse;
	NSAttributedString *displayString;
	
	for (int i = 0; i < count; ++i) {
		
		attributesToUse = [self getAttributes: i];
		baseString = (NSString *)[mList objectAtIndex:i];
		displayString = [[[NSAttributedString alloc] initWithString:[baseString lastPathComponent]
													attributes:attributesToUse]	retain]; 
		
		[mDisplayList addObject:displayString];		
	}
	[self reloadData];
}


#pragma mark __Dragging___

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender 
{
	if ([self isEnabled]) {
		NSPasteboard *pboard;
		NSDragOperation sourceDragMask;

		sourceDragMask = [sender draggingSourceOperationMask];
		pboard = [sender draggingPasteboard];

		if ( [[pboard types] containsObject:NSFilenamesPboardType] ) {
			if (sourceDragMask & NSDragOperationLink) {
				[self drawDragHighlightOutside:NO];
				return mCurrentDragOp = NSDragOperationLink;
			}
		}
	}
	
	return mCurrentDragOp = NSDragOperationNone;
}

- (void)draggingExited:(id <NSDraggingInfo>)sender 
{
	if ([self isEnabled]) {
		[self clearDragHighlightOutside];
		mCurrentDragOp = NSDragOperationNone;
	}
}

- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender 
{
	return mCurrentDragOp;
}

- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender 
{
	return [self isEnabled];
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender 
{
	if ([self isEnabled]) {
	
		NSPasteboard *pboard;
		NSDragOperation sourceDragMask;

		[self clearDragHighlightOutside];
		sourceDragMask = [sender draggingSourceOperationMask];
		pboard = [sender draggingPasteboard];
		
		if ( [[pboard types] containsObject:NSFilenamesPboardType] ) {
			NSArray *data = [pboard propertyListForType:NSFilenamesPboardType];

			// Depending on the dragging source and modifier keys,
			// the file data may be copied or linked
			if (sourceDragMask & NSDragOperationLink) {
				[self operateOnData:data];
			}
		}
	}
	
	return [self isEnabled];
}

#pragma mark ___Accessors___

- (NSArray *) selectedFiles
{
	NSMutableArray *files = [[NSMutableArray alloc] init];
	int n = [self numberOfRows];
	for (int i = 0; i < n; ++i) {
		if ([self isRowSelected: i])
			[files addObject: [mList objectAtIndex: i]];
	}
	return files;
}

#pragma mark ___ResponderFunctions___

- (void) keyDown:(NSEvent *)inEvent {
	
    if ([self isEnabled]) {
		
		NSString *	keyInput = [inEvent charactersIgnoringModifiers];
		UInt32 length = [keyInput length];
		unichar currentChar;
		
		for (UInt32 i = 0; i < length; ++i) {
			currentChar = [keyInput characterAtIndex:i];
			
			if (currentChar == NSDeleteCharacter) {
				[self removeItems:[self selectedRowIndexes]];
				break;
			}						
		}
	}	

}

#pragma mark __TableViewFunctions___

- (void)tableViewSelectionDidChange:(NSNotification *)inNotification 
{
	id object = nil;
	NSIndexSet *selectedIndexes = [self selectedRowIndexes];
	if ([selectedIndexes count] == 1) {
		object = [mList objectAtIndex:[selectedIndexes firstIndex]];
	}
}

- (int)numberOfRowsInTableView:(NSTableView *)inTableView
{
	return [mList count];
}

- (id) tableView:(NSTableView *)inTableView objectValueForTableColumn:(NSTableColumn *)inTableColumn row:(int)inRow 
{
	if (inRow < 0) return nil;
	if (inRow >= int([mDisplayList count])) return nil; //what abt column
	
	return [mDisplayList objectAtIndex:inRow];
}

- (void) tableView:(NSTableView *)inTableView didClickTableColumn:(NSTableColumn *)inTableColumn 
{
	if (![self isEnabled]) return;
	
	[self getExtensions];
		
	NSOpenPanel *openPanel = [NSOpenPanel openPanel];
	[openPanel setAllowsMultipleSelection:YES];
	
	int result = [openPanel runModalForTypes:mSupportedTypeCodes];
	
	if (result == NSOKButton)
		[self operateOnData:[openPanel filenames]];
}

@end
