/* MiscInspectorMediator_Protected.m created by todd on Sun 05-May-1996 */

#import <AppKit/AppKit.h>

#import "MiscInspectorManager.h"
#import "MiscInspectorController.h"
#import "MiscInspectorMediator.h"

@implementation MiscInspectorMediator (Protected)

/*"
   This is a protected category that contains methods for use by
   our class or it's subclasses only.
"*/


- (void) controllerWindowWillClose:(NSNotification*)notification
/*"
   Sends a #_stopInspecting message to ourselves.
"*/
{
    [self _stopInspecting];
}


- (void) controllerWindowDidBecomeMain:(NSNotification*)notification
/*"
   Sends a #_startInspectingIfPossible to ourselves.
"*/
{
    [self _startInspectingIfPossible];
}


- (void) controllerWindowDidResignKey:(NSNotification*)notification
/*"
   If our window is resigning it's key status, but it's still the
   main window then that means that a panel may have been given
   the key. Since this includes the inspector panel that may have
   just been created we should attempt to let it know to start
   inspecting.
"*/
{
    if ([notification object] == [NSApp mainWindow]) {
        [self _startInspectingIfPossible];
    }
}


- (void) controllerWindowDidResignMain:(NSNotification*)notification
/*"
   Sends ourself a #_stopInspecting.
"*/
{
    [self _stopInspecting];
}


//-------------------------------------------------------------------
// 	Inspector communication
//-------------------------------------------------------------------

- (void) _startInspectingIfPossible
/*"
   Lets our inspector manager (if one exists) know to start inspecting
   our controller to inspect.
"*/
{
    [[[self inspectorManager] inspectorController] startInspectingController:
        [self controllerToInspect]];
}


- (void) _stopInspecting
/*"
   Lets our inspector manager (if it hasn't gone away) know to stop
   inspecting us because we are going away or have lost the main
   window status.
"*/
{
    [[[self inspectorManager] inspectorController] stopInspectingController:
        [self controllerToInspect]];
}


//-------------------------------------------------------------------
// 	Notification setup
//-------------------------------------------------------------------

- (void) _registerControllerWindowNotifications
/*"
   We need to know what our window is doing so we can decide when
   to be inspecting and when not to. We register for
   NSWindowWillCloseNotification, NSWindowDidBecomeMainNotification,
   and NSWindowDidResignMainNotification.
"*/
{
    BOOL error = NO;

    // If we have no controller to inspect then why bother with
    // notification setup?
    if (!error) {
        error = ([self controllerToInspect] == nil);
    }

    if (!error) {
        NSWindow* ourWindow = [[[self controllerToInspect] controllerManager] window];

        if (ourWindow == nil) {
            NSLog(@"MiscInspectorMediator: We are going to find it pretty hard to inspect "
                  @"ourselves when our controller to inspect does not have a window.");
            error = YES;
        }

        if (!error) {
            NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
            
            [notificationCenter addObserver:self                           	
                                   selector:@selector(controllerWindowWillClose:)
                                       name:NSWindowWillCloseNotification
                                     object:ourWindow];

            [notificationCenter addObserver:self                           	
                                    selector:@selector(controllerWindowDidBecomeMain:)
                                        name:NSWindowDidBecomeMainNotification
                                      object:ourWindow];

            [notificationCenter addObserver:self                           	
                                    selector:@selector(controllerWindowDidResignKey:)
                                        name:NSWindowDidResignKeyNotification
                                      object:ourWindow];

            [notificationCenter addObserver:self                           	
                                    selector:@selector(controllerWindowDidResignMain:)
                                        name:NSWindowDidResignMainNotification
                                      object:ourWindow];
        }
    }
}


- (void) _unregisterControllerWindowNotifications
/*"
   Used to remove all our window notifications either when they
   have become invalid or we are going to be freed.
"*/
{
    BOOL error = NO;
    NSWindow* ourWindow = nil;

    if (!error) {
        ourWindow = [[[self controllerToInspect] controllerManager] window];
        error = (ourWindow == nil);

        // Programmer error if they use this method after removing our
        // controllerToInspect.
        if (error) {
            NSLog(@"MiscInspectorMediator: Tried to unregister controller window "
                  @"notifications when we no longer have a window.");
        }
    }

    if (!error) {
        NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter];

        [defaultCenter removeObserver:self name:NSWindowWillCloseNotification
                               object:ourWindow];
        [defaultCenter removeObserver:self name:NSWindowDidResignKeyNotification
                               object:ourWindow];
        [defaultCenter removeObserver:self name:NSWindowDidResignMainNotification
                               object:ourWindow];
    }
}


- (void) _registerWindowManagerNotifications
/*"
   Registers us for our #{controllerToInspect's} windowManager's
   MiscWindowManagerWillReleaseNotification. Therefore when the
   window manager is about to release we can autorelease ourselves
   too.
"*/
{
    MiscControllerManager* controllerManager = nil;
    BOOL error = NO;

    // Make sure we have a controller manager first.
    if (!error) {
        controllerManager = [[self controllerToInspect] controllerManager];
        error = (controllerManager == nil);
    }

    if (!error) {
        [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(windowManagerWillRelease:)
            name:MiscWindowManagerWillReleaseNotification
            object:controllerManager];
    }
}

- (void) _unregisterWindowManagerNotifications
/*"
   As long as we still have a controllerToInspect we'll remove ourselves
   as an observer from it's window manager's notifications.
"*/
{
    BOOL error = NO;
    MiscControllerManager* controllerManager = nil;

    if (!error) {
        controllerManager = [[self controllerToInspect] controllerManager];
        error = (controllerManager == nil);

        // Programmer error if they use this method after removing our
        // controllerToInspect.
        if (error) {
            NSLog(@"MiscInspectorMediator: Tried to unregister window manager "
                  @"notifications when we no longer have a window manager.");
        }
    }

    if (!error) {
        [[NSNotificationCenter defaultCenter] removeObserver:self
            name:MiscWindowManagerWillReleaseNotification
            object:controllerManager];
    }
}

    
/*" Nib initialization "*/
- (void) awakeFromNib
{
    // These are needed because the other invokations of these are
    // in setControllerToInspect: which if the connection is made in IB
    // probably will not work correctly.
    [self _registerControllerWindowNotifications];
    [self _registerWindowManagerNotifications];
}

@end
