KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > notification > NotificationEvent


1 /* ************************************************************************** *
2  * Copyright (C) 2004 NightLabs GmbH, Marco Schulze *
3  * All rights reserved. *
4  * http://www.NightLabs.de *
5  * *
6  * This program and the accompanying materials are free software; you can re- *
7  * distribute it and/or modify it under the terms of the GNU General Public *
8  * License as published by the Free Software Foundation; either ver 2 of the *
9  * License, or any later version. *
10  * *
11  * This module is distributed in the hope that it will be useful, but WITHOUT *
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FIT- *
13  * NESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more *
14  * details. *
15  * *
16  * You should have received a copy of the GNU General Public License along *
17  * with this module; if not, write to the Free Software Foundation, Inc.: *
18  * 59 Temple Place, Suite 330 *
19  * Boston MA 02111-1307 *
20  * USA *
21  * *
22  * Or get it online: *
23  * http://www.opensource.org/licenses/gpl-license.php *
24  * *
25  * In case, you want to use this module or parts of it in a proprietary pro- *
26  * ject, you can purchase it under the NightLabs Commercial License. Please *
27  * contact NightLabs GmbH under info AT nightlabs DOT com for more infos or *
28  * visit http://www.NightLabs.com *
29  * ************************************************************************** */

30
31 /*
32  * Created on Apr 14, 2005
33  */

34 package com.nightlabs.notification;
35
36 import java.util.ArrayList JavaDoc;
37 import java.util.Collection JavaDoc;
38 import java.util.Collections JavaDoc;
39 import java.util.EventObject JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.List JavaDoc;
42
43 /**
44  * @author Marco Schulze - marco at nightlabs dot de
45  */

