Re: Many-To-Many Relationship Interface in Core Data
Re: Many-To-Many Relationship Interface in Core Data
- Subject: Re: Many-To-Many Relationship Interface in Core Data
- From: mmalc crawford <email@hidden>
- Date: Fri, 27 Apr 2007 15:41:00 -0700
Here's one possible implementation...
It can probably be optimised.
mmalc
@interface ToManySelectionController : NSArrayController
{
NSString *belongsRelationshipName;
NSString *belongsColumnIdentifier;
IBOutlet NSTableView *myTableView;
IBOutlet NSArrayController *sourceArrayController;
}
- (NSString *)belongsRelationshipName;
- (void)setBelongsRelationshipName:(NSString *)name;
- (NSString *)belongsColumnIdentifier;
- (void)setBelongsColumnIdentifier:(NSString *)aBelongsColumnIdentifier;
@end
static void *SELECTED_OBJECTS_CONTEXT = (void *)2019;
static NSNumber *SELECTED, *NOT_SELECTED, *MIXED_SELECTION;
static BOOL initialized = NO;
@implementation ToManySelectionController
- (void)awakeFromNib
{
[super awakeFromNib];
[self setBelongsColumnIdentifier:@"belongs"];
[self setBelongsRelationshipName:@"movies"];
if (!initialized)
{
SELECTED = [[NSNumber numberWithInt:1] retain];
NOT_SELECTED = [[NSNumber numberWithInt:0] retain];
MIXED_SELECTION = [[NSNumber numberWithInt:-1] retain];
initialized = YES;
}
[sourceArrayController addObserver:self forKeyPath:@"selectedObjects"
options:0 context:SELECTED_OBJECTS_CONTEXT];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context
{
if (context == SELECTED_OBJECTS_CONTEXT)
{
[myTableView reloadData];
}
else
{
[super observeValueForKeyPath:keyPath ofObject:object change:change
context:context];
}
}
- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [[self arrangedObjects] count];
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:
(NSTableColumn *)aTableColumn row:(int)rowIndex
{
NSString *identifier = [aTableColumn identifier];
if (![identifier isEqualToString:[self belongsColumnIdentifier]])
{
return nil;
}
NSArray * sourceSelectionArray = [sourceArrayController
selectedObjects];
unsigned int sourceSelectionArrayCount = [sourceSelectionArray count];
if (sourceSelectionArrayCount == 0)
{
return MIXED_SELECTION;
}
id firstSourceSelection = [sourceSelectionArray objectAtIndex:0];
id firstRelationshipCollection = [firstSourceSelection valueForKey:
[self belongsRelationshipName]];
id objectForCurrentRow = [[self arrangedObjects]
objectAtIndex:rowIndex];
BOOL firstSelection = [firstRelationshipCollection
containsObject:objectForCurrentRow];
unsigned int counter;
for (counter = 1; counter < sourceSelectionArrayCount; counter++)
{
id nextSourceSelection = [sourceSelectionArray objectAtIndex:counter];
id nextRelationshipCollection = [nextSourceSelection valueForKey:
[self belongsRelationshipName]];
BOOL nextSelection = [nextRelationshipCollection
containsObject:objectForCurrentRow];
if (firstSelection != nextSelection)
{
return MIXED_SELECTION;
}
}
if (firstSelection)
{
return SELECTED;
}
return NOT_SELECTED;
}
- (void)tableView:(NSTableView *)aTableView setObjectValue:
(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:
(int)rowIndex
{
NSString *identifier = [aTableColumn identifier];
if (![identifier isEqualToString:[self belongsColumnIdentifier]])
{
return;
}
NSArray * sourceSelectionArray = [sourceArrayController
selectedObjects];
if ([sourceSelectionArray count] == 0)
{
return;
}
BOOL select = [anObject boolValue];
NSEnumerator *sourceSelectionEnumerator = [sourceSelectionArray
objectEnumerator];
id selectedObject;
NSString *relationship = [self belongsRelationshipName];
id objectForCurrentRow = [[self arrangedObjects]
objectAtIndex:rowIndex];
while (selectedObject = [sourceSelectionEnumerator nextObject])
{
id relationshipCollection = [selectedObject
mutableSetValueForKey:relationship];
if (select)
{
[relationshipCollection addObject:objectForCurrentRow];
}
else
{
[relationshipCollection removeObject:objectForCurrentRow];
}
}
}
- (NSString *)belongsRelationshipName
{
return belongsRelationshipName;
}
- (void)setBelongsRelationshipName:(NSString *)aBelongsRelationshipName
{
if (belongsRelationshipName != aBelongsRelationshipName) {
[belongsRelationshipName release];
belongsRelationshipName = [aBelongsRelationshipName copy];
}
}
- (NSString *)belongsColumnIdentifier
{
return belongsColumnIdentifier;
}
- (void)setBelongsColumnIdentifier:(NSString *)identifier
{
if (belongsColumnIdentifier != identifier) {
[belongsColumnIdentifier release];
belongsColumnIdentifier = [identifier copy];
}
}
- (void)dealloc
{
[belongsRelationshipName release];
[belongsRelationshipName release];
[super dealloc];
}
@end
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden