net.wotonomy.foundation
Class NSNotificationCenter

java.lang.Object
  extended by net.wotonomy.foundation.NSNotificationCenter

public class NSNotificationCenter
extends java.lang.Object

NSNotificationCenter broadcasts NSNotifications to registered observers. Observers can register for all notifications of a specific type, or all notifications about a specific object, or both. Observers specify the method that will be called when they are notified. A global notification center can be accessed with defaultCenter(), but other centers can be created and used independently of the default center.

This implementation uses weak references for observers and observables. The advantage to this approach is that you do not need to explicitly unregister observers or observables; they will be unregistered when they are garbage-collected. Note that you will need to retain a reference to any objects you register or they may become unregistered if no other object references them.

Version:
$Revision: 893 $
Author:
michael@mpowers.net, $Author: cgruber $

Field Summary
static java.lang.Object NullMarker
          Null marker class simplifies equals() logic for CompoundKey class below.
 
Constructor Summary
NSNotificationCenter()
          Default constructor creates a new notification center.
 
Method Summary
 void addObserver(java.lang.Object anObserver, NSSelector aSelector, java.lang.String notificationName, java.lang.Object anObject)
          Addes the specified observer to the notification queue for notifications with the specified name or the specified object or both.
static NSNotificationCenter defaultCenter()
          Returns the system default center, creating one if it has not yet been created.
 void postNotification(NSNotification aNotification)
          Posts the specified notification.
 void postNotification(java.lang.String notificationName, java.lang.Object anObject)
          Posts a notification created from the specified name and object.
 void postNotification(java.lang.String notificationName, java.lang.Object anObject, java.util.Map userInfo)
          Posts a notification created from the specified name, object, and info.
 void removeObserver(java.lang.Object anObserver)
          Unregisters the specified observer from all notification queues for which it is registered.
 void removeObserver(java.lang.Object anObserver, java.lang.String notificationName, java.lang.Object anObject)
          Unregisters the specified observer from all notifications queues associated with the specified name or object or both.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NullMarker

public static final java.lang.Object NullMarker
Null marker class simplifies equals() logic for CompoundKey class below.

Constructor Detail

NSNotificationCenter

public NSNotificationCenter()
Default constructor creates a new notification center.

Method Detail

defaultCenter

public static NSNotificationCenter defaultCenter()
Returns the system default center, creating one if it has not yet been created.


addObserver

public void addObserver(java.lang.Object anObserver,
                        NSSelector aSelector,
                        java.lang.String notificationName,
                        java.lang.Object anObject)
Addes the specified observer to the notification queue for notifications with the specified name or the specified object or both.

Parameters:
anObserver - The observer that wishes to be notified.
aSelector - The selector that will be invoked. Must have exactly one argument, to which a notification will be passed.
notificationName - The name of the notifications for which the observer will be notified. If null, will notify only based on matching anObject.
anObject - The object of the notifications for which the observer will be notified. If null, will notify only based on matching notificationName.

postNotification

public void postNotification(NSNotification aNotification)
Posts the specified notification. Notifies all registered observers that match either the notification name or the notification object, or both.

Parameters:
aNotification - The notification that will be passed to the observers selector.

postNotification

public void postNotification(java.lang.String notificationName,
                             java.lang.Object anObject)
Posts a notification created from the specified name and object. Calls postNotification( NSNotification ).

Parameters:
notificationName - a String key to distinguish this notification.
anObject - any object, by convention this is the originator of the notification.

postNotification

public void postNotification(java.lang.String notificationName,
                             java.lang.Object anObject,
                             java.util.Map userInfo)
Posts a notification created from the specified name, object, and info. Calls postNotification( NSNotification ).

Parameters:
notificationName - a String key to distinguish this notification.
anObject - any object, by convention this is the originator of the notification.
userInfo - a Map containing information specific to the originator of the notification and that may be of interest to a knowledgable observer.

removeObserver

public void removeObserver(java.lang.Object anObserver)
Unregisters the specified observer from all notification queues for which it is registered.

Parameters:
anObserver - The observer to be unregistered.

removeObserver

public void removeObserver(java.lang.Object anObserver,
                           java.lang.String notificationName,
                           java.lang.Object anObject)
Unregisters the specified observer from all notifications queues associated with the specified name or object or both.

Parameters:
anObserver - The observer to be unregistered, if null will unregister all observers for the specified notification name and object.
notificationName - The name of the notification for which the observer will be unregistered, if null will unregister the specified observer for all notifications with the specified object.
anObject - The object for the notification for which the observer will be unregistered, if null will unregister the specified observer for all objects with the specified notification.


Copyright © 2006 null. All Rights Reserved.