#import "PLTMatrixColumnView.h"


@implementation PLTMatrixColumnView

- (id) initColumnViewWithMatrix:(PLTMatrix *)mat withColumns:(NSArray *)columns
{
	[super init];
	_columns = columns;
	[_columns retain];
	
	_matrix = mat;
	[_matrix retain];
	
	return self;
}

- (void) setMatrix:(PLTMatrix *)mat
{
	[mat retain];
	[_matrix release];
	_matrix = mat;
}

- (PLTMatrix *) matrix
{
	return _matrix;
}

- (void) setCell:(double)value atRow:(int)row andCol:(int)viewCol
{
	int col = [[_columns objectAtIndex:viewCol] intValue];
	[_matrix setCell:value atRow:row andCol:col];
}

- (double) cellAtRow:(int)row andCol:(int)viewCol
{
	int col = [[_columns objectAtIndex:viewCol] intValue];
	return [_matrix cellAtRow:row andCol:col];
}

- (void) setColumnHeadings:(NSArray *)headings
{
	[_columnNames release];
	[_columnNames initWithArray:headings];
}

- (NSArray *)columnHeadings
{
	return [_matrix columnHeadings];
}

- (NSString *)columnHeadingForColumn:(int)viewCol
{
	int col = [[[self columns] objectAtIndex:viewCol] intValue];
	return [[_matrix columnHeadings] objectAtIndex:col];
}

- (void) setColumnHeading:(NSString *)columnHeading forColumn:(int)viewCol
{
	int col = [[[self columns] objectAtIndex:viewCol] intValue];
	[_columnNames replaceObjectAtIndex:col withObject:columnHeading];
}

- (NSArray *) columns
{
	return _columns;
}

- (void) setColumns:(NSArray *) columns
{
	[columns retain];
	[_columns release];
	_columns = columns;
}

- (int) rows
{
	return [_matrix rows];
}

- (int) cols
{
	return [_columns count];
}

- (void) print
{
	int i;
	int j;
	
	int rows = [_matrix rows];
	for(i=0; i<rows; i++) {
		printf("%4d ", i);
		for(j=0; j<[[self columns] count]; j++) {
			printf([[_matrix format] UTF8String], [_matrix cellAtRow:i andCol:[[_columns objectAtIndex:j] intValue]]);
		}
		printf("\n");
	} 
} 

- (void) dealloc
{
	[_matrix release];
	[_columns release];
	[super dealloc];
}

@end