46 public class NotificationEvent
47 extends EventObject JavaDoc
48 // implements Cloneable
49
{
50     private String JavaDoc zone = null;
51
52     private List JavaDoc subjectCarriers = new ArrayList JavaDoc();
53
54     /**
55      * What has been changed (or references to it - e.g. IDs).
56      * <p>
57      * This is created dynamically out of {@link #subjectCarriers}.
58      */

59     private transient List JavaDoc subjects = null;
60
61     /**
62      * <tt>null</tt> cannot be put into the subjects, hence there's no way to clear e.g. a
63      * selection by using solely <tt>subjects</tt>. To make this possible,
64      * <tt>subjectClassesToClear</tt> can contain classes for the control
65      * <p>
66      * This is created dynamically out of {@link #subjectCarriers}.
67      */

68     private transient List JavaDoc subjectClassesToClear = null;
69
70     protected void clearCache()
71     {
72         subjects = null;
73         subjectClassesToClear = null;
74     }
75
76     /**
77      * Convenience constructor calling {@link #NotificationEvent(Object, String, Object)}
78      * with <tt>zone = null</tt>.
79      */

80     public NotificationEvent(Object JavaDoc source, Object JavaDoc subject)
81     {
82         this(source, (String JavaDoc)null, subject);
83     }
84
85     /**
86      * This is a convenience constructor calling {@link #NotificationEvent(Object, String, Object, Class)}
87      * with <tt>subjectClassToClear = null</tt>.
88      *
89      * @param source The source of the event (e.g. a composite that caused the notification).
90      * @param zone The zone in which to notify. If <tt>null</tt> all listeners in all zones will be notified.
91      * @param subject A non-<tt>null</tt> <tt>Object</tt> - e.g. a JDO object-id.
92      */

93     public NotificationEvent(Object JavaDoc source, String JavaDoc zone, Object JavaDoc subject)
94     {
95         this(source, zone, subject, null);
96     }
97
98     /**
99      * Convenience constructor calling {@link #NotificationEvent(Object, String, Object, Class)}
100      * with <tt>zone = null</tt>.
101      */

102     public NotificationEvent(Object JavaDoc source, Object JavaDoc subject, Class JavaDoc subjectClassToClear)
103     {
104         this(source, (String JavaDoc)null, subject, subjectClassToClear);
105     }
106
107     /**
108      * @param source The source of the event (e.g. a composite that caused the notification).
109      * @param zone The zone in which to notify. If <tt>null</tt> all listeners in all zones will be notified.
110      * @param subject Either <tt>null</tt> (then the <tt>subjectClassToClear</tt> must be defined) or a non-<tt>null</tt> <tt>Object</tt> - e.g. a JDO object-id.
111      * @param subjectClassToClear The class of a subject which should represent <tt>null</tt>. This class must be defined if <tt>subject</tt> is <tt>null</tt>.
112      * Both, <tt>subject</tt> and <tt>subjectClassToClear</tt> can be non-<tt>null</tt> indicating a real subject and a null-subject in one event.
113      */

114     public NotificationEvent(Object JavaDoc source, String JavaDoc zone, Object JavaDoc subject, Class JavaDoc subjectClassToClear)
115     {
116         super(source);
117         this.zone = zone;
118         if (subject == null && subjectClassToClear == null)
119             throw new NullPointerException JavaDoc("subject and subjectClass are both null! At least one of them must be defined!");
120
121         if (subject != null)
122             subjectCarriers.add(new SubjectCarrier(subject));
123
124         if (subjectClassToClear != null)
125             subjectCarriers.add(new SubjectCarrier(subjectClassToClear));
126     }
127
128     /**
129      * Convenience constructor calling {@link #NotificationEvent(Object, String, Object[])}
130      * with <tt>zone = null</tt>.
131      */

132     public NotificationEvent(Object JavaDoc source, Object JavaDoc[] subjects)
133     {
134         this(source, (String JavaDoc)null, subjects);
135     }
136
137     /**
138      * This is a convenience constructor calling {@link #NotificationEvent(Object, String, Object[], Class[])} with
139      * <tt>subjectClassesToClear = null</tt>.
140      */

141     public NotificationEvent(Object JavaDoc source, String JavaDoc zone, Object JavaDoc[] subjects)
142     {
143         this(source, zone, subjects, (Class JavaDoc[])null);
144     }
145
146     /**
147      * Convenience constructor calling {@link #NotificationEvent(Object, String, Object[], Class[])}
148      * with <tt>zone = null</tt>.
149      */

150     public NotificationEvent(Object JavaDoc source, Object JavaDoc[] subjects, Class JavaDoc[] subjectClassesToClear)
151     {
152         this(source, (String JavaDoc)null, subjects, subjectClassesToClear);
153     }
154
155     /**
156      * @param source The source of the event (e.g. a composite that caused the notification).
157      * @param zone The zone in which to notify. If <tt>null</tt> all listeners in all zones will be notified.
158      * @param subjects An array of non-<tt>null</tt> objects about which to notify (<tt>null</tt> objects are silently ignored). This parameter may be <tt>null</tt> or empty, if <tt>subjectClassesToClear</tt> is defined and not empty.
159      * @param subjectClassesToClear An array of non-<tt>null</tt> classes, representing <tt>null</tt> objects that cannot be declared
160      * in the <tt>subjects</tt> array, because the system doesn't know how to dispatch <tt>null</tt> notifications (as the class is unknown).
161      */

162     public NotificationEvent(Object JavaDoc source, String JavaDoc zone, Object JavaDoc[] subjects, Class JavaDoc[] subjectClassesToClear)
163     {
164         super(source);
165         this.zone = zone;
166         if (subjects == null && subjectClassesToClear == null)
167             throw new NullPointerException JavaDoc("subjects and subjectClassesToClear are both null! At least one of them must be defined!");
168
169         if (subjects != null) {
170             for (int i = 0; i < subjects.length; ++i) {
171     // if (subjects[i] == null)
172
// throw new NullPointerException("Object array 'subjects' must not contain null!");
173

174                 if (subjects[i] != null)
175                     this.subjectCarriers.add(new SubjectCarrier(subjects[i]));
176             }
177         } // if (subjects != null) {
178

179         if (subjectClassesToClear != null) {
180             for (int i = 0; i < subjectClassesToClear.length; ++i) {
181                 if (subjectClassesToClear[i] == null)
182                     throw new NullPointerException JavaDoc("Class array 'subjectClassesToClear' must not contain null!");
183     
184                 this.subjectCarriers.add(new SubjectCarrier(subjectClassesToClear[i]));
185             }
186         } // if (subjectClassesToClear != null) {
187

188         if (this.subjectCarriers.isEmpty())
189             throw new IllegalArgumentException JavaDoc("subjects and subjectClassesToClear are both empty! Need at least one entry in either of them!");
190     }
191     
192     /**
193      * Convenience constructor calling {@link #NotificationEvent(Object, String, Collection)}
194      * with <tt>zone = null</tt>.
195      */

196     public NotificationEvent(Object JavaDoc source, Collection JavaDoc subjects)
197     {
198         this(source, (String JavaDoc)null, subjects);
199     }
200
201     /**
202      * Convenience constructor calling {@link #NotificationEvent(Object, String, Collection, Collection)}
203      * with <tt>subjectClassesToClear = null</tt>.
204      */

205     public NotificationEvent(Object JavaDoc source, String JavaDoc zone, Collection JavaDoc subjects)
206     {
207         this(source, zone, subjects, (Collection JavaDoc)null);
208     }
209
210     /**
211      * Convenience constructor calling {@link #NotificationEvent(Object, String, Collection, Collection)}
212      * with <tt>zone = null</tt>
213      */

214     public NotificationEvent(Object JavaDoc source, Collection JavaDoc subjects, Collection JavaDoc subjectClassesToClear)
215     {
216         this(source, (String JavaDoc)null, subjects, subjectClassesToClear);
217     }
218
219     /**
220      * This constructor does the same as {@link #NotificationEvent(Object, String, Object[], Class[])},
221      * but uses <tt>Collection</tt>s instead of arrays.
222      */

223     public NotificationEvent(Object JavaDoc source, String JavaDoc zone, Collection JavaDoc subjects, Collection JavaDoc subjectClassesToClear)
224     {
225         this(source, zone, subjects, subjectClassesToClear, null);
226     }
227
228     public NotificationEvent(Object JavaDoc source, String JavaDoc zone, Collection JavaDoc subjects, Collection JavaDoc subjectClassesToClear, Collection JavaDoc _subjectCarriers)
229     {
230         super(source);
231         this.zone = zone;
232         if (subjects == null && subjectClassesToClear == null && _subjectCarriers == null)
233             throw new NullPointerException JavaDoc("subjects and subjectClassesToClear and subjectCarriers are all null! At least one of them must be defined!");
234
235         if (subjects != null) {
236             for (Iterator JavaDoc it = subjects.iterator(); it.hasNext(); ) {
237                 Object JavaDoc subject = it.next();
238     
239                 if (subject != null)
240                     this.subjectCarriers.add(new SubjectCarrier(subject));
241             }
242         } // if (subjects != null) {
243

244         if (subjectClassesToClear != null) {
245             for (Iterator JavaDoc it = subjectClassesToClear.iterator(); it.hasNext(); ) {
246                 Class JavaDoc subjectClassToClear = (Class JavaDoc)it.next();
247                 if (subjectClassToClear == null)
248                     throw new NullPointerException JavaDoc("Class collection 'subjectClassesToClear' must not contain null!");
249
250                 this.subjectCarriers.add(new SubjectCarrier(subjectClassToClear));
251             }
252         } // if (subjectClassesToClear != null) {
253

254         if (_subjectCarriers != null) {
255             for (Iterator JavaDoc it = _subjectCarriers.iterator(); it.hasNext(); ) {
256                 SubjectCarrier carrier = (SubjectCarrier) it.next();
257                 if (carrier != null)
258                     this.subjectCarriers.add(carrier);
259             }
260         }
261
262         if (this.subjectCarriers.isEmpty())
263             throw new IllegalArgumentException JavaDoc("subjects and subjectClassesToClear are both empty! Need at least one entry in either of them!");
264     }
265     
266     public NotificationEvent(Object JavaDoc source, String JavaDoc zone, SubjectCarrier subjectCarrier)
267     {
268         super(source);
269         this.zone = zone;
270         if (subjectCarrier != null)
271             subjectCarriers.add(subjectCarrier);
272     }
273
274     public NotificationEvent(Object JavaDoc source, String JavaDoc zone, SubjectCarrier[] subjectCarriers)
275     {
276         super(source);
277         this.zone = zone;
278         if (subjectCarriers != null)
279             for (int i = 0; i < subjectCarriers.length; i++) {
280                 addSubjectCarrier(subjectCarriers[i]);
281             }
282     }
283
284     /**
285      * @return Returns the zone.
286      */

287     public String JavaDoc getZone()
288     {
289         return zone;
290     }
291
292     /**
293      * @return Returns a <b>read-only</b> <tt>List</tt> of subjects.
294      */

295     public List JavaDoc getSubjects()
296     {
297         if (subjects == null) {
298             ArrayList JavaDoc l = new ArrayList JavaDoc();
299             for (Iterator JavaDoc it = subjectCarriers.iterator(); it.hasNext(); ) {
300                 SubjectCarrier carrier = (SubjectCarrier) it.next();
301                 if (carrier.getSubject() != null)
302                     l.add(carrier.getSubject());
303             }
304             subjects = Collections.unmodifiableList(l);
305         }
306         return subjects;
307     }
308
309     /**
310      * @return Returns the first subject or <tt>null</tt> if none available.
311      */

312     public Object JavaDoc getFirstSubject()
313     {
314         List JavaDoc subjects = getSubjects();
315         if(subjects.size() > 0)
316             return subjects.get(0);
317         return null;
318     }
319
320     /**
321      * @return Returns a <b>read-only</b> <tt>List</tt> with instances of {@link Class}.
322      */

323     public List JavaDoc getSubjectClassesToClear()
324     {
325         if (subjectClassesToClear == null) {
326             ArrayList JavaDoc l = new ArrayList JavaDoc();
327             for (Iterator JavaDoc it = subjectCarriers.iterator(); it.hasNext(); ) {
328                 SubjectCarrier carrier = (SubjectCarrier) it.next();
329                 for (Iterator JavaDoc i2 = carrier.getSubjectClasses().iterator(); i2.hasNext(); ) {
330                     Class JavaDoc clazz = (Class JavaDoc) i2.next();
331                     l.add(clazz);
332                 }
333             }
334             subjectClassesToClear = Collections.unmodifiableList(l);
335         }
336
337         return subjectClassesToClear;
338     }
339
340     public void addSubjectCarrier(SubjectCarrier subjectCarrier)
341     {
342         subjectCarriers.add(subjectCarrier);
343         clearCache();
344     }
345
346     public void removeSubjectCarrier(int index)
347     {
348         subjectCarriers.remove(index);
349         clearCache();
350     }
351
352     public void removeSubjectCarrier(SubjectCarrier subjectCarrier)
353     {
354         subjectCarriers.remove(subjectCarrier);
355         clearCache();
356     }
357
358     private transient List JavaDoc subjectCarriersReadOnly = null;
359     /**
360      * @return Returns a <b>read-only</b> <tt>List</tt> with instances of type {@link SubjectCarrier}.
361      */

362     public List JavaDoc getSubjectCarriers()
363     {
364         if (subjectCarriersReadOnly == null)
365             subjectCarriersReadOnly = Collections.unmodifiableList(subjectCarriers);
366
367         return subjectCarriersReadOnly;
368     }
369
370 // /**
371
// * This method does NOT clone the source. See {@link SubjectCarrier#clone()} for details
372
// * about what is cloned within the <tt>SubjectCarrier</tt>s.
373
// *
374
// * @see java.lang.Object#clone()
375
// */
376
// public Object clone()
377
// {
378
// List newSubjectCarriers = new ArrayList();
379
// for (Iterator it = subjectCarriers.iterator(); it.hasNext(); ) {
380
// SubjectCarrier carrier = (SubjectCarrier) it.next();
381
// newSubjectCarriers.add(carrier.clone());
382
// }
383
// NotificationEvent n = new NotificationEvent(
384
// source, zone, null, null, newSubjectCarriers);
385
// return n;
386
// }
387
}
388
Popular Tags