// // LabelManager.m // Pennyworth // // Created by Chris Karr on 4/1/08. // Copyright 2008 Chris J. Karr. All rights reserved. // #import "LabelManager.h" #import "User.h" @implementation LabelManager @synthesize types; @synthesize labels; @synthesize currentLabels; @synthesize selectedLabel; @synthesize selectedType; @synthesize newLabel; @synthesize action; /* - (NSArray *) currentLabels { return [[self.labels valueForKey:selectedType] arrangedObjects]; } - (void) setSelectedType:(NSString *) type { [self willChangeValueForKey:@"selectedType"]; [self willChangeValueForKey:@"currentLabels"]; [selectedType release]; selectedType = [type retain]; [self didChangeValueForKey:@"selectedType"]; [self didChangeValueForKey:@"currentLabels"]; } - (NSString *) getSelectedType { return selectedType; } */ - (void) awakeFromNib { [self addObserver:self forKeyPath:@"selectedType" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:nil]; [self addObserver:self forKeyPath:@"selectedLabel" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:nil]; [self addObserver:self forKeyPath:@"action" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh:) name:LABELS_UPDATED object:nil]; } - (void) refresh:(NSNotification *) theNote { NSDictionary * newLabels = [theNote userInfo]; self.types = [newLabels allKeys]; self.labels = [NSMutableDictionary dictionaryWithDictionary:newLabels]; self.selectedLabel = nil; self.selectedType = nil; if (self.action == nil) self.action = [NSNumber numberWithInteger:0]; } - (BOOL) rename { return [self.action boolValue]; } - (IBAction) doAction:(id) sender { NSString * operation = DELETE_OPERATION; if ([self rename]) operation = RENAME_OPERATION; NSMutableDictionary * op = [NSMutableDictionary dictionaryWithObjectsAndKeys:selectedType, LABEL_TYPE, operation, OPERATION, self.selectedLabel, ORIGINAL_LABEL, self.newLabel, NEW_LABEL, nil]; [[NSNotificationCenter defaultCenter] postNotificationName:LABEL_OPERATION object:self userInfo:op]; } - (void) observeValueForKeyPath: (NSString *)keyPath ofObject:(id)object change:(NSDictionary *) change context:(void *) context { if ([keyPath isEqual:@"selectedType"]) { self.currentLabels = [[self.labels valueForKey:selectedType] arrangedObjects]; self.selectedLabel = nil; } else if ([keyPath isEqual:@"selectedLabel"]) { if (self.selectedLabel != nil) { [deleteButton setTitle:[NSString stringWithFormat:@"Delete \"%@\"", self.selectedLabel, nil]]; [renameButton setTitle:[NSString stringWithFormat:@"Rename \"%@\"", self.selectedLabel, nil]]; } else { [deleteButton setTitle:@"Delete label"]; [renameButton setTitle:@"Rename label"]; } self.newLabel = @""; } else if ([keyPath isEqual:@"action"]) { [self willChangeValueForKey:@"rename"]; [self didChangeValueForKey:@"rename"]; } } @end