From: <ho...@us...> - 2011-12-02 17:50:40
|
Revision: 18270 http://bibdesk.svn.sourceforge.net/bibdesk/?rev=18270&view=rev Author: hofman Date: 2011-12-02 17:50:33 +0000 (Fri, 02 Dec 2011) Log Message: ----------- Set formatter and if needed icon for icon+text cell in base class, so we only need to give the formatter class in the subclasses Modified Paths: -------------- trunk/bibdesk/BDSKFilePathCell.m trunk/bibdesk/BDSKIconTextFieldCell.h trunk/bibdesk/BDSKIconTextFieldCell.m trunk/bibdesk/BDSKTextWithIconCell.m Modified: trunk/bibdesk/BDSKFilePathCell.m =================================================================== --- trunk/bibdesk/BDSKFilePathCell.m 2011-12-02 15:56:44 UTC (rev 18269) +++ trunk/bibdesk/BDSKFilePathCell.m 2011-12-02 17:50:33 UTC (rev 18270) @@ -43,34 +43,17 @@ @implementation BDSKFilePathCell -static BDSKFilePathFormatter *filePathFormatter = nil; - -+ (void)initialize { - BDSKINITIALIZE; - - filePathFormatter = [[BDSKFilePathFormatter alloc] init]; ++ (Class)formatterClass { + return [BDSKFilePathFormatter class]; } -- (id)initTextCell:(NSString *)aString { - self = [super initTextCell:aString]; - if (self) { - [self setFormatter:filePathFormatter]; - } - return self; -} +@end -- (id)initWithCoder:(NSCoder *)coder { - self = [super initWithCoder:coder]; - if (self) { - if ([self formatter] == nil) - [self setFormatter:filePathFormatter]; - } - return self; -} +#pragma mark - -- (void)setObjectValue:(id <NSCopying>)obj { - [super setObjectValue:obj]; - +@implementation BDSKFilePathFormatter + +- (NSImage *)imageForObjectValue:(id)obj { NSImage *image = nil; if ([(id)obj isKindOfClass:[NSString class]]) { NSString *path = [(NSString *)obj stringByStandardizingPath]; @@ -81,15 +64,9 @@ if([[NSFileManager defaultManager] objectExistsAtFileURL:fileURL]) image = [NSImage imageForURL:fileURL]; } - [self setIcon:image]; + return image; } -@end - -#pragma mark - - -@implementation BDSKFilePathFormatter - - (NSString *)stringForObjectValue:(id)obj { NSString *path = [obj isKindOfClass:[NSURL class]] ? [obj path] : [obj description]; return [path stringByAbbreviatingWithTildeInPath]; Modified: trunk/bibdesk/BDSKIconTextFieldCell.h =================================================================== --- trunk/bibdesk/BDSKIconTextFieldCell.h 2011-12-02 15:56:44 UTC (rev 18269) +++ trunk/bibdesk/BDSKIconTextFieldCell.h 2011-12-02 17:50:33 UTC (rev 18270) @@ -43,6 +43,8 @@ NSImageCell *imageCell; } ++ (Class)formatterClass; + - (NSImage *)icon; - (void)setIcon:(NSImage *)newIcon; @@ -50,3 +52,8 @@ - (NSRect)iconRectForBounds:(NSRect)aRect; @end + + +@interface NSFormatter (BDSKIconTextFieldCell) +- (NSImage *)imageForObjectValue:(id)obj; +@end Modified: trunk/bibdesk/BDSKIconTextFieldCell.m =================================================================== --- trunk/bibdesk/BDSKIconTextFieldCell.m 2011-12-02 15:56:44 UTC (rev 18269) +++ trunk/bibdesk/BDSKIconTextFieldCell.m 2011-12-02 17:50:33 UTC (rev 18270) @@ -51,11 +51,15 @@ #define BORDER_BETWEEN_IMAGE_AND_TEXT_BEZELED (-2.0) #define IMAGE_OFFSET (1.0) ++ (Class)formatterClass { return Nil; } + - (id)initTextCell:(NSString *)aString { self = [super initTextCell:aString]; if (self) { imageCell = [[NSImageCell alloc] init]; [imageCell setImageScaling:NSImageScaleProportionallyUpOrDown]; + if ([[self class] formatterClass]) + [self setFormatter:[[[[[self class] formatterClass] alloc] init] autorelease]]; } return self; } @@ -68,6 +72,8 @@ imageCell = [[NSImageCell alloc] init]; [imageCell setImageScaling:NSImageScaleProportionallyUpOrDown]; } + if ([self formatter] == nil && [[self class] formatterClass]) + [self setFormatter:[[[[[self class] formatterClass] alloc] init] autorelease]]; } return self; } @@ -168,6 +174,12 @@ } } +- (void)setObjectValue:(id <NSCopying>)obj { + [super setObjectValue:obj]; + if ([[self formatter] respondsToSelector:@selector(imageForObjectValue:)]) + [self setIcon:[[self formatter] imageForObjectValue:obj]]; +} + - (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(NSInteger)selStart length:(NSInteger)selLength { [super selectWithFrame:[self textRectForBounds:aRect] inView:controlView editor:textObj delegate:anObject start:selStart length:selLength]; } @@ -183,4 +195,8 @@ return hit; } +- (NSColor *)highlightColorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { + return nil; +} + @end Modified: trunk/bibdesk/BDSKTextWithIconCell.m =================================================================== --- trunk/bibdesk/BDSKTextWithIconCell.m 2011-12-02 15:56:44 UTC (rev 18269) +++ trunk/bibdesk/BDSKTextWithIconCell.m 2011-12-02 17:50:33 UTC (rev 18270) @@ -41,62 +41,31 @@ NSString *BDSKTextWithIconCellStringKey = @"string"; NSString *BDSKTextWithIconCellImageKey = @"image"; -static id nonNullObjectValueForKey(id object, NSString *key) { +static id nonNullObjectValueForKey(id object, id stringObject, NSString *key) { + if ([object isKindOfClass:[NSString class]]) + return stringObject; id value = [object valueForKey:key]; return [value isEqual:[NSNull null]] ? nil : value; } @implementation BDSKTextWithIconCell -static BDSKTextWithIconFormatter *textWithIconFormatter = nil; - -+ (void)initialize { - BDSKINITIALIZE; - textWithIconFormatter = [[BDSKTextWithIconFormatter alloc] init]; ++ (Class)formatterClass { + return [BDSKTextWithIconFormatter class]; } -- (id)initTextCell:(NSString *)aString { - self = [super initTextCell:aString]; - if (self) { - [self setFormatter:textWithIconFormatter]; - } - return self; -} - -- (id)initWithCoder:(NSCoder *)coder { - self = [super initWithCoder:coder]; - if (self) { - if ([self formatter] == nil) - [self setFormatter:textWithIconFormatter]; - } - return self; -} - -- (NSColor *)highlightColorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { - return nil; -} - -- (void)setObjectValue:(id <NSCopying>)obj { - // the objectValue should be an object that's KVC compliant for the "string" and "image" keys - - // this can happen initially from the init, as there's no initializer passing an objectValue - if ([(id)obj isKindOfClass:[NSString class]]) - obj = [NSDictionary dictionaryWithObjectsAndKeys:obj, BDSKTextWithIconCellStringKey, nil]; - - // we should not set a derived value such as the string here, otherwise NSTableView will call tableView:setObjectValue:forTableColumn:row: whenever a cell is selected - [super setObjectValue:obj]; - - [self setIcon:nonNullObjectValueForKey(obj, BDSKTextWithIconCellImageKey)]; -} - @end #pragma mark - @implementation BDSKTextWithIconFormatter +- (NSImage *)imageForObjectValue:(id)obj { + return nonNullObjectValueForKey(obj, nil, BDSKTextWithIconCellImageKey); +} + - (NSString *)stringForObjectValue:(id)obj { - return [obj isKindOfClass:[NSString class]] ? obj : nonNullObjectValueForKey(obj, BDSKTextWithIconCellStringKey); + return nonNullObjectValueForKey(obj, obj, BDSKTextWithIconCellStringKey); } - (BOOL)getObjectValue:(id *)obj forString:(NSString *)string errorDescription:(NSString **)error { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |