Re: Saving table/outline table configuration...
Re: Saving table/outline table configuration...
- Subject: Re: Saving table/outline table configuration...
- From: Ambroise Confetti <email@hidden>
- Date: Sun, 16 Nov 2003 21:34:27 +0100
Le 16 nov. 03, ` 21:02, Shawn Erickson a icrit :
>
I know that NSTableView and NSOutlineView have the ability to
>
automatically save column order, size and sorting user preferences in
>
user defaults. This however doesn't really help in the case of a
>
document based application where the views are associated with the
>
document.
>
>
So before I jump in and code up a way of saving table view related
>
user prefs in my document am I missing a feature of Cocoa that does
>
this for me?
If that can help you, here is some code I wrote that you can add to an
NSTableView subclass to do the trick.
This code allows you to:
- hide and show columns very simply,
- save and restore the order, size and visible state of the columns
(but NOT the sorting state, since I wrote it before Panther and I had
implemented my own sorting system -- I leave that to you as an
exercise!).
Here is how to use it:
- Once your table view is set up with all its columns, call
setInitialState (you must call it before any other method provided
here).
- To retrieve the current state in order to save it, call columnState
(this gives you a codable object, which happens to be an NSArray).
- To set the current state back, call restoreColumnState:.
- To hide or show a column, call setColumnWithIdentifier:visible:. (To
query the visible state, call isColumnWithIdentifierVisible:.)
- To retrieve the NSTableColumn object of a currently column given its
identifier, call initialColumnWithIdentifier:. (This works also with
columns that are currently hidden.)
Hope this helps...
(You also have to add an instance variable of type NSArray named
"allColumns". You also have to take care about its deallocation.)
- (NSObject < NSCoding > *)columnState
{
NSMutableArray *state;
NSArray *columns;
NSEnumerator *enumerator;
NSTableColumn *column;
columns = [self tableColumns];
state = [NSMutableArray arrayWithCapacity:[columns count]];
enumerator = [columns objectEnumerator];
while( column = [enumerator nextObject] )
{
[state addObject:
[NSDictionary dictionaryWithObjectsAndKeys:
[column identifier], @"Identifier",
[NSNumber numberWithFloat:[column width]], @"Width",
nil]];
}
return state;
}
- (void)restoreColumnState:(NSObject *)columnState
{
NSArray *state;
NSEnumerator *enumerator;
NSDictionary *params;
NSTableColumn *column;
NSAssert( columnState != nil, @"nil columnState!" );
NSAssert( [columnState isKindOfClass:[NSArray class]], @"columnState
is not an NSArray!" );
state = (NSArray *)columnState;
enumerator = [state objectEnumerator];
[self removeAllColumns];
while( params = [enumerator nextObject] )
{
column = [self initialColumnWithIdentifier:[params
objectForKey:@"Identifier"]];
if( column != nil )
{
[column setWidth:[[params objectForKey:@"Width"] floatValue]];
[self addTableColumn:column];
[self setIndicatorImage:nil inTableColumn:column];
[self setNeedsDisplay:YES];
}
}
[self sizeLastColumnToFit];
}
- (void)setColumnWithIdentifier:(id)identifier visible:(BOOL)visible
{
NSTableColumn *column;
column = [self initialColumnWithIdentifier:identifier];
NSAssert( column != nil, @"nil column!" );
if( visible )
{
if( ![self isColumnWithIdentifierVisible:identifier] )
{
[self addTableColumn:column];
[self sizeLastColumnToFit];
[self setNeedsDisplay:YES];
}
}
else
{
if( [self isColumnWithIdentifierVisible:identifier] )
{
[self removeTableColumn:column];
[self sizeLastColumnToFit];
[self setNeedsDisplay:YES];
}
}
}
- (BOOL)isColumnWithIdentifierVisible:(id)identifier
{
return [self columnWithIdentifier:identifier] != -1;
}
- (NSTableColumn *)initialColumnWithIdentifier:(id)identifier
{
NSEnumerator *enumerator;
NSTableColumn *column = nil;
enumerator = [allColumns objectEnumerator];
while( column = [enumerator nextObject] )
if( [[column identifier] isEqual:identifier] )
break;
return column;
}
- (void)removeAllColumns
{
NSArray *columns;
NSEnumerator *enumerator;
NSTableColumn *column;
columns = [NSArray arrayWithArray:[self tableColumns]];
enumerator = [columns objectEnumerator];
while( column = [enumerator nextObject] )
[self removeTableColumn:column];
}
- (void)setInitialState
{
allColumns = [[NSArray arrayWithArray:[self tableColumns]] retain];
}
Ambroise
http://www.cellulo.info/
ICQ 4508259
AIM atvaark
[demime 0.98b removed an attachment of type text/directory which had a name of Ambroise Confetti.vcf]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.