[Cocoapsql-developer] commit; moved autoSizeTableColumns to Category
Status: Alpha
Brought to you by:
alkirkus
From: Olaf v. O. <ol...@ad...> - 2002-06-08 18:23:26
|
Moved the autoSizeColums from PSQLController.m to a new Catergory PSQLTableViewCat so the method will be available to all NSTableView's. Modified the method to be prepaired for the use of a formatter on NSTableColumn's dataCell (not tested!). Further the method name is altered and can point to one column or all (-1). The maximum number of rows to be processed is part of the method name as well. "PSQLTableViewCat" Grouped these files and the PSQLTextView files to the new Group "ClassExtensions" in the project. Added the choice to autosize one or all columns. Added to PSQLController.m #import "PSQLTableViewCat.h" Modified PSQLController submitQuery and setTableShouldAutoSizeColums: [self autoSizeTableColumns]; now is [tableView autoSizePSQLTableColumn:-1 maxRows:200]; PSQLTableViewCat.h #import <AppKit/NSTableView.h> @interface NSTableView ( PSQLTableView ) - (void)autoSizePSQLTableColumn:(int)cn maxRows:(int)r; @end PSQLTableViewCat.m #import "PSQLTableViewCat.h" #import <Cocoa/Cocoa.h> @implementation NSTableView ( PSQLTableView ) - (void)autoSizePSQLTableColumn:(int)cn maxRows:(int)r { NSFont *dataCellsFont; NSDictionary *attributes; NSSize cellToCellSpacing, cellsSize; float cellWidthFitsAll; int i, j, firstColumn, lastColumn; NSArray *curColumns = [self tableColumns]; id curColId; id datSo = [self dataSource]; id formatter; NSString *valueWithFormat; // need access to dataSource's method tableView:objectValueForTableColumn:row: SEL targetMethod = @selector(tableView:objectValueForTableColumn:row:); if ( ![datSo respondsToSelector:targetMethod] ) { NSLog(@"PSQLTableViewCat: need tableView:objectValueForTableColumn:row: \n for calculating column width"); return; } lastColumn = [curColumns count]; // process one or all columns (or none) if ( cn >= 0 ) { if ( cn > lastColumn -1 ) { NSLog(@"columnNumber grater than number of columns -1"); return; } else { firstColumn = cn; lastColumn = firstColumn; } } else { firstColumn = 0; } cellToCellSpacing = [self intercellSpacing]; for(i=firstColumn;i<lastColumn;i++) { // headerCell curColId = [curColumns objectAtIndex:i]; dataCellsFont = [[curColId dataCell] font]; if ( dataCellsFont == nil ) { // dataCellsFont = nil if headerCell is not a text-type cell // set column width to reasonable minimum cellWidthFitsAll = 10; } else { cellsSize = [[curColId headerCell] cellSize]; cellWidthFitsAll = cellsSize.width; } // dataCell for all rows dataCellsFont = [[curColId dataCell] font]; if ( dataCellsFont == nil ) { // dataCellsFont = nil if dataCell is not a text-type cell // leave column width as it is } else { attributes = [NSDictionary dictionaryWithObject:NSFontAttributeName forKey:[dataCellsFont fontName]]; formatter = [[curColId dataCell] formatter]; for(j=0;r>j && j<[self numberOfRows];j++) { if (formatter) valueWithFormat = [formatter stringForObjectValue:[datSo tableView:self objectValueForTableColumn:curColId row:j]]; else valueWithFormat = [datSo tableView:self objectValueForTableColumn:curColId row:j]; /* PSQLQuery's tableView:objectValueForTableColumn:row: returns an NSCFString */ cellsSize =[valueWithFormat sizeWithAttributes:attributes]; } if ( cellsSize.width > cellWidthFitsAll ) { cellWidthFitsAll = cellsSize.width; } } // set column width [curColId setWidth: ((cellWidthFitsAll+cellToCellSpacing.width+0)*1.2)]; /* added 0 for a border. corrected with factor 1.2 for an undefined fault */ } [self setNeedsDisplay:YES]; } @end |