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

#import <AppKit/AppKit.h>
#import <MiscControllerKit/MiscControllerManager.h>
#import "AdderController.h"
#import "ResultController.h"
#import "Adder.h"


@implementation ResultController

/*"
   The ResultController only exists to test out the
   MiscControllerDidEditNotification mechanism. Normally one controller
   could handle what goes on in the adder window. All it does
   is display the result whenever the AdderController
   changes.
"*/


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

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


//-------------------------------------------------------------------
// 	Our model object
//-------------------------------------------------------------------

- (Adder*) adder { return _adder; }

- (void) setAdder:(Adder*)newAdder
{
    [_adder autorelease];
    _adder = [newAdder retain];
}


- (NSTextField*) resultTF { return resultTF; }


//-------------------------------------------------------------------
// 	Notifications
//-------------------------------------------------------------------

- (void) controllerDidEdit:(NSNotification*)notification
/*"
   We get this notification when the adder controller edits. We register
   for this notification in our _initializeUI method (sent from some
   awakeFromNib method in our framework).
   We just set our result textfield with the adder's new result.
"*/
{
    [[self resultTF] setFloatValue:[[self adder] result]];
}


- (void) _initializeUI
/*"
   This gets called from our #awakeFromNib method. It might be removed
   in later releases.
"*/
{
    AdderController* adderController;

    // Get notification when our adder controller edits. The default is controllerDidEdit:.

    // Our controller manager knows about us and the adder controller. We can
    // ask it for the adder controller without having an outlet to it.
    adderController = (AdderController*)
        [[self controllerManager] controllerOfClass:[AdderController class]];

    // Whenever the adder controller changes we should be notified. Using the
    // method below our #controllerDidEdit: method will be called everytime
    // the adder controller changes.
    if (adderController != nil) {
        [self _registerForControllerDidEditNotification:adderController];
    }

    // Use the same model object as our adderController.
    [self setAdder:[adderController adder]];
    // Make sure we are displaying an accurate result.
    [[self resultTF] setFloatValue:[[self adder] result]];
}

@end
