/*--------------------------------------------------------------------------
 *
 * 	You may freely copy, distribute, and reuse the code in this example.
 * 	SHL Systemhouse disclaims any warranty of any kind, expressed or  
 *	implied, as to its fitness for any particular use.
 *
 *
 *	Observer
 *
 *	Inherits From:		NSObject
 *
 *	Conforms To:		None
 *
 *	Declared In:		Observer.h
 *
 *
 *------------------------------------------------------------------------*/
#import "Observer.h"
#import "Poster.h"
#import <appkit/appkit.h>
#import <foundation/NSNotification.h>
#import <foundation/NSUtilities.h>




@implementation Observer

- registerObserver: sender
{
	// Registers this object for the notification named "FlagRaised".
	// Registering from object nil means that it will be 
	// notified for all FlagRaised notifications regardless of what
	// instance sends it. "flagRaised:" is the method that will
	// be called when the notification occurs.

	[[NSNotificationCenter defaultCenter] 
		addObserver:self
	 	selector:@selector(flagRaised:) 
		notificationName:@"RaiseFlag" 
		object:nil];

	[registerButton setEnabled:NO];
	[removeButton setEnabled:YES];
	return self;
}


- removeObserver: sender
{
	// Removes this observer from the notification center's observer list. 
	
	[[NSNotificationCenter defaultCenter] 
		removeObserver:self 
		notificationName:@"RaiseFlag" 
		object:nil];
		
	[registerButton setEnabled:YES];
	[removeButton setEnabled:NO];
	return self;
}	


- (void) flagRaised: (NSNotification*) aNotification
{
	// Each notification has an associated sender. Note that sender
	// may contain any arbitrary data that you wish. If observer is
	// aware of this data and how to get to it (in this example
	// the observer knows it can get the current flag from the
	// sender) application relevant info can be passed between them.

	switch (NXRunAlertPanel (NULL, 
		"Event '%s' was observed !", 
		"Acknowledge", "Ignore", NULL, 
		[[aNotification notificationName] cString]))
		{
		case NX_ALERTALTERNATE:
			break;
		case NX_ALERTDEFAULT:
		default:
			{
			id superview = [[flagButton superview] superview];
			int	y = 0;
			NXRect rect;
			[flagButton getFrame:&rect];
			[flagButton setIcon:[[[aNotification notificationObject]
				 currentFlag] icon]];

			while (y <= 50) 
				{
				[flagButton moveTo:NX_X(&rect) :NX_Y(&rect) + y];
				y += 3;
				[superview display];
				}
				
			while (y >= 0) 
				{
				[flagButton moveTo:NX_X(&rect) :NX_Y(&rect) + y];
				y -= 3;
				[superview display];
				}
				
			break;
			}
		}
}

@end
