
#import "Controller.h"

@implementation Controller

- init
{
	[super init];
	undoManager = [[MiscUndoManager alloc] init];
	testValue = 0;
	return self;
}

- (void)dealloc
{
	[undoManager release];
	return [super dealloc];
}

- (void)dec:(id)sender
{	
	// start a new group 
	[undoManager startUndo:@"Undo Dec" :@"Redo Inc"];

	// set target for undo message
	[undoManager setCurrentTarget:self];

	// send the undo message
	[(id)undoManager inc:sender];
	
	// commit group (group consists only of this record)
	[undoManager commitUndo];
	
	// do the actual action
	testValue--;
	[textField setIntValue:testValue];
	[textField display];
}

- (void)inc:(id)sender
{	
	// start a new group 
	[undoManager startUndo:@"Undo Inc" :@"Redo Dec"];
	
	// set target for undo message
	[undoManager setCurrentTarget:self];

	// send the undo message
	[(id)undoManager dec:sender];
	
	// commit group (group consists only of this record)
	[undoManager commitUndo];
	
	// do the actual action
	testValue++;
	[textField setIntValue:testValue];
	[textField display];
}

- (void)redo:(id)sender
{
	[undoManager redo];
}

- (void)undo:(id)sender
{
	[undoManager undo];
}

- (BOOL)validateMenuItem:(NSMenuItem *)anItem
{
    NSString *menuTitle = [anItem title];

    if([menuTitle hasPrefix:@"Undo"]){
        if([undoManager numberOfUndos] == 0){
            [anItem setTitle:@"Undo"];
            return NO;
        }
        [anItem setTitle:[undoManager undoName]];
        return YES;
    }
    if([menuTitle hasPrefix:@"Redo"]){
        if([undoManager numberOfRedos] == 0){
                [anItem setTitle:@"Redo"];
                return NO;
        }
        [anItem setTitle:[undoManager redoName]];
        return YES;
    }
    return YES;
}

@end
