/* AdderController.m created by todd on Sun 28-Apr-1996 */

#import <AppKit/AppKit.h>

#import "Adder.h"
#import "AdderController.h"


@implementation AdderController

/*"
   AdderController is responsible for displaying the contents of the Adder
   object as well pushing new values entered by the user back to the 
   Adder object.
"*/


//-------------------------------------------------------------------
// 	Initialization/deallocation
//-------------------------------------------------------------------

- (id) init
{
    BOOL error = ([super init] == nil);

    if (!error) {
        // Create our model object.
        _adder = [[Adder alloc] init];
    }

    return error ? nil : self;
}


- (void) dealloc
{
    [_adder release];
    [_firstNumberTF release];
    [_secondNumberTF release];
    [super dealloc];
}


//-------------------------------------------------------------------
// 	Accessor methods
//-------------------------------------------------------------------

- (Adder*) adder { return _adder; }
- (NSTextField*) firstNumberTF { return _firstNumberTF; }

- (void) setFirstNumberTF:(NSTextField*)newTextField
{
    [_firstNumberTF autorelease];
    _firstNumberTF = [newTextField retain];
}


- (NSTextField*) secondNumberTF { return _secondNumberTF; }

- (void) setSecondNumberTF:(NSTextField*)newTextField
{
    [_secondNumberTF autorelease];
    _secondNumberTF = [newTextField retain];
}


//-------------------------------------------------------------------
// 	Actions
//-------------------------------------------------------------------

- (void) addedNumbersChanged:(id)sender
/*"
   Either the first or second number changed so do some updating.
"*/
{
    float firstVal = [[self firstNumberTF] floatValue];
    float secondVal = [[self secondNumberTF] floatValue];
    [[self adder] add:firstVal and:secondVal];

    // You should send this any time you change the model object.
    // That way you know you are "dirty" and edit notification
    // will also be sent.
    [self didEdit];

    // Make whichever textfield changed selected again so the
    // user doesn't have to click in one of the textfields again if
    // they want to change the number again.
    if ([sender respondsToSelector:@selector(selectText:)]) {
        [sender selectText:nil];
    }
}


- (void) _initializeUI
{
    // This just syncs what's shown in the TextFields (as entered in IB)
    // and the result in the result textfield.
    [[self firstNumberTF] setFloatValue:[[self adder] firstNumber]];
    [[self secondNumberTF] setFloatValue:[[self adder] secondNumber]];
}

@end
