Re: Undo logic location?
Re: Undo logic location?
- Subject: Re: Undo logic location?
- From: Tom Davies <email@hidden>
- Date: Thu, 6 Jan 2005 22:15:11 +1100
[apologies for the thread-jacking]
What I ended up doing was to make MyDocument observe the model like
this:
- (void)startObservingCatalogue:(Catalogue*)cat
{
[cat addObserver:self forKeyPath:@"definitionItems"
options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew
context:NULL];
NSEnumerator* e = [[cat definitionItems] objectEnumerator];
DefinitionItem* d;
while (d = [e nextObject])
{
[self startObservingDefinitionItem:d];
}
}
and process the notifications like this:
- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object
change:(NSDictionary*)change context:(void*)context
{
NSUndoManager* undo = [self undoManager];
if ([keyPath isEqual:@"definitionItems"])
{
NSNumber* valueChangeKind = [change
valueForKey:NSKeyValueChangeKindKey];
NSIndexSet* indexes = [change
valueForKey:NSKeyValueChangeIndexesKey];
int index = [indexes firstIndex];
if ([valueChangeKind intValue] == NSKeyValueChangeInsertion)
{
DefinitionItem* item = [[change
valueForKey:NSKeyValueChangeNewKey] objectAtIndex:0];
[[undo prepareWithInvocationTarget:object]
removeObjectFromDefinitionItemsAtIndex:index];
if (![undo isUndoing])
{
[undo setActionName:@"Insert Definition Item"];
}
[self startObservingDefinitionItem:item];
}
else if ([valueChangeKind intValue] == NSKeyValueChangeRemoval)
{
DefinitionItem* oldItem = [[change
valueForKey:NSKeyValueChangeOldKey] objectAtIndex:0];
[[undo prepareWithInvocationTarget:object] insertObject:oldItem
inDefinitionItemsAtIndex:index];
if (![undo isUndoing])
{
[undo setActionName:@"Delete Definition Item"];
}
[self stopObservingDefinitionItem:oldItem];
}
}
else
{
id oldValue = [change objectForKey:NSKeyValueChangeOldKey];
[[undo prepareWithInvocationTarget:self] changeKeyPath:keyPath
ofObject: object toValue:oldValue];
[undo setActionName:@"Edit"];
}
}
So now the model has no undo code at all.
Tom
--
email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden