home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / Database / OTC_EOFBetaExamples_V1.0 / NSFoundation / Notification / Observer.m < prev    next >
Encoding:
Text File  |  1994-07-31  |  2.5 KB  |  109 lines

  1. /*--------------------------------------------------------------------------
  2.  *
  3.  *     You may freely copy, distribute, and reuse the code in this example.
  4.  *     SHL Systemhouse disclaims any warranty of any kind, expressed or  
  5.  *    implied, as to its fitness for any particular use.
  6.  *
  7.  *
  8.  *    Observer
  9.  *
  10.  *    Inherits From:        NSObject
  11.  *
  12.  *    Conforms To:        None
  13.  *
  14.  *    Declared In:        Observer.h
  15.  *
  16.  *
  17.  *------------------------------------------------------------------------*/
  18. #import "Observer.h"
  19. #import "Poster.h"
  20. #import <appkit/appkit.h>
  21. #import <foundation/NSNotification.h>
  22. #import <foundation/NSUtilities.h>
  23.  
  24.  
  25.  
  26.  
  27. @implementation Observer
  28.  
  29. - registerObserver: sender
  30. {
  31.     // Registers this object for the notification named "FlagRaised".
  32.     // Registering from object nil means that it will be 
  33.     // notified for all FlagRaised notifications regardless of what
  34.     // instance sends it. "flagRaised:" is the method that will
  35.     // be called when the notification occurs.
  36.  
  37.     [[NSNotificationCenter defaultCenter] 
  38.         addObserver:self
  39.          selector:@selector(flagRaised:) 
  40.         notificationName:@"RaiseFlag" 
  41.         object:nil];
  42.  
  43.     [registerButton setEnabled:NO];
  44.     [removeButton setEnabled:YES];
  45.     return self;
  46. }
  47.  
  48.  
  49. - removeObserver: sender
  50. {
  51.     // Removes this observer from the notification center's observer list. 
  52.     
  53.     [[NSNotificationCenter defaultCenter] 
  54.         removeObserver:self 
  55.         notificationName:@"RaiseFlag" 
  56.         object:nil];
  57.         
  58.     [registerButton setEnabled:YES];
  59.     [removeButton setEnabled:NO];
  60.     return self;
  61. }    
  62.  
  63.  
  64. - (void) flagRaised: (NSNotification*) aNotification
  65. {
  66.     // Each notification has an associated sender. Note that sender
  67.     // may contain any arbitrary data that you wish. If observer is
  68.     // aware of this data and how to get to it (in this example
  69.     // the observer knows it can get the current flag from the
  70.     // sender) application relevant info can be passed between them.
  71.  
  72.     switch (NXRunAlertPanel (NULL, 
  73.         "Event '%s' was observed !", 
  74.         "Acknowledge", "Ignore", NULL, 
  75.         [[aNotification notificationName] cString]))
  76.         {
  77.         case NX_ALERTALTERNATE:
  78.             break;
  79.         case NX_ALERTDEFAULT:
  80.         default:
  81.             {
  82.             id superview = [[flagButton superview] superview];
  83.             int    y = 0;
  84.             NXRect rect;
  85.             [flagButton getFrame:&rect];
  86.             [flagButton setIcon:[[[aNotification notificationObject]
  87.                  currentFlag] icon]];
  88.  
  89.             while (y <= 50) 
  90.                 {
  91.                 [flagButton moveTo:NX_X(&rect) :NX_Y(&rect) + y];
  92.                 y += 3;
  93.                 [superview display];
  94.                 }
  95.                 
  96.             while (y >= 0) 
  97.                 {
  98.                 [flagButton moveTo:NX_X(&rect) :NX_Y(&rect) + y];
  99.                 y -= 3;
  100.                 [superview display];
  101.                 }
  102.                 
  103.             break;
  104.             }
  105.         }
  106. }
  107.  
  108. @end
  109.