Revision: 3554
          http://skim-app.svn.sourceforge.net/skim-app/?rev=3554&view=rev
Author:   hofman
Date:     2008-03-25 12:51:27 -0700 (Tue, 25 Mar 2008)

Log Message:
-----------
Class cluster for bookmarks.

Modified Paths:
--------------
    trunk/SKBookmark.h
    trunk/SKBookmark.m

Modified: trunk/SKBookmark.h
===================================================================
--- trunk/SKBookmark.h  2008-03-25 18:20:12 UTC (rev 3553)
+++ trunk/SKBookmark.h  2008-03-25 19:51:27 UTC (rev 3554)
@@ -50,13 +50,7 @@
 @class BDAlias;
 
 @interface SKBookmark : NSObject <NSCopying> {
-    BDAlias *alias;
-    NSData *aliasData;
-    NSString *label;
-    unsigned int pageIndex;
-    NSMutableArray *children;
     SKBookmark *parent;
-    int bookmarkType;
 }
 
 - (id)initWithAlias:(BDAlias *)anAlias pageIndex:(unsigned)aPageIndex 
label:(NSString *)aLabel;
@@ -71,22 +65,25 @@
 
 - (int)bookmarkType;
 
+- (NSString *)label;
+- (void)setLabel:(NSString *)newLabel;
+
+- (NSImage *)icon;
+
 - (NSString *)path;
 - (BDAlias *)alias;
 - (NSData *)aliasData;
-- (NSImage *)icon;
 - (unsigned int)pageIndex;
 - (NSNumber *)pageNumber;
-- (NSString *)label;
-- (void)setLabel:(NSString *)newLabel;
 
-- (SKBookmark *)parent;
-- (void)setParent:(SKBookmark *)newParent;
 - (NSArray *)children;
 - (void)insertChild:(SKBookmark *)child atIndex:(unsigned int)index;
 - (void)addChild:(SKBookmark *)child;
 - (void)removeChild:(SKBookmark *)child;
 
+- (SKBookmark *)parent;
+- (void)setParent:(SKBookmark *)newParent;
+
 - (BOOL)isDescendantOf:(SKBookmark *)bookmark;
 - (BOOL)isDescendantOfArray:(NSArray *)bookmarks;
 

Modified: trunk/SKBookmark.m
===================================================================
--- trunk/SKBookmark.m  2008-03-25 18:20:12 UTC (rev 3553)
+++ trunk/SKBookmark.m  2008-03-25 19:51:27 UTC (rev 3554)
@@ -40,6 +40,7 @@
 #import "SKBookmarkController.h"
 #import "BDAlias.h"
 #import "NSImage_SKExtensions.h"
+#import "OBUtilities.h"
 
 NSString *SKBookmarkChangedNotification = @"SKBookmarkChangedNotification";
 NSString *SKBookmarkWillBeRemovedNotification = 
@"SKBookmarkWillBeRemovedNotification";
@@ -54,51 +55,44 @@
 #define ALIAS_DATA_KEY  @"_BDAlias"
 #define TYPE_KEY        @"type"
 
[EMAIL PROTECTED] SKFileBookmark : SKBookmark {
+    BDAlias *alias;
+    NSData *aliasData;
+    NSString *label;
+    unsigned int pageIndex;
+}
[EMAIL PROTECTED]
+
[EMAIL PROTECTED] SKFolderBookmark : SKBookmark {
+    NSString *label;
+    NSMutableArray *children;
+}
[EMAIL PROTECTED]
+
[EMAIL PROTECTED] SKSeparatorBookmark : SKBookmark
[EMAIL PROTECTED]
+
 @implementation SKBookmark
 
