// Tom Robinson
// http://tlrobinson.net/280/TableTest/
// http://tlrobinson.net/280/TableTest/AppController.j
//
// Philippe Laval (2009/03/22) : added some comments and the categories

@import <Foundation/CPObject.j>
@import <AppKit/CPTableView.j>


@implementation AppController : CPObject
{
    CPTableView _tableView;
    CPImage     _iconImage1;
    CPImage     _iconImage2;
    CPImage     _iconImage3;
}

- (void)applicationDidFinishLaunching:(CPNotification)aNotification
{
	CPLogRegister(CPLogConsole);

	var mainBundle = [CPBundle mainBundle];
	var path = [mainBundle pathForResource:@"DSC6888_550_368.jpg"];
	_iconImage1 = [[CPImage alloc] initWithContentsOfFile:path];
	path = [mainBundle pathForResource:@"cappuccino-icon.png"];
	_iconImage2 = [[CPImage alloc] initWithContentsOfFile:path];
	path = [mainBundle pathForResource:@"PLL_9929_368_550.jpg"];
	_iconImage3 = [[CPImage alloc] initWithContentsOfFile:path];

	// Choose one of these
//    [self showWindowWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask name:@"Main Window"];
    [self showWindowWithContentRect:CGRectMake(20.0, 40.0, 600.0, 600.0) styleMask:CPTitledWindowMask | CPResizableWindowMask | CPClosableWindowMask name:@"Standard Window"];
//    [self showWindowWithContentRect:CGRectMake(20.0, 40.0, 600.0, 600.0) styleMask:CPTitledWindowMask | CPResizableWindowMask | CPHUDBackgroundWindowMask name:@"HUD Window"];
}

- (void)showWindowWithContentRect:(CGRect)aContentRect styleMask:(unsigned)aStyleMask name:(CPString)aName
{
    var theWindow = [[CPWindow alloc] initWithContentRect:aContentRect styleMask:aStyleMask];
    var contentView = [theWindow contentView];
        
    [theWindow setTitle:@"Window"];
    [theWindow orderFront:self];
	    
	// Create the table view
    _tableView = [[CPTableView alloc] initWithFrame:[contentView bounds]];
    //[_tableView setBackgroundColor:[CPColor blueColor]];
    [_tableView setAutoresizingMask:CPViewWidthSizable];
    
	// Create the column for the icons
    var iconColumn = [[CPTableColumn alloc] initWithIdentifier:"icons"];
    [iconColumn setWidth:100];

	// The column for the icons is using a custom view (here a CPImageView instance)
    var iconView = [[CPImageView alloc] initWithFrame:CGRectMake(0,0,0,0)];
    [iconView setImageScaling:CPScaleProportionally];
    
    [iconColumn setDataView:iconView];
    
    [_tableView addTableColumn:iconColumn];
    
	// Create the columns 
    for (var i = 1; i <= 10; i++)
    {
        var column = [[CPTableColumn alloc] initWithIdentifier:[CPString stringWithFormat:@"%d", i]];

		// Modify the column header
        [[column headerView] setStringValue:"Number"];
        [[column headerView] sizeToFit];
		// The column width is the same as the column header width
        [column setWidth:[[column headerView] frame].size.width];
        
        // PLL : we try this to show text is aligned to the left
	    [column setWidth:80];
        
		var dataView = [column dataView];
		// [dataView setAlignment:CPCenterTextAlignment];
		// [dataView setVerticalAlignment:CPCenterVerticalTextAlignment];
        
        [dataView setValue:CPCenterTextAlignment forThemedAttributeName:"alignment"];
        [dataView setValue:CPCenterVerticalTextAlignment forThemedAttributeName:"vertical-alignment"];
        [dataView setValue:CPLineBreakByWordWrapping forThemedAttributeName:"line-break-mode"];
        

        [_tableView addTableColumn:column];
    }
    
	// Create the scroll view that will contain the tableview
    var scrollView = [[CPScrollView alloc] initWithFrame:[contentView bounds]];
    [scrollView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
    [scrollView setDocumentView:_tableView];
    
    [contentView addSubview:scrollView];
    
	// Indicate the data source (data for the tableview) and the delagate (behaviour of the tableview)
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    
}

@end

@implementation AppController (TableViewDataSource)

- (int)numberOfRowsInTableView:(CPTableView)tableView
{
    return 50;
}

- (id)tableView:(CPTableView)tableView objectValueForTableColumn:(CPTableColumn)tableColumn row:(int)row
{
    if ([tableColumn identifier] == "icons")
	{	
		// return a CPImage instance that will be displayed in the custom column (CPImageView)
        if (row % 2)
        	return _iconImage1;
        else if (row % 3)
        	return _iconImage3;
        else
        	return _iconImage2;
	}
    else
	{
		// return a CPString instance that will be displayed in a regular column (CPTextField)
        return [CPString stringWithFormat:@"This is a multiline text\n%d", (row + 1) * [[tableColumn identifier] intValue]];
	}
}

@end

@implementation AppController (TableViewDelegate)

- (id)tableView:(CPTableView)tableView heightOfRow:(int)row
{
	// Well is is just for fun here. The height of the row will increase.
	// In real application, this allows to have row with more information to be displayed entirely.
	//var height = 20.0 + ROUND(row * 0.5);
	height = 100;
	
    CPLog.info("heightOfRow: " + row + " = " + height);
    return height;
}

//- (void)tableViewSelectionIsChanging:(CPNotification)aNotification
//{
//	CPLog.debug(@"changing! %@", [aNotification description]);
//}
//
//- (void)tableViewSelectionDidChange:(CPNotification)aNotification
//{
//	CPLog.debug(@"did change! %@", [aNotification description]);
//}

- (BOOL)tableView:(CPTableView)aTableView shouldSelectRow:(int)rowIndex
{
	// We can refuse the user the right to select some rows.
	
	//CPLog.debug(@"shouldSelectRow %d", rowIndex);
	if (rowIndex % 2 == 0)
		return NO;

	return YES;
}

- (BOOL)selectionShouldChangeInTableView:(CPTableView)aTableView
{
	//CPLog.debug(@"selectionShouldChangeInTableView");
	return YES;
}

//- (CPIndexSet)tableView:(CPTableView)tableView selectionIndexesForProposedSelection:(CPIndexSet)proposedSelectionIndexes
//{
//	CPLog.debug(@"selectionIndexesForProposedSelection %@", [proposedSelectionIndexes description]);
//	return proposedSelectionIndexes;
//}

@end
