/* Copyright (C) 2002-2003, Jeffrey D. Argast. The authors make NO WARRANTY or representation, either express or implied, with respect to this software, its quality, accuracy, merchantability, or fitness for a particular purpose. This software is provided "AS IS", and you, its user, assume the entire risk as to its quality and accuracy. Permission is hereby granted to use, copy, modify, and distribute this software or portions thereof for any purpose, without fee, subject to these conditions: (1) If any part of the source code is distributed, then this statement must be included, with this copyright and no-warranty notice unaltered. (2) Permission for use of this software is granted only if the user accepts full responsibility for any undesirable consequences; the authors accept NO LIABILITY for damages of any kind. */ #import "BasicTableController.h" #import "PerforceAction.h" #import "OutlineItem.h" #import "AppDefaults.h" #import "MessageDefs.h" #import "PerforceChanges.h" #import "PerforceSubmittedChange.h" #import "IconDefs.h" #import "IconController.h" @implementation BasicTableController - (void) comeForward { fForward = YES; [fTableView reloadData]; } - (void) stepBack { fForward = NO; } - (void) dealloc { NSNotificationCenter* nCenter = [NSNotificationCenter defaultCenter]; [nCenter removeObserver:self]; [fChildren release]; [fUpSortImage release]; [fDownSortImage release]; [fIcon release]; [super dealloc]; } - (void) buildChildren { NSLog (@"Overide this"); /* fChildren = [[NSMutableArray alloc] init]; [fChildren addObjectsFromArray:[[PerforceAction defaultAction] performSelector:fPerforceActionMethod]]; */ } - (void) setUpCell { NSTableColumn *tableColumn = nil; NSBrowserCell *browserCell = nil; tableColumn = [fTableView tableColumnWithIdentifier:fIconColumnName]; browserCell = [[[NSBrowserCell alloc] init] autorelease]; [browserCell setEditable:NO]; [browserCell setLeaf:YES]; [tableColumn setDataCell:browserCell]; { NSNotificationCenter* nCenter = [NSNotificationCenter defaultCenter]; [nCenter addObserver:self selector:@selector(handleDefaultsChanged:) name:kDefaultsUserChanged object:nil]; [nCenter addObserver:self selector:@selector(handleDefaultsChanged:) name:kDefaultsClientChanged object:nil]; [nCenter addObserver:self selector:@selector(handleDefaultsChanged:) name:kDefaultsPortChanged object:nil]; [nCenter addObserver:self selector:@selector(handleDefaultsChanged:) name:kDefaultsConfigurationsChanged object:nil]; [nCenter addObserver:self selector:@selector(handleDefaultsChanged:) name:kRefreshAllViews object:nil]; } fUpSortImage = [[[IconController defaultIconController] getIcon:kSortingUpArrowIcon] retain]; fDownSortImage = [[[IconController defaultIconController] getIcon:kSortingDownArrowIcon] retain]; fIcon = [[[IconController defaultIconController] getIcon:fIconID] retain]; NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults]; NSString* sortColIdentifier = [userDefaults objectForKey:fDefaultSortColumnKey]; fSortAscending = [userDefaults boolForKey:fDefaultSortDirectionKey]; fSortedByColumn = [fTableView tableColumnWithIdentifier:sortColIdentifier]; [fTableView setHighlightedTableColumn:fSortedByColumn]; if ( fSortAscending == YES ) { [fTableView setIndicatorImage:fUpSortImage inTableColumn:fSortedByColumn]; } else { [fTableView setIndicatorImage:fDownSortImage inTableColumn:fSortedByColumn]; } [fTableView setDoubleAction:@selector(doubleAction:)]; [fTableView setTarget:self]; } - (void) handleDefaultsChanged: (NSNotification*) notification { [self refreshData]; } - (void) refreshData { if ( fChildren ) { [fChildren release]; fChildren = nil; [fTableView reloadData]; } } - (id) getSelection { int rowIdx = [fTableView selectedRow]; if ( rowIdx < 0 ) return nil; return [fChildren objectAtIndex:rowIdx]; } // // table view data source methods // - (int) numberOfRowsInTableView: (NSTableView*) aTableView { if ( !fForward ) { if ( fChildren ) { return [fChildren count]; } return 0; } if ( !fChildren ) { [self buildChildren]; } return [fChildren count]; } - (void) doubleClick:(id)sender { // do nothing. sub classes to override } - (void) doubleAction: (id) sender { NSWindow* window = [fTableView window]; NSTableHeaderView* headerView = [fTableView headerView]; NSPoint clickPoint = [NSEvent mouseLocation]; clickPoint = [window convertScreenToBase:clickPoint]; clickPoint = [headerView convertPoint:clickPoint fromView:nil]; int colIdx = [headerView columnAtPoint:clickPoint]; if ( colIdx < 0 ) { [self doubleClick:sender]; } } //static NSString* kFooString = @"Foo Yea Data see me"; - (id) tableView: (NSTableView*) aTableView objectValueForTableColumn: (NSTableColumn *) aTableColumn row: (int) rowIdx { // return kFooString; NSString* identifier = [aTableColumn identifier]; id childObj = [fChildren objectAtIndex:rowIdx]; return [childObj valueForKey:identifier]; } /* ** table view delegate methods */ - (void) tableView: (NSTableView *) tableView willDisplayCell: (NSCell *) cell forTableColumn: (NSTableColumn *) tableColumn row: (int) rowIndex { if ( [[tableColumn identifier] isEqualToString:fIconColumnName] ) { [(NSBrowserCell*)cell setImage:fIcon]; } } /* ** table view delegate methods */ const float kSlop = 4.0; - (void) tableView: (NSTableView*) tableView mouseDownInHeaderOfTableColumn: (NSTableColumn *) tableColumn { NSWindow* window = [tableView window]; NSTableHeaderView* headerView = [tableView headerView]; NSPoint clickPoint = [NSEvent mouseLocation]; clickPoint = [window convertScreenToBase:clickPoint]; clickPoint = [headerView convertPoint:clickPoint fromView:nil]; int colIdx = [headerView columnAtPoint:clickPoint]; if ( colIdx >= 0 ) { NSRect headerRect = [headerView headerRectOfColumn:colIdx]; // We have to do all this crap because the other delegate method, // didClickTableColumn isn't called, and/or I don't know how to // get it called headerRect.origin.x += kSlop; headerRect.size.width -= (2.0 * kSlop); if ( NSPointInRect (clickPoint, headerRect) ) { if ( fSortedByColumn != tableColumn ) { [tableView setHighlightedTableColumn:tableColumn]; [tableView setIndicatorImage:nil inTableColumn:fSortedByColumn]; fSortedByColumn = tableColumn; } else { fSortAscending = !fSortAscending; } if ( fSortAscending ) { [tableView setIndicatorImage:fUpSortImage inTableColumn:tableColumn]; } else { [tableView setIndicatorImage:fDownSortImage inTableColumn:tableColumn]; } NSUserDefaults* userDefs = [NSUserDefaults standardUserDefaults]; [userDefs setObject:[tableColumn identifier] forKey:fDefaultSortColumnKey]; [userDefs setBool:fSortAscending forKey:fDefaultSortDirectionKey]; [self sortChildren]; } } } - (void) sortChildren { NSMutableString* methodName = [NSMutableString stringWithString:@"compare"]; [methodName appendString:[fSortedByColumn identifier]]; if ( fSortAscending ) { [methodName appendString:@"Ascending"]; } else { [methodName appendString:@"Descending"]; } [methodName appendString:@":"]; SEL sortSelector = sel_getUid ([methodName cString]); if ( sortSelector ) { [fChildren sortUsingSelector:sortSelector]; [fTableView reloadData]; } } @end