-+ (NSImage *)smallImageForFile:(NSString *)filePath {
-    static NSMutableDictionary *smallIcons = nil;
-    
-    if (filePath == nil)
-        return [NSImage smallMissingFileImage];
-    
-    NSString *extension = [filePath pathExtension];
-    NSImage *icon = [smallIcons objectForKey:extension];
-    
-    if (icon == nil) {
-        if (smallIcons == nil)
-            smallIcons = [[NSMutableDictionary alloc] init];
-        NSImage *image = [[NSWorkspace sharedWorkspace] 
iconForFileType:extension];
-        if (image) {
-            NSRect sourceRect = {NSZeroPoint, [image size]};
-            NSRect targetRect = NSMakeRect(0.0, 0.0, 16.0, 16.0);
-            icon = [[NSImage alloc] initWithSize:targetRect.size];
-            [icon lockFocus];
-            [[NSGraphicsContext currentContext] 
setImageInterpolation:NSImageInterpolationHigh];
-            [image drawInRect:targetRect fromRect:sourceRect 
operation:NSCompositeCopy fraction:1.0];
-            [icon unlockFocus];
-            [smallIcons setObject:icon forKey:extension];
-            [icon release];
-        }
+static SKBookmark *defaultPlaceholderBookmark = nil;
+static Class SKBookmarkClass = Nil;
+
++ (void)initialize {
+    OBINITIALIZE;
+    if (self == [SKBookmark class]) {
+        SKBookmarkClass = self;
+        defaultPlaceholderBookmark = (SKBookmark 
*)NSAllocateObject(SKBookmarkClass, 0, NSDefaultMallocZone());
     }
-    return icon;
 }
 
++ (id)allocWithZone:(NSZone *)aZone {
+    return SKBookmarkClass == self ? defaultPlaceholderBookmark : 
NSAllocateObject(self, 0, aZone);
+}
+
 - (id)initWithAlias:(BDAlias *)anAlias pageIndex:(unsigned)aPageIndex 
label:(NSString *)aLabel {
-    if (self = [super init]) {
-        if (anAlias) {
-            bookmarkType = SKBookmarkTypeBookmark;
-            alias = [anAlias retain];
-            aliasData = [[alias aliasData] retain];
-            pageIndex = aPageIndex;
-            label = [aLabel copy];
-            children = nil;
-        } else {
-            [self release];
-            self = nil;
-        }
-    }
-    return self;
+    if (self != defaultPlaceholderBookmark)
+        [self release];
+    return [[SKFileBookmark alloc] initWithAlias:anAlias pageIndex:aPageIndex 
label:aLabel];
 }
 
 - (id)initWithAliasData:(NSData *)aData pageIndex:(unsigned)aPageIndex 
label:(NSString *)aLabel {
@@ -110,16 +104,9 @@
 }
 
 - (id)initFolderWithChildren:(NSArray *)aChildren label:(NSString *)aLabel {
-    if (self = [super init]) {
-        bookmarkType = SKBookmarkTypeFolder;
-        alias = nil;
-        aliasData = nil;
-        pageIndex = NSNotFound;
-        label = [aLabel copy];
-        children = [aChildren mutableCopy];
-        [children makeObjectsPerformSelector:@selector(setParent:) 
withObject:self];
-    }
-    return self;
+    if (self != defaultPlaceholderBookmark)
+        [self release];
+    return [[SKFolderBookmark alloc] initFolderWithChildren:aChildren 
label:aLabel];
 }
 
 - (id)initFolderWithLabel:(NSString *)aLabel {
@@ -127,15 +114,9 @@
 }
 
 - (id)initSeparator {
-    if (self = [super init]) {
-        bookmarkType = SKBookmarkTypeSeparator;
-        alias = nil;
-        aliasData = nil;
-        pageIndex = NSNotFound;
-        label = nil;
-        children = nil;
-    }
-    return self;
+    if (self != defaultPlaceholderBookmark)
+        [self release];
+    return [[SKSeparatorBookmark alloc] init];
 }
 
 - (id)initWithDictionary:(NSDictionary *)dictionary {
@@ -154,43 +135,107 @@
 }
 
 - (id)copyWithZone:(NSZone *)aZone {
-    if (bookmarkType == SKBookmarkTypeFolder)
-        return [[[self class] allocWithZone:aZone] 
initFolderWithChildren:[[[NSArray alloc] initWithArray:children copyItems:YES] 
autorelease] label:label];
-    else if (bookmarkType == SKBookmarkTypeSeparator)
-        return [[[self class] allocWithZone:aZone] initSeparator];
-    else
-        return [[[self class] allocWithZone:aZone] initWithAlias:alias 
pageIndex:pageIndex label:label];
+    [self doesNotRecognizeSelector:_cmd];
+    return nil;
 }
 
 - (void)dealloc {
+    if (self != defaultPlaceholderBookmark)
+        [super dealloc];
+}
+
+- (NSDictionary *)dictionaryValue { return nil; }
+
+- (int)bookmarkType { return SKBookmarkTypeSeparator; }
+
+- (NSImage *)icon { return nil; }
+
+- (NSString *)label { return nil; }
+- (void)setLabel:(NSString *)newLabel {}
+
+- (NSString *)path { return nil; }
+- (BDAlias *)alias { return nil; }
+- (NSData *)aliasData { return nil; }
+- (unsigned int)pageIndex { return NSNotFound; }
+- (NSNumber *)pageNumber { return nil; }
+
+- (NSArray *)children { return nil; }
+- (void)insertChild:(SKBookmark *)child atIndex:(unsigned int)anIndex {}
+- (void)addChild:(SKBookmark *)child {}
+- (void)removeChild:(SKBookmark *)child {}
+
+- (SKBookmark *)parent {
+    return parent;
+}
+
+- (void)setParent:(SKBookmark *)newParent {
+    parent = newParent;
+}
+
+- (BOOL)isDescendantOf:(SKBookmark *)bookmark {
+    if (self == bookmark)
+        return YES;
+    NSEnumerator *childEnum = [[bookmark children] objectEnumerator];
+    SKBookmark *child;
+    while (child = [childEnum nextObject]) {
+        if ([self isDescendantOf:child])
+            return YES;
+    }
+    return NO;
+}
+
+- (BOOL)isDescendantOfArray:(NSArray *)bookmarks {
+    NSEnumerator *bmEnum = [bookmarks objectEnumerator];
+    SKBookmark *bm = nil;
+    while (bm = [bmEnum nextObject]) {
+        if ([self isDescendantOf:bm]) return YES;
+    }
+    return NO;
+}
+
[EMAIL PROTECTED]
+
+#pragma mark -
+
[EMAIL PROTECTED] SKFileBookmark
+
+- (id)initWithAlias:(BDAlias *)anAlias pageIndex:(unsigned)aPageIndex 
label:(NSString *)aLabel {
+    if (self = [super init]) {
+        if (anAlias) {
+            alias = [anAlias retain];
+            aliasData = [[alias aliasData] retain];
+            pageIndex = aPageIndex;
+            label = [aLabel copy];
+        } else {
+            [self release];
+            self = nil;
+        }
+    }
+    return self;
+}
+
+- (id)copyWithZone:(NSZone *)aZone {
+    return [[[self class] allocWithZone:aZone] initWithAlias:alias 
pageIndex:pageIndex label:label];
+}
+
+- (void)dealloc {
     [[[SKBookmarkController sharedBookmarkController] undoManager] 
removeAllActionsWithTarget:self];
     [alias release];
     [aliasData release];
     [label release];
-    [children release];
     [super dealloc];
 }
 
 - (NSString *)description {
-    if (bookmarkType == SKBookmarkTypeFolder)
-        return [NSString stringWithFormat:@"<%@: label=%@, children=%@>", 
[self class], label, children];
-    else if (bookmarkType == SKBookmarkTypeSeparator)
-        return [NSString stringWithFormat:@"<%@: separator>", [self class]];
-    else
-        return [NSString stringWithFormat:@"<%@: label=%@, path=%@, page=%i>", 
[self class], label, [self path], pageIndex];
+    return [NSString stringWithFormat:@"<%@: label=%@, path=%@, page=%i>", 
[self class], label, [self path], pageIndex];
 }
 
 - (NSDictionary *)dictionaryValue {
-    if (bookmarkType == SKBookmarkTypeFolder)
-        return [NSDictionary 
dictionaryWithObjectsAndKeys:SKBookmarkTypeFolderString, TYPE_KEY, [children 
valueForKey:@"dictionaryValue"], CHILDREN_KEY, label, LABEL_KEY, nil];
-    else if (bookmarkType == SKBookmarkTypeSeparator)
-        return [NSDictionary 
dictionaryWithObjectsAndKeys:SKBookmarkTypeSeparatorString, TYPE_KEY, nil];
-    else
-        return [NSDictionary 
dictionaryWithObjectsAndKeys:SKBookmarkTypeBookmarkString, TYPE_KEY, [self 
aliasData], ALIAS_DATA_KEY, [NSNumber numberWithUnsignedInt:pageIndex], 
PAGE_INDEX_KEY, label, LABEL_KEY, nil];
+    return [NSDictionary 
dictionaryWithObjectsAndKeys:SKBookmarkTypeBookmarkString, TYPE_KEY, [self 
aliasData], ALIAS_DATA_KEY, [NSNumber numberWithUnsignedInt:pageIndex], 
PAGE_INDEX_KEY, label, LABEL_KEY, nil];
 }
 
 - (int)bookmarkType {
-    return bookmarkType;
+    return SKBookmarkTypeBookmark;
 }
 
 - (NSString *)path {
@@ -206,12 +251,33 @@
 }
 
 - (NSImage *)icon {
-    if ([self bookmarkType] == SKBookmarkTypeFolder)
-        return [NSImage smallFolderImage];
-    else if (bookmarkType == SKBookmarkTypeSeparator)
-        return nil;
-    else
-        return [[self class] smallImageForFile:[self path]];
+    static NSMutableDictionary *smallIcons = nil;
+    
+    NSString *filePath = [self path];
+    
+    if (filePath == nil)
+        return [NSImage smallMissingFileImage];
+    
+    NSString *extension = [filePath pathExtension];
+    NSImage *icon = [smallIcons objectForKey:extension];
+    
+    if (icon == nil) {
+        if (smallIcons == nil)
+            smallIcons = [[NSMutableDictionary alloc] init];
+        NSImage *image = [[NSWorkspace sharedWorkspace] 
iconForFileType:extension];
+        if (image) {
+            NSRect sourceRect = {NSZeroPoint, [image size]};
+            NSRect targetRect = NSMakeRect(0.0, 0.0, 16.0, 16.0);
+            icon = [[NSImage alloc] initWithSize:targetRect.size];
+            [icon lockFocus];
+            [[NSGraphicsContext currentContext] 
setImageInterpolation:NSImageInterpolationHigh];
+            [image drawInRect:targetRect fromRect:sourceRect 
operation:NSCompositeCopy fraction:1.0];
+            [icon unlockFocus];
+            [smallIcons setObject:icon forKey:extension];
+            [icon release];
+        }
+    }
+    return icon;
 }
 
 - (unsigned int)pageIndex {
@@ -236,14 +302,62 @@
     }
 }
 
-- (SKBookmark *)parent {
-    return parent;
[EMAIL PROTECTED]
+
+#pragma mark -
+
[EMAIL PROTECTED] SKFolderBookmark
+
+- (id)initFolderWithChildren:(NSArray *)aChildren label:(NSString *)aLabel {
+    if (self = [super init]) {
+        label = [aLabel copy];
+        children = [aChildren mutableCopy];
+        [children makeObjectsPerformSelector:@selector(setParent:) 
withObject:self];
+    }
+    return self;
 }
 
-- (void)setParent:(SKBookmark *)newParent {
-    parent = newParent;
+- (id)copyWithZone:(NSZone *)aZone {
+    return [[[self class] allocWithZone:aZone] 
initFolderWithChildren:[[[NSArray alloc] initWithArray:children copyItems:YES] 
autorelease] label:label];
 }
 
+- (void)dealloc {
+    [[[SKBookmarkController sharedBookmarkController] undoManager] 
removeAllActionsWithTarget:self];
+    [label release];
+    [children release];
+    [super dealloc];
+}
+
+- (NSString *)description {
+    return [NSString stringWithFormat:@"<%@: label=%@, children=%@>", [self 
class], label, children];
+}
+
+- (NSDictionary *)dictionaryValue {
+    return [NSDictionary 
dictionaryWithObjectsAndKeys:SKBookmarkTypeFolderString, TYPE_KEY, [children 
valueForKey:@"dictionaryValue"], CHILDREN_KEY, label, LABEL_KEY, nil];
+}
+
+- (int)bookmarkType {
+    return SKBookmarkTypeFolder;
+}
+
+- (NSImage *)icon {
+    return [NSImage smallFolderImage];
+}
+
+- (NSString *)label {
+    return label;
+}
+
+- (void)setLabel:(NSString *)newLabel {
+    if (label != newLabel) {
+        NSUndoManager *undoManager = [[SKBookmarkController 
sharedBookmarkController] undoManager];
+        [(SKBookmark *)[undoManager prepareWithInvocationTarget:self] 
setLabel:label];
+        [label release];
+        label = [newLabel retain];
+        [[NSNotificationCenter defaultCenter] 
postNotificationName:SKBookmarkChangedNotification object:self];
+    }
+}
+
 - (NSArray *)children {
     return children;
 }
@@ -269,25 +383,26 @@
     [[NSNotificationCenter defaultCenter] 
postNotificationName:SKBookmarkChangedNotification object:self];
 }
 
-- (BOOL)isDescendantOf:(SKBookmark *)bookmark {
-    if (self == bookmark)
-        return YES;
-    NSEnumerator *childEnum = [[bookmark children] objectEnumerator];
-    SKBookmark *child;
-    while (child = [childEnum nextObject]) {
-        if ([self isDescendantOf:child])
-            return YES;
-    }
-    return NO;
[EMAIL PROTECTED]
+
+#pragma mark -
+
[EMAIL PROTECTED] SKSeparatorBookmark
+
+- (id)copyWithZone:(NSZone *)aZone {
+    return [[[self class] allocWithZone:aZone] init];
 }
 
-- (BOOL)isDescendantOfArray:(NSArray *)bookmarks {
-    NSEnumerator *bmEnum = [bookmarks objectEnumerator];
-    SKBookmark *bm = nil;
-    while (bm = [bmEnum nextObject]) {
-        if ([self isDescendantOf:bm]) return YES;
-    }
-    return NO;
+- (NSString *)description {
+    return [NSString stringWithFormat:@"<%@: separator>", [self class]];
 }
 
+- (NSDictionary *)dictionaryValue {
+    return [NSDictionary 
dictionaryWithObjectsAndKeys:SKBookmarkTypeSeparatorString, TYPE_KEY, nil];
+}
+
+- (int)bookmarkType {
+    return SKBookmarkTypeSeparator;
+}
+
 @end


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Skim-app-commit mailing list
Skim-app-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to