VocalEasel/Sources/VLKeyValueUndo.mm

59 lines
1.4 KiB
Plaintext
Raw Normal View History

2006-12-04 07:04:24 +00:00
//
// VLKeyValueUndo.mm
// Vocalese
//
// Created by Matthias Neeracher on 12/3/06.
// Copyright 2006 __MyCompanyName__. All rights reserved.
//
#import "VLKeyValueUndo.h"
@implementation VLKeyValueUndo
- (id)initWithOwner:(id)o keysAndNames:(NSDictionary *)kn
{
owner = o;
keysAndNames = [kn retain];
2007-04-16 05:35:52 +00:00
[owner addObserver:self];
2006-12-04 07:04:24 +00:00
for (NSEnumerator * e = [keysAndNames keyEnumerator];
NSString * key = [e nextObject];
)
[owner addObserver:self forKeyPath:key
options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew
context:[keysAndNames objectForKey:key]];
2006-12-04 07:04:24 +00:00
return self;
}
2007-04-16 05:35:52 +00:00
- (void)removeObservers:(id)target
2006-12-04 07:04:24 +00:00
{
for (NSEnumerator * e = [keysAndNames keyEnumerator];
NSString * key = [e nextObject];
)
2007-04-16 05:35:52 +00:00
[target removeObserver:self forKeyPath:key];
}
2006-12-04 07:04:24 +00:00
2007-04-16 05:35:52 +00:00
- (void) dealloc
{
2006-12-04 07:04:24 +00:00
[keysAndNames release];
[super dealloc];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
id oldVal = [change objectForKey:NSKeyValueChangeOldKey];
id newVal = [change objectForKey:NSKeyValueChangeNewKey];
if (![oldVal isEqual:newVal]) {
NSUndoManager * undo = [owner undoManager];
NSString * name = [keysAndNames objectForKey:keyPath];
[undo registerUndoWithTarget:owner selector:@selector(setValuesForKeysWithDictionary:)
object: [NSDictionary dictionaryWithObjectsAndKeys: oldVal, keyPath, nil]];
[undo setActionName: name];
}
}
@end