View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2000 Blacksmith, Inc.
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
17  */
18  
19  package net.wotonomy.foundation;
20  
21  import java.util.Map;
22  
23  /***
24  * An NSNotification is a generic message that can be
25  * dispatched by the NSNotificationCenter.
26  *
27  * @author michael@mpowers.net
28  * @author $Author: cgruber $
29  * @version $Revision: 893 $
30  */
31  public class NSNotification
32  {
33      public static boolean showStack = false;
34      
35      protected String name;
36      protected Object object;
37      protected Map userInfo;
38      
39      // for debugging only
40      private Throwable stackTrace;
41  
42      /***
43      * Default constructor creates a new notification
44      * with no name, object, or info dictionary.
45      */
46      public NSNotification ()
47      {
48          this( null, null, null );
49      }
50  
51      /***
52      * Constructor specifying name and object.
53      */
54      public NSNotification ( String aName, Object anObject )
55      {	
56          this( aName, anObject, null );
57      }
58  
59      /***
60      * Constructor specifying name, object, and a Map
61      * containing application specific information.
62      */
63      public NSNotification ( 
64          String aName, Object anObject, Map aUserInfo )
65      {
66          name = aName;
67          object = anObject;
68          if ( showStack ) stackTrace = new RuntimeException();
69          userInfo = aUserInfo;
70      }
71  
72      /***
73      * Returns the name of this notification.
74      */ 
75      public String name ()
76      {
77          return name;
78      }
79  
80      /***
81      * Returns the object of this notification.
82      */
83      public Object object ()
84      {
85          return object;
86      }
87  
88      /***
89      * Returns an NSDictionary that is a copy of 
90      * the map containing application specific 
91      * information relating to this notification,
92      * or null if no such data exists.
93      */ 
94      public NSDictionary userInfo ()
95      {
96          if ( userInfo == null ) return null;
97          return new NSDictionary( userInfo );
98      }
99      
100     /***
101     * Returns a Map containing application specific 
102     * information relating to this notification,
103     * or null if no such data exists.
104     * Note: this method is not in the spec.
105     */ 
106     public Map userInfoMap ()
107     {
108         return userInfo;
109     }
110     
111     /***
112     * Returns the stack trace when this notification was generated,
113     * or null if showStack is false, which is the default.
114     * NOTE: This method is not part of the specification.
115     */
116     public Throwable stackTrace()
117     {
118         return stackTrace;   
119     }
120     
121     /***
122     * Returns a human-readable string representation.
123     */
124     public String toString()
125     {
126         return "[ " + name() + " : " + object() + " : " + userInfo() + " ]";
127     }
128 }
129 
130 /*
131  * $Log$
132  * Revision 1.2  2006/02/16 13:15:00  cgruber
133  * Check in all sources in eclipse-friendly maven-enabled packages.
134  *
135  * Revision 1.7  2002/10/24 18:16:04  mpowers
136  * No longer generating stack trace by default.
137  *
138  * Revision 1.6  2002/06/21 22:02:47  mpowers
139  * Oops: Fixed NPE.
140  *
141  * Revision 1.5  2002/06/21 21:50:41  mpowers
142  * Added a method to get the map directly from the notification.
143  * Changed the internal representation to a map not a dictionary.
144  * We had been creating a new dictionary with each creation.
145  * This also allows people to modify the contents of the userInfo.
146  *
147  * Revision 1.4  2001/04/09 21:41:49  mpowers
148  * Better debugging.
149  *
150  * Revision 1.3  2001/02/21 18:31:07  mpowers
151  * Finished and tested implementation of NSNotificationCenter.
152  *
153  * Revision 1.2  2001/02/20 23:57:03  mpowers
154  * Implemented NSNotificationCenter.
155  *
156  * Revision 1.1.1.1  2000/12/21 15:47:36  mpowers
157  * Contributing wotonomy.
158  *
159  * Revision 1.3  2000/12/20 16:25:38  michael
160  * Added log to all files.
161  *
162  *
163  */
164