// // BPDocumentGroup.m // Attempt2 // // Created by Bea on 10/05/05. // Copyright 2005 __MyCompanyName__. All rights reserved. // #import "BPDocumentGroup.h" #import "BPDocument.h" #import "BPPerson.h" @implementation BPDocumentGroup + (id)groupWithLabel:(NSString *)groupLabel atPosition:(NSPoint)groupPosition documents:(NSArray *)groupDocuments { BPDocumentGroup *dg = [[BPDocumentGroup alloc] initWithLabel:groupLabel atPosition:groupPosition documents:groupDocuments]; [dg autorelease]; return dg; } - (id)initWithLabel:(NSString *)groupLabel atPosition:(NSPoint)groupPosition documents:(NSArray *)groupDocuments { [super init]; ident = [self hash]; // is this OK??? isSystemGroup = NO; position = malloc(sizeof(NSPoint)); if (groupDocuments == nil) { documents = [[NSMutableArray alloc] initWithCapacity:10]; } else { //mutableCopy so don't need to retain groupDocuments documents = [groupDocuments mutableCopy]; [documents makeObjectsPerformSelector:@selector(setDocumentGroup:) withObject:self]; } [self setLabel:groupLabel]; [self setPosition:groupPosition]; labelSetBySystem = YES; return self; } - (id)initWithLabel:(NSString *)groupLabel atPosition:(NSPoint)groupPosition { return [self initWithLabel:groupLabel atPosition:groupPosition documents:nil]; } - (int)identifier { return ident; } - (void)setIsSystemGroup:(BOOL)systemPile { isSystemGroup = systemPile; } - (BOOL)isSystemGroup { return isSystemGroup; } - (void)add:(NSObject *)document { int index = [documents indexOfObject:document]; if (index == NSNotFound) { [documents addObject:document]; [document setDocumentGroup:self]; } } - (void)remove:(NSObject *)document { int index = [documents indexOfObject:document]; if (index != NSNotFound) { [documents removeObjectAtIndex:index]; [document setDocumentGroup:nil]; } } - (int)documentCount { return [documents count]; } - (NSArray *)documents { return documents; } - (void)setLabel:(NSString *)theLabel { [theLabel retain]; [label release]; label = theLabel; } - (NSString *)label { return label; } - (void)setLabelWasSetBySystem:(BOOL)setBySystem { labelSetBySystem = setBySystem; } - (BOOL)canAutoLabel { return labelSetBySystem; } - (void)setPosition:(NSPoint)point { position->x = point.x; position->y = point.y; } - (NSPoint)position { return *position; } - (BOOL)allDocumentsCorrespondSameAuthor { if ([documents count] == 0) return YES; BPPerson *author = [[documents objectAtIndex:0] author]; NSObject *doc; int i; for (i=0; i<[documents count]; i++) { doc = [documents objectAtIndex:i]; if (![[doc author] isEqual:author]) { return NO; } } return YES; } - (NSString *)summary { if ([documents count] == 0) return @""; if ([self allDocumentsCorrespondSameAuthor]) { NSString *authorDesc = [[[documents objectAtIndex:0] author] name]; return [NSString stringWithFormat:@"from %@", authorDesc]; } return @""; } - (NSString *)description { return [NSString stringWithFormat:@"BPDocumentGroup %@ %d (%d items)", label, ident, [documents count]]; } /* - (BOOL)isEqual:(id)anObject { if (![anObject isKindOfClass:[BPDocumentGroup class]]) return NO; if (anObject == self) return YES; BPDocumentGroup *otherGroup = (BPDocumentGroup *)anObject; if ([documents isEqual:[otherGroup documents]] && [label isEqual:[otherGroup label]] && NSEqualPoints(*position, [otherGroup position])) { return YES; } return NO; } */ - (void)dealloc { NSLog(@"destroying %@", self); [documents release]; [label release]; free(position); // do I have to dealloc Point instances somehow?? [super dealloc]; } #pragma - #pragma archival - (void)encodeWithCoder:(NSCoder *)coder { // super doesn't implement NSCoding, so don't need to call super impl. [coder encodeInt:ident forKey:@"ident"]; [coder encodeBool:isSystemGroup forKey:@"isSystemGroup"]; [coder encodeObject:documents forKey:@"documents"]; [coder encodeObject:label forKey:@"label"]; [coder encodePoint:*position forKey:@"position"]; [coder encodeBool:labelSetBySystem forKey:@"labelSetBySystem"]; } - (id)initWithCoder:(NSCoder *)coder { [super init]; ident = [coder decodeIntForKey:@"ident"]; isSystemGroup = [coder decodeBoolForKey:@"isSystemGroup"]; documents = [[coder decodeObjectForKey:@"documents"] retain]; [self setLabel:[coder decodeObjectForKey:@"label"]]; position = malloc(sizeof(NSPoint)); [self setPosition:[coder decodePointForKey:@"position"]]; labelSetBySystem = [coder decodeBoolForKey:@"labelSetBySystem"]; return self; } @end