//************************************************************************
//
//	OICNSTableView.m
//
//	by Juergen Moellenhoff <jm@oic.de>		
//
//
//	This code is supplied "as is" the author makes no warranty as to its 
//	suitability for any purpose.  This code is free and may be distributed 
//	in accordance with the terms of the:
//		
//			GNU GENERAL PUBLIC LICENSE
//			Version 2, June 1991
//			copyright (C) 1989, 1991 Free Software Foundation, Inc.
// 			675 Mass Ave, Cambridge, MA 02139, USA
//
//************************************************************************

#import "OICNSTableView.h"

#import "OICNSTableHeaderCell.h"
#import "OICNSTableHeaderView.h"
#import "OICNSTableColumn.h"
#import "OICNSTableViewPrivate.h"

#import <AppKit/AppKit.h>

#define	OICNSTABLEVIEW_CURRENT_VERSION	3

@implementation OICNSTableView

//
// Private Methods
//
+ (OICNSTableColumn *)_createNewTableColumn:(NSTableColumn *)oldColumn
{
	OICNSTableColumn	*newColumn;

	newColumn = [[OICNSTableColumn alloc] initWithIdentifier:[oldColumn identifier]];

	// Try to copy the "outfit" of the old column, but don't copy the
	// headerCell...
	[newColumn setTableView:[oldColumn tableView]];
	[newColumn setWidth:[oldColumn width]];
	[newColumn setMinWidth:[oldColumn minWidth]];
	[newColumn setMaxWidth:[oldColumn maxWidth]];
	[newColumn setResizable:[oldColumn isResizable]];
	[newColumn setEditable:[oldColumn isEditable]];
	[newColumn setDataCell:[oldColumn dataCell]];

	return [newColumn autorelease];
}

//
// This bumps the class version so that we can compatibly read
// old objects out of an archive.
//
+ (void)initialize 
{
	if (self == [OICNSTableView class]) 
		[OICNSTableView setVersion:OICNSTABLEVIEW_CURRENT_VERSION];
}

//
// Init
//
- initWithFrame:(NSRect)newFrame
{
	OICNSTableHeaderView	*newTableHeaderView;

	if ([super initWithFrame:newFrame] == nil)
		return nil;

	// Replace the headerView with a new (my own)
	// headerView, but try to copy the "outfit" 
	// of the old headerView.
	newTableHeaderView = [[[OICNSTableHeaderView alloc] initWithFrame:[[self headerView] frame]] autorelease];
	[self setHeaderView:newTableHeaderView];

	return self;
}

//
// NSCoding protocol
//
- (id)initWithCoder:(NSCoder *)aDecoder
{
	[super initWithCoder:aDecoder];

	if ([aDecoder versionForClassName:@"OICNSTableView"] < OICNSTABLEVIEW_CURRENT_VERSION)
	{
		unsigned int		i, iCount;
		NSTableColumn		*oldColumn;
		OICNSTableColumn	*newColumn;
		NSArray				*colArray;
		int					iDummy;

		if ([aDecoder versionForClassName:@"OICNSTableView"] == 1)
		{
			// read code for old version
			[aDecoder decodeValueOfObjCType:@encode(typeof(iDummy)) at:&iDummy];
		}

		// Replace old columns
		colArray = [NSArray arrayWithArray:[self tableColumns]];
		for (i = 0, iCount = [colArray count]; i < iCount; i++)
		{
			[self removeTableColumn:[colArray objectAtIndex:i]];
		}

		for (i = 0, iCount = [colArray count]; i < iCount; i++)
		{
			oldColumn = [colArray objectAtIndex:i];

			newColumn = [OICNSTableView _createNewTableColumn:oldColumn];
			[newColumn setHeaderCell:[OICNSTableColumn _createNewHeaderCell:[oldColumn headerCell]]];
			[newColumn setOrdering:[[oldColumn headerCell] ordering]];

			[self addTableColumn:newColumn];
		}
	}

	return self;
}

//
// NSCoding protocol
//
- (void)encodeWithCoder:(NSCoder *)aCoder
{
	[super encodeWithCoder:aCoder];
}

@end

